switch tests to Test::TempDir::Tiny to enable parallelization
[dbsrgits/DBIx-Class-Fixtures.git] / t / 02-dump-basic.t
1 #!perl
2
3 use DBIx::Class::Fixtures;
4 use Test::More tests => 17;
5 use lib qw(t/lib);
6 use DBICTest;
7 use Path::Class;
8 use Data::Dumper;
9 use Test::TempDir::Tiny;
10 use IO::All;
11
12 my $tempdir = tempdir;
13
14 use if $^O eq 'MSWin32','Devel::Confess';
15 # set up and populate schema
16 ok(my $schema = DBICTest->init_schema(db_dir => $tempdir), 'got schema');
17
18 my $config_dir = io->catfile(qw't var configs')->name;
19
20 {
21     # do dump
22     ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir');
23     ok($fixtures->dump({ config => 'simple.json', schema => $schema, directory => $tempdir }), 'simple dump executed okay');
24
25     # check dump is okay
26     my $dir = dir(io->catfile($tempdir, qw'artist')->name);
27     ok(-e io->catfile($tempdir, qw'artist')->name, 'artist directory created');
28
29     my @children = $dir->children;
30     is(scalar(@children), 1, 'right number of fixtures created');
31
32     my $fix_file = $children[0];
33     my $HASH1; eval($fix_file->slurp());
34     is(ref $HASH1, 'HASH', 'fixture evals into hash');
35
36     is_deeply([sort $schema->source('Artist')->columns], [sort keys %{$HASH1}], 'fixture has correct keys');
37
38     my $artist = $schema->resultset('Artist')->find($HASH1->{artistid});
39     is_deeply({$artist->get_columns}, $HASH1, 'dumped fixture is equivalent to artist row');
40
41     $schema->resultset('Artist')->delete; # so we can create the row again on the next line
42     ok($schema->resultset('Artist')->create($HASH1), 'new dbic row created from fixture');
43 }
44
45 {
46     # do dump with hashref config
47     ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir');
48     ok($fixtures->dump({
49         config => {
50             "might_have" => {
51                 "fetch" => 0
52             },
53             "has_many" => {
54                 "fetch" => 0
55             },
56             "sets" => [{
57                 "class" => "Artist",
58                 "quantity" => 1
59             }]
60         },
61         schema => $schema,
62         directory => $tempdir,
63     }), 'simple dump executed okay');
64
65     # check dump is okay
66     my $dir = dir(io->catfile($tempdir, qw'artist')->name);
67     ok(-e io->catfile($tempdir, qw'artist')->name, 'artist directory created');
68
69     my @children = $dir->children;
70     is(scalar(@children), 1, 'right number of fixtures created');
71
72     my $fix_file = $children[0];
73     my $HASH1; eval($fix_file->slurp());
74     is(ref $HASH1, 'HASH', 'fixture evals into hash');
75
76     is_deeply([sort $schema->source('Artist')->columns], [sort keys %{$HASH1}], 'fixture has correct keys');
77
78     my $artist = $schema->resultset('Artist')->find($HASH1->{artistid});
79     is_deeply({$artist->get_columns}, $HASH1, 'dumped fixture is equivalent to artist row');
80
81     $schema->resultset('Artist')->delete; # so we can create the row again on the next line
82     ok($schema->resultset('Artist')->create($HASH1), 'new dbic row created from fixture');
83 }