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

« back to all changes in this revision

Viewing changes to backend/wbprivate/workbench/unit-tests/wb_copy_paste_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
/* 
 
2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; version 2 of the
 
7
 * License.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
 * GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 */
 
19
 
 
20
#include "tut_stdafx.h"
 
21
 
 
22
#include "wb_helpers.h"
 
23
#include "grtdb/db_object_helpers.h"
 
24
#include "grt_test_utility.h"
 
25
#include "grtpp_util.h"
 
26
#include "base/string_utilities.h"
 
27
 
 
28
 
 
29
using namespace base;
 
30
using namespace wb;
 
31
 
 
32
BEGIN_TEST_DATA_CLASS(wb_copy_paste)
 
33
 
 
34
END_TEST_DATA_CLASS;
 
35
 
 
36
 
 
37
 
 
38
TEST_MODULE(wb_copy_paste, "copy/paste related tests");
 
39
 
 
40
 
 
41
static bool match_member(const grt::MetaClass::Member *member, const grt::ObjectRef &copy, const grt::ObjectRef &source)
 
42
{
 
43
  if (!grt::is_simple_type(member->type.base.type))
 
44
    return true;
 
45
 
 
46
  grt::ValueRef value1;
 
47
  grt::ValueRef value2;
 
48
 
 
49
  value1= source.get_member(member->name);
 
50
  value2= copy.get_member(member->name);
 
51
 
 
52
  ensure_equals(member->name, value1.repr(), value2.repr());
 
53
  
 
54
  return true;
 
55
}
 
56
 
 
57
 
 
58
static void ensure_simple_contents_match(const grt::ObjectRef &copy, const grt::ObjectRef &source)
 
59
{
 
60
  grt::MetaClass *mc= copy.get_metaclass();
 
61
  
 
62
  mc->foreach_member(boost::bind(&match_member, _1, copy, source));
 
63
}
 
64
 
 
65
 
 
66
static void ensure_list_contents_copy(const grt::BaseListRef &copy, const grt::BaseListRef &source)
 
67
{
 
68
  ensure("table columns list are not same", copy.valueptr() != source.valueptr());
 
69
 
 
70
  ensure_equals("list size", copy.count(), source.count());
 
71
 
 
72
  for (size_t c= copy.count(), i= 0; i < c; i++)
 
73
  {
 
74
    ensure("list content not same", copy[i].valueptr() != source[i].valueptr());
 
75
 
 
76
    ObjectRef copyRef= ObjectRef::cast_from(copy[i]);
 
77
    ObjectRef sourceRef= ObjectRef::cast_from(source[i]);
 
78
    ensure_simple_contents_match(grt::ObjectRef(copyRef), grt::ObjectRef(sourceRef));
 
79
  }
 
80
}
 
81
 
 
82
 
 
83
TEST_FUNCTION(1)
 
84
{
 
85
  WBTester wb;
 
86
 
 
87
  wb.wb->open_document("data/workbench/all_objects.mwb");
 
88
 
 
89
  ensure_equals("figure count", wb.get_pview()->figures().count(), 6U);
 
90
 
 
91
  workbench_physical_TableFigureRef source, copy;
 
92
  source= workbench_physical_TableFigureRef::cast_from(grt::find_named_object_in_list(wb.get_pview()->figures(), "table1"));
 
93
 
 
94
  ensure("table1", source.is_valid());
 
95
 
 
96
  WBComponent *compo= wb.wb->get_component_handling(source);
 
97
  ensure("table is copiable", compo!=0);
 
98
 
 
99
  grt::CopyContext context(wb.grt);
 
100
  copy= workbench_physical_TableFigureRef::cast_from(compo->clone_object(source, wb.get_pview()->rootLayer(), 
 
101
    context, false));
 
102
 
 
103
  ensure("copy is valid", copy.is_valid());
 
104
 
 
105
  ensure("both in same view", copy->owner() == source->owner());
 
106
  ensure("both in same layer", copy->layer() == source->layer());
 
107
 
 
108
  ensure("copy is not identical", copy.valueptr() != source.valueptr());
 
109
 
 
110
  ensure("name is not same", copy->table()->name() != source->table()->name());
 
111
 
 
112
  ensure("table is not same", copy->table() != source->table());
 
113
  
 
114
  ensure_list_contents_copy(copy->table()->columns(), source->table()->columns());
 
115
 
 
116
  ensure_list_contents_copy(copy->table()->indices(), source->table()->indices());
 
117
}
 
118
 
 
119
 
 
120
TEST_FUNCTION(2)
 
121
{
 
122
  // create a table with PK and make sure that a copy will contain 
 
123
  // proper refs to the copied objects
 
124
 
 
125
  WBTester wb;
 
126
  wb.create_new_document();
 
127
 
 
128
  db_mysql_TableRef table;
 
129
 
 
130
  table= db_mysql_TableRef(wb.grt);
 
131
  table->name("person");
 
132
  
 
133
  for (int i= 0; i < 5; i++)
 
134
  {
 
135
    db_mysql_ColumnRef column(wb.grt);
 
136
 
 
137
    column->owner(table);
 
138
    column->name(strfmt("col%i", i));
 
139
    if (i > 2)
 
140
      column->setParseType("VARCHAR(32)", wb.get_rdbms()->simpleDatatypes());
 
141
    else
 
142
      column->setParseType("INT", wb.get_rdbms()->simpleDatatypes());
 
143
    table->columns().insert(column);
 
144
 
 
145
    if (i == 0)
 
146
      table->addPrimaryKeyColumn(column);
 
147
  }
 
148
 
 
149
  db_mysql_TableRef copy= db_mysql_TableRef::cast_from(grt::copy_object(wb.grt, table));
 
150
 
 
151
  ensure("copy", copy.is_valid());
 
152
  ensure("copy != orig", copy.valueptr() != table.valueptr());
 
153
 
 
154
  ensure_list_contents_copy(table->columns(), copy->columns());
 
155
 
 
156
  ensure("pk exists", copy->primaryKey().is_valid());
 
157
  ensure("primary key copied", copy->primaryKey().valueptr() != table->primaryKey().valueptr());
 
158
  ensure("pk copy points to column copy",
 
159
    copy->primaryKey()->columns()[0].valueptr() != table->primaryKey()->columns()[0].valueptr());
 
160
  ensure("pk points to correct index", 
 
161
    copy->indices().get(0).valueptr() == copy->primaryKey().valueptr());
 
162
 
 
163
  ensure_equals("column0", *copy->columns().get(0)->name(), "col0");
 
164
  ensure("column owner", copy->columns().get(0)->owner() == copy);
 
165
 
 
166
  ensure_equals("pk correct", copy->columns().get(0).valueptr(), copy->primaryKey()->columns().get(0)->referencedColumn().valueptr());
 
167
}
 
168
 
 
169
 
 
170
END_TESTS
 
171
 
 
172
  
 
173
  
 
174