releasing as stable
[dbsrgits/DBIx-Class-Fixtures.git] / t / 05-dump-rules.t
CommitLineData
5eab44a9 1#!perl
2
3use DBIx::Class::Fixtures;
570384ac 4use Test::More tests => 15;
5eab44a9 5use lib qw(t/lib);
6use DBICTest;
7use Path::Class;
8use Data::Dumper;
66d02e24 9use IO::All;
5eab44a9 10
11# set up and populate schema
12ok(my $schema = DBICTest->init_schema(), 'got schema');
13
66d02e24 14my $config_dir = io->catfile(qw't var configs')->name;
5eab44a9 15
16# do dump
17ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir');
66d02e24 18ok($fixtures->dump({ config => 'rules.json', schema => $schema, directory => io->catfile(qw't var fixtures')->name }), 'fetch dump executed okay');
5eab44a9 19
20# check dump is okay
66d02e24 21my $dir = dir(io->catfile(qw't var fixtures')->name);
dcdf675f 22my $cd_dir = dir($dir, 'CD');
5eab44a9 23my $track_dir = dir($dir, 'track');
24
25# check only artist1's cds that matched the rule were fetched
26my $artist1 = $schema->resultset('Artist')->find(1);
27my $artist1_cds = $artist1->cds;
28while (my $a1_cd = $artist1_cds->next) {
29 my $cd_fix_file = file($cd_dir, $a1_cd->id . '.fix');
30 if ($a1_cd->tags->search({ tag => 'Cheesy' })->count) {
31 ok(-e $cd_fix_file, 'cd matching rule fetched');
32 } else {
33 isnt(-e $cd_fix_file, 1, 'cd not matching rule not fetched');
34 }
35}
36
37# check only cds' tracks that matched the rule were fetched
38foreach my $cd_fix_file ($cd_dir->children) {
39 my $HASH1; eval($cd_fix_file->slurp());
40 is(ref $HASH1, 'HASH', 'cd fixture evals into hash');
41
42 my $cd = $schema->resultset('CD')->find($HASH1->{cdid});
43 foreach my $track ($cd->tracks->all) {
44 my $track_fix_file = file($track_dir, $track->id . '.fix');
45 if ($track->get_column('position') eq 2) {
46 is(-e $track_fix_file, 1, 'track matching rule fetched');
47 } else {
48 isnt(-e $track_fix_file, 1, 'track not matching rule not fetched');
49 }
50 }
51}
570384ac 52
53ok $fixtures->available_config_sets, 'Found sets';