Add failing test
Marc Mims [Thu, 14 Sep 2017 23:33:56 +0000 (16:33 -0700)]
Fixtures dumps virtual columns which causes populate to fail because the
column doesn't exist in the table.

This failing test demonstrates the problem.

dist.ini
t/20-virtual-column.t [new file with mode: 0644]
t/lib/DBICTest/Schema3.pm [new file with mode: 0644]
t/var/configs/virtual-columns.json [new file with mode: 0644]

index 2d17c3e..22301fd 100644 (file)
--- a/dist.ini
+++ b/dist.ini
@@ -64,6 +64,7 @@ DBIx::Class::InflateColumn::FS = 0.01007
 ExtUtils::MakeMaker = 6.59
 Test::More = 0.98
 Devel::Confess = 0
+Test::Fatal = 0
 
 [Prereqs]
 Class::Accessor::Grouped = 0.1001
diff --git a/t/20-virtual-column.t b/t/20-virtual-column.t
new file mode 100644 (file)
index 0000000..6d8de64
--- /dev/null
@@ -0,0 +1,57 @@
+use DBIx::Class::Fixtures;
+use Test::More;
+use Test::Fatal;
+use File::Path 'rmtree';
+
+use lib qw(t/lib/DBICTest);
+use Schema3;
+use Test::TempDir::Tiny;
+use IO::All;
+
+my $tempdir = tempdir;
+
+(my $schema = Schema3->connect(
+  'DBI:SQLite::memory:','',''))->init_schema;
+
+ok my $row = $schema
+  ->resultset('Person')
+  ->first;
+
+ok $row->get_column('weight_to_height_ratio'),
+    'has virtual column';
+
+my $fixtures = DBIx::Class::Fixtures
+  ->new({
+    config_dir => io->catfile(qw't var configs')->name,
+    debug => 0 });
+
+ok(
+  $fixtures->dump({
+    config => 'virtual-columns.json',
+    schema => $schema,
+    directory => io->catfile($tempdir, 'people')->name }),
+  'fetch dump executed okay');
+
+ok $schema->resultset('Person')->delete;
+
+is exception {
+  $fixtures->populate({
+    no_deploy => 1,
+    schema => $schema,
+    directory => io->catfile($tempdir, 'people')->name
+  })
+}, undef, 'populated';
+
+$row = $schema->resultset('Person')->first;
+
+BAIL_OUT("can't continue without data") unless $row;
+
+ok $row->get_column('weight_to_height_ratio'),
+  'still has virtual column';
+
+done_testing;
+
+END {
+    rmtree io->catfile(qw't var files')->name;
+    rmtree io->catfile($tempdir, 'people')->name;
+}
diff --git a/t/lib/DBICTest/Schema3.pm b/t/lib/DBICTest/Schema3.pm
new file mode 100644 (file)
index 0000000..0150aa9
--- /dev/null
@@ -0,0 +1,71 @@
+package Schema3::Result::Person;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('person');
+
+__PACKAGE__->add_columns(
+  id => {
+       data_type => 'integer',
+       is_auto_increment => 1,
+  },
+  name => {
+    data_type => 'varchar',
+    size => 255,
+  },
+  weight => {
+    datatype => 'float',
+  },
+  height => {
+    datatype => 'float',
+  },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+# Add virtual column
+__PACKAGE__->resultset_attributes({
+  '+select' => [ \'weight/height' ],
+  '+as'     => [ 'weight_to_height_ratio' ],
+});
+
+package Schema3;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Schema';
+
+__PACKAGE__->register_class(Person => 'Schema3::Result::Person');
+
+sub load_sql {
+  local $/ = undef;
+  my $sql = <DATA>;
+}
+
+sub init_schema {
+  my $sql = (my $schema = shift)
+    ->load_sql;
+
+  ($schema->storage->dbh->do($_) ||
+   die "Error on SQL: $_\n")
+    for split(/;\n/, $sql);
+}
+
+1;
+
+__DATA__
+CREATE TABLE person (
+  id INTEGER PRIMARY KEY NOT NULL,
+  name varchar(255) NOT NULL,
+  weight FLOAT NOT NULL,
+  height FLOAT NOT NULL
+);
+
+INSERT INTO person (name, weight, height)
+VALUES
+('Fred Flintstone', 220, 5.2),
+('Barney Rubble', 190, 4.8)
diff --git a/t/var/configs/virtual-columns.json b/t/var/configs/virtual-columns.json
new file mode 100644 (file)
index 0000000..a7fd694
--- /dev/null
@@ -0,0 +1,6 @@
+{
+       "sets": [{
+               "class": "Person",
+               "quantity": "all",
+       }]
+}