~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to library/grt/unit-tests/diff/grtdiff_test.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "stdafx.h"
 
2
 
 
3
#include "testgrt.h"
 
4
 
 
5
#include "grt_test_utility.h"
 
6
 
 
7
#include "grtpp.h"
 
8
#include "grts/structs.db.mgmt.h"
 
9
#include "util_functions.h"
 
10
#include "grtdb/diff_dbobjectmatch.h"
 
11
 
 
12
using namespace std;
 
13
using namespace tut;
 
14
 
 
15
BEGIN_TEST_DATA_CLASS(grt_diff)
 
16
public:
 
17
  GRT grt;
 
18
  db_mgmt_RdbmsRef rdbms;
 
19
END_TEST_DATA_CLASS
 
20
 
 
21
BEGIN_TEST_DATA_CLASS(grt_diff_suite)
 
22
END_TEST_DATA_CLASS
 
23
 
 
24
BEGIN_TEST_DATA_CLASS(grt_diff_omf_suite)
 
25
END_TEST_DATA_CLASS
 
26
 
 
27
struct DiffSubSuiteParam
 
28
{
 
29
  GRT* grt;
 
30
  std::string source_file;
 
31
  std::string target_file;
 
32
  bool logging;
 
33
};
 
34
 
 
35
typedef std::map<std::string, std::vector<DiffSubSuiteParam> > DiffTestsParams;
 
36
DiffTestsParams tests_params;
 
37
 
 
38
void test_files(GRT& grt, std::string source_file, std::string target_file, bool logging= false, grt::Omf* omf= NULL);
 
39
 
 
40
void test_suites(GRT& grt, 
 
41
                 const char* rootpath, 
 
42
                 bool run_single_test, 
 
43
                 const char *single_suite, 
 
44
                 const char *single_test, 
 
45
                 grt::Omf* omf= NULL);
 
46
 
 
47
TEST_MODULE(grt_diff, "GRT: diff");
 
48
 
 
49
TEST_FUNCTION(1)
 
50
{
 
51
  grt.scan_metaclasses_in("../../res/grt/");
 
52
  grt.scan_metaclasses_in("data");
 
53
  grt.end_loading_metaclasses();
 
54
 
 
55
  rdbms= db_mgmt_RdbmsRef::cast_from(grt.unserialize("../../modules/db.mysql/res/mysql_rdbms_info.xml"));
 
56
  ensure("db_mgmt_RdbmsRef initialization", rdbms.is_valid());
 
57
 
 
58
  grt.set_root(rdbms);
 
59
}
 
60
 
 
61
TEST_FUNCTION(2)
 
62
{
 
63
  const bool run_single_test= false;
 
64
  const char *single_suite= "list-reorder-simple-type";
 
65
  const char *single_test= "s04_order_end_beg.xml";
 
66
 
 
67
  std::string rootpath= "data/diff";
 
68
 
 
69
  test_suites(grt, rootpath.c_str(), run_single_test, single_suite, single_test);
 
70
}
 
71
 
 
72
TEST_FUNCTION(3)
 
73
{
 
74
  const bool run_single_test= false;
 
75
  const char *single_suite= "";
 
76
  const char *single_test= "";
 
77
 
 
78
  std::string rootpath= "data/diff-omf";
 
79
 
 
80
  test_suites(grt, rootpath.c_str(), run_single_test, single_suite, single_test);
 
81
}
 
82
 
 
83
END_TESTS
 
84
 
 
85
#if 1
 
86
 
 
87
void test_suites(
 
88
      GRT &grt,
 
89
      const char* rootpath, 
 
90
      bool run_single_test,   
 
91
      const char *single_suite,
 
92
      const char *single_test,
 
93
      grt::Omf* omf
 
94
    )
 
95
{
 
96
  GError* err= NULL;
 
97
  GDir *root= g_dir_open(rootpath, 0, &err);
 
98
  if (err)
 
99
  {
 
100
    std::cout << ((err->message)? (const char*)err->message: (const char*)"unknown error") << std::endl; 
 
101
    g_error_free(err);
 
102
    err= NULL;
 
103
  }
 
104
 
 
105
  const char* suite_name= single_suite;
 
106
 
 
107
  while (suite_name= g_dir_read_name(root))
 
108
  {
 
109
    if (run_single_test)
 
110
      suite_name= single_suite;
 
111
 
 
112
    // Filter out unwanted folders (e.g. from version control or disabled test cases).
 
113
    // TODO: x_integration and x_sakira-db must be fixed yet (xml recreated from model). They are disabled currently in file system.
 
114
    if (strlen(suite_name) < 1 || suite_name[0] == '.')
 
115
      continue;
 
116
 
 
117
    GDir *test_suite= g_dir_open((rootpath +std::string("/")+ suite_name).c_str(), 0, &err);
 
118
    if (err)
 
119
    {
 
120
      g_error_free(err);
 
121
      err= NULL;
 
122
    }
 
123
    else
 
124
    { 
 
125
      std::string test_group_name= std::string("grt_diff::") + suite_name;
 
126
      // TODO LEAK!
 
127
      DiffTestsParams::iterator iter= tests_params.insert(std::make_pair(test_group_name, std::vector<DiffSubSuiteParam>())).first;
 
128
      //Test_group<testsuite> *group= new Test_group<testsuite>(&test_params, iter->first.c_str(), iter->first.c_str());
 
129
 
 
130
      std::string initial_object;
 
131
      const char* test_name= single_test;
 
132
      int i= 1;
 
133
      while (test_name= g_dir_read_name(test_suite))
 
134
      {
 
135
        // Let's assume that initial_object goes first - TODO!!
 
136
        if (initial_object.empty())
 
137
        {
 
138
          initial_object= test_name;
 
139
          if (!run_single_test)
 
140
            continue;
 
141
        }
 
142
        DiffSubSuiteParam p;
 
143
        p.grt= &grt;
 
144
        p.source_file= rootpath + std::string("/") + suite_name + "/" + initial_object;
 
145
        p.target_file= rootpath + std::string("/") + suite_name + "/" + (run_single_test? single_test: test_name);
 
146
        p.logging= run_single_test;
 
147
 
 
148
        test_files(grt, p.source_file, p.target_file, p.logging, omf);
 
149
 
 
150
        if (run_single_test)
 
151
          break;
 
152
      };
 
153
    }
 
154
    if (test_suite)
 
155
      g_dir_close(test_suite);
 
156
 
 
157
    if (run_single_test)
 
158
      break;
 
159
  }
 
160
  if (root)
 
161
    g_dir_close(root);
 
162
}
 
163
 
 
164
void test_files(GRT& grt, std::string source_file, std::string target_file, bool logging, grt::Omf* omf)
 
165
{
 
166
  ValueRef v(source_file.size() > 0 ? grt.unserialize(source_file) : ValueRef());
 
167
  ValueRef target(grt.unserialize(target_file));
 
168
  ValueRef target1(grt.unserialize(target_file));
 
169
 
 
170
  assure(v.is_valid());
 
171
  assure(target.is_valid());
 
172
  if (logging)
 
173
  {
 
174
    // std::ofstream target_log(target_file + ".old.log");
 
175
    // std::cout << myx_grt_value_to_xml(grt.grt(), v) << std::endl;
 
176
    // target_log << myx_grt_value_to_xml(grt.grt(), target) << std::endl;
 
177
  }
 
178
  
 
179
  test_time_point t1;
 
180
  DiffChange* change= diff_make(v, target, omf, grt::NormalizedComparer(&grt));
 
181
  test_time_point t2;
 
182
  if (logging)
 
183
  {
 
184
    double time_rate= test_time_point(t2 - t1).get_ticks()/1000.;
 
185
    std::cout << "Xmldiff: " << time_rate << " [sec]" << std::endl;
 
186
    std::cout << std::endl;
 
187
    if (change)
 
188
      diff_dump(*change);
 
189
  }
 
190
  
 
191
  ValueRef source_bkup= copy_value(v, true);
 
192
  ValueRef source_to_change= copy_value(v, true);
 
193
  test_time_point t3;
 
194
//  if (change)
 
195
//    diff_apply(source_to_change, *change);
 
196
  test_time_point t4;
 
197
 
 
198
  if (logging)
 
199
  {
 
200
    double time_rate= (t4 - t3).get_ticks()/1000.;
 
201
    std::cout << "Xmlapply: " << time_rate << " [sec]" << std::endl;
 
202
 
 
203
    grt.serialize(source_to_change, target_file + ".log");
 
204
  }
 
205
  
 
206
  // QQQ Likely will fail due to diff_apply removal
 
207
  DiffChange* zero_change= diff_make(source_to_change, target1, omf, grt::NormalizedComparer<grt::GRT*>(&grt));
 
208
  
 
209
  ensure("unexpected change", zero_change == NULL);
 
210
 
 
211
  // QQQ
 
212
  // Must be fixed as the diff code uses pointer invariance for grt::Object values to determine changed entries.
 
213
  assure_grt_values_equal(source_bkup, v);
 
214
}
 
215
 
 
216
#endif