Skip to content

Commit 40afe67

Browse files
committed
main: convert tests to pytest
1 parent 1082125 commit 40afe67

File tree

2 files changed

+76
-86
lines changed

2 files changed

+76
-86
lines changed

main/tests/test_donor_import.py

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,82 @@
11
# -*- coding: utf-8 -*-
22

3-
import sys
43
from email.header import Header
54
from email.message import Message
6-
from io import StringIO
7-
from tempfile import mkdtemp
5+
from mailbox import Maildir
6+
7+
import pytest
88

9-
from django.test import TransactionTestCase
109
from django.core.management import call_command
1110
from django.core.management.base import CommandError
1211

1312
from main.models import Donor
1413
from main.management.commands.donor_import import Command
1514

1615

17-
class DonorImportTest(TransactionTestCase):
18-
19-
def setUp(self):
20-
self.command = Command()
21-
22-
def test_parse_subject(self):
23-
self.assertIsNone(self.command.parse_subject('garbage'))
24-
25-
# Valid
26-
valid = u'Receipt [$25.00] By: John Doe [[email protected]]'
27-
output = self.command.parse_subject(valid)
28-
self.assertEqual(output, u'John Doe')
29-
30-
def test_parse_name(self):
31-
self.assertEqual(self.command.sanitize_name(u'1244'), u'')
32-
self.assertEqual(self.command.sanitize_name(u'John Doe'), u'John Doe')
33-
self.assertEqual(self.command.sanitize_name(u' John Doe '), u'John Doe')
34-
self.assertEqual(self.command.sanitize_name(u'John Doe 23'), u'John Doe')
35-
36-
def test_decode_subject(self):
37-
text = u'メイル'
38-
subject = Header(text, 'utf-8')
39-
self.assertEqual(self.command.decode_subject(subject), text)
40-
41-
def test_invalid_args(self):
42-
stdin = sys.stdin
43-
with self.assertRaises(CommandError) as e:
44-
sys.stdin = StringIO('')
45-
call_command('donor_import')
46-
self.assertIn('Failed to read from STDIN', str(e.exception))
47-
sys.stdin = stdin
48-
49-
def test_invalid_path(self):
50-
with self.assertRaises(CommandError) as e:
51-
call_command('donor_import', '/tmp/non-existant')
52-
self.assertIn('argument input: can\'t open', str(e.exception))
53-
54-
def test_import(self):
55-
tmpmail = mkdtemp('archweb') + "/mail"
56-
57-
msg = Message()
58-
msg['subject'] = 'John Doe'
59-
msg['to'] = 'John Doe <[email protected]>'
60-
with open(tmpmail, 'wb') as fp:
61-
fp.write(msg.as_bytes())
62-
63-
# Invalid
64-
with self.assertRaises(SystemExit):
65-
call_command('donor_import', tmpmail)
66-
self.assertEqual(len(Donor.objects.all()), 0)
67-
68-
# Valid
69-
msg = Message()
70-
msg['subject'] = 'Receipt [$25.00] By: David Doe [[email protected]]'
71-
msg['to'] = 'John Doe <[email protected]>'
72-
with open(tmpmail, 'wb') as fp:
73-
fp.write(msg.as_bytes())
74-
75-
call_command('donor_import', tmpmail)
76-
self.assertEqual(len(Donor.objects.all()), 1)
77-
78-
# Re-running should result in no new donor
79-
call_command('donor_import', tmpmail)
80-
self.assertEqual(len(Donor.objects.all()), 1)
16+
command = Command()
17+
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
25+
26+
# Valid
27+
valid = 'Receipt [$25.00] By: John Doe [[email protected]]'
28+
output = command.parse_subject(valid)
29+
assert output == 'John Doe'
30+
31+
32+
def test_parse_name():
33+
assert command.sanitize_name('1244') == ''
34+
assert command.sanitize_name('John Doe') == 'John Doe'
35+
assert command.sanitize_name(' John Doe ') == 'John Doe'
36+
assert command.sanitize_name('John Doe 23') == 'John Doe'
37+
38+
39+
def test_decode_subject():
40+
text = u'メイル'
41+
subject = Header(text, 'utf-8')
42+
assert command.decode_subject(subject) == text
43+
44+
45+
def test_invalid_args():
46+
with pytest.raises(CommandError) as e:
47+
call_command('donor_import')
48+
assert 'Error: the following arguments are required' in str(e.value)
49+
50+
51+
def test_invalid_path():
52+
with pytest.raises(CommandError) as e:
53+
call_command('donor_import', '/tmp/non-existant')
54+
assert 'Failed to open maildir' in str(e.value)
55+
56+
57+
def test_maildir(db, tmp_path):
58+
tmpdir = tmp_path / 'archweb'
59+
tmpdir.mkdir()
60+
mdir = tmpdir / 'maildir'
61+
62+
maildir = Maildir(mdir)
63+
msg = Message()
64+
msg['subject'] = 'John Doe'
65+
msg['to'] = 'John Doe <[email protected]>'
66+
maildir.add(msg)
67+
68+
# Invalid
69+
call_command('donor_import', mdir)
70+
assert len(Donor.objects.all()) == 0
71+
72+
# Valid
73+
msg = Message()
74+
msg['subject'] = 'Receipt [$25.00] By: David Doe [[email protected]]'
75+
msg['to'] = 'John Doe <[email protected]>'
76+
maildir.add(msg)
77+
call_command('donor_import', mdir)
78+
assert len(Donor.objects.all()) == 1
79+
80+
# Re-running should result in no new donor
81+
call_command('donor_import', mdir)
82+
assert len(Donor.objects.all()) == 1
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
from django.test import TestCase
2-
3-
41
from main.templatetags.flags import country_flag
5-
from mirrors.models import CheckLocation
6-
7-
8-
class FlagsTemplateTest(TestCase):
92

10-
def setUp(self):
11-
self.checkloc = CheckLocation.objects.create(hostname='arch.org',
12-
source_ip='127.0.0.1',
13-
country='US')
3+
from mirrors.tests.conftest import checklocation
144

15-
def tearDown(self):
16-
self.checkloc.delete()
175

18-
def test_country_flag(self):
19-
flag = country_flag(self.checkloc.country)
20-
self.assertIn(self.checkloc.country.name, flag)
21-
self.assertIn(self.checkloc.country.code.lower(), flag)
22-
self.assertEqual(country_flag(None), '')
6+
def test_country_flag(checklocation):
7+
flag = country_flag(checklocation.country)
8+
assert checklocation.country.name in flag
9+
assert checklocation.country.code.lower() in flag
10+
assert country_flag(None) == ''

0 commit comments

Comments
 (0)