Project

General

Profile

Bug #2535 » DboTestReload.C

Saif Rehman, 01/03/2014 10:00 PM

 
1
/*
2
 * Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium.
3
 *
4
 * See the LICENSE file for terms of use.
5
 */
6

    
7
#include <boost/test/unit_test.hpp>
8

    
9
#include <Wt/Dbo/Dbo>
10
#include <Wt/Dbo/backend/Postgres>
11
#include <Wt/Dbo/backend/MySQL>
12
#include <Wt/Dbo/backend/Sqlite3>
13
#include <Wt/Dbo/backend/Firebird>
14
#include <Wt/WDate>
15
#include <Wt/WDateTime>
16
#include <Wt/WTime>
17
#include <Wt/Dbo/WtSqlTraits>
18
#include <Wt/Dbo/ptr_tuple>
19

    
20
#include <string>
21
namespace dbo = Wt::Dbo;
22

    
23
class TestDbo;
24
typedef dbo::collection< dbo::ptr<TestDbo> > TestDboCollection;
25
class TestDbo : public Wt::Dbo::Dbo<TestDbo>
26
{
27
public:
28
        int intC;
29
        double doubleC;
30

    
31
        template<class Action>
32
        void persist(Action &a)
33
        {
34
                Wt::Dbo::field(a, intC, "intC");
35
                Wt::Dbo::field(a, doubleC, "doubleC");
36
        }
37
        static const char *TableName()
38
        {
39
                return "func";
40
        }
41
};
42

    
43

    
44
struct Dbo3Fixture
45
{
46
	typedef std::map<long long, Wt::Dbo::ptr<TestDbo>> TestMapType;
47
	TestMapType TestMap;
48
	dbo::SqlConnection *connection_;
49
	dbo::Session *session_;
50

    
51
  Dbo3Fixture()
52
  {
53
    connection_ = new dbo::backend::Sqlite3(":memory:");
54
    connection_->setProperty("show-queries", "true");
55

    
56
    session_ = new dbo::Session();
57
    session_->setConnection(*connection_);
58
    session_->mapClass<TestDbo>(TestDbo::TableName());
59
    session_->createTables();
60

    
61
	{
62
		dbo::Transaction tr(*session_);
63
		dbo::ptr<TestDbo> New1 = session_->add(new TestDbo());
64
		New1.modify()->intC = 1;
65
		New1.modify()->doubleC = 1.1;
66
		dbo::ptr<TestDbo> New2 = session_->add(new TestDbo());
67
		New2.modify()->intC = 2;
68
		New2.modify()->doubleC = 2.2;
69
	}
70

    
71
	{
72
		//Check initial values
73
		FetchAll();
74
		dbo::ptr<TestDbo> TestPtr = TestMap[1]; //id: 1
75
		std::cout << "Before modified: intC: " << TestPtr->intC << ", expected: 1" << std::endl;
76

    
77
		//Modify from outside session_'s knowledge
78
		dbo::Transaction tr(*session_);
79
		session_->execute(std::string("UPDATE ") + TestDbo::TableName() + " SET intC = 99 WHERE id = 1;");
80
		tr.commit();
81

    
82
		//Check again
83
		FetchAll();
84
		//should remain unchanged because in FetchAll() none of the ptr<C>s are deleted or modified but swapped with a local TestMap object
85
		std::cout << "After re-fetched: intC: " << TestPtr->intC << ", expected: 1" << std::endl;
86
		//should be a new value because FetchAll uses a collection to map all ptr<C> with newer values from the database
87
		TestPtr = TestMap[1]; //id: 1
88
		std::cout << "After after re-assignment: intC: " << TestPtr->intC << ", expected: 99" << std::endl;
89
		BOOST_REQUIRE(TestPtr->intC == 99);
90
	}
91
  }
92

    
93
  void FetchAll()
94
  {
95
	  //Strong exception safety(make TestMap empty by swapping with an empty map, swap back if exception is caught)
96
	  TestMapType testmap;
97
	  TestMap.swap(testmap);
98

    
99
	  try
100
	  {
101
		  dbo::Transaction tr(*session_);
102
		  TestDboCollection collection = session_->find<TestDbo>();
103
		  for(TestDboCollection::const_iterator itr = collection.begin();
104
			  itr != collection.end();
105
			  ++itr)
106
		  {
107
			  TestMap[itr->id()] = *itr;
108
		  }
109
		  tr.commit();
110
	  }
111
	  catch(...)
112
	  {
113
		  testmap.swap(TestMap);
114
	  }
115
  }
116

    
117
  ~Dbo3Fixture()
118
  {
119
    session_->dropTables();
120

    
121
    delete session_;
122
    delete connection_;
123
  }
124
};
125

    
126
BOOST_AUTO_TEST_CASE( dbo3_reload_test )
127
{
128
	Dbo3Fixture f;
129
}
(1-1/2)