switch tests to Test::TempDir::Tiny to enable parallelization
[dbsrgits/DBIx-Class-Fixtures.git] / t / 02-dump-basic.t
CommitLineData
c1a04675 1#!perl
2
3use DBIx::Class::Fixtures;
8ce68ce4 4use Test::More tests => 17;
08a9f84c 5use lib qw(t/lib);
c1a04675 6use DBICTest;
7use Path::Class;
a1f70f6f 8use Data::Dumper;
01a3246a 9use Test::TempDir::Tiny;
66d02e24 10use IO::All;
c1a04675 11
01a3246a 12my $tempdir = tempdir;
13
a1f70f6f 14use if $^O eq 'MSWin32','Devel::Confess';
c1a04675 15# set up and populate schema
01a3246a 16ok(my $schema = DBICTest->init_schema(db_dir => $tempdir), 'got schema');
c1a04675 17
66d02e24 18my $config_dir = io->catfile(qw't var configs')->name;
c1a04675 19
8ce68ce4 20{
21 # do dump
22 ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir');
01a3246a 23 ok($fixtures->dump({ config => 'simple.json', schema => $schema, directory => $tempdir }), 'simple dump executed okay');
c1a04675 24
8ce68ce4 25 # check dump is okay
01a3246a 26 my $dir = dir(io->catfile($tempdir, qw'artist')->name);
27 ok(-e io->catfile($tempdir, qw'artist')->name, 'artist directory created');
c1a04675 28
8ce68ce4 29 my @children = $dir->children;
30 is(scalar(@children), 1, 'right number of fixtures created');
c1a04675 31
8ce68ce4 32 my $fix_file = $children[0];
33 my $HASH1; eval($fix_file->slurp());
34 is(ref $HASH1, 'HASH', 'fixture evals into hash');
c1a04675 35
8ce68ce4 36 is_deeply([sort $schema->source('Artist')->columns], [sort keys %{$HASH1}], 'fixture has correct keys');
c1a04675 37
8ce68ce4 38 my $artist = $schema->resultset('Artist')->find($HASH1->{artistid});
39 is_deeply({$artist->get_columns}, $HASH1, 'dumped fixture is equivalent to artist row');
c1a04675 40
8ce68ce4 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 },
01a3246a 61 schema => $schema,
62 directory => $tempdir,
8ce68ce4 63 }), 'simple dump executed okay');
64
65 # check dump is okay
01a3246a 66 my $dir = dir(io->catfile($tempdir, qw'artist')->name);
67 ok(-e io->catfile($tempdir, qw'artist')->name, 'artist directory created');
8ce68ce4 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}