SQL-Translator

 view release on metacpan or  search on metacpan

t/13schema.t  view on Meta::CPAN

use warnings;
use Test::More;
use Test::Exception;
use SQL::Translator::Schema::Constants;

require_ok('SQL::Translator');
require_ok('SQL::Translator::Schema');

{
  #
  # Schema
  #
  my $schema = SQL::Translator::Schema->new(
    name     => 'foo',
    database => 'MySQL',
  );
  isa_ok($schema, 'SQL::Translator::Schema');

  is($schema->name,        'foo', 'Schema name is "foo"');
  is($schema->name('bar'), 'bar', 'Schema name changed to "bar"');

  is($schema->database,               'MySQL',      'Schema database is "MySQL"');
  is($schema->database('PostgreSQL'), 'PostgreSQL', 'Schema database changed to "PostgreSQL"');

  is($schema->is_valid, undef, 'Schema not valid...');
  like($schema->error, qr/no tables/i, '...because there are no tables');

  #
  # $schema->add_*
  #
  my $foo_table = $schema->add_table(name => 'foo') or warn $schema->error;
  isa_ok($foo_table, 'SQL::Translator::Schema::Table', 'Table "foo"');

  my $bar_table = SQL::Translator::Schema::Table->new(name => 'bar')
      or warn SQL::Translator::Schema::Table->error;
  $bar_table = $schema->add_table($bar_table);
  isa_ok($bar_table, 'SQL::Translator::Schema::Table', 'Table "bar"');
  is($bar_table->name, 'bar', 'Add table "bar"');

  $schema = $bar_table->schema($schema);
  isa_ok($schema, 'SQL::Translator::Schema', 'Schema');

  is($bar_table->name('foo'), undef, q[Can't change name of table "bar" to "foo"...]);
  like($bar_table->error, qr/can't use table name/i, q[...because "foo" exists]);

  my $redundant_table = $schema->add_table(name => 'foo');
  is($redundant_table, undef, qq[Can't create another "foo" table...]);
  like($schema->error, qr/can't use table name/i, '... because "foo" exists');

  $redundant_table = $schema->add_table(name => '');
  is($redundant_table, undef, qq[Can't add an anonymous table...]);
  like($schema->error, qr/No table name/i, '... because it has no name ');

  $redundant_table = SQL::Translator::Schema::Table->new(name => '');
  is($redundant_table, undef, qq[Can't create an anonymous table]);
  like(SQL::Translator::Schema::Table->error, qr/No table name/i, '... because it has no name ');

  #
  # $schema-> drop_table
  #
  my $dropped_table = $schema->drop_table($foo_table->name, cascade => 1);
  isa_ok($dropped_table, 'SQL::Translator::Schema::Table', 'Dropped table "foo"');
  $schema->add_table($foo_table);
  my $dropped_table2 = $schema->drop_table($foo_table, cascade => 1);
  isa_ok($dropped_table2, 'SQL::Translator::Schema::Table', 'Dropped table "foo" by object');
  my $dropped_table3 = $schema->drop_table($foo_table->name, cascade => 1);
  like($schema->error, qr/doesn't exist/, qq[Can't drop non-existant table "foo"]);

  $schema->add_table($foo_table);
  #
  # Table default new
  #
  is($foo_table->name,     'foo', 'Table name is "foo"');
  is("$foo_table",         'foo', 'Table stringifies to "foo"');
  is($foo_table->is_valid, undef, 'Table "foo" is not yet valid');

  my $fields = $foo_table->get_fields;
  is(scalar @{ $fields || [] }, 0, 'Table "foo" has no fields');
  like($foo_table->error, qr/no fields/i, 'Error for no fields');

  is($foo_table->comments, undef, 'No comments');

  #
  # New table with args
  #
  my $person_table = $schema->add_table(
    name     => 'person',
    comments => 'foo',
  );
  is($person_table->name,                       'person',   'Table name is "person"');
  is($person_table->is_valid,                   undef,      'Table is not yet valid');
  is($person_table->comments,                   'foo',      'Comments = "foo"');
  is(join(',', $person_table->comments('bar')), 'foo,bar',  'Table comments = "foo,bar"');
  is($person_table->comments,                   "foo\nbar", 'Table comments = "foo,bar"');

  #
  # Field default new
  #
  my $f1 = $person_table->add_field(name => 'foo')
      or warn $person_table->error;
  isa_ok($f1, 'SQL::Translator::Schema::Field', 'Field');
  is($f1->name,             'foo',        'Field name is "foo"');
  is($f1->full_name,        'person.foo', 'Field full_name is "person.foo"');
  is("$f1",                 'foo',        'Field stringifies to "foo"');
  is($f1->data_type,        '',           'Field data type is blank');
  is($f1->size,             0,            'Field size is "0"');
  is($f1->is_primary_key,   '0',          'Field is_primary_key is false');
  is($f1->is_nullable,      1,            'Field can be NULL');
  is($f1->default_value,    undef,        'Field default is undefined');
  is($f1->comments,         '',           'No comments');
  is($f1->table,            'person',     'Field table is person');
  is($f1->schema->database, 'PostgreSQL', 'Field schema shortcut works');

  my $f2 = SQL::Translator::Schema::Field->new(
    name     => 'f2',
    comments => 'foo',
  ) or warn SQL::Translator::Schema::Field->error;
  $f2 = $person_table->add_field($f2);
  isa_ok($f1, 'SQL::Translator::Schema::Field', 'f2');
  is($f2->name,                       'f2',       'Add field "f2"');
  is($f2->is_nullable(0),             0,          'Field cannot be NULL');
  is($f2->is_nullable(''),            0,          'Field cannot be NULL');
  is($f2->is_nullable('0'),           0,          'Field cannot be NULL');
  is($f2->default_value(''),          '',         'Field default is empty string');
  is($f2->comments,                   'foo',      'Field comment = "foo"');
  is(join(',', $f2->comments('bar')), 'foo,bar',  'Field comment = "foo,bar"');
  is($f2->comments,                   "foo\nbar", 'Field comment = "foo,bar"');

  $person_table = $f2->table($person_table);
  isa_ok($person_table, 'SQL::Translator::Schema::Table', 'person_table');

  is($f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...]);
  like($f2->error, qr/can't use field name/i, '...because name exists');

  my $redundant_field = $person_table->add_field(name => 'f2');
  is($redundant_field, undef, qq[Didn't create another "f2" field...]);
  like($person_table->error, qr/can't use field/i, '... because it exists');

  $redundant_field = $person_table->add_field(name => '');
  is($redundant_field, undef, qq[Didn't add a "" field...]);
  like($person_table->error, qr/No field name/i, '... because it has no name');

  $redundant_field = SQL::Translator::Schema::Field->new(name => '');
  is($redundant_field, undef, qq[Didn't create a "" field...]);
  like(SQL::Translator::Schema::Field->error, qr/No field name/i, '... because it has no name');

  my @fields = $person_table->get_fields;
  is(scalar @fields, 2, 'Table "foo" has 2 fields');

  is($fields[0]->name,                      'foo',    'First field is "foo"');
  is($fields[1]->name,                      'f2',     'Second field is "f2"');
  is(join(",", $person_table->field_names), 'foo,f2', 'field_names is "foo,f2"');

  my $ci_field = $person_table->get_field('FOO', 'case_insensitive');
  is($ci_field->name, 'foo', 'Got field case-insensitively');
  #
  # $table-> drop_field
  #
  my $dropped_field = $person_table->drop_field($f2->name, cascade => 1);
  isa_ok($dropped_field, 'SQL::Translator::Schema::Field', 'Dropped field "f2"');
  $person_table->add_field($f2);
  my $dropped_field2 = $person_table->drop_field($f2, cascade => 1);
  isa_ok($dropped_field2, 'SQL::Translator::Schema::Field', 'Dropped field "f2" by object');
  my $dropped_field3 = $person_table->drop_field($f2->name, cascade => 1);
  like($person_table->error, qr/doesn't exist/, qq[Can't drop non-existant field "f2"]);

  $person_table->add_field($f2);

  #
  # Field methods
  #
  is($f1->name('person_name'),  'person_name', 'Field name is "person_name"');
  is($f1->data_type('varchar'), 'varchar',     'Field data type is "varchar"');
  is($f1->size('30'),           '30',          'Field size is "30"');
  is($f1->is_primary_key(0),    '0',           'Field is_primary_key is negative');

  $f1->extra(foo => 'bar');
  $f1->extra({ baz => 'quux' });
  my %extra = $f1->extra;
  is($extra{'foo'}, 'bar',  'Field extra "foo" is "bar"');
  is($extra{'baz'}, 'quux', 'Field extra "baz" is "quux"');

  #
  # New field with args
  #
  my $age = $person_table->add_field(
    name      => 'age',
    data_type => 'float',
    size      => '10,2',
  );
  is($age->name,             'age',   'Field name is "age"');
  is($age->data_type,        'float', 'Field data type is "float"');
  is($age->size,             '10,2',  'Field size is "10,2"');
  is($age->size(10, 2),      '10,2',  'Field size still "10,2"');
  is($age->size([ 10, 2 ]),  '10,2',  'Field size still "10,2"');
  is($age->size(qw[ 10 2 ]), '10,2',  'Field size still "10,2"');
  is(join(':', $age->size),  '10:2',  'Field size returns array');

  #
  # Index
  #
  my @indices = $person_table->get_indices;
  is(scalar @indices, 0, 'No indices');
  like($person_table->error, qr/no indices/i, 'Error for no indices');
  my $index1 = $person_table->add_index(name => "foo")
      or warn $person_table->error;
  isa_ok($index1, 'SQL::Translator::Schema::Index', 'Index');
  is($index1->name, 'foo', 'Index name is "foo"');

  is($index1->is_valid, undef, 'Index name is not valid...');
  like($index1->error, qr/no fields/i, '...because it has no fields');

  is(join(':', $index1->fields('foo,bar')), 'foo:bar', 'Index accepts fields');

  is($index1->is_valid, undef, 'Index name is not valid...');
  like($index1->error, qr/does not exist in table/i, '...because it used fields not in the table');

  is(join(':', $index1->fields(qw[foo age])), 'foo:age', 'Index accepts fields');
  is($index1->is_valid,                       1,         'Index name is now valid');

  is($index1->type, NORMAL, 'Index type is "normal"');

  my $index2 = SQL::Translator::Schema::Index->new(name => "bar")
      or warn SQL::Translator::Schema::Index->error;



( run in 0.980 second using v1.01-cache-2.11-cpan-524268b4103 )