Commits

Palmer, 2E0EOL  committed 2c47ce1

Add first example of mocked DB derived from DBMock

  • Participants
  • Parent commits 8797935

Comments (0)

Files changed (4)

File DBMockExample.cpp

+/*
+  This is a base class which shows the basic concept of creating a table
+  in a dynamic, faked database.
+*/
+
+#include "DBMockExample.hpp"
+
+DBMockExample::DBMockExample()
+{
+	this->CreateTables();
+	this->PopulateData();
+}
+
+void DBMockExample::CreateTables()
+{
+	this->query(
+		"CREATE TABLE offices (\n"
+		"	id INTEGER NOT NULL PRIMARY KEY,\n"
+		"	city CHAR(16)\n"
+		")\n"
+	);
+	this->query(
+		"CREATE TABLE employees (\n"
+		"	id INTEGER NOT NULL AUTOINCREMENT PRIMARY KEY,\n"
+		"	enrolled DATETIME,\n"
+		"	terminated DATETIME\n"
+		");\n"
+	);
+}
+
+void DBMockExample::PopulateData()
+{
+	this->query("INSERT INTO offices (city) VALUES(\'London\')");
+	this->query("INSERT INTO offices (city) VALUES(\'Bristol\')");
+	this->query("INSERT INTO offices (city) VALUES(\'Leeds\')");
+	this->query("INSERT INTO offices (city) VALUES(\'Newport\')");
+}

File DBMockExample.hpp

+/*
+  This class represents the mocked variant of the database class.
+  It inherits from the main DB class, but then sets up a database
+  which is SQlite, and will be created in a temporary directory.
+*/
+
+#ifndef INC_DBMOCKEXAMPLE_H
+#define INC_DBMOCKEXAMPLE_H
+
+#include "DBMock.hpp"
+
+class DBMockExample : public DBMock
+{
+private:
+protected:
+public:
+	DBMockExample();
+	void CreateTables(void);
+	void PopulateData(void);
+};
+
+#endif //INC_DBMOCKEXAMPLE_H
-concept : concept.o DB.o DBMock.o
-	g++ -o concept concept.o DB.o DBMock.o
+concept : concept.o DB.o DBMock.o DBMockExample.o
+	g++ -o concept concept.o DB.o DBMock.o DBMockExample.o
 
 DB.o : DB.cpp DB.hpp
 	g++ -c -o DB.o DB.cpp
 DBMock.o : DBMock.cpp DBMock.hpp
 	g++ -c -o DBMock.o DBMock.cpp
 
-concept.o : concept.cpp DB.hpp DBMock.o
+DBMockExample.o : DBMockExample.cpp DBMockExample.hpp
+	g++ -c -o DBMockExample.o DBMockExample.cpp
+
+concept.o : concept.cpp DB.hpp DBMock.hpp DBMockExample.hpp
 	g++ -c -o concept.o concept.cpp
 
 test : concept
 #include <iostream>
 #include "DB.hpp"
 #include "DBMock.hpp"
+#include "DBMockExample.hpp"
 
 using namespace std;
 
 int main()
 {
 	//DB database;
-	DBMock database;
+	DBMockExample database;
 	database.query(
 		"SELECT e.id,enrolled,given_name,city FROM employees e, offices o "
 		"WHERE terminated IS NULL AND e.office_id=o.id"