TsDataSetType.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib 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 Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file TsDataSetType.cpp
22 
23  \brief Test suite for the DataSetType class.
24  */
25 
26 // Unit-Test TerraLib
27 #include "TsDataSetType.h"
28 
29 // TerraLib
30 #include <terralib/dataaccess.h>
31 #include <terralib/geometry.h>
32 #include <terralib/datatype.h>
33 // STL
34 #include <ctime>
35 
37 
39 {
40 }
41 
43 {
44 }
45 
47 {
48 //#ifdef TE_COMPILE_ALL
49  {
50  te::da::DataSetType dt("dataset_type", 666);
51  dt.add(new te::dt::NumericProperty("attribute_numeric", 10, 3, true, new std::string("34.78"), 1));
52  dt.add(new te::dt::StringProperty("attribute_fixed_string", te::dt::FIXED_STRING, 50));
53  dt.add(new te::dt::StringProperty("attribute_var_string", te::dt::VAR_STRING, 50, false, 0, 82));
54  dt.add(new te::dt::StringProperty("attribute_var_unlimited_string", te::dt::STRING, 0, true, 0, 79));
55  dt.add(new te::gm::GeometryProperty("attribute_geom_polm", 4326, te::gm::PolygonMType, true, 0, 23331));
56  dt.add(new te::dt::SimpleProperty("attribute_int_serial", te::dt::INT32_TYPE, false, 0, 23));
57 
58  CPPUNIT_ASSERT(dt.getType() == te::dt::DATASET_TYPE);
59  CPPUNIT_ASSERT(dt.getName() == "dataset_type");
60  CPPUNIT_ASSERT(dt.getId() == 666);
61  CPPUNIT_ASSERT(dt.getParent() == 0);
62  CPPUNIT_ASSERT(dt.size() == 6);
63  CPPUNIT_ASSERT(dt.has(dt.getProperty(0)));
64  CPPUNIT_ASSERT(dt.has(dt.getProperty(1)));
65  CPPUNIT_ASSERT(dt.has(dt.getProperty(2)));
66  CPPUNIT_ASSERT(dt.has(dt.getProperty(3)));
67  CPPUNIT_ASSERT(dt.has(dt.getProperty(4)));
68  CPPUNIT_ASSERT(dt.has(dt.getProperty(5)));
69 
70  te::dt::SimpleProperty intt("attribute_int", te::dt::INT32_TYPE, true, 0, 23);
71  CPPUNIT_ASSERT(dt.has(&intt) == false);
72  }
73 
74  {
75  te::da::DataSetType dt("dataset_type", 666);
76 
77  dt.add(new te::dt::NumericProperty("attribute_numeric", 10, 3, true, new std::string("34.78"), 1));
78  dt.add(new te::dt::StringProperty("attribute_fixed_string", te::dt::FIXED_STRING, 50));
79  dt.add(new te::dt::StringProperty("attribute_var_string", te::dt::VAR_STRING, 50, false, 0, 82));
80  dt.add(new te::dt::StringProperty("attribute_var_unlimited_string", te::dt::STRING, 0, true, 0, 79));
81  dt.add(new te::gm::GeometryProperty("attribute_geom_polm", 4326, te::gm::PolygonMType, true, 0, 23331));
82  dt.add(new te::dt::SimpleProperty("attribute_int_serial", te::dt::INT32_TYPE, false, 0, 23));
83  te::da::PrimaryKey* pk = new te::da::PrimaryKey(&dt, 1000);
84  pk->setName("pk_dataset_type");
85  pk->add(dt.getProperty(5));
86 
87  te::da::UniqueKey* uk = new te::da::UniqueKey(&dt, 1001);
88  uk->setName("uk_dataset_type");
89  uk->add(dt.getProperty(1));
90  uk->add(dt.getProperty(2));
91 
92  te::da::Index* idx = new te::da::Index(&dt, 1002);
93  idx->setName("uk_dataset_type");
94  idx->add(dt.getProperty(1));
95  idx->add(dt.getProperty(2));
96 
97  uk->setAssociatedIndex(idx);
98 
100  cc->setName("cc_dataset_type");
101  cc->setExpression("a > b && c < d && z + 18 == 23");
102 
103  te::da::ForeignKey* fk = new te::da::ForeignKey(101011);
104  fk->setName("fk1_dataset_type");
105  fk->add(dt.getProperty(1));
106  fk->add(dt.getProperty(2));
107  dt.add(fk);
108 
109  CPPUNIT_ASSERT(dt.getType() == te::dt::DATASET_TYPE);
110  CPPUNIT_ASSERT(dt.getName() == "dataset_type");
111  CPPUNIT_ASSERT(dt.getId() == 666);
112  CPPUNIT_ASSERT(dt.getParent() == 0);
113  CPPUNIT_ASSERT(dt.size() == 6);
114  CPPUNIT_ASSERT(dt.has(dt.getProperty(0)));
115  CPPUNIT_ASSERT(dt.has(dt.getProperty(1)));
116  CPPUNIT_ASSERT(dt.has(dt.getProperty(2)));
117  CPPUNIT_ASSERT(dt.has(dt.getProperty(3)));
118  CPPUNIT_ASSERT(dt.has(dt.getProperty(4)));
119  CPPUNIT_ASSERT(dt.has(dt.getProperty(5)));
120 
121  CPPUNIT_ASSERT(dt.getPrimaryKey() == pk);
122  CPPUNIT_ASSERT(dt.getPrimaryKey()->has(dt.getProperty(5)));
123  CPPUNIT_ASSERT(dt.getPrimaryKey()->getDataSetType() == &dt);
124 
125  CPPUNIT_ASSERT(dt.getNumberOfUniqueKeys() == 1);
126  CPPUNIT_ASSERT(dt.getUniqueKey(0)->has(dt.getProperty(1)));
127  CPPUNIT_ASSERT(dt.getUniqueKey(0)->has(dt.getProperty(2)));
128  CPPUNIT_ASSERT(dt.getUniqueKey(0)->getAssociatedIndex() == idx);
129 
130  CPPUNIT_ASSERT(dt.getNumberOfIndexes() == 1);
131  CPPUNIT_ASSERT(dt.getIndex(0)->has(dt.getProperty(1)));
132  CPPUNIT_ASSERT(dt.getIndex(0)->has(dt.getProperty(2)));
133  CPPUNIT_ASSERT(dt.getIndex(0)->getDataSetType() == &dt);
134 
135  CPPUNIT_ASSERT(dt.getNumberOfCheckConstraints() == 1);
136  CPPUNIT_ASSERT(dt.getCheckConstraint(0)->getName() == "cc_dataset_type");
137  CPPUNIT_ASSERT(dt.getCheckConstraint(0)->getExpression() == "a > b && c < d && z + 18 == 23");
138  CPPUNIT_ASSERT(dt.getCheckConstraint(0)->getDataSetType() == &dt);
139 
140  CPPUNIT_ASSERT(dt.getNumberOfForeignKeys() == 1);
141  CPPUNIT_ASSERT(dt.getForeignKey(0)->has(dt.getProperty(1)));
142  CPPUNIT_ASSERT(dt.getForeignKey(0)->has(dt.getProperty(2)));
143  CPPUNIT_ASSERT(dt.getForeignKey(0)->getDataSetType() == &dt);
144  CPPUNIT_ASSERT(dt.getForeignKey(0)->getReferencedDataSetType() == 0);
145 
146  te::dt::SimpleProperty intt("attribute_int", te::dt::INT32_TYPE, true, 0, 23);
147  CPPUNIT_ASSERT(dt.has(&intt) == false);
148  CPPUNIT_ASSERT(dt.getPrimaryKey()->has(&intt) == false);
149  CPPUNIT_ASSERT(dt.getUniqueKey(0)->has(&intt) == false);
150  CPPUNIT_ASSERT(dt.getIndex(0)->has(&intt) == false);
151  CPPUNIT_ASSERT(dt.getForeignKey(0)->has(&intt) == false);
152  }
153 
154  {
155  te::da::DataSetType dt("dataset_type", 666);
156  dt.add(new te::dt::DateTimeProperty("attr_date", te::dt::DATE));
157  dt.add(new te::dt::DateTimeProperty("attr_date_dutarion", te::dt::DATE_DURATION));
158  dt.add(new te::dt::DateTimeProperty("attr_date_period", te::dt::DATE_PERIOD));
159  dt.add(new te::dt::DateTimeProperty("attr_time_duration", te::dt::TIME_DURATION));
160  dt.add(new te::dt::DateTimeProperty("attr_time_instant", te::dt::TIME_INSTANT));
161  dt.add(new te::dt::DateTimeProperty("attr_time_period", te::dt::TIME_PERIOD));
162  dt.add(new te::dt::DateTimeProperty("attr_time_instant_tz", te::dt::TIME_INSTANT_TZ));
163  dt.add(new te::dt::DateTimeProperty("attr_time_period_tz", te::dt::TIME_PERIOD_TZ));
164  dt.add(new te::dt::DateTimeProperty("attr_ordinal_instant", te::dt::ORDINAL_TIME_INSTANT));
165  dt.add(new te::dt::DateTimeProperty("attr_ordinal_period", te::dt::ORDINAL_TIME_PERIOD));
166  //int i = 0;
167  }
168 
169  {
170  te::da::DataSetType dt("dataset_type", 666);
171 
172  dt.add(new te::dt::NumericProperty("attribute_numeric", 10, 3, true, new std::string("34.78"), 1));
173  dt.add(new te::dt::StringProperty("attribute_fixed_string", te::dt::FIXED_STRING, 50));
174  dt.add(new te::dt::StringProperty("attribute_var_string", te::dt::VAR_STRING, 50, false, 0, 82));
175  dt.add(new te::dt::StringProperty("attribute_var_unlimited_string", te::dt::STRING, 0, true, 0, 79));
176  dt.add(new te::gm::GeometryProperty("attribute_geom_polm", 4326, te::gm::PolygonMType, true, 0, 23331));
177  dt.add(new te::dt::SimpleProperty("attribute_int_serial", te::dt::INT32_TYPE, false, 0, 23));
178 
179  te::da::PrimaryKey* pk = new te::da::PrimaryKey(&dt, 1000);
180  pk->setName("pk_dataset_type");
181  pk->add(dt.getProperty(5));
182 
183  te::da::UniqueKey* uk = new te::da::UniqueKey(&dt, 1001);
184  uk->setName("uk_dataset_type");
185  uk->add(dt.getProperty(1));
186  uk->add(dt.getProperty(2));
187 
188  te::da::Index* uk_idx = new te::da::Index(&dt, 1002);
189  uk_idx->setName("uk_dataset_type");
190  uk_idx->add(dt.getProperty(1));
191  uk_idx->add(dt.getProperty(2));
192 
193  uk->setAssociatedIndex(uk_idx);
194 
195  te::da::Index* pk_idx = new te::da::Index(&dt, 1002);
196  pk_idx->setName("pk_dataset_type");
197  pk_idx->add(dt.getProperty(0));
198  pk_idx->add(dt.getProperty(2));
199 
200  pk->setAssociatedIndex(pk_idx);
201 
202  te::da::Index* idx = new te::da::Index(&dt, 1002);
203  idx->setName("pk_dataset_type");
204  idx->add(dt.getProperty(4));
205 
206  te::da::CheckConstraint* cc = new te::da::CheckConstraint(&dt, 10034);
207  cc->setName("cc_dataset_type");
208  cc->setExpression("a > b && c < d && z + 18 == 23");
209 
210  te::da::ForeignKey* fk = new te::da::ForeignKey(101011);
211  fk->setName("fk1_dataset_type");
212  fk->add(dt.getProperty(1));
213  fk->add(dt.getProperty(2));
214  dt.add(fk);
215 
216  CPPUNIT_ASSERT(dt.getType() == te::dt::DATASET_TYPE);
217  CPPUNIT_ASSERT(dt.getName() == "dataset_type");
218  CPPUNIT_ASSERT(dt.getId() == 666);
219  CPPUNIT_ASSERT(dt.getParent() == 0);
220  CPPUNIT_ASSERT(dt.size() == 6);
221  CPPUNIT_ASSERT(dt.has(dt.getProperty(0)));
222  CPPUNIT_ASSERT(dt.has(dt.getProperty(1)));
223  CPPUNIT_ASSERT(dt.has(dt.getProperty(2)));
224  CPPUNIT_ASSERT(dt.has(dt.getProperty(3)));
225  CPPUNIT_ASSERT(dt.has(dt.getProperty(4)));
226  CPPUNIT_ASSERT(dt.has(dt.getProperty(5)));
227 
228  CPPUNIT_ASSERT(dt.getPrimaryKey() == pk);
229  CPPUNIT_ASSERT(dt.getPrimaryKey()->has(dt.getProperty(5)));
230  CPPUNIT_ASSERT(dt.getPrimaryKey()->getDataSetType() == &dt);
231  CPPUNIT_ASSERT(dt.getPrimaryKey()->getAssociatedIndex() == pk_idx);
232 
233  CPPUNIT_ASSERT(dt.getNumberOfUniqueKeys() == 1);
234  CPPUNIT_ASSERT(dt.getUniqueKey(0)->has(dt.getProperty(1)));
235  CPPUNIT_ASSERT(dt.getUniqueKey(0)->has(dt.getProperty(2)));
236  CPPUNIT_ASSERT(dt.getUniqueKey(0)->getAssociatedIndex() == uk_idx);
237 
238  CPPUNIT_ASSERT(dt.getNumberOfIndexes() == 3);
239  CPPUNIT_ASSERT(dt.getIndex(0)->has(dt.getProperty(1)));
240  CPPUNIT_ASSERT(dt.getIndex(0)->has(dt.getProperty(2)));
241  CPPUNIT_ASSERT(dt.getIndex(0)->getDataSetType() == &dt);
242  CPPUNIT_ASSERT(dt.getIndex(1)->has(dt.getProperty(0)));
243  CPPUNIT_ASSERT(dt.getIndex(1)->has(dt.getProperty(2)));
244  CPPUNIT_ASSERT(dt.getIndex(1)->getDataSetType() == &dt);
245  CPPUNIT_ASSERT(dt.getIndex(2)->has(dt.getProperty(4)));
246  CPPUNIT_ASSERT(dt.getIndex(2)->getDataSetType() == &dt);
247 
248  CPPUNIT_ASSERT(dt.getNumberOfCheckConstraints() == 1);
249  CPPUNIT_ASSERT(dt.getCheckConstraint(0)->getName() == "cc_dataset_type");
250  CPPUNIT_ASSERT(dt.getCheckConstraint(0)->getExpression() == "a > b && c < d && z + 18 == 23");
251  CPPUNIT_ASSERT(dt.getCheckConstraint(0)->getDataSetType() == &dt);
252 
253  CPPUNIT_ASSERT(dt.getNumberOfForeignKeys() == 1);
254  CPPUNIT_ASSERT(dt.getForeignKey(0)->has(dt.getProperty(1)));
255  CPPUNIT_ASSERT(dt.getForeignKey(0)->has(dt.getProperty(2)));
256  CPPUNIT_ASSERT(dt.getForeignKey(0)->getDataSetType() == &dt);
257  CPPUNIT_ASSERT(dt.getForeignKey(0)->getReferencedDataSetType() == 0);
258 
259  te::dt::SimpleProperty intt("attribute_int", te::dt::INT32_TYPE, true, 0, 23);
260  CPPUNIT_ASSERT(dt.has(&intt) == false);
261  CPPUNIT_ASSERT(dt.getPrimaryKey()->has(&intt) == false);
262  CPPUNIT_ASSERT(dt.getUniqueKey(0)->has(&intt) == false);
263  CPPUNIT_ASSERT(dt.getIndex(0)->has(&intt) == false);
264  CPPUNIT_ASSERT(dt.getForeignKey(0)->has(&intt) == false);
265 
266  CPPUNIT_ASSERT_NO_THROW(dt.remove(pk_idx));
267  CPPUNIT_ASSERT(dt.getPrimaryKey() == 0);
268  CPPUNIT_ASSERT(dt.getNumberOfIndexes() == 2);
269 
270  CPPUNIT_ASSERT_NO_THROW(dt.remove(uk_idx));
271  CPPUNIT_ASSERT(dt.getNumberOfUniqueKeys() == 0);
272  CPPUNIT_ASSERT(dt.getNumberOfIndexes() == 1);
273 
274  CPPUNIT_ASSERT_NO_THROW(dt.remove(fk));
275  CPPUNIT_ASSERT(dt.getNumberOfForeignKeys() == 0);
276 
277  CPPUNIT_ASSERT_NO_THROW(dt.remove(dt.getIndex(0)));
278  CPPUNIT_ASSERT(dt.getNumberOfIndexes() == 0);
279 
280  CPPUNIT_ASSERT_NO_THROW(dt.remove(dt.getCheckConstraint(0)));
281  CPPUNIT_ASSERT(dt.getNumberOfCheckConstraints() == 0);
282  }
283 
284 //#endif // TE_COMPILE_ALL
285 }
286 
287 
virtual void setName(const std::string &name)
It sets the constraint name.
Definition: Constraint.h:126
This file contains include headers for the Data Type module of TerraLib.
Property * getProperty(std::size_t i) const
It returns the i-th property.
void add(te::dt::Property *p)
It adds the property to the list of properties that participates in the unique key.
Definition: UniqueKey.h:124
bool has(te::dt::Property *p)
It verifies if Property is associated to the index.
bool has(Property *p) const
It checks if the Property "p" is associated to this property or any other parent. ...
Geometric property.
CPPUNIT_TEST_SUITE_REGISTRATION(TsDataSetType)
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
Index * getAssociatedIndex() const
It returns the associated index if one exists.
Definition: UniqueKey.h:131
bool has(te::dt::Property *p)
It verifies if Property takes part of the foreign key.
An atomic property like an integer or double.
A class that models the description of a dataset.
Definition: DataSetType.h:72
std::size_t getNumberOfUniqueKeys() const
It returns the number of unique keys defined for the dataset type.
Definition: DataSetType.h:269
CheckConstraint * getCheckConstraint(std::size_t i) const
It returns the i-th check-constraint associated to the dataset type.
Definition: DataSetType.h:354
bool has(const te::dt::Property *p) const
It verifies if Property is associated to the unique key.
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
bool has(const te::dt::Property *p) const
It verifies if Property is associated to the primary key.
Test suite for the DataSetType class.
Definition: TsDataSetType.h:53
void add(te::dt::Property *p)
It adds the property to the list of properties of the index.
A class that describes a check constraint.
DataSetType * getDataSetType() const
It returns the DataSetType associated to the index.
Index * getIndex(std::size_t i) const
It returns the i-th index associated to the dataset type.
Definition: DataSetType.h:428
Test suite for the DataSetType class.
The type for arbitrary precison numbers, like numeric(p, q).
void remove(Constraint *c)
It removes the constraint.
std::size_t getNumberOfForeignKeys() const
It returns the number of foreign keys defined for the dataset type.
Definition: DataSetType.h:467
const std::string & getExpression() const
It returns the check constraint expression.
void add(te::dt::Property *p)
It adds a property to the foreign key constraint.
Definition: ForeignKey.h:112
static te::dt::TimeDuration dt(20, 30, 50, 11)
Index * getAssociatedIndex() const
It returns the associated index if one exists.
Definition: PrimaryKey.h:137
std::size_t getNumberOfIndexes() const
It returns the number of indexes defined for the dataset type.
Definition: DataSetType.h:391
DataSetType * getReferencedDataSetType() const
It returns the referenced DataSetType of this foreign key constraint.
Definition: ForeignKey.h:153
It models a foreign key constraint for a DataSetType.
Definition: ForeignKey.h:50
The type for string types: FIXED_STRING, VAR_STRING or STRING.
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
void setAssociatedIndex(Index *idx)
It sets the associated index.
Definition: UniqueKey.h:138
Property * getParent() const
It returns the parent of this property, or NULL, if it doesn&#39;t have one.
Definition: Property.h:168
std::size_t size() const
It returns the number of properties of the CompositeProperty.
int getType() const
It returns the property data type.
Definition: Property.h:161
void add(Constraint *c)
It adds a new constraint.
void setAssociatedIndex(Index *idx)
It sets the associated index.
Definition: PrimaryKey.h:130
ForeignKey * getForeignKey(std::size_t i) const
It returns the i-th foreign key associated to the dataset type.
Definition: DataSetType.h:480
std::size_t getNumberOfCheckConstraints() const
It returns the number of check-constraints defined over the dataset type.
Definition: DataSetType.h:331
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
void setExpression(const std::string &e)
It sets the check constraint expression.
The type for date and time types: date, date period, date duration, time duration, time instant, time period, time instant with time zone or time period with time zone.
virtual DataSetType * getDataSetType() const
It returns the DataSetType associated to the constraint.
Definition: Constraint.h:133
void setName(const std::string &name)
It sets the index name.
This file contains include headers for the Vector Geometry model of TerraLib.
virtual const std::string & getName() const
It returns the constraint name.
Definition: Constraint.h:119
This file contains include headers for the Data Access module of TerraLib.
void tcCreateDataSetTypeAndChecks()
Test Case: creating a DataSetType and checking all methods.
It describes an index associated to a DataSetType.
unsigned int getId() const
It returns the property identifier.
Definition: Property.h:109
UniqueKey * getUniqueKey(std::size_t i) const
It returns the i-th unique key.
Definition: DataSetType.h:294
const std::string & getName() const
It returns the property name.
Definition: Property.h:127