tests: init_schema now accepts a db_dir parameter
[dbsrgits/DBIx-Class-Fixtures.git] / t / 12-populate-basic.t
CommitLineData
4fb695f4 1#!perl
2
3use DBIx::Class::Fixtures;
675669cc 4use Test::More no_plan;
4fb695f4 5use lib qw(t/lib);
6use DBICTest;
7use Path::Class;
3c528252 8use Data::Dumper;
66d02e24 9use IO::All;
4fb695f4 10
11# set up and populate schema
12ok(my $schema = DBICTest->init_schema(), 'got schema');
66d02e24 13my $config_dir = io->catfile(qw't var configs')->name;
4fb695f4 14
15# do dump
8a1df391 16ok(my $fixtures = DBIx::Class::Fixtures->new({
17 config_dir => $config_dir,
18 debug => 0
19 }), 'object created with correct config dir'
20);
4fb695f4 21
3c528252 22foreach my $set ('simple', 'quantity', 'fetch', 'rules') {
23 no warnings 'redefine';
24 DBICTest->clear_schema($schema);
25 DBICTest->populate_schema($schema);
8a1df391 26 ok($fixtures->dump({
27 config => "$set.json",
28 schema => $schema,
66d02e24 29 directory => io->catfile(qw't var fixtures')->name
8a1df391 30 }), "$set dump executed okay"
31 );
32 $fixtures->populate({
66d02e24 33 ddl => io->catfile(qw[t lib sqlite.sql])->name,
34 connection_details => ['dbi:SQLite:'.io->catfile(qw[ t var DBIxClass.db ])->name, '', ''],
35 directory => io->catfile(qw't var fixtures')->name
8a1df391 36 });
37
65a80d4e 38 $schema = DBICTest->init_schema( no_deploy => 1);
3c528252 39
66d02e24 40 my $fixture_dir = dir(io->catfile(qw't var fixtures')->name);
3c528252 41 foreach my $class ($schema->sources) {
42 my $source_dir = dir($fixture_dir, lc($class));
8a1df391 43 is($schema->resultset($class)->count,
44 (-e $source_dir) ? scalar($source_dir->children) : 0,
45 "correct number of $set " . lc($class)
46 );
47
3c528252 48 next unless (-e $source_dir);
49
50 my $rs = $schema->resultset($class);
51 foreach my $row ($rs->all) {
52 my $file = file($source_dir, $row->id . '.fix');
53 my $HASH1; eval($file->slurp());
8a1df391 54 is_deeply(
55 $HASH1,
56 {$row->get_columns},
57 "$set " . lc($class) . " row " . $row->id . " imported okay"
58 );
3c528252 59 }
60 }
61}
65a80d4e 62
63# use_create => 1
64$schema = DBICTest->init_schema();
65$fixtures = DBIx::Class::Fixtures->new({
66 config_dir => $config_dir,
67 debug => 0
68});
69ok( $fixtures->dump({
70 config => "use_create.json",
71 schema => $schema,
66d02e24 72 directory => io->catfile(qw't var fixtures')->name
65a80d4e 73 }), "use_create dump executed okay"
74);
75$schema = DBICTest->init_schema( no_populate => 1 );
76$fixtures->populate({
66d02e24 77 directory => io->catfile(qw't var fixtures')->name,
78 connection_details => ['dbi:SQLite:'.io->catfile(qw[ t var DBIxClass.db ])->name, '', ''],
65a80d4e 79 schema => $schema,
80 no_deploy => 1,
81 use_create => 1
82});
83$schema = DBICTest->init_schema( no_deploy => 1, no_populate => 1 );
84is( $schema->resultset( "Artist" )->find({ artistid => 4 })->name, "Test Name", "use_create => 1 ok" );
e54ab7f3 85
86$schema = DBICTest->init_schema( no_populate => 1 );
87$fixtures->populate({
66d02e24 88 directory => io->catfile(qw't var fixtures')->name,
89 connection_details => ['dbi:SQLite:'.io->catfile(qw[ t var DBIxClass.db ])->name, '', ''],
e54ab7f3 90 schema => $schema,
91 no_deploy => 1,
92 use_find_or_create => 1
93});
94is( $schema->resultset( "Artist" )->find({ artistid => 4 })->name, "Test Name", "use_find_or_create => 1 ok" );
95$fixtures->populate({
66d02e24 96 directory => io->catfile(qw't var fixtures')->name,
97 connection_details => ['dbi:SQLite:'.io->catfile(qw[ t var DBIxClass.db ])->name, '', ''],
e54ab7f3 98 schema => $schema,
99 no_deploy => 1,
100 use_find_or_create => 1
101});
102is( $schema->resultset( "Artist" )->find({ artistid => 4 })->name, "Test Name", "idempotent use_find_or_create => 1 ok" );