2
2
3
3
from email .header import Header
4
4
from email .message import Message
5
- from mailbox import Maildir
5
+ from io import StringIO
6
6
7
7
import pytest
8
8
16
16
command = Command ()
17
17
18
18
19
- def gen_parse_subject (data ):
20
- return command .parse_subject (data )
21
-
22
-
23
- def test_parse_subject (self ):
24
- assert self .command .parse_subject ('garbage' ) is None
19
+ def test_parse_subject ():
20
+ assert command .parse_subject ('garbage' ) is None
25
21
26
22
# Valid
27
23
valid = 'Receipt [$25.00] By: John Doe [[email protected] ]'
@@ -42,41 +38,39 @@ def test_decode_subject():
42
38
assert command .decode_subject (subject ) == text
43
39
44
40
45
- def test_invalid_args ():
41
+ def test_invalid_args (monkeypatch ):
42
+ monkeypatch .setattr ('sys.stdin' , StringIO ('' ))
46
43
with pytest .raises (CommandError ) as e :
47
44
call_command ('donor_import' )
48
- assert 'Error: the following arguments are required ' in str (e .value )
45
+ assert 'Failed to read from STDIN ' in str (e .value )
49
46
50
47
51
48
def test_invalid_path ():
52
49
with pytest .raises (CommandError ) as e :
53
50
call_command ('donor_import' , '/tmp/non-existant' )
54
- assert 'Failed to open maildir' in str (e .value )
55
-
51
+ assert 'argument input: can\' t open' in str (e .value )
56
52
57
- def test_maildir (db , tmp_path ):
58
- tmpdir = tmp_path / 'archweb'
59
- tmpdir .mkdir ()
60
- mdir = tmpdir / 'maildir'
61
53
62
- maildir = Maildir ( mdir )
54
+ def test_maildir ( db , monkeypatch ):
63
55
msg = Message ()
64
56
msg ['subject' ] = 'John Doe'
65
57
msg [
'to' ]
= 'John Doe <[email protected] >'
66
- maildir .add (msg )
67
58
68
59
# Invalid
69
- call_command ('donor_import' , mdir )
60
+ monkeypatch .setattr ('sys.stdin' , StringIO (msg .as_string ()))
61
+ with pytest .raises (SystemExit ):
62
+ call_command ('donor_import' )
70
63
assert len (Donor .objects .all ()) == 0
71
64
72
- # Valid
65
+ # # Valid
73
66
msg = Message ()
74
67
msg [
'subject' ]
= 'Receipt [$25.00] By: David Doe [[email protected] ]'
75
68
msg [
'to' ]
= 'John Doe <[email protected] >'
76
- maildir . add ( msg )
77
- call_command ('donor_import' , mdir )
69
+ monkeypatch . setattr ( 'sys.stdin' , StringIO ( msg . as_string ()) )
70
+ call_command ('donor_import' )
78
71
assert len (Donor .objects .all ()) == 1
79
72
80
- # Re-running should result in no new donor
81
- call_command ('donor_import' , mdir )
73
+ # # Re-running should result in no new donor
74
+ monkeypatch .setattr ('sys.stdin' , StringIO (msg .as_string ()))
75
+ call_command ('donor_import' )
82
76
assert len (Donor .objects .all ()) == 1
0 commit comments