TsConnectedDataSet.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/TsConnectedDataSet.cpp
22 
23  \brief A test suite for the ConnectedDataSet 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> GetConnectedDataSet(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();
60  const bool isConnected = dsCap.isConnected();
61 
62  dataSet = transactor->getDataSet(name, te::common::RANDOM, isConnected);
63  }
64  catch(const std::exception& e)
65  {
66  std::cout << std::endl
67  << "An exception has occurred in the PostGIS Example: "
68  << e.what() << std::endl;
69  }
70  catch(...)
71  {
72  std::cout << std::endl
73  << "An unexpected exception has occurred in the PostGIS Example!"
74  << std::endl;
75  }
76 
77  return dataSet;
78 }
79 
80 
81 
82 BOOST_AUTO_TEST_SUITE( connectedDataSet_tests )
83 
84 BOOST_AUTO_TEST_CASE( moveBeforeFirst_test )
85 {
86  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
87  std::unique_ptr<te::da::DataSet> dataSet = GetConnectedDataSet(ds.get());
88  dataSet->moveBeforeFirst();
89  BOOST_CHECK(dataSet->isBeforeBegin());
90 }
91 
92 BOOST_AUTO_TEST_CASE( moveFirst_test )
93 {
94  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
95  std::unique_ptr<te::da::DataSet> dataSet = GetConnectedDataSet(ds.get());
96  dataSet->moveFirst();
97 
98  BOOST_CHECK(dataSet->isAtBegin());
99 
100  int id = dataSet->getInt32(0);
101  std::cout << std::endl
102  << "First ID = " << boost::lexical_cast<std::string>(id)
103  << std::endl;
104 }
105 
106 BOOST_AUTO_TEST_CASE( moveLast_test )
107 {
108  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
109  std::unique_ptr<te::da::DataSet> dataSet = GetConnectedDataSet(ds.get());
110  dataSet->moveLast();
111 
112  BOOST_CHECK(dataSet->isAtEnd());
113 
114  int id = dataSet->getInt32(0);
115  std::cout << std::endl
116  << "Last ID = " << boost::lexical_cast<std::string>(id)
117  << std::endl;
118 }
119 
121 {
122  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
123  std::unique_ptr<te::da::DataSet> dataSet = GetConnectedDataSet(ds.get());
124  if(dataSet->move(8))
125  {
126  int id = dataSet->getInt32(0);
127  std::cout << std::endl
128  << "ID = " << boost::lexical_cast<std::string>(id)
129  << std::endl;
130  }
131  else
132  {
133  std::cout << std::endl
134  << "ID not found."
135  << std::endl;
136  }
137 
138  if(dataSet->move(20))
139  {
140  int id = dataSet->getInt32(0);
141  std::cout << std::endl
142  << "ID = " << boost::lexical_cast<std::string>(id)
143  << std::endl;
144  }
145  else
146  {
147  std::cout << std::endl
148  << "ID not found."
149  << std::endl;
150  }
151 
152  if(dataSet->move(5000))
153  {
154  int id = dataSet->getInt32(0);
155  std::cout << std::endl
156  << "ID = " << boost::lexical_cast<std::string>(id)
157  << std::endl;
158  }
159  else
160  {
161  std::cout << std::endl
162  << "ID not found."
163  << std::endl;
164  }
165 }
166 
167 BOOST_AUTO_TEST_CASE( moveNext_test )
168 {
169  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
170  std::unique_ptr<te::da::DataSet> dataSet = GetConnectedDataSet(ds.get());
171  dataSet->moveBeforeFirst();
172 
173  BOOST_CHECK(dataSet->isBeforeBegin());
174 
175  while(dataSet->moveNext())
176  {
177  int id = dataSet->getInt32(0);
178  std::cout << std::endl
179  << "Current ID = " << boost::lexical_cast<std::string>(id)
180  << std::endl;
181  }
182 }
183 
184 BOOST_AUTO_TEST_CASE( movePrevious_test )
185 {
186  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
187  std::unique_ptr<te::da::DataSet> dataSet = GetConnectedDataSet(ds.get());
188  dataSet->moveLast();
189 
190  BOOST_CHECK(dataSet->isAtEnd());
191 
192  do
193  {
194  int id = dataSet->getInt32(0);
195  std::cout << std::endl
196  << "Current ID = " << boost::lexical_cast<std::string>(id)
197  << std::endl;
198  }while(dataSet->movePrevious());
199 }
200 
201 BOOST_AUTO_TEST_SUITE_END()
202 
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.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
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 class that informs what the dataset implementation of a given data source can perform.
A Transactor can be viewed as a connection to the data source for reading/writing things into it...
std::unique_ptr< te::da::DataSet > GetConnectedDataSet(te::da::DataSource *ds)
virtual const DataSourceCapabilities & getCapabilities() const =0
It returns the known capabilities of the data source.
BOOST_AUTO_TEST_SUITE(connectedDataSet_tests) BOOST_AUTO_TEST_CASE(moveBeforeFirst_test)
A factory for data sources.
Geometric property.
Utility functions for the data access module.
A dataset is the unit of information manipulated by the data access module of TerraLib.
const DataSetCapabilities & getDataSetCapabilities() const
BOOST_AUTO_TEST_CASE(moveFirst_test)