Commit | Line | Data |
8c4efd11 |
1 | #!/usr/bin/perl |
2 | use strict; |
3 | |
4 | use FindBin qw/$Bin/; |
5 | use Test::More; |
6 | use Test::SQL::Translator; |
7 | use Test::Exception; |
4d438549 |
8 | use Test::Differences; |
8c4efd11 |
9 | use Data::Dumper; |
10 | use SQL::Translator; |
11 | use SQL::Translator::Schema::Constants; |
12 | |
13 | |
14 | BEGIN { |
24d9fe69 |
15 | maybe_plan(2, 'SQL::Translator::Parser::XML::SQLFairy', |
8c4efd11 |
16 | 'SQL::Translator::Producer::SQLite'); |
17 | } |
18 | |
19 | my $xmlfile = "$Bin/data/xml/schema.xml"; |
20 | |
21 | my $sqlt; |
22 | $sqlt = SQL::Translator->new( |
23 | no_comments => 1, |
ee91ab6f |
24 | show_warnings => 0, |
8c4efd11 |
25 | add_drop_table => 1, |
26 | ); |
27 | |
28 | die "Can't find test schema $xmlfile" unless -e $xmlfile; |
29 | |
30 | my $sql = $sqlt->translate( |
31 | from => 'XML-SQLFairy', |
32 | to => 'SQLite', |
33 | filename => $xmlfile, |
34 | ) or die $sqlt->error; |
35 | |
4d438549 |
36 | eq_or_diff($sql, << "SQL"); |
f9c96971 |
37 | |
38 | |
8c4efd11 |
39 | BEGIN TRANSACTION; |
40 | |
8c4efd11 |
41 | DROP TABLE Basic; |
24d9fe69 |
42 | |
8c4efd11 |
43 | CREATE TABLE Basic ( |
44 | id INTEGER PRIMARY KEY NOT NULL, |
45 | title varchar(100) NOT NULL DEFAULT 'hello', |
46 | description text DEFAULT '', |
1022b157 |
47 | email varchar(500), |
8c4efd11 |
48 | explicitnulldef varchar, |
49 | explicitemptystring varchar DEFAULT '', |
50 | -- Hello emptytagdef |
08d91aad |
51 | emptytagdef varchar DEFAULT '', |
b08b5416 |
52 | another_id int(10) DEFAULT '2', |
08d91aad |
53 | timest timestamp |
8c4efd11 |
54 | ); |
55 | |
963fd5fc |
56 | CREATE INDEX titleindex ON Basic (title); |
24d9fe69 |
57 | |
963fd5fc |
58 | CREATE UNIQUE INDEX emailuniqueindex ON Basic (email); |
8c4efd11 |
59 | |
b08b5416 |
60 | DROP TABLE Another; |
24d9fe69 |
61 | |
b08b5416 |
62 | CREATE TABLE Another ( |
63 | id INTEGER PRIMARY KEY NOT NULL |
64 | ); |
65 | |
a25ac5d2 |
66 | DROP VIEW IF EXISTS email_list; |
67 | CREATE VIEW email_list AS |
3910f248 |
68 | SELECT email FROM Basic WHERE (email IS NOT NULL); |
ec59a597 |
69 | |
f9c96971 |
70 | DROP TRIGGER IF EXISTS foo_trigger; |
71 | |
72 | CREATE TRIGGER foo_trigger after insert on Basic BEGIN update modified=timestamp(); END; |
73 | |
410d4a42 |
74 | DROP TRIGGER IF EXISTS bar_trigger_insert; |
75 | |
76 | CREATE TRIGGER bar_trigger_insert before insert on Basic BEGIN update modified2=timestamp(); END; |
77 | |
78 | DROP TRIGGER IF EXISTS bar_trigger_update; |
79 | |
80 | CREATE TRIGGER bar_trigger_update before update on Basic BEGIN update modified2=timestamp(); END; |
81 | |
8c4efd11 |
82 | COMMIT; |
83 | SQL |
24d9fe69 |
84 | |
85 | # Test in list context |
86 | my @sql = $sqlt->translate( |
87 | from => 'XML-SQLFairy', |
88 | to => 'SQLite', |
89 | filename => $xmlfile, |
90 | ) or die $sqlt->error; |
91 | |
f9c96971 |
92 | eq_or_diff(\@sql, |
24d9fe69 |
93 | [ |
f9c96971 |
94 | "\n\nBEGIN TRANSACTION", |
24d9fe69 |
95 | 'DROP TABLE Basic', |
96 | 'CREATE TABLE Basic ( |
97 | id INTEGER PRIMARY KEY NOT NULL, |
98 | title varchar(100) NOT NULL DEFAULT \'hello\', |
99 | description text DEFAULT \'\', |
1022b157 |
100 | email varchar(500), |
24d9fe69 |
101 | explicitnulldef varchar, |
102 | explicitemptystring varchar DEFAULT \'\', |
103 | -- Hello emptytagdef |
104 | emptytagdef varchar DEFAULT \'\', |
105 | another_id int(10) DEFAULT \'2\', |
106 | timest timestamp |
107 | )', |
963fd5fc |
108 | 'CREATE INDEX titleindex ON Basic (title)', |
109 | 'CREATE UNIQUE INDEX emailuniqueindex ON Basic (email)', |
24d9fe69 |
110 | 'DROP TABLE Another', |
111 | 'CREATE TABLE Another ( |
112 | id INTEGER PRIMARY KEY NOT NULL |
113 | )', |
114 | 'DROP VIEW IF EXISTS email_list; |
115 | CREATE VIEW email_list AS |
3910f248 |
116 | SELECT email FROM Basic WHERE (email IS NOT NULL)', |
f9c96971 |
117 | 'DROP TRIGGER IF EXISTS foo_trigger', |
118 | 'CREATE TRIGGER foo_trigger after insert on Basic BEGIN update modified=timestamp(); END', |
410d4a42 |
119 | 'DROP TRIGGER IF EXISTS bar_trigger_insert', |
120 | 'CREATE TRIGGER bar_trigger_insert before insert on Basic BEGIN update modified2=timestamp(); END', |
121 | 'DROP TRIGGER IF EXISTS bar_trigger_update', |
122 | 'CREATE TRIGGER bar_trigger_update before update on Basic BEGIN update modified2=timestamp(); END', |
123 | 'COMMIT', |
124 | |
24d9fe69 |
125 | ], 'SQLite translate in list context matches'); |
126 | |
127 | |