Improve trigger 'scope' attribute support (RT#119997)
[dbsrgits/SQL-Translator.git] / t / 56-sqlite-producer.t
index 2129c29..d0d2cfe 100644 (file)
@@ -5,6 +5,7 @@ use strict;
 use Test::More;
 use Test::SQL::Translator qw(maybe_plan);
 
+use SQL::Translator::Schema;
 use SQL::Translator::Schema::View;
 use SQL::Translator::Schema::Table;
 use SQL::Translator::Producer::SQLite;
@@ -166,38 +167,88 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0;
 
 {
    my $table = SQL::Translator::Schema::Table->new(
-       name => 'foo_auto_increment',
+       name => 'some_table',
    );
    $table->add_field(
        name => 'id',
        data_type => 'integer',
-       is_nullable => 0,
        is_auto_increment => 1,
+       is_nullable => 0,
+       extra => {
+           auto_increment_type => 'monotonic',
+       },
    );
    $table->primary_key('id');
-   my $expected = [ qq<CREATE TABLE "foo_auto_increment" (
-  "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
-)>];
+   my $expected = [ qq<CREATE TABLE "some_table" (\n  "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n)>];
    my $result =  [SQL::Translator::Producer::SQLite::create_table($table, { no_comments => 1 })];
-   is_deeply($result, $expected, 'correctly built table with autoincrement on primary key');
+   is_deeply($result, $expected, 'correctly built monotonicly autoincremened PK');
 }
 
 {
-   my $table = SQL::Translator::Schema::Table->new(
-       name => 'foo_no_auto_increment',
-   );
-   $table->add_field(
-       name => 'id',
-       data_type => 'integer',
-       is_nullable => 0,
-       is_auto_increment => 0,
-   );
-   $table->primary_key('id');
-   my $expected = [ qq<CREATE TABLE "foo_no_auto_increment" (
-  "id" INTEGER PRIMARY KEY NOT NULL
-)>];
-   my $result =  [SQL::Translator::Producer::SQLite::create_table($table, { no_comments => 1 })];
-   is_deeply($result, $expected, 'correctly built table without autoincrement on primary key');
+    my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => ['foo'] );
+
+    {
+        my $index = $table->add_index(name => 'myindex', fields => ['foo']);
+        my ($def) = SQL::Translator::Producer::SQLite::create_index($index);
+        is($def, 'CREATE INDEX "myindex" ON "foobar" ("foo")', 'index created');
+    }
+
+    {
+        my $index = $table->add_index(fields => ['foo']);
+        my ($def) = SQL::Translator::Producer::SQLite::create_index($index);
+        is($def, 'CREATE INDEX "foobar_idx" ON "foobar" ("foo")', 'index created');
+    }
+
+    {
+        my $constr = $table->add_constraint(name => 'constr', fields => ['foo']);
+        my ($def) = SQL::Translator::Producer::SQLite::create_constraint($constr);
+        is($def, 'CREATE UNIQUE INDEX "constr" ON "foobar" ("foo")', 'constraint created');
+    }
+
+    {
+        my $constr = $table->add_constraint(fields => ['foo']);
+        my ($def) = SQL::Translator::Producer::SQLite::create_constraint($constr);
+        is($def, 'CREATE UNIQUE INDEX "foobar_idx02" ON "foobar" ("foo")', 'constraint created');
+    }
+}
+
+{
+    my $schema = SQL::Translator::Schema->new();
+    my $table = $schema->add_table( name => 'foo', fields => ['bar'] );
+
+    {
+        my $trigger = $schema->add_trigger(
+            name                => 'mytrigger',
+            perform_action_when => 'before',
+            database_events     => 'update',
+            on_table            => 'foo',
+            fields              => ['bar'],
+            action              => 'BEGIN baz() END'
+        );
+        my ($def) = SQL::Translator::Producer::SQLite::create_trigger($trigger);
+        is($def, 'CREATE TRIGGER "mytrigger" before update on "foo" BEGIN baz() END', 'trigger created');
+    }
+
+    {
+        my $trigger = $schema->add_trigger(
+            name                => 'mytrigger2',
+            perform_action_when => 'after',
+            database_events     => ['insert'],
+            on_table            => 'foo',
+            fields              => ['bar'],
+            action              => 'baz()'
+        );
+        my ($def) = SQL::Translator::Producer::SQLite::create_trigger($trigger);
+        is($def, 'CREATE TRIGGER "mytrigger2" after insert on "foo" BEGIN baz() END', 'trigger created');
+    }
+}
+
+{
+    my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => ['foo'] );
+    my $constr = $table->add_constraint(name => 'constr', expression => "foo != 'baz'");
+    my ($def) = SQL::Translator::Producer::SQLite::create_check_constraint($constr);
+
+    is($def, q{CONSTRAINT "constr" CHECK(foo != 'baz')}, 'check constraint created');
 }
 
 done_testing;