summaryrefslogtreecommitdiff
path: root/t/tests/manager.t
blob: 1ee509f9433a756d215ccf769e1d4a1473e97c10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!perl 
use 5.024;
use strict;
use warnings;
use Test2::Bundle::Extended;
use experimental 'signatures';
use Path::Tiny;
use AniDB::Manager;
use Data::Dump 'pp';
 
my $tempdir = Path::Tiny->tempdir;
 
package AniDB::APIClient {
    use Moo;
    use experimental 'signatures';
 
    has [qw(username password)] => (is=>'ro',required=>1);
 
    sub episode_info_for($self,$path_info) {
        return {
            aid => 12,
            eid => 73,
            title => 'random episode',
        };
    }
 
    sub anime_info_for($self,$episode_info) {
        return {
            aid => 12,
            title => 'random series',
        };
    }
};
$INC{'AniDB/APIClient.pm'}=__FILE__;
 
package Testing::Renamer {
    use Moo;
    use experimental 'signatures';
 
    sub new_name_for($self,$path,$info) {
        return $tempdir->child(
            $info->{anime}{title},
            $info->{episode}{title},
        );
    }
};
 
my $next_hash = 'abcd';
sub fake_hash($fh) {
    return $next_hash;
}
 
my $db_file = $tempdir->child('test.db');
 
my $m;
subtest 'construction' => sub {
    try_ok {
        $m = AniDB::Manager->new({
            database => $db_file,
            username => 'gino',
            password => 'pino',
            renamer => Testing::Renamer->new(),
            hash_function => \&fake_hash,
        });
    'builds';
 
    isa_ok(
        $m->datastore,
        ['AniDB::Datastore'],
        'datastore builds',
    );
 
    is(
        $m->api_client,
        object {
            prop blessed => 'AniDB::APIClient';
            call username => 'gino';
            call password => 'pino';
        },
        'api client builds',
    );
};
 
my $test_file = $tempdir->child('some.video');
$test_file->spew_raw('data');
 
subtest 'update' => sub {
    try_ok {
        $m->update($test_file);
    'lives';
 
    is(
        $m->datastore->full_info_for($test_file),
        {
            path => hash {
                field name => $test_file->stringify;
                field hash => $next_hash;
                etc;
            },
            episode => {
                aid => 12,
                eid => 73,
                title => 'random episode',
            },
            anime => {
                aid => 12,
                title => 'random series',
            },
        },
        'updates',
    );
 
    $next_hash = 'different';
    try_ok {
        $m->update($test_file);
    'lives if not changed';
 
    is(
        $m->datastore->path_info_for($test_file),
        hash {
            field hash => 'abcd';
            etc;
        },
        'does not update if not changed',
    );
 
    $test_file->append_raw('more');
 
    try_ok {
        $m->update($test_file);
    'lives if changed';
 
    is(
        $m->datastore->path_info_for($test_file),
        hash {
            field hash => $next_hash;
            etc;
        },
        'updates if changed',
    );
};
 
subtest 'renaming' => sub {
    is(
        my $new_name = $m->new_name_for($test_file),
        $tempdir->child(
            'random series',
            'random episode',
        )->stringify,
        'new name',
    );
 
    try_ok {
        $m->rename($test_file);
    'renaming lives';
 
    ok(not($test_file->exists),'old file not there');
    ok(path($new_name)->exists,'new file is there');
 
    is(
        $m->datastore->path_info_for(path($new_name)),
        hash {
            field hash => $next_hash;
            etc;
        },
        'updates if renamed',
    );
 
    ok(not($m->maybe_new_name_for($new_name)),'double renaming is no-op');
};
 
done_testing;