File: wb_context_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 (830 lines) | stat: -rw-r--r-- 29,964 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/* 
 * 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
 */

// High-level testing for Workbench
// This tests WBContext, which will test the integration of all components.

#include "base/util_functions.h"

#include "wb_helpers.h"
#include "grtdb/db_helpers.h"
#include "grtdb/db_object_helpers.h"
#include "grt_test_utility.h"

#include "stub/stub_utilities.h"

using namespace wb;
using namespace base;
using namespace bec;

BEGIN_TEST_DATA_CLASS(wb_context_test)
protected:
  WBTester tester;

TEST_DATA_CONSTRUCTOR(wb_context_test)
{
  // Init datatypes and RDBMS.
  populate_grt(tester.grt, tester);

  // Modeling uses a default server version, which is not related to any server it might have
  // reverse engineered content from, nor where it was sync'ed to. So we have to mimic this here.
  std::string target_version = tester.wb->get_grt_manager()->get_app_option_string("DefaultTargetMySQLVersion");
  if (target_version.empty())
    target_version = "5.5.49";
  tester.get_rdbms()->version(parse_version(tester.grt, target_version));

}

END_TEST_DATA_CLASS;

TEST_MODULE(wb_context_test, "high-level tests for Workbench");

TEST_FUNCTION(5)
{
  // Test creating a new document.
  // General note: many other tests depend on this to work, so there should really be a test
  // order where more complicated tests are based on simpler ones.
  tester.wb->new_document();

  ensure("doc", tester.wb->get_document().is_valid());
  ensure("physicalModels", tester.wb->get_document()->physicalModels().count() == 1);

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

TEST_FUNCTION(10)
{
  // Test loading documents (saved as xml). Check if the test file is correct.
  {
    FILE *f = fopen("data/workbench/test_model_xml.mwb", "r");
    ensure("Model file does not exist", f != NULL);
    
    char buffer[100];
    buffer[0] = 0;

    fgets(buffer, sizeof(buffer), f);    
    fclose(f);
    
    ensure("Model file is not pure XML (uncompressed)", strncmp(buffer, "<?xml", 5) == 0);
  }

  ensure("Failed opening document", tester.wb->open_document("data/workbench/test_model_xml.mwb"));
  
  workbench_WorkbenchRef root(tester.wb->get_root());

  ensure("document ok", root->doc().is_valid());

  ensure_equals("physicalModels", root->doc()->physicalModels().count(), 1U);
  ensure_equals("diagrams", root->doc()->physicalModels()[0]->diagrams().count(), 1U);

  model_DiagramRef view(root->doc()->physicalModels()[0]->diagrams()[0]);
  ensure("figure count", view->figures().count() > 0);

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

/**
 * Test saving + loading of a document and check the canvas state.
 * This test uses by intention local testers to ensure a clean state when loading the test file.
 */
TEST_FUNCTION(15)
{ 
  WBTester *local_tester1 = new WBTester(false, Size(2970, 2100));
  
  local_tester1->wb->new_document();
  local_tester1->add_view();
  
  ensure_equals("view count after adding", local_tester1->wb->get_document()->physicalModels()[0]->diagrams().count(), 1U);
  
  local_tester1->add_table_figure("sometable", 100, 100);
  local_tester1->flush_until(2); // TODO: this is not deterministic and hence should be replaced in tests.

  ensure_equals("original phys model count",
                local_tester1->wb->get_document()->physicalModels().count(),1U);
  ensure_equals("original view count",
                local_tester1->wb->get_document()->physicalModels()[0]->diagrams().count(),1U);
  ensure_equals("original element count",
                local_tester1->wb->get_document()->physicalModels()[0]->diagrams()[0]->figures().count(),1U);
  ensure_equals("original element count in rootLayer",
                local_tester1->wb->get_document()->physicalModels()[0]->diagrams()[0]->rootLayer()->figures().count(),1U);
  local_tester1->sync_view();
  ensure("Saving document failed", local_tester1->wb->save_as("data/test1_doc.mwb"));

  // Second instance.
  WBTester *local_tester2 = new WBTester(false);
  ensure("Failed opening document test1_doc", local_tester2->wb->open_document("data/test1_doc.mwb"));

  ensure_equals("loaded phys model count",
                local_tester2->wb->get_document()->physicalModels().count(),1U);
  ensure_equals("loaded view count",
                local_tester2->wb->get_document()->physicalModels()[0]->diagrams().count(),1U);
  ensure_equals("loaded element count",
                local_tester2->wb->get_document()->physicalModels()[0]->diagrams()[0]->figures().count(),1U);
  ensure_equals("loaded element count in rootLayer",
                local_tester2->wb->get_document()->physicalModels()[0]->diagrams()[0]->rootLayer()->figures().count(),1U);

  local_tester2->open_all_diagrams();
  local_tester2->sync_view();

  grt_ensure_equals("save/load test", local_tester1->wb->get_document(), local_tester2->wb->get_document(), true);

  ensure("view created", local_tester2->last_view != 0);

  ensure("Could not close document", local_tester1->close_document());
  local_tester1->wb->close_document_finish();
  delete local_tester1;

  ensure("Could not close document", local_tester2->close_document());
  local_tester2->wb->close_document_finish();
  delete local_tester2;
}

TEST_FUNCTION(20)
{
  // bug: opening a model with selection will cause a crash
  tester.wb->new_document();

  tester.add_view();

  tester.add_table_figure("table", 10, 10);
  model_DiagramRef view(tester.get_pmodel()->diagrams()[0]);

  ensure_equals("check selection", view->selection().count(), 1U);

  tester.wb->save_as("test4.mwb");
  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();

  ensure("Failed opening document", tester.wb->open_document("test4.mwb"));

  view = tester.get_pmodel()->diagrams()[0];

  ensure("check loaded doc", view->figures().count()==1);
  ensure_equals("check loaded doc", view->selection().count(),1U);

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

TEST_FUNCTION(25)
{
  // bug: dragging related tables to the view won't create the connection

  // test dragging the table with fk 1st
  {
    WBTester tester(false); // TODO: Is it really needed to create local tester instances?
                            // Creating a tester is quite resource costly.

    ensure("Failed opening document", tester.wb->open_document("data/workbench/2tables_1fk.mwb"));
    ensure("Document is not valid", tester.wb->get_document().is_valid());
    ensure_equals("Table count does not fit", tester.get_schema()->tables().count(), 2U);
    ensure_equals("Unexpected figures", tester.get_pview()->figures().count(), 0U);
    ensure_equals("Unexpected connections", tester.get_pview()->connections().count(), 0U);

    std::list<db_DatabaseObjectRef> objects;
    objects.push_back(grt::find_named_object_in_list(tester.get_schema()->tables(), "table2"));

    tester.open_all_diagrams();
    tester.sync_view();

    tester.interactive_place_db_objects(10, 10, objects);

    ensure_equals("Wrong table count in view", tester.get_pview()->figures().count(), 1U);
    ensure_equals("Wrong connection count in view", tester.get_pview()->connections().count(), 0U);

    objects.clear();
    objects.push_back(grt::find_named_object_in_list(tester.get_schema()->tables(), "table1"));
    tester.interactive_place_db_objects(10, 150, objects);

    grt::BaseListRef figures(tester.get_pview()->figures());
    tester.flush_until(1, boost::bind(&grt::BaseListRef::count, figures), 2);
    ensure_equals("There must be 2 tables in the view now", tester.get_pview()->figures().count(), 2U);

    grt::BaseListRef connections(tester.get_pview()->connections());
    tester.flush_until(5, boost::bind(&grt::BaseListRef::count, connections), 1);

    ensure_equals("connections created", tester.get_pview()->connections().count(), 1U);

    ensure("Could not close document", tester.close_document());
    tester.wb->close_document_finish();
  }

  // test dragging the table with fk last
  {
    WBTester tester(false);

    ensure("Failed opening document", tester.wb->open_document("data/workbench/2tables_1fk.mwb"));
    ensure("load doc", tester.wb->get_document().is_valid());
    ensure_equals("contains tables", tester.get_schema()->tables().count(), 2U);
    ensure_equals("no figures", tester.get_pview()->figures().count(), 0U);
    ensure_equals("no connections", tester.get_pview()->connections().count(), 0U);

    tester.open_all_diagrams();
    tester.sync_view();

    std::list<db_DatabaseObjectRef> objects;
    objects.push_back(grt::find_named_object_in_list(tester.get_schema()->tables(), "table1"));

    tester.interactive_place_db_objects(10, 10, objects);

    ensure_equals("drop 1 table", tester.get_pview()->figures().count(), 1U);
    ensure_equals("no connections", tester.get_pview()->connections().count(), 0U);

    objects.clear();
    objects.push_back(grt::find_named_object_in_list(tester.get_schema()->tables(), "table2"));

    tester.interactive_place_db_objects(10, 150, objects);

    ensure_equals("drop 2nd table", tester.get_pview()->figures().count(), 2U);
    ensure_equals("connections created", tester.get_pview()->connections().count(), 1U);

    ensure("Could not close document", tester.close_document());
    tester.wb->close_document_finish();
  }

  // test dragging both tables
  {
    WBTester tester(false);

    ensure("Failed opening document", tester.wb->open_document("data/workbench/2tables_1fk.mwb"));
    ensure("load doc", tester.wb->get_document().is_valid());
    ensure_equals("contains tables", tester.get_schema()->tables().count(), 2U);
    ensure_equals("no figures", tester.get_pview()->figures().count(), 0U);
    ensure_equals("no connections", tester.get_pview()->connections().count(), 0U);

    tester.open_all_diagrams();
    tester.sync_view();

    std::list<db_DatabaseObjectRef> objects;
    objects.push_back(grt::find_named_object_in_list(tester.get_schema()->tables(), "table1"));
    objects.push_back(grt::find_named_object_in_list(tester.get_schema()->tables(), "table2"));

    tester.interactive_place_db_objects(10, 150, objects);

    ensure_equals("drop 2 tables", tester.get_pview()->figures().count(), 2U);
    ensure_equals("connections created", tester.get_pview()->connections().count(), 1U);

    ensure("Could not close document", tester.close_document());
    tester.wb->close_document_finish();
  }

  // test with a recursive relationship
  {
   WBTester tester(false);
   populate_grt(tester.grt, tester);

    ensure("Failed opening document", tester.wb->open_document("data/workbench/2tables_1fk.mwb"));
    ensure("load doc", tester.wb->get_document().is_valid());
    ensure_equals("contains tables", tester.get_schema()->tables().count(), 2U);
    ensure_equals("no figures", tester.get_pview()->figures().count(), 0U);
    ensure_equals("no connections", tester.get_pview()->connections().count(), 0U);

    tester.open_all_diagrams();
    tester.sync_view();

    db_TableRef table(grt::find_named_object_in_list(tester.get_schema()->tables(), "table1"));

    ensure("no fk", table->foreignKeys().count()==0);

    bec::TableHelper::create_foreign_key_to_table(table, table, true, true, true, true,
      tester.get_rdbms(), grt::DictRef(tester.grt), grt::DictRef(tester.grt));

    std::list<db_DatabaseObjectRef> objects;
    objects.push_back(table);
    tester.interactive_place_db_objects(10, 150, objects);

    ensure_equals("drop 1 table", tester.get_pview()->figures().count(), 1U);
    ensure_equals("connections created", tester.get_pview()->connections().count(), 1U);

    ensure("Could not close document", tester.close_document());
    tester.wb->close_document_finish();
  }
}

TEST_FUNCTION(30)
{
  // bug: deleting tables with relationship won't delete the connection

  ensure("Failed opening document", tester.wb->open_document("data/workbench/2tables_1fk.mwb"));
  ensure("load doc", tester.wb->get_document().is_valid());
  ensure_equals("contains tables", tester.get_schema()->tables().count(), 2U);
  ensure_equals("no figures", tester.get_pview()->figures().count(), 0U);
  ensure_equals("no connections", tester.get_pview()->connections().count(), 0U);

  tester.open_all_diagrams();
  tester.sync_view();

  std::list<db_DatabaseObjectRef> objects;
  objects.push_back(grt::find_named_object_in_list(tester.get_schema()->tables(), "table1"));
  objects.push_back(grt::find_named_object_in_list(tester.get_schema()->tables(), "table2"));

  tester.interactive_place_db_objects(10, 150, objects);

  ensure_equals("place 2 tables", tester.get_pview()->figures().count(), 2U);
  ensure_equals("connections created", tester.get_pview()->connections().count(), 1U);

  // Delete 1 of the tables and see if connection is gone.

  model_FigureRef figure= tester.get_pview()->figures()[0];

  ensure("root global", tester.wb->get_root()->is_global());
  ensure("doc is global", tester.get_pview()->is_global());

  ensure("figure is global", figure->is_global());
    
  tester.wb->get_model_context()->delete_object(figure);

  ensure_equals("delete 1 table", tester.get_pview()->figures().count(), 1U);
  ensure_equals("connection count after expected auto-delete", tester.get_pview()->connections().count(), 0U);

  // undo
  tester.grt->get_undo_manager()->undo();

  ensure_equals("undo delete table", tester.get_pview()->figures().count(), 2U);
  ensure_equals("undo delete table (connection)", tester.get_pview()->connections().count(), 1U);

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

TEST_FUNCTION(35)
{
  // check if figure/dbobject deletion works
  WBFrontendCallbacks callbacks;

  //TODO: Investigate what was the purpose of this and if it continues needed
  //callbacks.confirm_action= boost::bind(&delegated_confirm_action, &result);

//  WBTester tester(Size(800, 800), callbacks);
//  WBTester& tester = wb;

  tester.wb->new_document();
  tester.add_view();

  tester.add_table_figure("table", 10, 10);

  ensure_equals("table object", tester.get_schema()->tables().count(), 1U);
  ensure_equals("table figure", tester.get_pview()->figures().count(), 1U);

  

  // delete table with dbobject (1)
  tester.wb->get_model_context()->delete_object(tester.get_pview()->figures()[0]);

  ensure_equals("table object", tester.get_schema()->tables().count(), 0U);
  ensure_equals("table figure", tester.get_pview()->figures().count(), 0U);

  tester.grt->get_undo_manager()->undo();

  ensure_equals("table object", tester.get_schema()->tables().count(), 1U);
  ensure_equals("table figure", tester.get_pview()->figures().count(), 1U);

  // delete table without dbobject (0)
  tester.wb->get_model_context()->remove_figure(tester.get_pview()->figures()[0]);

  ensure_equals("table object", tester.get_schema()->tables().count(), 1U);
  ensure_equals("table figure", tester.get_pview()->figures().count(), 0U);

  tester.grt->get_undo_manager()->undo();

  ensure_equals("table object", tester.get_schema()->tables().count(), 1U);
  ensure_equals("table figure", tester.get_pview()->figures().count(), 1U);

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

TEST_FUNCTION(40)
{
  // make sure connections are deleted and recreated with undo/redo

  tester.wb->new_document();
  tester.add_view();

  grt::UndoManager *um= tester.grt->get_undo_manager();

  db_mysql_TableRef table1= tester.add_table_figure("table1", 10, 10);
  db_mysql_ColumnRef column1(tester.grt);
  column1->owner(table1);
  column1->name("pk");
  db_mysql_TableRef table2= tester.add_table_figure("table2", 100, 10);

  db_mysql_ColumnRef column2(tester.grt);
  column2->owner(table2);
  column2->name("pk");

  ensure_equals("table object", tester.get_schema()->tables().count(), 2U);
  ensure_equals("table figure", tester.get_pview()->figures().count(), 2U);

  tester.grt->start_tracking_changes();
  table1->addPrimaryKeyColumn(column1);
  table2->addPrimaryKeyColumn(column2);
  tester.grt->stop_tracking_changes();

  // create 1:n rel and test undo

  grt::AutoUndo undo(tester.grt);
  bec::TableHelper::create_foreign_key_to_table(table2, table1, true, true, true, true,
                                                tester.get_rdbms(),
                                                grt::DictRef(tester.grt),
                                                grt::DictRef(tester.grt));
  undo.end("create fk");

  ensure_equals("table fks", table2->foreignKeys().count(), 1U);

  grt::BaseListRef connections(tester.get_pview()->connections());

  tester.flush_until(2, boost::bind(&BaseListRef::count, connections), 1);

  ensure_equals("connection created", connections.count(), 1U);

  um->undo();

  tester.flush_until(2, boost::bind(&BaseListRef::count, connections), 0);

  ensure_equals("connection undone", connections.count(), 0U);

  um->redo();

  tester.flush_until(2, boost::bind(&BaseListRef::count, connections), 1);

  ensure_equals("connection redone", connections.count(), 1U);

  um->undo();
  tester.flush_until(2, boost::bind(&BaseListRef::count, connections), 0);

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

//--------------------------------------------------------------------------------

TEST_FUNCTION(45)
{
  // bug: copy/paste object across schemas are not updating the owner

  tester.wb->new_document();

  WBComponentPhysical *ph= tester.wb->get_component<WBComponentPhysical>();
  ensure("physical component", ph != 0);

  workbench_physical_ModelRef model(tester.get_pmodel());

  ph->add_new_db_schema(model);

  ensure_equals("schemas", model->catalog()->schemata().count(), 2U);

  db_SchemaRef srcschema(model->catalog()->schemata()[0]);
  db_SchemaRef tarschema(model->catalog()->schemata()[1]);

  ensure("schemas !same", srcschema != tarschema);

  // add stuff

  ph->add_new_db_table(srcschema);
  ph->add_new_db_view(srcschema);
  ph->add_new_db_routine(srcschema);
  ph->add_new_db_routine_group(srcschema);

  grt::CopyContext context(tester.grt);
  ph->clone_db_object_to_schema(tarschema, srcschema->tables()[0], context);
  ph->clone_db_object_to_schema(tarschema, srcschema->views()[0], context);
  ph->clone_db_object_to_schema(tarschema, srcschema->routines()[0], context);
  ph->clone_db_object_to_schema(tarschema, srcschema->routineGroups()[0], context);

  ensure_equals("table copied", tarschema->tables().count(), 1U);
  ensure_equals("view copied", tarschema->views().count(), 1U);
  ensure_equals("routine copied", tarschema->routines().count(), 1U);
  ensure_equals("routinegroup copied", tarschema->routineGroups().count(), 1U);

  ensure("table owner", tarschema->tables()[0]->owner() == tarschema);
  ensure("view owner", tarschema->views()[0]->owner() == tarschema);
  ensure("routine owner", tarschema->routines()[0]->owner() == tarschema);
  ensure("routinegroup owner", tarschema->routineGroups()[0]->owner() == tarschema);

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}


static mforms::DialogResult message_other_callback()
{
  return mforms::ResultOther;
}

TEST_FUNCTION(50)
{
  // bug: deleting an identifying relationship doesn't delete indexes.

  ensure("Failed opening document", tester.wb->open_document("data/workbench/identifying_relationship.mwb"));
  tester.open_all_diagrams();
  tester.sync_view();

  workbench_DocumentRef doc(tester.wb->get_document());

  db_TableRef table1(doc->physicalModels()[0]->catalog()->schemata()[0]->tables()[0]);
  db_TableRef table2(doc->physicalModels()[0]->catalog()->schemata()[0]->tables()[1]);
  
  ensure_equals("check index count", table1->indices().count(), 2U);
  ensure_equals("connections", doc->physicalModels()[0]->diagrams()[0]->connections().count(), 1U);

  workbench_physical_ConnectionRef conn= workbench_physical_ConnectionRef::cast_from(doc->physicalModels()[0]->diagrams()[0]->connections().get(0));
  ensure("connection fk", conn->foreignKey().is_valid());
  
  // delete the connection

  mforms::stub::UtilitiesWrapper::set_message_callback(message_other_callback);
  tester.wb->get_model_context()->delete_object(doc->physicalModels()[0]->diagrams()[0]->connections()[0]);

  ensure_equals("connections", doc->physicalModels()[0]->diagrams()[0]->connections().count(), 0U);
  ensure_equals("check index count", table1->indices().count(), 1U);

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

TEST_FUNCTION(55)
{
  ensure("Failed opening document", tester.wb->open_document("data/workbench/identifying_relationship.mwb"));
  tester.open_all_diagrams();
  tester.sync_view();

  workbench_DocumentRef doc(tester.wb->get_document());

  db_TableRef table1(doc->physicalModels()[0]->catalog()->schemata()[0]->tables()[0]);
  ensure_equals("check index count", table1->indices().count(), 2U);
  ensure("pk", table1->primaryKey().is_valid());
  
  // Delete column.
  table1->removeColumn(table1->columns()[0]);
  //bec::TableHelper::remove_column(table1, table1->columns()[0]);

  ensure("Primary key should no longer exist", !table1->primaryKey().is_valid());

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

TEST_FUNCTION(60)
{
  // test whether relationship is created when a FK is added and its details
  // are set in steps (non-atomically)

  tester.wb->new_document();
  tester.add_view();

  db_mysql_TableRef table1= tester.add_table_figure("table1", 10, 10);
  db_mysql_ColumnRef column1(tester.grt);
  column1->owner(table1);
  column1->name("pk");
  db_mysql_TableRef table2= tester.add_table_figure("table2", 100, 10);

  db_mysql_ColumnRef column2(tester.grt);
  column2->owner(table2);
  column2->name("fkcol");

  ensure_equals("table object", tester.get_schema()->tables().count(), 2U);
  ensure_equals("table figure", tester.get_pview()->figures().count(), 2U);

  tester.grt->start_tracking_changes();
  table1->addPrimaryKeyColumn(column1);
  tester.grt->stop_tracking_changes();
  
  db_mysql_ForeignKeyRef fk(tester.grt);
  fk->owner(table2);
  fk->name("fk");
  
  table2->foreignKeys().insert(fk);
  tester.flush_until(2);

  fk->columns().insert(column2);
  fk->referencedColumns().insert(column1);
  fk->referencedTable(table1);

  grt::BaseListRef connections(tester.get_pview()->connections());
  tester.flush_until(2, boost::bind(&grt::BaseListRef::count, connections), 1);

  ensure_equals("rel count", tester.get_pview()->connections().count(), 1U);

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

TEST_FUNCTION(65)
{
  // test if new view has layer size set 

  tester.wb->new_document();
  tester.add_view();

  ensure_equals("rootLayer width", *tester.get_pview()->rootLayer()->width(), tester.last_view->get_total_view_size().width);
  ensure_equals("rootLayer height", *tester.get_pview()->rootLayer()->height(), tester.last_view->get_total_view_size().height);

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

TEST_FUNCTION(70)
{ // test creation of a simple model with a relationship
  
  tester.wb->new_document();
  tester.add_view();

  tester.add_table_figure("table1", 100, 100);
  tester.add_table_figure("table2", 300, 100);
  
  tester.export_png("test20_dump.png");

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

TEST_FUNCTION(75)
{
  // bug: loading a model with layers and then hitting new crashes

  ensure("Failed opening document", tester.wb->open_document("data/workbench/2tables_conn_layer.mwb"));
  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
  tester.wb->new_document();

  while (dynamic_cast<ModelDiagramForm*>(tester.wbui->get_active_main_form())!=0)
    tester.wb->flush_idle_tasks();

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}


TEST_FUNCTION(80)
{
  // bug: loading model twice causes bad internal state in GUI
  
  ensure("Failed opening document", tester.wb->open_document("data/workbench/sakila.mwb"));
  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();

  ensure("Failed opening document", tester.wb->open_document("data/workbench/sakila.mwb"));
  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

TEST_FUNCTION(85)
{
  // bug: loading a model with selection won't reselect the items in the canvas
  ensure("Failed opening document", tester.wb->open_document("data/workbench/selected_table.mwb"));
  tester.open_all_diagrams();
  tester.sync_view();

  tester.flush_while(3, boost::bind(&mdc::Selection::empty, tester.last_view->get_selection()));

  ensure("has selection in canvas", !tester.last_view->get_selection()->empty());

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

static void set_note_content(grt::GRT *grt, GrtStoredNoteRef note, const std::string &text)
{
  grt::Module *module= grt->get_module("Workbench");
  if (!module)
    throw std::runtime_error("Workbench module not found");

  note->lastChangeDate(base::fmttime());

  grt::BaseListRef args(grt);

  args.ginsert(note->filename());
  args.ginsert(grt::StringRef(text));

  module->call_function("setAttachedFileContents", args);
}

static std::string get_note_content(grt::GRT *grt, const GrtStoredNoteRef &note)
{
  grt::Module *module= grt->get_module("Workbench");
  if (!module)
    throw std::runtime_error("Workbench module not found");

  grt::BaseListRef args(grt);

  args.ginsert(note->filename());

  return *grt::StringRef::cast_from(module->call_function("getAttachedFileContents", args));
}

TEST_FUNCTION(90)
{
  // check stored note management
  tester.wb->new_document();

  // add a note
  WBComponentPhysical *ph= tester.wb->get_component<WBComponentPhysical>();

  ph->add_new_stored_note(tester.get_pmodel());

  tester.flush_until(0.5);

  // check if created
  ensure_equals("note created", tester.get_pmodel()->notes().count(), 1U);
  ensure("note created with file", tester.get_pmodel()->notes().get(0)->filename() != "");

  grt::GRT *grt= tester.wb->get_grt();

  // edit the note like an editor would
  set_note_content(grt, tester.get_pmodel()->notes().get(0), "hello world");
  std::string filename= tester.get_pmodel()->notes().get(0)->filename();

  tester.wb->save_as("notetest.mwb");
  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();

  // check if stored on disk
  ensure("Failed opening document", tester.wb->open_document("notetest.mwb"));

  ensure_equals("note still in model", tester.get_pmodel()->notes().count(), 1U);
  ensure_equals("note filename", *tester.get_pmodel()->notes().get(0)->filename(), filename);
  
  // get note contents as an editor
  std::string text= get_note_content(grt, tester.get_pmodel()->notes().get(0));
  ensure_equals("note contents", text, "hello world");

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}



TEST_FUNCTION(95)
{
  // check undo for stored notes

  // create note
  tester.wb->new_document();

  // add a note
  WBComponentPhysical *ph= tester.wb->get_component<WBComponentPhysical>();

  ph->add_new_stored_note(tester.get_pmodel());

  tester.flush_until(0.5);

  // check if created
  ensure_equals("note created", tester.get_pmodel()->notes().count(), 1U);
  ensure("note created with file", tester.get_pmodel()->notes().get(0)->filename()!="");

  std::string fname= tester.get_pmodel()->notes().get(0)->filename();

  set_note_content(tester.grt, tester.get_pmodel()->notes().get(0), "some text");
  
  ensure_equals("note content set", get_note_content(tester.grt, tester.get_pmodel()->notes().get(0)), "some text");

  // delete note and undo
  tester.grt->get_undo_manager()->add_undo(new grt::UndoListRemoveAction(tester.get_pmodel()->notes(), 0));
  tester.get_pmodel()->notes().remove(0);
  tester.grt->get_undo_manager()->undo();

  ensure_equals("note undeleted", tester.get_pmodel()->notes().count(), 1U);
  ensure_equals("note file", *tester.get_pmodel()->notes().get(0)->filename(), fname);
  ensure_equals("note content", get_note_content(tester.grt, tester.get_pmodel()->notes().get(0)), "some text");


  for (int i= 0; i < 10; i++)
  {
    tester.grt->get_undo_manager()->redo();
    ensure_equals("note re-deleted", tester.get_pmodel()->notes().count(), 0U);
    tester.grt->get_undo_manager()->undo();
    ensure_equals("note un-deleted", tester.get_pmodel()->notes().count(), 1U);
  }
  ensure_equals("note file", *tester.get_pmodel()->notes().get(0)->filename(), fname);
  ensure_equals("note content", get_note_content(tester.grt, tester.get_pmodel()->notes().get(0)), "some text");

  // undo (should undo create note)

  ensure("Could not close document", tester.close_document());
  tester.wb->close_document_finish();
}

END_TESTS