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