changelog
[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;
8use Data::Dumper;
9
10# set up and populate schema
11ok(my $schema = DBICTest->init_schema(), 'got schema');
12
13my $config_dir = 't/var/configs';
14
8ce68ce4 15{
16 # do dump
17 ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir');
18 ok($fixtures->dump({ config => 'simple.json', schema => $schema, directory => 't/var/fixtures' }), 'simple dump executed okay');
c1a04675 19
8ce68ce4 20 # check dump is okay
21 my $dir = dir('t/var/fixtures/artist');
22 ok(-e 't/var/fixtures/artist', 'artist directory created');
c1a04675 23
8ce68ce4 24 my @children = $dir->children;
25 is(scalar(@children), 1, 'right number of fixtures created');
c1a04675 26
8ce68ce4 27 my $fix_file = $children[0];
28 my $HASH1; eval($fix_file->slurp());
29 is(ref $HASH1, 'HASH', 'fixture evals into hash');
c1a04675 30
8ce68ce4 31 is_deeply([sort $schema->source('Artist')->columns], [sort keys %{$HASH1}], 'fixture has correct keys');
c1a04675 32
8ce68ce4 33 my $artist = $schema->resultset('Artist')->find($HASH1->{artistid});
34 is_deeply({$artist->get_columns}, $HASH1, 'dumped fixture is equivalent to artist row');
c1a04675 35
8ce68ce4 36 $schema->resultset('Artist')->delete; # so we can create the row again on the next line
37 ok($schema->resultset('Artist')->create($HASH1), 'new dbic row created from fixture');
38}
39
40{
41 # do dump with hashref config
42 ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir');
43 ok($fixtures->dump({
44 config => {
45 "might_have" => {
46 "fetch" => 0
47 },
48 "has_many" => {
49 "fetch" => 0
50 },
51 "sets" => [{
52 "class" => "Artist",
53 "quantity" => 1
54 }]
55 },
56 schema => $schema,
57 directory => 't/var/fixtures',
58 }), 'simple dump executed okay');
59
60 # check dump is okay
61 my $dir = dir('t/var/fixtures/artist');
62 ok(-e 't/var/fixtures/artist', 'artist directory created');
63
64 my @children = $dir->children;
65 is(scalar(@children), 1, 'right number of fixtures created');
66
67 my $fix_file = $children[0];
68 my $HASH1; eval($fix_file->slurp());
69 is(ref $HASH1, 'HASH', 'fixture evals into hash');
70
71 is_deeply([sort $schema->source('Artist')->columns], [sort keys %{$HASH1}], 'fixture has correct keys');
72
73 my $artist = $schema->resultset('Artist')->find($HASH1->{artistid});
74 is_deeply({$artist->get_columns}, $HASH1, 'dumped fixture is equivalent to artist row');
75
76 $schema->resultset('Artist')->delete; # so we can create the row again on the next line
77 ok($schema->resultset('Artist')->create($HASH1), 'new dbic row created from fixture');
78}