22
33from email .header import Header
44from email .message import Message
5- from mailbox import Maildir
5+ from io import StringIO
66
77import pytest
88
1616command = Command ()
1717
1818
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
2521
2622 # Valid
2723 valid = 'Receipt [$25.00] By: John Doe [john.doe@archlinux.org]'
@@ -42,41 +38,39 @@ def test_decode_subject():
4238 assert command .decode_subject (subject ) == text
4339
4440
45- def test_invalid_args ():
41+ def test_invalid_args (monkeypatch ):
42+ monkeypatch .setattr ('sys.stdin' , StringIO ('' ))
4643 with pytest .raises (CommandError ) as e :
4744 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 )
4946
5047
5148def test_invalid_path ():
5249 with pytest .raises (CommandError ) as e :
5350 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 )
5652
57- def test_maildir (db , tmp_path ):
58- tmpdir = tmp_path / 'archweb'
59- tmpdir .mkdir ()
60- mdir = tmpdir / 'maildir'
6153
62- maildir = Maildir ( mdir )
54+ def test_maildir ( db , monkeypatch ):
6355 msg = Message ()
6456 msg ['subject' ] = 'John Doe'
6557 msg ['to' ] = 'John Doe <john@doe.com>'
66- maildir .add (msg )
6758
6859 # 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' )
7063 assert len (Donor .objects .all ()) == 0
7164
72- # Valid
65+ # # Valid
7366 msg = Message ()
7467 msg ['subject' ] = 'Receipt [$25.00] By: David Doe [david@doe.com]'
7568 msg ['to' ] = 'John Doe <david@doe.com>'
76- maildir . add ( msg )
77- call_command ('donor_import' , mdir )
69+ monkeypatch . setattr ( 'sys.stdin' , StringIO ( msg . as_string ()) )
70+ call_command ('donor_import' )
7871 assert len (Donor .objects .all ()) == 1
7972
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' )
8276 assert len (Donor .objects .all ()) == 1
0 commit comments