remove trailing comma
[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;
01a3246a 9use Test::TempDir::Tiny;
66d02e24 10use IO::All;
5eab44a9 11
01a3246a 12my $tempdir = tempdir;
13
5eab44a9 14# set up and populate schema
01a3246a 15ok(my $schema = DBICTest->init_schema(db_dir => $tempdir), 'got schema');
5eab44a9 16
66d02e24 17my $config_dir = io->catfile(qw't var configs')->name;
5eab44a9 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 => 'rules.json', schema => $schema, directory => $tempdir }), 'fetch dump executed okay');
5eab44a9 22
23# check dump is okay
01a3246a 24my $dir = dir($tempdir);
dcdf675f 25my $cd_dir = dir($dir, 'CD');
5eab44a9 26my $track_dir = dir($dir, 'track');
27
28# check only artist1's cds that matched the rule were fetched
29my $artist1 = $schema->resultset('Artist')->find(1);
30my $artist1_cds = $artist1->cds;
31while (my $a1_cd = $artist1_cds->next) {
32 my $cd_fix_file = file($cd_dir, $a1_cd->id . '.fix');
33 if ($a1_cd->tags->search({ tag => 'Cheesy' })->count) {
34 ok(-e $cd_fix_file, 'cd matching rule fetched');
35 } else {
36 isnt(-e $cd_fix_file, 1, 'cd not matching rule not fetched');
37 }
38}
39
40# check only cds' tracks that matched the rule were fetched
41foreach my $cd_fix_file ($cd_dir->children) {
42 my $HASH1; eval($cd_fix_file->slurp());
43 is(ref $HASH1, 'HASH', 'cd fixture evals into hash');
44
45 my $cd = $schema->resultset('CD')->find($HASH1->{cdid});
46 foreach my $track ($cd->tracks->all) {
47 my $track_fix_file = file($track_dir, $track->id . '.fix');
48 if ($track->get_column('position') eq 2) {
49 is(-e $track_fix_file, 1, 'track matching rule fetched');
50 } else {
51 isnt(-e $track_fix_file, 1, 'track not matching rule not fetched');
52 }
53 }
54}
570384ac 55
56ok $fixtures->available_config_sets, 'Found sets';