add tests and test data
Justin Hunter [Wed, 27 Jan 2010 07:18:41 +0000 (23:18 -0800)]
t/07p_args.t [new file with mode: 0644]
t/38-mysql-producer.t [new file with mode: 0644]
t/47postgres-producer.t [new file with mode: 0644]
t/56-sqlite-producer.t [new file with mode: 0644]
t/62roundtrip_datacheck.t
t/data/roundtrip.xml [new file with mode: 0644]

diff --git a/t/07p_args.t b/t/07p_args.t
new file mode 100644 (file)
index 0000000..32cca1d
--- /dev/null
@@ -0,0 +1,76 @@
+use SQL::Translator;
+use Test::More;
+
+sub silly_parser {
+    my ($tr, $data) = @_;
+    my $pargs = $tr->parser_args;
+
+    my @fields = split /$pargs->{'delimiter'}/, $data;
+
+    my $schema = $tr->schema;
+    my $table  = $schema->add_table( name => 'foo') or die $schema->error;
+    for my $value ( @fields ) {
+        my $field = $table->add_field( name => $value ) or die $table->error;
+    }
+
+    return 1;
+}
+
+# The "data" to be parsed
+my $data = q(Id|Name|Phone Number|Favorite Flavor|);
+
+my $tr = SQL::Translator->new;
+
+# Pass parser_args as an explicit method call
+$tr->parser(\&silly_parser);
+$tr->parser_args(delimiter => '\|');
+
+my $pargs  = $tr->parser_args;
+$tr->translate(\$data);
+my $schema = $tr->schema;
+
+is($pargs->{'delimiter'}, '\|', "parser_args works when called directly");
+my @tables = $schema->get_tables;
+is(scalar @tables, 1, "right number of tables");
+my $table = shift @tables;
+my @fields = $table->get_fields;
+is(scalar @fields, 4, "right number of fields");
+
+#
+# Blow away the existing schema object.
+#
+$tr->schema (undef);
+
+# Now, pass parser_args indirectly...
+$tr->parser(\&silly_parser, { delimiter => "\t" });
+$data =~ s/\|/\t/g;
+
+$pargs = $tr->parser_args;
+$tr->translate(\$data);
+
+is($pargs->{'delimiter'}, "\t",
+    "parser_args works when called indirectly");
+
+@tables = $schema->get_tables;
+is(scalar @tables, 1, "right number of tables");
+$table = shift @tables;
+@fields = $table->get_fields;
+is(scalar @fields, 4, "right number of fields");
+
+undef $tr;
+$tr = SQL::Translator->new(parser => \&silly_parser,
+                           parser_args => { delimiter => ":" });
+$data =~ s/\t/:/g;
+$pargs = $tr->parser_args;
+$tr->translate(\$data);
+
+is($pargs->{'delimiter'}, ":",
+    "parser_args works when called as constructor arg");
+
+@tables = $schema->get_tables;
+is(scalar @tables, 1, "right number of tables");
+$table = shift @tables;
+@fields = $table->get_fields;
+is(scalar @fields, 4, "right number of fields with new delimiter");
+
+done_testing;
diff --git a/t/38-mysql-producer.t b/t/38-mysql-producer.t
new file mode 100644 (file)
index 0000000..442b028
--- /dev/null
@@ -0,0 +1,448 @@
+#!/usr/bin/perl -w
+# vim:filetype=perl
+
+#
+# Note that the bulk of the testing for the mysql producer is in
+# 08postgres-to-mysql.t. This test is for additional stuff that can't be tested
+# using an Oracle schema as source e.g. extra attributes.
+#
+
+use strict;
+use Test::More;
+use Test::Exception;
+#use Test::SQL::Translator qw(maybe_plan);
+
+use Data::Dumper;
+use FindBin qw/$Bin/;
+
+# Testing 1,2,3,4...
+#=============================================================================
+
+#BEGIN {
+#    maybe_plan(40,
+#        'YAML',
+#        'SQL::Translator::Producer::MySQL',
+#        'Test::Differences',
+#    )
+#}
+use Test::Differences;
+use SQL::Translator;
+
+# Main test.
+{
+my $yaml_in = <<EOSCHEMA;
+---
+schema:
+  tables:
+    thing:
+      name: thing
+      extra:
+        mysql_charset: latin1 
+        mysql_collate: latin1_danish_ci 
+      order: 1
+      fields:
+        id:
+          name: id
+          data_type: unsigned int
+          is_primary_key: 1
+          is_auto_increment: 1
+          order: 0
+        name:
+          name: name
+          data_type: varchar
+          size:
+            - 32
+          order: 1
+        swedish_name:
+          name: swedish_name
+          data_type: varchar
+          size: 32
+          extra:
+            mysql_charset: swe7
+          order: 2
+        description:
+          name: description
+          data_type: text
+          extra:
+            mysql_charset: utf8
+            mysql_collate: utf8_general_ci
+          order: 3
+      constraints:
+        - type: UNIQUE
+          fields:
+            - name
+          name: idx_unique_name
+
+    thing2:
+      name: some.thing2
+      extra:
+      order: 2
+      fields:
+        id:
+          name: id
+          data_type: int
+          is_primary_key: 0
+          order: 0
+          is_foreign_key: 1
+        foo:
+          name: foo
+          data_type: int
+          order: 1
+          is_not_null: 1
+        foo2:
+          name: foo2
+          data_type: int
+          order: 2
+          is_not_null: 1
+        bar_set:
+          name: bar_set
+          data_type: set
+          order: 3
+          is_not_null: 1
+          extra:
+            list:
+              - foo
+              - bar
+              - baz
+      indices:
+        - type: NORMAL
+          fields:
+            - id
+          name: index_1
+        - type: NORMAL
+          fields:
+            - id
+          name: really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaaaaaaaaaaaa
+      constraints:
+        - type: PRIMARY_KEY
+          fields:
+            - id
+            - foo
+        - reference_table: thing
+          type: FOREIGN_KEY
+          fields: foo
+          name: fk_thing
+        - reference_table: thing
+          type: FOREIGN_KEY
+          fields: foo2
+          name: fk_thing
+
+EOSCHEMA
+
+my @stmts = (
+"SET foreign_key_checks=0",
+
+"DROP TABLE IF EXISTS `thing`",
+"CREATE TABLE `thing` (
+  `id` unsigned int auto_increment,
+  `name` varchar(32),
+  `swedish_name` varchar(32) character set swe7,
+  `description` text character set utf8 collate utf8_general_ci,
+  PRIMARY KEY (`id`),
+  UNIQUE `idx_unique_name` (`name`)
+) ENGINE=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci",
+
+"DROP TABLE IF EXISTS `some`.`thing2`",
+"CREATE TABLE `some`.`thing2` (
+  `id` integer,
+  `foo` integer,
+  `foo2` integer,
+  `bar_set` set('foo', 'bar', 'baz'),
+  INDEX index_1 (`id`),
+  INDEX really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaa_aed44c47 (`id`),
+  INDEX (`foo`),
+  INDEX (`foo2`),
+  PRIMARY KEY (`id`, `foo`),
+  CONSTRAINT `fk_thing` FOREIGN KEY (`foo`) REFERENCES `thing` (`id`),
+  CONSTRAINT `fk_thing_1` FOREIGN KEY (`foo2`) REFERENCES `thing` (`id`)
+) ENGINE=InnoDB",
+
+"SET foreign_key_checks=1",
+
+);
+
+my @stmts_no_drop = grep {$_ !~ /^DROP TABLE/} @stmts;
+
+my $mysql_out = join(";\n\n", @stmts_no_drop) . ";\n\n";
+
+
+    my $sqlt;
+    $sqlt = SQL::Translator->new(
+        show_warnings  => 1,
+        no_comments    => 1,
+#        debug          => 1,
+        from           => "YAML",
+        to             => "MySQL",
+        quote_table_names => 1,
+        quote_field_names => 1
+    );
+
+    my $out = $sqlt->translate(data => $yaml_in)
+    or die "Translate error:".$sqlt->error;
+    ok $out ne "",                    "Produced something!";
+    eq_or_diff $out, $mysql_out,      "Scalar output looks right with quoting";
+
+    my @out = $sqlt->translate(data => $yaml_in)
+      or die "Translat eerror:".$sqlt->error;
+    is_deeply \@out, \@stmts_no_drop, "Array output looks right with quoting";
+
+
+    @{$sqlt}{qw/quote_table_names quote_field_names/} = (0,0);
+    $out = $sqlt->translate(data => $yaml_in)
+      or die "Translate error:".$sqlt->error;
+
+    @out = $sqlt->translate(data => $yaml_in)
+      or die "Translate error:".$sqlt->error;
+    $mysql_out =~ s/`//g;
+    my @unquoted_stmts = map { s/`//g; $_} @stmts_no_drop;
+    eq_or_diff $out, $mysql_out,       "Output looks right without quoting";
+    is_deeply \@out, \@unquoted_stmts, "Array output looks right without quoting";
+
+    @{$sqlt}{qw/add_drop_table quote_field_names quote_table_names/} = (1,1,1);
+    @out = $sqlt->translate(data => $yaml_in)
+      or die "Translat eerror:".$sqlt->error;
+    $out = $sqlt->translate(data => $yaml_in)
+      or die "Translat eerror:".$sqlt->error;
+
+    eq_or_diff $out, join(";\n\n", @stmts) . ";\n\n", "Output looks right with DROP TABLEs";
+    is_deeply \@out, \@stmts,          "Array output looks right with DROP TABLEs";
+}
+
+###############################################################################
+# New alter/add subs
+
+my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
+
+my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
+                                                  table => $table,
+                                                  data_type => 'VARCHAR',
+                                                  size => 10,
+                                                  default_value => undef,
+                                                  is_auto_increment => 0,
+                                                  is_nullable => 1,
+                                                  is_foreign_key => 0,
+                                                  is_unique => 0 );
+
+my $field1_sql = SQL::Translator::Producer::MySQL::create_field($field1);
+
+is($field1_sql, 'myfield VARCHAR(10)', 'Create field works');
+
+my $field2 = SQL::Translator::Schema::Field->new( name      => 'myfield',
+                                                  table => $table,
+                                                  data_type => 'VARCHAR',
+                                                  size      => 25,
+                                                  default_value => undef,
+                                                  is_auto_increment => 0,
+                                                  is_nullable => 0,
+                                                  is_foreign_key => 0,
+                                                  is_unique => 0 );
+
+my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1,
+                                                                $field2);
+is($alter_field, 'ALTER TABLE mytable CHANGE COLUMN myfield myfield VARCHAR(25) NOT NULL', 'Alter field works');
+
+my $add_field = SQL::Translator::Producer::MySQL::add_field($field1);
+
+is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10)', 'Add field works');
+
+my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2);
+is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
+
+my $field3 = SQL::Translator::Schema::Field->new( name      => 'myfield',
+                                                  table => $table,
+                                                  data_type => 'boolean',
+                                                  is_nullable => 0,
+                                                  is_foreign_key => 0,
+                                                  is_unique => 0 );
+
+my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1 });
+is($field3_sql, 'myfield boolean NOT NULL', 'For Mysql >= 4, use boolean type');
+$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22 });
+is($field3_sql, "myfield enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type');
+$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,);
+is($field3_sql, "myfield enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');
+
+my $number_sizes = {
+    '3, 2' => 'double',
+    12 => 'bigint',
+    1 => 'tinyint',
+    4 => 'int',
+};
+for my $size (keys %$number_sizes) {
+    my $expected = $number_sizes->{$size};
+    my $number_field = SQL::Translator::Schema::Field->new( 
+        name => "numberfield_$expected",
+        table => $table,
+        data_type => 'number',
+        size => $size,
+        is_nullable => 1,
+        is_foreign_key => 0,
+        is_unique => 0 
+    );
+
+    is(
+        SQL::Translator::Producer::MySQL::create_field($number_field),
+        "numberfield_$expected $expected($size)",
+        "Use $expected for NUMBER types of size $size"
+    );
+}
+
+my $varchars;
+for my $size (qw/255 256 65535 65536/) {
+    $varchars->{$size} = SQL::Translator::Schema::Field->new( 
+        name => "vch_$size",
+        table => $table,
+        data_type => 'varchar',
+        size => $size,
+        is_nullable => 1,
+    );
+}
+
+
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003 }),
+    'vch_255 varchar(255)', 
+    'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
+);
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0 }),
+    'vch_255 varchar(255)', 
+    'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
+);
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{255}),
+    'vch_255 varchar(255)', 
+    'VARCHAR(255) is not substituted with TEXT when no version specified',
+);
+
+
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003 }),
+    'vch_256 varchar(256)', 
+    'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
+);
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0 }),
+    'vch_256 text', 
+    'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
+);
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{256}),
+    'vch_256 text', 
+    'VARCHAR(256) is substituted with TEXT when no version specified',
+);
+
+
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003 }),
+    'vch_65535 varchar(65535)', 
+    'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
+);
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0 }),
+    'vch_65535 text', 
+    'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
+);
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{65535}),
+    'vch_65535 text', 
+    'VARCHAR(65535) is substituted with TEXT when no version specified',
+);
+
+
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003 }),
+    'vch_65536 text', 
+    'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
+);
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0 }),
+    'vch_65536 text', 
+    'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
+);
+is (
+    SQL::Translator::Producer::MySQL::create_field($varchars->{65536}),
+    'vch_65536 text', 
+    'VARCHAR(65536) is substituted with TEXT when no version specified',
+);
+
+
+{
+  my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo',
+                                                  fields => [qw/id name/],
+                                                  sql => 'SELECT id, name FROM thing',
+                                                  extra => {
+                                                    mysql_definer => 'CURRENT_USER',
+                                                    mysql_algorithm => 'MERGE',
+                                                    mysql_security => 'DEFINER',
+                                                  });
+  my $create_opts = { add_replace_view => 1, no_comments => 1 };
+  my $view1_sql1 = SQL::Translator::Producer::MySQL::create_view($view1, $create_opts);
+
+  my $view_sql_replace = "CREATE OR REPLACE
+   ALGORITHM = MERGE
+   DEFINER = CURRENT_USER
+   SQL SECURITY DEFINER
+  VIEW view_foo ( id, name ) AS (
+    SELECT id, name FROM thing
+  )";
+  is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
+
+
+  my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo',
+                                                  fields => [qw/id name/],
+                                                  sql => 'SELECT id, name FROM thing',);
+  my $create2_opts = { add_replace_view => 0, no_comments => 1 };
+  my $view1_sql2 = SQL::Translator::Producer::MySQL::create_view($view2, $create2_opts);
+  my $view_sql_noreplace = "CREATE
+  VIEW view_foo ( id, name ) AS (
+    SELECT id, name FROM thing
+  )";
+  is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');
+  
+  {
+    my %extra = $view1->extra;
+    is_deeply \%extra,
+      {
+        'mysql_algorithm' => 'MERGE',
+        'mysql_definer'   => 'CURRENT_USER',
+        'mysql_security'  => 'DEFINER'
+      },
+      'Extra attributes';
+  }
+
+  $view1->remove_extra(qw/mysql_definer mysql_security/);
+  {
+    my %extra = $view1->extra;
+    is_deeply \%extra, { 'mysql_algorithm' => 'MERGE', }, 'Extra attributes after first reset_extra call';
+  }
+
+  $view1->remove_extra();
+  {
+    my %extra = $view1->extra;
+    is_deeply \%extra, {}, 'Extra attributes completely removed';
+  }
+}
+
+{
+
+    # certain types do not support a size, see also:
+    # http://dev.mysql.com/doc/refman/5.1/de/create-table.html
+    for my $type (qw/date time timestamp datetime year/) {
+        my $field = SQL::Translator::Schema::Field->new(
+            name              => "my$type",
+            table             => $table,
+            data_type         => $type,
+            size              => 10,
+            default_value     => undef,
+            is_auto_increment => 0,
+            is_nullable       => 1,
+            is_foreign_key    => 0,
+            is_unique         => 0
+        );
+        my $sql = SQL::Translator::Producer::MySQL::create_field($field);
+        is($sql, "my$type $type", "Skip length param for type $type");
+    }
+}
+
+done_testing;
diff --git a/t/47postgres-producer.t b/t/47postgres-producer.t
new file mode 100644 (file)
index 0000000..60600e5
--- /dev/null
@@ -0,0 +1,352 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Exception;
+use Data::Dumper;
+use FindBin qw/$Bin/;
+use Test::Differences;
+use SQL::Translator;
+use SQL::Translator::Object::Table;
+use SQL::Translator::Object::Column;
+
+my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field;
+
+my $table = SQL::Translator::Object::Table->new( name => 'mytable');
+
+my $field1 = SQL::Translator::Object::Column->new( name => 'myfield',
+                                                  table => $table,
+                                                  data_type => 'VARCHAR',
+                                                  size => 10,
+                                                  default_value => undef,
+                                                  is_auto_increment => 0,
+                                                  is_nullable => 1,
+                                                  is_foreign_key => 0,
+                                                  is_unique => 0 );
+
+my $field1_sql = SQL::Translator::Producer::PostgreSQL::create_field($field1);
+
+is($field1_sql, 'myfield character varying(10)', 'Create field works');
+
+my $field2 = SQL::Translator::Object::Column->new( name      => 'myfield',
+                                                  table => $table,
+                                                  data_type => 'VARCHAR',
+                                                  size      => 25,
+                                                  default_value => undef,
+                                                  is_auto_increment => 0,
+                                                  is_nullable => 0,
+                                                  is_foreign_key => 0,
+                                                  is_unique => 0 );
+
+my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1,
+                                                                $field2);
+is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL
+ALTER TABLE mytable ALTER COLUMN myfield TYPE character varying(25)],
+ 'Alter field works');
+
+$field1->name('field3');
+my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1);
+
+is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works');
+
+my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2);
+is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
+
+my $field3 = SQL::Translator::Object::Column->new( name      => 'time_field',
+                                                  table => $table,
+                                                  data_type => 'TIME',
+                                                  default_value => undef,
+                                                  is_auto_increment => 0,
+                                                  is_nullable => 0,
+                                                  is_foreign_key => 0,
+                                                  is_unique => 0 );
+
+my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3);
+
+is($field3_sql, 'time_field time NOT NULL', 'Create time field works');
+
+my $field3_datetime_with_TZ = SQL::Translator::Object::Column->new(
+    name      => 'datetime_with_TZ',
+    table     => $table,
+    data_type => 'timestamp with time zone',
+    size      => 7,
+);
+
+my $field3_datetime_with_TZ_sql = 
+    SQL::Translator::Producer::PostgreSQL::create_field(
+        $field3_datetime_with_TZ
+    );
+
+is(
+    $field3_datetime_with_TZ_sql, 
+    'datetime_with_TZ timestamp(6) with time zone', 
+    'Create time field with time zone and size, works'
+);
+
+my $field3_time_without_TZ = SQL::Translator::Object::Column->new(
+    name      => 'time_without_TZ',
+    table     => $table,
+    data_type => 'time without time zone',
+    size      => 2,
+);
+
+my $field3_time_without_TZ_sql 
+    = SQL::Translator::Producer::PostgreSQL::create_field(
+        $field3_time_without_TZ
+    );
+
+is(
+    $field3_time_without_TZ_sql, 
+    'time_without_TZ time(2) without time zone', 
+    'Create time field without time zone but with size, works'
+);
+
+my $field4 = SQL::Translator::Object::Column->new( name      => 'bytea_field',
+                                                  table => $table,
+                                                  data_type => 'bytea',
+                                                  size => '16777215',
+                                                  default_value => undef,
+                                                  is_auto_increment => 0,
+                                                  is_nullable => 0,
+                                                  is_foreign_key => 0,
+                                                  is_unique => 0 );
+
+my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4);
+
+is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works');
+
+my $field5 = SQL::Translator::Object::Column->new( name => 'enum_field',
+                                                   table => $table,
+                                                   data_type => 'enum',
+                                                   extra => { list => [ 'Foo', 'Bar' ] },
+                                                   is_auto_increment => 0,
+                                                   is_nullable => 0,
+                                                   is_foreign_key => 0,
+                                                   is_unique => 0 );
+
+my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 });
+
+is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
+
+
+
+
+my $field6 = SQL::Translator::Object::Column->new(
+                                                  name      => 'character',
+                                                  table => $table,
+                                                  data_type => 'character',
+                                                  size => '123',
+                                                  default_value => 'foobar',
+                                                    is_auto_increment => 0,
+                                                    is_nullable => 0,
+                                                    is_foreign_key => 0,
+                                                    is_unique => 0);
+
+my $field7 = SQL::Translator::Object::Column->new(
+                                name      => 'character',
+                                table => $table,
+                                data_type => 'character',
+                                size => '123',
+                                default_value => undef,
+                                  is_auto_increment => 0,
+                                  is_nullable => 0,
+                                  is_foreign_key => 0,
+                                  is_unique => 0);
+
+$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
+                                                                $field7);
+
+is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP DEFAULT), 'DROP DEFAULT');
+
+$field7->default_value(q(foo'bar'));
+
+$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
+                                                                $field7);
+
+is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT 'foo''bar'''), 'DEFAULT with escaping');
+
+$field7->default_value(\q(foobar));
+
+$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
+                                                                $field7);
+
+is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT foobar), 'DEFAULT unescaped if scalarref');
+
+$field7->is_nullable(1);
+$field7->default_value(q(foobar));
+
+$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
+                                                                $field7);
+
+is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL');
+
+my $field8 = SQL::Translator::Object::Column->new( name => 'ts_field',
+                                                   table => $table,
+                                                   data_type => 'timestamp with time zone',
+                                                   size => 6,
+                                                   is_auto_increment => 0,
+                                                   is_nullable => 0,
+                                                   is_foreign_key => 0,
+                                                   is_unique => 0 );
+
+my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 });
+
+is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision');
+
+my $field9 = SQL::Translator::Object::Column->new( name => 'time_field',
+                                                   table => $table,
+                                                   data_type => 'time with time zone',
+                                                   size => 6,
+                                                   is_auto_increment => 0,
+                                                   is_nullable => 0,
+                                                   is_foreign_key => 0,
+                                                   is_unique => 0 );
+
+my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 });
+
+is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision');
+
+my $field10 = SQL::Translator::Object::Column->new( name => 'interval_field',
+                                                   table => $table,
+                                                   data_type => 'interval',
+                                                   size => 6,
+                                                   is_auto_increment => 0,
+                                                   is_nullable => 0,
+                                                   is_foreign_key => 0,
+                                                   is_unique => 0 );
+
+my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 });
+
+is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision');
+
+
+my $field11 = SQL::Translator::Object::Column->new( name => 'time_field',
+                                                   table => $table,
+                                                   data_type => 'time without time zone',
+                                                   size => 6,
+                                                   is_auto_increment => 0,
+                                                   is_nullable => 0,
+                                                   is_foreign_key => 0,
+                                                   is_unique => 0 );
+
+my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 });
+
+is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision');
+
+
+
+my $field12 = SQL::Translator::Object::Column->new( name => 'time_field',
+                                                   table => $table,
+                                                   data_type => 'timestamp',
+                                                   is_auto_increment => 0,
+                                                   is_nullable => 0,
+                                                   is_foreign_key => 0,
+                                                   is_unique => 0 );
+
+my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 });
+
+is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision');
+
+
+{
+    # let's test default values! -- rjbs, 2008-09-30
+    my %field = (
+        table => $table,
+        data_type => 'VARCHAR',
+        size => 10,
+        is_auto_increment => 0,
+        is_nullable => 1,
+        is_foreign_key => 0,
+        is_unique => 0,
+    );
+
+    {
+        my $simple_default = SQL::Translator::Object::Column->new(
+            %field,
+            name => 'str_default',
+            default_value => 'foo',
+        );
+
+        is(
+            $PRODUCER->($simple_default),
+            q{str_default character varying(10) DEFAULT 'foo'},
+            'default str',
+        );
+    }
+
+    {
+        my $null_default = SQL::Translator::Object::Column->new(
+            %field,
+            name => 'null_default',
+            default_value => \'NULL',
+        );
+
+        is(
+            $PRODUCER->($null_default),
+            q{null_default character varying(10) DEFAULT NULL},
+            'default null',
+        );
+    }
+
+    {
+        my $null_default = SQL::Translator::Object::Column->new(
+            %field,
+            name => 'null_default_2',
+            default_value => 'NULL', # XXX: this should go away
+        );
+
+        is(
+            $PRODUCER->($null_default),
+            q{null_default_2 character varying(10) DEFAULT NULL},
+            'default null from special cased string',
+        );
+    }
+
+    {
+        my $func_default = SQL::Translator::Object::Column->new(
+            %field,
+            name => 'func_default',
+            default_value => \'func(funky)',
+        );
+
+        is(
+            $PRODUCER->($func_default),
+            q{func_default character varying(10) DEFAULT func(funky)},
+            'unquoted default from scalar ref',
+        );
+    }
+}
+
+
+my $view1 = SQL::Translator::Schema::View->new(
+    name   => 'view_foo',
+    fields => [qw/id name/],
+    sql    => 'SELECT id, name FROM thing',
+);
+my $create_opts = { add_replace_view => 1, no_comments => 1 };
+my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
+
+my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS
+    SELECT id, name FROM thing
+";
+is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
+
+my $view2 = SQL::Translator::Schema::View->new(
+    name   => 'view_foo2',
+    sql    => 'SELECT id, name FROM thing',
+    extra  => {
+      'temporary'    => '1',
+      'check_option' => 'cascaded',
+    },
+);
+my $create2_opts = { add_replace_view => 1, no_comments => 1 };
+my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
+
+my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS
+    SELECT id, name FROM thing
+ WITH CASCADED CHECK OPTION";
+is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');
+
+done_testing;
diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t
new file mode 100644 (file)
index 0000000..2bf3881
--- /dev/null
@@ -0,0 +1,34 @@
+use strict;
+use Test::More;
+use FindBin qw/$Bin/;
+
+use SQL::Translator::Object::View;
+use SQL::Translator::Producer::SQL::SQLite;
+
+{
+  my $view1 = SQL::Translator::Object::View->new( name => 'view_foo',
+                                                  fields => [qw/id name/],
+                                                  sql => 'SELECT id, name FROM thing',
+                                                  extra => {
+                                                    temporary => 1,
+                                                    if_not_exists => 1,
+                                                  });
+  my $create_opts = { no_comments => 1 };
+  my $view1_sql1 = SQL::Translator::Producer::SQL::SQLite::create_view($view1, $create_opts);
+
+  my $view_sql_replace = "CREATE TEMPORARY VIEW IF NOT EXISTS view_foo AS
+    SELECT id, name FROM thing";
+  is($view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL');
+
+
+  my $view2 = SQL::Translator::Object::View->new( name => 'view_foo',
+                                                  fields => [qw/id name/],
+                                                  sql => 'SELECT id, name FROM thing',);
+
+  my $view1_sql2 = SQL::Translator::Producer::SQL::SQLite::create_view($view2, $create_opts);
+  my $view_sql_noreplace = "CREATE VIEW view_foo AS
+    SELECT id, name FROM thing";
+  is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');
+}
+
+done_testing;
index ffcb9da..0bdf0c7 100644 (file)
@@ -1,14 +1,9 @@
 use warnings;
 use strict;
-use Test::SQL::Translator;
+use Test::More;
 use Test::Differences;
 use FindBin qw/$Bin/;
 
-BEGIN {
-    maybe_plan(1, 'SQL::Translator::Parser::XML',
-                  'SQL::Translator::Producer::XML');
-}
-
 # It's very hard to read and modify YAML by hand. Thus we
 # use an XML file for definitions, and generate a YAML from
 # it in Makefile.PL, so we do not saddle the user with XML
@@ -31,11 +26,15 @@ sub _parse_to_xml {
   my $tr = SQL::Translator->new;
   $tr->no_comments (1); # this will drop the XML header
 
+  open (my $fh, '<', $fn) or die "$fn: $!"; 
+
   my $xml = $tr->translate (
     parser => $type,
-    file => $fn,
+    data => do { local $/; <$fh> },
     producer => 'XML',
   ) or die $tr->error;
 
   return $xml;
 }
+
+done_testing;
diff --git a/t/data/roundtrip.xml b/t/data/roundtrip.xml
new file mode 100644 (file)
index 0000000..db07e42
--- /dev/null
@@ -0,0 +1,115 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 
+Created by SQL::Translator::Producer::SqlfXML
+Created on Fri Aug 15 15:08:18 2003
+
+ -->
+<schema xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
+    
+  <tables>
+      <table order="1" name="Basic">
+        <fields>
+          <field
+              name="id"
+              is_primary_key="1" is_foreign_key="0"
+              size="10" data_type="int" is_auto_increment="1" order="1"
+              is_nullable="0">
+              <extra ZEROFILL="1" />
+          </field>
+          <field
+              name="title"
+              is_primary_key="0" is_foreign_key="0"
+              size="100" is_auto_increment="0" data_type="varchar"
+              order="2" default_value="hello" is_nullable="0" />
+          <field
+              name="description"
+              size="0" data_type="text" order="3" default_value="" />
+          <field name="email" size="500" data_type="varchar" order="4">
+              <extra foo="bar" hello="world" bar="baz" />
+          </field>
+          <field name="explicitnulldef" size="0" data_type="varchar" order="5" />
+          <field name="explicitemptystring" size="0"
+              data_type="varchar" order="6" default_value="" />
+          <field name="emptytagdef" size="0"
+              data_type="varchar" order="7" default_value="" >
+              <comments>Hello emptytagdef</comments>
+          </field>
+          <field name="another_id" size="10"
+              data_type="int" default_value="2" />
+          <field name="timest" size="0"
+              data_type="timestamp" order="7" >
+          </field>
+        </fields>
+
+        <indices>
+          <index name="titleindex" fields="title" type="NORMAL">
+            <extra foo="bar" hello="world" bar="baz" />
+          </index>
+        </indices>
+
+        <constraints>
+          <constraint name="" type="PRIMARY KEY" fields="id"
+              reference_table="" options="" deferrable="1" match_type=""
+              expression="" on_update="" on_delete="">
+              <extra foo="bar" hello="world" bar="baz" />
+          </constraint>
+          <constraint name="emailuniqueindex" type="UNIQUE" fields="email" />
+          <constraint name="Basic" type="FOREIGN KEY" fields="another_id"
+              reference_table="Another" options="" deferrable="1" match_type=""
+              expression="" on_update="" on_delete="">
+          </constraint>
+        </constraints>
+
+        <extra foo="bar" hello="world" bar="baz" mysql_table_type="InnoDB" />
+      </table>
+
+      <table order="1" name="Another">
+        <fields>
+          <field
+              name="id"
+              is_primary_key="1" is_foreign_key="0"
+              size="10" data_type="int" is_auto_increment="1" order="1"
+              is_nullable="0">
+          </field>
+        </fields>
+
+        <constraints>
+          <constraint name="" type="PRIMARY KEY" fields="id"
+              reference_table="" options="" deferrable="1" match_type=""
+              expression="" on_update="" on_delete="">
+          </constraint>
+        </constraints>
+
+        <extra foo="bar" hello="world" bar="baz" mysql_table_type="InnoDB" />
+      </table>
+  </tables>
+
+  <views>
+      <view name="email_list" fields="email" order="1">
+          <sql>SELECT email FROM Basic WHERE (email IS NOT NULL)</sql>
+          <extra foo="bar" hello="world" bar="baz" />
+      </view>
+  </views>
+
+  <triggers>
+      <trigger name="foo_trigger" database_event="insert" on_table="Basic"
+          perform_action_when="after" order="1">
+          <action>update modified=timestamp();</action>
+          <extra foo="bar" hello="world" bar="baz" />
+      </trigger>
+      <trigger name="bar_trigger" database_events="insert , update" on_table="Basic"
+          perform_action_when="before" order="1">
+          <action>update modified2=timestamp();</action>
+          <extra hello="aliens" />
+      </trigger>
+  </triggers>
+
+  <procedures>
+      <procedure name="foo_proc" order="1" owner="Nomar" parameters="foo,bar">
+          <sql>select foo from bar</sql>
+          <comments>Go Sox!</comments>
+          <extra foo="bar" hello="world" bar="baz" />
+      </procedure>
+  </procedures>
+
+</schema>