aboutsummaryrefslogtreecommitdiff
path: root/t/tests/sietima/mailstore.t
blob: 7a2aa084c9024d0ed117494b1952af1c5a27ed67 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!perl 
use lib 't/lib';
use Test::Sietima;
use Email::Stuffer;
use Path::Tiny;
 
package TestClassWithMS {
    use Moo;
    use Sietima::Policy;
    with 'Sietima::Role::WithMailStore';
};
 
subtest 'Role::WithMailStore' => sub {
    subtest 'plain instance' => sub {
        require Test::Sietima::MailStore;
        ok(
            lives {
                TestClassWithMS->new({
                    mail_store => Test::Sietima::MailStore->new,
                })
            },
            'passing a test instance should work',
        );
    };
    subtest 'type coercion' => sub {
        my $tc;
        my $root = Path::Tiny->tempdir;
        ok(
            lives {
                $tc = TestClassWithMS->new({
                    mail_store => {
                        class => 'Sietima::MailStore::FS',
                        root => $root,
                    },
                })
            },
            'passing a hashref should work (and load the class)',
        );
        is(
            $tc->mail_store,
            object {
                prop blessed => 'Sietima::MailStore::FS';
                call root => $root;
            },
            'the mailstore should be built correctly',
        );
    };
};
 
sub mkmail($id) {
    Email::Stuffer
          ->from("from-${id}\@example.com")
          ->to("to-${id}\@example.com")
          ->subject("subject $id")
          ->text_body("body $id \nbody body\n")
          ->email;
}
 
sub chkmail($id) {
    object {
        call [header=>'from'] => "from-${id}\@example.com";
        call [header=>'to'] => "to-${id}\@example.com";
        call [header=>'subject'] => "subject $id";
        call body => match qr{\bbody \Q$id\E\b};
    };
}
 
sub chk_multimail(@ids) {
    return bag {
        for my $id (@ids) {
            item hash {
                field id => D();
                field mail => chkmail($id);
                end;
            };
        }
        end;
    };
}
 
sub test_store($store) {
    my %stored_id;
 
    subtest 'storing' => sub {
        ok($stored_id{1}=$store->store(mkmail(1),'tag1','tag2'));
        ok($stored_id{2}=$store->store(mkmail(2),'tag2'));
        ok($stored_id{3}=$store->store(mkmail(3),'tag1'));
    };
 
    subtest 'retrieving by id' => sub {
        is(
            $store->retrieve_by_id($stored_id{$_}),
            chkmail($_),
        for 1..3;
    };
 
    subtest 'retrieving by tag' => sub {
        my $tag1 = $store->retrieve_by_tags('tag1');
        is(
            $tag1,
            chk_multimail(1,3),
            'tag1 should have mails 1 & 3',
        );
 
        my $tag2 = $store->retrieve_by_tags('tag2');
        is(
            $tag2,
            chk_multimail(1,2),
            'tag1 should have mails 1 & 2',
        );
 
        my $tag12 = $store->retrieve_by_tags('tag2','tag1');
        is(
            $tag12,
            chk_multimail(1),
            'tag1+tag2 should have mail 1',
        );
 
        my $tag_all = $store->retrieve_by_tags();
        is(
            $tag_all,
            chk_multimail(1,2,3),
            'no tags should retrieve all mails',
        );
    };
 
    subtest 'retrieving ids by tag' => sub {
        my $tag1 = $store->retrieve_ids_by_tags('tag1');
        is(
            $tag1,
            bag { item $stored_id{1}; item $stored_id{3}; end },
            'tag1 should have ids 1 & 3',
        );
 
        my $tag2 = $store->retrieve_ids_by_tags('tag2');
        is(
            $tag2,
            bag { item $stored_id{1}; item $stored_id{2}; end },
            'tag1 should have ids 1 & 2',
        );
 
        my $tag12 = $store->retrieve_ids_by_tags('tag2','tag1');
        is(
            $tag12,
            bag { item $stored_id{1}; end },
            'tag1+tag2 should have id 1',
        );
 
        my $tag_all = $store->retrieve_ids_by_tags();
        is(
            $tag_all,
            bag { item $stored_id{1}; item $stored_id{2}; item $stored_id{3}; end },
            'no tags should retrieve all ids',
        );
    };
 
    subtest 'removing' => sub {
        $store->remove($stored_id{2});
        is(
            $store->retrieve_by_tags('tag2'),
            chk_multimail(1),
            'remove should remove',
        );
    };
 
    subtest 'clearing' => sub {
        $store->clear;
        is(
            $store->retrieve_by_tags(),
            [],
            'clear should clear',
        );
    };
}
 
subtest 'test store' => sub {
    test_store(Test::Sietima::MailStore->new);
};
 
subtest 'file store' => sub {
    my $root = Path::Tiny->tempdir;
 
    test_store(Sietima::MailStore::FS->new({root => $root}));
};
 
done_testing;