postgis/dataset/TsDataSet.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 terralib/unittest/dataaccess/TsDataSet.cpp
22 
23  \brief A test suite for the DataSet Class.
24 
25  */
26 
27 // TerraLib
32 #include <terralib/datatype.h>
35 #include "../Utils.h"
36 
37 // STL
38 #include <iostream>
39 #include <exception>
40 
41 // Boost
42 #include <boost/test/unit_test.hpp>
43 
44 std::unique_ptr<te::da::DataSet> GetDataSet(te::da::DataSource* ds)
45 {
46  std::unique_ptr<te::da::DataSet> dataSet;
47 
48  try
49  {
50  if (!ds)
51  return dataSet;
52 
53  ds->open();
54 
55  std::string name = "munic_goias";
56 
57  std::unique_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
58 
59  dataSet = transactor->getDataSet(name, te::common::RANDOM, false);
60  }
61  catch(const std::exception& e)
62  {
63  std::cout << std::endl
64  << "An exception has occurred in the PostGIS Example: "
65  << e.what() << std::endl;
66  }
67  catch(...)
68  {
69  std::cout << std::endl
70  << "An unexpected exception has occurred in the PostGIS Example!"
71  << std::endl;
72  }
73 
74  return dataSet;
75 }
76 
77 
78 
79 BOOST_AUTO_TEST_SUITE( dataSet_tests )
80 
81 BOOST_AUTO_TEST_CASE( moveBeforeFirst_test )
82 {
83  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
84  std::unique_ptr<te::da::DataSet> dataSet = GetDataSet(ds.get());
85  dataSet->moveBeforeFirst();
86  BOOST_CHECK(dataSet->isBeforeBegin());
87 }
88 
89 BOOST_AUTO_TEST_CASE( moveFirst_test )
90 {
91  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
92  std::unique_ptr<te::da::DataSet> dataSet = GetDataSet(ds.get());
93  dataSet->moveFirst();
94  BOOST_CHECK(dataSet->isAtBegin());
95 }
96 
97 BOOST_AUTO_TEST_CASE( moveLast_test )
98 {
99  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
100  std::unique_ptr<te::da::DataSet> dataSet = GetDataSet(ds.get());
101  dataSet->moveLast();
102  BOOST_CHECK(dataSet->isAtEnd());
103 }
104 
106 {
107  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
108  std::unique_ptr<te::da::DataSet> dataSet = GetDataSet(ds.get());
109  if(dataSet->move(8))
110  {
111  int id = dataSet->getInt32(0);
112  std::cout << std::endl
113  << "ID = " << boost::lexical_cast<std::string>(id)
114  << std::endl;
115  }
116  else
117  {
118  std::cout << std::endl
119  << "ID not found."
120  << std::endl;
121  }
122 
123  if(dataSet->move(20))
124  {
125  int id = dataSet->getInt32(0);
126  std::cout << std::endl
127  << "ID = " << boost::lexical_cast<std::string>(id)
128  << std::endl;
129  }
130  else
131  {
132  std::cout << std::endl
133  << "ID not found."
134  << std::endl;
135  }
136 
137  if(dataSet->move(5000))
138  {
139  int id = dataSet->getInt32(0);
140  std::cout << std::endl
141  << "ID = " << boost::lexical_cast<std::string>(id)
142  << std::endl;
143  }
144  else
145  {
146  std::cout << std::endl
147  << "ID not found."
148  << std::endl;
149  }
150 }
151 
152 BOOST_AUTO_TEST_CASE( moveNext_test )
153 {
154  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
155  std::unique_ptr<te::da::DataSet> dataSet = GetDataSet(ds.get());
156  dataSet->moveBeforeFirst();
157 
158  BOOST_CHECK(dataSet->isBeforeBegin());
159 
160  while(dataSet->moveNext())
161  {
162  BOOST_CHECK(dataSet->isPositionValid());
163  }
164 }
165 
166 BOOST_AUTO_TEST_CASE( movePrevious_test )
167 {
168  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
169  std::unique_ptr<te::da::DataSet> dataSet = GetDataSet(ds.get());
170  dataSet->moveLast();
171 
172  BOOST_CHECK(dataSet->isAtEnd());
173 
174  do
175  {
176  BOOST_CHECK(dataSet->isPositionValid());
177  }while(dataSet->movePrevious());
178 }
179 
180 BOOST_AUTO_TEST_SUITE_END()
181 
This file contains include headers for the Data Type module of TerraLib.
virtual std::unique_ptr< DataSourceTransactor > getTransactor()=0
It returns the set of parameters used to set up the access channel to the underlying repository...
virtual void open()=0
It opens the data source and makes it ready for using.
std::unique_ptr< te::da::DataSet > GetDataSet(te::da::DataSource *ds)
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
static te::dt::Date ds(2010, 01, 01)
std::unique_ptr< te::da::DataSource > GetPostGISConnection()
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
A Transactor can be viewed as a connection to the data source for reading/writing things into it...
BOOST_AUTO_TEST_CASE(moveFirst_test)
BOOST_AUTO_TEST_SUITE(dataSet_tests) BOOST_AUTO_TEST_CASE(moveBeforeFirst_test)
A factory for data sources.
Geometric property.
Implementation of a dataset for the PostGIS driver.
Utility functions for the data access module.