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