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; |
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 }; |
4c0d31c1 |
22 | my $view1_sql1 = [ SQL::Translator::Producer::SQLite::create_view($view1, $create_opts) ]; |
24d9fe69 |
23 | |
f6e49d34 |
24 | my $view_sql_replace = [ 'CREATE TEMPORARY VIEW IF NOT EXISTS "view_foo" AS |
25 | SELECT id, name FROM thing' ]; |
4c0d31c1 |
26 | is_deeply($view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL'); |
24d9fe69 |
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 | |
4c0d31c1 |
33 | my $view1_sql2 = [ SQL::Translator::Producer::SQLite::create_view($view2, $create_opts) ]; |
f6e49d34 |
34 | my $view_sql_noreplace = [ 'CREATE VIEW "view_foo" AS |
35 | SELECT id, name FROM thing' ]; |
4c0d31c1 |
36 | is_deeply($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL'); |
24d9fe69 |
37 | } |
ea4a3ecc |
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 | |
f6e49d34 |
59 | my $expected = [ 'FOREIGN KEY ("foreign_key") REFERENCES "foo"("id") ON DELETE RESTRICT ON UPDATE CASCADE']; |
ea4a3ecc |
60 | my $result = [SQL::Translator::Producer::SQLite::create_foreignkey($constraint,$create_opts)]; |
61 | is_deeply($result, $expected, 'correct "FOREIGN KEY"'); |
62 | } |