File: grtdiff_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 (239 lines) | stat: -rw-r--r-- 6,859 bytes parent folder | download
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright (c) 2012, 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 "grtpp.h"
#include <diffchange.h>
#include "grts/structs.db.mgmt.h"
#include "util_functions.h"
#include "grtdb/diff_dbobjectmatch.h"

using namespace std;
using namespace tut;

BEGIN_TEST_DATA_CLASS(grt_diff)
public:
  GRT grt;
  db_mgmt_RdbmsRef rdbms;
END_TEST_DATA_CLASS

BEGIN_TEST_DATA_CLASS(grt_diff_suite)
END_TEST_DATA_CLASS

BEGIN_TEST_DATA_CLASS(grt_diff_omf_suite)
END_TEST_DATA_CLASS

struct DiffSubSuiteParam
{
  GRT* grt;
  std::string source_file;
  std::string target_file;
  bool logging;
};

typedef std::map<std::string, std::vector<DiffSubSuiteParam> > DiffTestsParams;
DiffTestsParams tests_params;

void test_files(GRT& grt, std::string source_file, std::string target_file, bool logging= false, grt::Omf* omf= NULL);

void test_suites(GRT& grt, 
                 const char* rootpath, 
                 bool run_single_test, 
                 const char *single_suite, 
                 const char *single_test, 
                 grt::Omf* omf= NULL);

TEST_MODULE(grt_diff, "GRT: diff");

TEST_FUNCTION(1)
{
  grt.scan_metaclasses_in("../../res/grt/");
  grt.scan_metaclasses_in("data");
  grt.end_loading_metaclasses();

  rdbms= db_mgmt_RdbmsRef::cast_from(grt.unserialize("data/res/mysql_rdbms_info.xml"));
  ensure("db_mgmt_RdbmsRef initialization", rdbms.is_valid());

  grt.set_root(rdbms);
}

TEST_FUNCTION(2)
{
  const bool run_single_test= false;
  const char *single_suite= "list-reorder-simple-type";
  const char *single_test= "s04_order_end_beg.xml";

  std::string rootpath= "data/diff";
  grt::default_omf omf;
  test_suites(grt, rootpath.c_str(), run_single_test, single_suite, single_test, &omf);
}

TEST_FUNCTION(3)
{
  const bool run_single_test= false;
  const char *single_suite= "";
  const char *single_test= "";

  std::string rootpath= "data/diff-omf";
  grt::default_omf omf;
  test_suites(grt, rootpath.c_str(), run_single_test, single_suite, single_test, &omf);
}

END_TESTS

#if 1

void test_suites(
      GRT &grt,
      const char* rootpath, 
      bool run_single_test,   
      const char *single_suite,
      const char *single_test,
      grt::Omf* omf
    )
{
  GError* err= NULL;
  GDir *root= g_dir_open(rootpath, 0, &err);
  if (err)
  {
    std::cout << ((err->message)? (const char*)err->message: (const char*)"unknown error") << std::endl; 
    g_error_free(err);
    err= NULL;
  }

  const char* suite_name= single_suite;

  while ((suite_name= g_dir_read_name(root)))
  {
    if (run_single_test)
      suite_name= single_suite;

    // Filter out unwanted folders (e.g. from version control or disabled test cases).
    // TODO: x_integration and x_sakira-db must be fixed yet (xml recreated from model). They are disabled currently in file system.
    if (strlen(suite_name) < 1 || suite_name[0] == '.')
      continue;

    GDir *test_suite= g_dir_open((rootpath +std::string("/")+ suite_name).c_str(), 0, &err);
    if (err)
    {
      g_error_free(err);
      err= NULL;
    }
    else
    { 
      std::string test_group_name= std::string("grt_diff::") + suite_name;
      // TODO LEAK!
      DiffTestsParams::iterator iter= tests_params.insert(std::make_pair(test_group_name, std::vector<DiffSubSuiteParam>())).first;
      //Test_group<testsuite> *group= new Test_group<testsuite>(&test_params, iter->first.c_str(), iter->first.c_str());

      std::string initial_object;
      const char* test_name= single_test;

      while ((test_name= g_dir_read_name(test_suite)))
      {
        // Let's assume that initial_object goes first - TODO!!
        if (initial_object.empty())
        {
          initial_object= test_name;
          if (!run_single_test)
            continue;
        }
        DiffSubSuiteParam p;
        p.grt= &grt;
        p.source_file= rootpath + std::string("/") + suite_name + "/" + initial_object;
        p.target_file= rootpath + std::string("/") + suite_name + "/" + (run_single_test? single_test: test_name);
        p.logging= run_single_test;

        test_files(grt, p.source_file, p.target_file, p.logging, omf);

        if (run_single_test)
          break;
      };
    }
    if (test_suite)
      g_dir_close(test_suite);

    if (run_single_test)
      break;
  }
  if (root)
    g_dir_close(root);
}

void test_files(GRT& grt, std::string source_file, std::string target_file, bool logging, grt::Omf* omf)
{
  ValueRef v(source_file.size() > 0 ? grt.unserialize(source_file) : ValueRef());
  ValueRef target(grt.unserialize(target_file));
  ValueRef target1(grt.unserialize(target_file));

  assure(v.is_valid());
  assure(target.is_valid());
  if (logging)
  {
    // std::ofstream target_log(target_file + ".old.log");
    // std::cout << myx_grt_value_to_xml(grt.grt(), v) << std::endl;
    // target_log << myx_grt_value_to_xml(grt.grt(), target) << std::endl;
  }
  
  test_time_point t1;
  grt::NormalizedComparer normalizer(&grt);
  normalizer.init_omf(omf);
  boost::shared_ptr<DiffChange> change= diff_make(v, target, omf);
  test_time_point t2;
  if (logging)
  {
    double time_rate= test_time_point(t2 - t1).ticks_/1000.;
    std::cout << "Xmldiff: " << time_rate << " [sec]" << std::endl;
    std::cout << std::endl;
    if (change)
      change->dump_log(0);
  }
  
  ValueRef source_bkup= copy_value(v, true);
  ValueRef source_to_change= copy_value(v, true);
  test_time_point t3;
//  if (change)
//    diff_apply(source_to_change, *change);
  test_time_point t4;

  if (logging)
  {
    double time_rate= (t4 - t3).ticks_/1000.;
    std::cout << "Xmlapply: " << time_rate << " [sec]" << std::endl;

    grt.serialize(source_to_change, target_file + ".log");
  }
  
  grt::NormalizedComparer normalizer2(&grt);
  normalizer2.init_omf(omf);
  
  // QQQ Likely will fail due to diff_apply removal
  boost::shared_ptr<DiffChange> zero_change= diff_make(source_to_change, target1, omf);
  
  ensure("unexpected change", zero_change == NULL);

  // QQQ
  // Must be fixed as the diff code uses pointer invariance for grt::Object values to determine changed entries.
  assure_grt_values_equal(source_bkup, v);
}

#endif