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