Get SQLite to produce saner output
[dbsrgits/SQL-Translator.git] / t / 48xml-to-sqlite.t
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;
8 use Test::Differences;
9 use Data::Dumper;
10 use SQL::Translator;
11 use SQL::Translator::Schema::Constants;
12
13
14 BEGIN {
15     maybe_plan(2, 'SQL::Translator::Parser::XML::SQLFairy',
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,
24     show_warnings  => 0,
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
36 eq_or_diff($sql, << "SQL");
37 BEGIN TRANSACTION;
38
39 DROP TABLE Basic;
40
41 CREATE TABLE Basic (
42   id INTEGER PRIMARY KEY NOT NULL,
43   title varchar(100) NOT NULL DEFAULT 'hello',
44   description text DEFAULT '',
45   email varchar(500),
46   explicitnulldef varchar,
47   explicitemptystring varchar DEFAULT '',
48   -- Hello emptytagdef
49   emptytagdef varchar DEFAULT '',
50   another_id int(10) DEFAULT '2',
51   timest timestamp
52 );
53
54 CREATE INDEX titleindex ON Basic (title);
55
56 CREATE UNIQUE INDEX emailuniqueindex ON Basic (email);
57
58 DROP TABLE Another;
59
60 CREATE TABLE Another (
61   id INTEGER PRIMARY KEY NOT NULL,
62   num numeric(10,2)
63 );
64
65 DROP VIEW IF EXISTS email_list;
66 CREATE VIEW email_list AS
67     SELECT email FROM Basic WHERE (email IS NOT NULL);
68
69 DROP TRIGGER IF EXISTS foo_trigger;
70
71 CREATE TRIGGER foo_trigger after insert on Basic BEGIN update modified=timestamp(); END;
72
73 DROP TRIGGER IF EXISTS bar_trigger_insert;
74
75 CREATE TRIGGER bar_trigger_insert before insert on Basic BEGIN update modified2=timestamp(); END;
76
77 DROP TRIGGER IF EXISTS bar_trigger_update;
78
79 CREATE TRIGGER bar_trigger_update before update on Basic BEGIN update modified2=timestamp(); END;
80
81 COMMIT;
82 SQL
83
84 # Test in list context
85 my @sql = $sqlt->translate(
86     from     => 'XML-SQLFairy',
87     to       => 'SQLite',
88     filename => $xmlfile,
89 ) or die $sqlt->error;
90
91 eq_or_diff(\@sql, 
92           [
93           'BEGIN TRANSACTION',
94           'DROP TABLE Basic',
95           'CREATE TABLE Basic (
96   id INTEGER PRIMARY KEY NOT NULL,
97   title varchar(100) NOT NULL DEFAULT \'hello\',
98   description text DEFAULT \'\',
99   email varchar(500),
100   explicitnulldef varchar,
101   explicitemptystring varchar DEFAULT \'\',
102   -- Hello emptytagdef
103   emptytagdef varchar DEFAULT \'\',
104   another_id int(10) DEFAULT \'2\',
105   timest timestamp
106 )',
107           'CREATE INDEX titleindex ON Basic (title)',
108           'CREATE UNIQUE INDEX emailuniqueindex ON Basic (email)',
109           'DROP TABLE Another',
110           'CREATE TABLE Another (
111   id INTEGER PRIMARY KEY NOT NULL,
112   num numeric(10,2)
113 )',
114           'DROP VIEW IF EXISTS email_list;
115 CREATE VIEW email_list AS
116     SELECT email FROM Basic WHERE (email IS NOT NULL)',
117           'DROP TRIGGER IF EXISTS foo_trigger',
118           'CREATE TRIGGER foo_trigger after insert on Basic BEGIN update modified=timestamp(); END',
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
125           ], 'SQLite translate in list context matches');
126
127