Combined patches from RT#70734 and RT#44769
[dbsrgits/SQL-Translator.git] / t / 56-sqlite-producer.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3
4 use strict;
5 use Test::More tests => 3;
6 use Test::SQL::Translator qw(maybe_plan);
7 use FindBin qw/$Bin/;
8
9 use SQL::Translator::Schema::View;
10 use SQL::Translator::Schema::Table;
11 use SQL::Translator::Producer::SQLite;
12
13 {
14   my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo',
15                                                   fields => [qw/id name/],
16                                                   sql => 'SELECT id, name FROM thing',
17                                                   extra => {
18                                                     temporary => 1,
19                                                     if_not_exists => 1,
20                                                   });
21   my $create_opts = { no_comments => 1 };
22   my $view1_sql1 = [ SQL::Translator::Producer::SQLite::create_view($view1, $create_opts) ];
23
24   my $view_sql_replace = [ "CREATE TEMPORARY VIEW IF NOT EXISTS 'view_foo' AS
25     SELECT id, name FROM thing" ];
26   is_deeply($view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL');
27
28
29   my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo',
30                                                   fields => [qw/id name/],
31                                                   sql => 'SELECT id, name FROM thing',);
32
33   my $view1_sql2 = [ SQL::Translator::Producer::SQLite::create_view($view2, $create_opts) ];
34   my $view_sql_noreplace = [ "CREATE VIEW 'view_foo' AS
35     SELECT id, name FROM thing" ];
36   is_deeply($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');
37 }
38 {
39     my $create_opts;
40
41     my $table = SQL::Translator::Schema::Table->new(
42         name => 'foo_table',
43     );
44     $table->add_field(
45         name => 'foreign_key',
46         data_type => 'integer',
47     );
48     my $constraint = SQL::Translator::Schema::Constraint->new(
49         table => $table,
50         name => 'fk',
51         type => 'FOREIGN_KEY',
52         fields => ['foreign_key'],
53         reference_fields => ['id'],
54         reference_table => 'foo',
55         on_delete => 'RESTRICT',
56         on_update => 'CASCADE',
57     );
58
59     my $expected = [ "FOREIGN KEY ('foreign_key') REFERENCES 'foo'('id') ON DELETE RESTRICT ON UPDATE CASCADE"];
60     my $result =  [SQL::Translator::Producer::SQLite::create_foreignkey($constraint,$create_opts)];
61     is_deeply($result, $expected, 'correct "FOREIGN KEY"');
62 }