File: grtpp_serialization_test.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (215 lines) | stat: -rw-r--r-- 5,597 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* 
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "testgrt.h"
#include "grt_test_utility.h"
#include "structs.test.h"
#include "grtdb/db_object_helpers.h"
#include "grts/structs.db.mysql.h"

BEGIN_TEST_DATA_CLASS(grtpp_serialization_test)
public:
  GRT grt;
END_TEST_DATA_CLASS

TEST_MODULE(grtpp_serialization_test, "GRT: serialization");

//using namespace grt;

//BEGIN_TESTS

//struct test_data {
//
//};
//
//
//typedef test_group<test_data> serializ_tg;
//typedef serializ_tg::object itgo;
//serializ_tg serialization_group("serizalization");



/*
 * - create bridged object for testing
 * - create tree with all item types
 * - repeat appearance of some item types
 * - serialize tree
 * - unserialize tree
 * - compare trees
 *   - check whether object ids continue the same
 *   - check whether referenced objects continue the same
 *   - perform self-test of bridged objects
 */

TEST_FUNCTION(1)
{
  grt.load_metaclasses("data/structs.test.xml");

  ensure_equals("load structs", grt.get_metaclasses().size(), 6U);
  grt.scan_metaclasses_in("../../res/grt/");
  grt.end_loading_metaclasses();
}

void test_serialization(GRT& grt, const ValueRef& val)
{
  static const std::string filename("serialization_test.xml");
  grt.serialize(val, filename);
  ValueRef res_val(grt.unserialize(filename));
  grt_ensure_equals(
    "serialization test",
    res_val,
    val,
    true);
}

TEST_FUNCTION(2)
{
  StringRef sv("<tag1>%string_value/</tag1>");
  IntegerRef iv(-1);
  DoubleRef dv(1.12345678901234);

  // test simple types
  test_serialization(grt, sv);
  test_serialization(grt, iv);
  test_serialization(grt, dv);
  test_serialization(grt, ValueRef(sv));
  test_serialization(grt, ValueRef(iv));
  test_serialization(grt, ValueRef(dv));

  // test object type
  //const char* OBJ_BOOK_PATH("test.Book");
  //const char* OBJ_PUBLISHER_PATH("test.Publisher");
  //const char* OBJ_AUTHOR_PATH("test.Author");
  const size_t AUTHORS_COUNT(3);

  //ObjectRef obj(grt.create_object_from_va<ObjectRef>(OBJ_BOOK_PATH, NULL));
  test_BookRef obj(&grt);
    
  obj->set_member("title", sv);
  obj->set_member("pages", iv);

  test_PublisherRef publisher(&grt);
  publisher->set_member("name", sv);
  publisher->set_member("phone", StringRef(((std::string)sv) + " 555-55-55"));
  obj->set_member("publisher", publisher);

  //ObjectRef authors_arr[AUTHORS_COUNT];
  std::stringstream ss;
  for (size_t n= 0; n<AUTHORS_COUNT; n++)
  {
    test_AuthorRef author(&grt);
    ss << n;
    author->set_member("name", StringRef(std::string("Author") + ss.str().c_str()));
    obj->authors().insert(author);
  }

  obj->set_member("price", DoubleRef(dv+1.1));

  test_AuthorRef author(&grt);
  DictRef extras(DictRef::cast_from(obj->get_member("extras")));
  extras.set("extra_string", sv);
  extras.set("extra_int", iv);
  extras.set("extra_double", dv);
  extras.set("extra_obj", author);

  test_serialization(grt, obj);
}


TEST_FUNCTION(3)
{
  ObjectListRef list(&grt);

  test_BookRef book1(&grt);
  test_BookRef book2(&grt);
  test_AuthorRef author(&grt);

  author->name("the author");
  
  book1->title("the book1");
  book1->authors().insert(author);

  book2->title("the book2");
  book2->authors().insert(author);
  
  list.insert(book1);
  list.insert(book2);

  test_serialization(grt, list);
}

TEST_FUNCTION(4)
{
  db_mysql_CatalogRef catalog(db_mysql_CatalogRef::cast_from(grt.unserialize("data/serialization/catalog.xml")));

  ObjectRef owner = catalog->schemata().get(0)->tables().get(0)->indices().get(0)->owner();
  tut::ensure("Check owner set", NULL != owner.valueptr());
  
  tut::ensure("Check owner", catalog->schemata().get(0)->tables().get(0).valueptr() == owner.valueptr());
}


TEST_FUNCTION(5)
{
  // test serialization of lists with NULL values

  grt::ListRef<db_Table> list(&grt);

  list.insert(db_TableRef(&grt));
  list.insert(db_TableRef());
  list.insert(db_TableRef(&grt));

  grt.serialize(list, "null_list.xml");

  list= grt::ListRef<db_Table>::cast_from(grt.unserialize("null_list.xml"));

  ensure("list[0]", list[0].is_valid());
  ensure("list[1]", list[1].is_valid()==false);
  ensure("list[2]", list[2].is_valid());
}


#ifdef badtest
TEST_FUNCTION(5)
{
  // dontfollow means the object will be saved as a link, not that it wont be saved

  static const std::string filename("serialization_test.xml");

  {
    db_CatalogRef catalog(&grt);
    ListRef<db_SimpleDatatype> datatypes(&grt);
    catalog.simpleDatatypes(datatypes);
    db_SimpleDatatypeRef datatype(&grt);
    datatypes.insert(datatype);
    grt.serialize(catalog, filename); // the only set attr simpleDatatypes shouldn't be serialized
  }

  // now compare with empty catalog
  db_CatalogRef catalog(&grt);
  ValueRef res_catalog(grt.unserialize(filename));
  grt_ensure_equals(
    "Check attr:dontfollow=\"1\"",
    res_catalog,
    catalog);
}
#endif


END_TESTS