1 /*
2 # PostgreSQL Database Modeler (pgModeler)
3 #
4 # Copyright 2006-2020 - Raphael Araújo e Silva <raphael@pgmodeler.io>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation version 3.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # The complete text of GPLv3 is at LICENSE file on source code root directory.
16 # Also, you can get the complete GNU General Public License at <http://www.gnu.org/licenses/>
17 */
18 
19 #include <QtTest/QtTest>
20 #include "relationship.h"
21 
22 class PartRelationhipTest: public QObject {
23   private:
24     Q_OBJECT
25 
26   private slots:
27 	void connRelTableNoColumnsAndTableTwoColumns();
28 	void connRelTablesTwoColumnsSameNameShouldNotRaiseError();
29 	void connRelTablePartionHasMoreColsThanPartitionedTableShouldRaiseError();
30 	void connRelTablePartionHasDifferentColsThanPartitionedTableShouldRaiseError();
31 	void connRelShouldInvalidateIfPartitionedColumnChanges();
32 };
33 
connRelTableNoColumnsAndTableTwoColumns()34 void PartRelationhipTest::connRelTableNoColumnsAndTableTwoColumns()
35 {
36   Schema *schema = new Schema;
37   Table *partitioned = new Table;
38   Table *partition = new Table;
39   Column *col = new Column, *col1 = new Column;
40   Relationship *part_rel = nullptr;
41 
42   try
43   {
44 	schema->setName("public");
45 	partitioned->setSchema(schema);
46 	partition->setSchema(schema);
47 
48 	partitioned->setPartitioningType(PartitioningType::List);
49 	partitioned->setName("table_a");
50 	partition->setName("partion_a");
51 
52 	col->setName("id");
53 	col->setType(PgSqlType("serial"));
54 	col1->setName("sku");
55 	col1->setType(PgSqlType("smallint"));
56 
57 	partitioned->addColumn(col);
58 	partitioned->addColumn(col1);
59 
60 	part_rel = new Relationship(BaseRelationship::RelationshipPart, partition, partitioned);
61 	part_rel->connectRelationship();
62 
63 	QVERIFY(partitioned->getColumnCount() == partition->getColumnCount());
64   }
65   catch(Exception &e)
66   {
67 	QFAIL(e.getErrorMessage().toStdString().c_str());
68   }
69 }
70 
connRelTablesTwoColumnsSameNameShouldNotRaiseError()71 void PartRelationhipTest::connRelTablesTwoColumnsSameNameShouldNotRaiseError()
72 {
73   Schema *schema = new Schema;
74   Table *partitioned = new Table;
75   Table *partition = new Table;
76   Column *col = new Column, *col1 = new Column;
77   Relationship *part_rel = nullptr;
78 
79   try
80   {
81 	schema->setName("public");
82 	partitioned->setSchema(schema);
83 	partition->setSchema(schema);
84 
85 	partitioned->setPartitioningType(PartitioningType::List);
86 	partitioned->setName("table_a");
87 	partition->setName("partion_a");
88 
89 	col->setName("id");
90 	col->setType(PgSqlType("serial"));
91 	col1->setName("sku");
92 	col1->setType(PgSqlType("smallint"));
93 
94 	partitioned->addColumn(col);
95 	partitioned->addColumn(col1);
96 
97 	col = new Column;
98 	col1 = new Column;
99 	col->setName("id");
100 	col->setType(PgSqlType("serial"));
101 	col1->setName("sku");
102 	col1->setType(PgSqlType("smallint"));
103 
104 	partition->addColumn(col);
105 	partition->addColumn(col1);
106 
107 	part_rel = new Relationship(BaseRelationship::RelationshipPart, partition, partitioned);
108 	part_rel->connectRelationship();
109 
110 	QVERIFY(partitioned->getColumnCount() == partition->getColumnCount());
111   }
112   catch(Exception &e)
113   {
114 	QFAIL(e.getErrorMessage().toStdString().c_str());
115   }
116 }
117 
connRelTablePartionHasMoreColsThanPartitionedTableShouldRaiseError()118 void PartRelationhipTest::connRelTablePartionHasMoreColsThanPartitionedTableShouldRaiseError()
119 {
120   Schema *schema = new Schema;
121   Table *partitioned = new Table;
122   Table *partition = new Table;
123   Column *col = new Column, *col1 = new Column, *col2 = nullptr;
124   Relationship *part_rel = nullptr;
125 
126   try
127   {
128 	schema->setName("public");
129 	partitioned->setSchema(schema);
130 	partition->setSchema(schema);
131 
132 	partitioned->setPartitioningType(PartitioningType::List);
133 	partitioned->setName("table_a");
134 	partition->setName("partion_a");
135 
136 	col->setName("id");
137 	col->setType(PgSqlType("serial"));
138 	col1->setName("sku");
139 	col1->setType(PgSqlType("smallint"));
140 
141 	partitioned->addColumn(col);
142 	partitioned->addColumn(col1);
143 
144 	col = new Column;
145 	col1 = new Column;
146 	col->setName("id");
147 	col->setType(PgSqlType("serial"));
148 	col1->setName("sku");
149 	col1->setType(PgSqlType("smallint"));
150 	col2 = new Column;
151 	col2->setName("date");
152 	col2->setType(PgSqlType("date"));
153 
154 	partition->addColumn(col);
155 	partition->addColumn(col1);
156 	partition->addColumn(col2);
157 
158 	part_rel = new Relationship(BaseRelationship::RelationshipPart, partition, partitioned);
159 	part_rel->connectRelationship();
160 
161 	QFAIL("Should return exception because tables have different column count!");
162   }
163   catch(Exception &e)
164   {
165 	QVERIFY(e.getErrorCode() == ErrorCode::InvColumnCountPartRel);
166   }
167 }
168 
connRelTablePartionHasDifferentColsThanPartitionedTableShouldRaiseError()169 void PartRelationhipTest::connRelTablePartionHasDifferentColsThanPartitionedTableShouldRaiseError()
170 {
171   Schema *schema = new Schema;
172   Table *partitioned = new Table;
173   Table *partition = new Table;
174   Column *col = new Column, *col1 = new Column;
175   Relationship *part_rel = nullptr;
176 
177   try
178   {
179 	schema->setName("public");
180 	partitioned->setSchema(schema);
181 	partition->setSchema(schema);
182 
183 	partitioned->setPartitioningType(PartitioningType::List);
184 	partitioned->setName("table_a");
185 	partition->setName("partion_a");
186 
187 	col->setName("id");
188 	col->setType(PgSqlType("serial"));
189 	col1->setName("sku");
190 	col1->setType(PgSqlType("smallint"));
191 
192 	partitioned->addColumn(col);
193 	partitioned->addColumn(col1);
194 
195 	col = new Column;
196 	col1 = new Column;
197 	col->setName("tag_id");
198 	col->setType(PgSqlType("serial"));
199 	col1->setName("serial_number");
200 	col1->setType(PgSqlType("smallint"));
201 
202 	partition->addColumn(col);
203 	partition->addColumn(col1);
204 
205 	part_rel = new Relationship(BaseRelationship::RelationshipPart, partition, partitioned);
206 	part_rel->connectRelationship();
207 
208 	QFAIL("Should return exception because tables have different columns names!");
209   }
210   catch(Exception &e)
211   {
212 	QVERIFY(e.getErrorCode() == ErrorCode::InvColumnCountPartRel);
213   }
214 }
215 
connRelShouldInvalidateIfPartitionedColumnChanges()216 void PartRelationhipTest::connRelShouldInvalidateIfPartitionedColumnChanges()
217 {
218   Schema *schema = new Schema;
219   Table *partitioned = new Table;
220   Table *partition = new Table;
221   Column *col = new Column, *col1 = new Column;
222   Relationship *part_rel = nullptr;
223 
224   try
225   {
226 	schema->setName("public");
227 	partitioned->setSchema(schema);
228 	partition->setSchema(schema);
229 
230 	partitioned->setPartitioningType(PartitioningType::List);
231 	partitioned->setName("table_a");
232 	partition->setName("partion_a");
233 
234 	col->setName("id");
235 	col->setType(PgSqlType("serial"));
236 	col1->setName("sku");
237 	col1->setType(PgSqlType("smallint"));
238 
239 	partitioned->addColumn(col);
240 	partitioned->addColumn(col1);
241 
242 	col = new Column;
243 	col1 = new Column;
244 	col->setName("id");
245 	col->setType(PgSqlType("serial"));
246 	col1->setName("sku");
247 	col1->setType(PgSqlType("smallint"));
248 
249 	partition->addColumn(col);
250 	partition->addColumn(col1);
251 
252 	part_rel = new Relationship(BaseRelationship::RelationshipPart, partition, partitioned);
253 	part_rel->connectRelationship();
254 
255 	partitioned->getColumn(0)->setName("new_id");
256 	QVERIFY(part_rel->isInvalidated() == true);
257 
258 	partitioned->getColumn(0)->setName("id");
259 	partitioned->getColumn(0)->setType(PgSqlType("date"));
260 	QVERIFY(part_rel->isInvalidated() == true);
261   }
262   catch(Exception &e)
263   {
264 	QFAIL(e.getErrorMessage().toStdString().c_str());
265   }
266 }
267 
268 QTEST_MAIN(PartRelationhipTest)
269 #include "partrelationshiptest.moc"
270