-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEfProviderTest.cpp
More file actions
210 lines (165 loc) · 6.01 KB
/
Copy pathEfProviderTest.cpp
File metadata and controls
210 lines (165 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include "EFDrivers/EFDriverSQLight.hpp"
#include <AbstractEFProvider.hpp>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
#include <QSqlQueryModel>
#include <QFile>
#include <QTest>
#define DATABASE_FILE_NAME "MyDBFile.db"
class Country :public CAbstractDatabaseModel
{
REGISTER_PROPERTY(std::string, Name, Country, &Country::SetName, &Country::GetName)
REGISTER_PROPERTY(std::string, Display, Country, &Country::SetDisplay, &Country::GetDisplay)
public:
Country() : CAbstractDatabaseModel(){}
void SetName(CVariant name) { Name = name.toString(); }
CVariant GetName() {return Name; }
void SetDisplay(CVariant display) { Display = display.toString(); }
CVariant GetDisplay() {return Display; }
};
class AreaRegion:public CAbstractDatabaseModel
{
REGISTER_PROPERTY(std::string, name, AreaRegion, &AreaRegion::SetName, &AreaRegion::GetName)
REGISTER_PROPERTY(int, CountryId, AreaRegion, &AreaRegion::SetCountryId, &AreaRegion::GetCountryId)
public:
AreaRegion():CAbstractDatabaseModel(){}
void SetName(CVariant name){this->name = name.toString();}
CVariant GetName(){return this->name;}
void SetCountryId(CVariant id){this->CountryId = id.toInt();}
CVariant GetCountryId(){return this->CountryId;}
Country* GetCountry(){return GetRelationship<Country>("CountryId");}
};
namespace EFProvider{
class CQSqlQueryWrapper : public CAbstracSqlDataBaseQuery
{
public:
CQSqlQueryWrapper(QSqlQuery* q) : LocalQuery(q) {}
bool Next() override
{
return LocalQuery->next();
}
CVariant Value(int index) override
{
auto v = LocalQuery->value(index);
return CVariant(v.toString().toStdString());
}
CVariant Value(std::string key) override
{
auto v = LocalQuery->value(QString::fromStdString(key));
return CVariant(v.toString().toStdString());
}
private:
QSqlQuery *LocalQuery;
};
class CSQLightWrapper : public CAbstractSQLDatabase
{
public:
CSQLightWrapper(std::string db_path,std::string database_name) : CAbstractSQLDatabase()
,LastQuery(new QSqlQuery)
{
// create db file
QString db_address = QString::fromStdString(database_name);
if(db_path != "")
db_address = QString::fromStdString(db_path + "\\" + database_name);
QFile f(db_address);
if(!f.open(QIODevice::ReadWrite))
assert(false);
f.close();
// initialize db file
LocalDatabase = QSqlDatabase::addDatabase("QSQLITE");
LocalDatabase.setHostName(db_address);
LocalDatabase.setDatabaseName(QString::fromStdString(database_name));
}
public:
bool Open() override {return LocalDatabase.open();}
bool Close() override {LocalDatabase.close(); return true;}
CAbstracSqlDataBaseQuery* Execute(std::string cmd) override
{
*LastQuery = LocalDatabase.exec(QString::fromStdString(cmd));
return new CQSqlQueryWrapper(LastQuery);
}
private:
QSqlDatabase LocalDatabase;
QSqlQuery* LastQuery = nullptr;
};
}
class EFProviderBasicTests: public QObject
{
Q_OBJECT
private:
std::unique_ptr<EFProvider::CEFDriverSQLight<Country>> CountryDBProvider;
std::unique_ptr<EFProvider::CEFDriverSQLight<AreaRegion>> AreaRegionDBProvider;
private slots:
void initTestCase()
{
using namespace EFProvider;
QFile::remove(DATABASE_FILE_NAME);
CountryDBProvider = std::make_unique<CEFDriverSQLight<Country>>();
AreaRegionDBProvider = std::make_unique<CEFDriverSQLight<AreaRegion>>();
auto sql_connection = new CSQLightWrapper("","MyDBFile.db");
CountryDBProvider->Initialize(sql_connection, EDatabaseType::SQLight2);
AreaRegionDBProvider->Initialize(sql_connection, EDatabaseType::SQLight2);
}
void CreationTest()
{
// given
using namespace EFProvider;
SDatabaseRelationship rel;
rel.EFProvider = CountryDBProvider.get();
rel.ForeignKey = "CountryId";
AreaRegionDBProvider->AddRelationship(rel);
// when
Country my_country;
my_country.SetName(std::string("Malaysia"));
my_country.SetDisplay(std::string("Malaysia In East Asia"));
CountryDBProvider->Append(&my_country);
CountryDBProvider->SaveChanges();
AreaRegion my_reg;
my_reg.SetName(std::string("Asia"));
my_reg.SetCountryId(my_country.GetId());
AreaRegionDBProvider->Append(&my_reg);
AreaRegionDBProvider->SaveChanges();
// then
QFile db(DATABASE_FILE_NAME);
QVERIFY(db.exists());
}
void ToListFunctionTest()
{
auto all_area = AreaRegionDBProvider->ToList();
QVERIFY(all_area.size() == 1);
QCOMPARE(all_area.front()->GetId().toInt(), 1);
QCOMPARE(all_area.front()->GetName().toString(), "Asia");
QCOMPARE(all_area.front()->GetCountryId().toInt(), 1);
QCOMPARE(all_area.front()->GetCountry()->GetDisplay().toString(), "Malaysia In East Asia");
}
void SingleOrDefaultTest()
{
auto malaysia_country = CountryDBProvider->SingleOrDefault("Name == 'Malaysia'");
QCOMPARE(malaysia_country->GetId().toInt(), 1);
QCOMPARE(malaysia_country->GetName().toString(), "Malaysia");
}
void WhereTest()
{
auto greater_id = CountryDBProvider->Where("Id > 0");
QVERIFY(greater_id.size() == 1);
QCOMPARE(greater_id.front()->GetId().toInt(), 1);
QCOMPARE(greater_id.front()->GetName().toString(), "Malaysia");
}
void TopTest()
{
auto top_countries = CountryDBProvider->Top(4);
QVERIFY(top_countries.size() == 4);
QCOMPARE(top_countries.front()->GetId().toInt(), 1);
QCOMPARE(top_countries.front()->GetName().toString(), "Malaysia");
}
void FindTest()
{
auto find_id = CountryDBProvider->Find(1);
QCOMPARE(find_id->GetId().toInt(), 1);
QCOMPARE(find_id->GetName().toString(), "Malaysia");
}
};
QTEST_MAIN(EFProviderBasicTests)
#include "EfProviderTest.moc"