changelog
[dbsrgits/DBIx-Class-Fixtures.git] / t / 15-multiple-belongs-to.t
CommitLineData
610be089 1#!perl
2
3use DBIx::Class::Fixtures;
4use Test::More tests => 7;
5use lib qw(t/lib);
6use DBICTest;
7use Path::Class;
8use Data::Dumper;
01a3246a 9use Test::TempDir::Tiny;
66d02e24 10use IO::All;
610be089 11
01a3246a 12my $tempdir = tempdir;
13
610be089 14# set up and populate schema
01a3246a 15ok(my $schema = DBICTest->init_schema(db_dir => $tempdir), 'got schema');
610be089 16
66d02e24 17my $config_dir = io->catfile(qw't var configs')->name;
610be089 18
19# do dump
20ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir');
01a3246a 21ok($fixtures->dump({ config => 'multiple-has-many.json', schema => $schema, directory => $tempdir }), 'fetch dump executed okay');
610be089 22
23# check dump is okay
01a3246a 24my $dir = dir($tempdir);
610be089 25
01a3246a 26ok( -e io->catfile($tempdir, qw'producer')->name, "We fetched some producers" );
27ok( -e io->catfile($tempdir, qw'cd_to_producer')->name, "We fetched some cd producer xrefs" );
28ok( -e io->catfile($tempdir, qw'CD')->name, "We fetched some cds" );
29ok( -e io->catfile($tempdir, qw'artist')->name, "We fetched some artists" );
610be089 30
31__END__
32while ( my ($dirname, $sourcename) = each %dirs ) {
33 my $this_dir = dir($dir, $dirname);
34}
35
36my $cd_dir = dir($dir, 'cd');
37my $track_dir = dir($dir, 'track');
38
39# check only artist1's cds that matched the rule were fetched
40my $artist1 = $schema->resultset('Artist')->find(1);
41my $artist1_cds = $artist1->cds;
42while (my $a1_cd = $artist1_cds->next) {
43 my $cd_fix_file = file($cd_dir, $a1_cd->id . '.fix');
44 if ($a1_cd->tags->search({ tag => 'Cheesy' })->count) {
45 ok(-e $cd_fix_file, 'cd matching rule fetched');
46 } else {
47 isnt(-e $cd_fix_file, 1, 'cd not matching rule not fetched');
48 }
49}
50
51# check only cds' tracks that matched the rule were fetched
52foreach my $cd_fix_file ($cd_dir->children) {
53 my $HASH1; eval($cd_fix_file->slurp());
54 is(ref $HASH1, 'HASH', 'cd fixture evals into hash');
55
56 my $cd = $schema->resultset('CD')->find($HASH1->{cdid});
57 foreach my $track ($cd->tracks->all) {
58 my $track_fix_file = file($track_dir, $track->id . '.fix');
59 if ($track->get_column('position') eq 2) {
60 is(-e $track_fix_file, 1, 'track matching rule fetched');
61 } else {
62 isnt(-e $track_fix_file, 1, 'track not matching rule not fetched');
63 }
64 }
65}
66