Skip to content

Commit 27d8319

Browse files
updated unittests to python3 and switched to pytest
1 parent 34c8ef6 commit 27d8319

5 files changed

Lines changed: 73 additions & 134 deletions

File tree

tests/test_comparators.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
# type: ignore
22

3-
import unittest
3+
import pytest
44

55
import sifter.comparator
66
from sifter.grammar.comparator import Comparator
77

88

9-
class MockComparator(Comparator):
9+
def test_mock_comparator() -> None:
10+
class MockComparator(Comparator):
11+
COMPARATOR_ID = 'i;vnd-mock'
1012

11-
COMPARATOR_ID = 'i;vnd-mock'
12-
13-
14-
class TestMatchTypes(unittest.TestCase):
15-
16-
def setUp(self) -> None:
17-
MockComparator.register()
18-
19-
def test_unimplemented_match_type(self) -> None:
20-
self.assertRaises(
21-
RuntimeError,
22-
sifter.comparator.get_match_fn,
23-
'i;vnd-mock', 'IS',
24-
)
25-
26-
27-
if __name__ == '__main__':
28-
unittest.main()
13+
MockComparator.register()
14+
with pytest.raises(RuntimeError):
15+
sifter.comparator.get_match_fn('i;vnd-mock', 'IS')

tests/test_evaluation.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
import email
44
import os.path
5-
import unittest
6-
import codecs
75

86
import sifter.parser
97

108

11-
class TestEvaluateRules(unittest.TestCase):
9+
def eval_rules(filename_message, filename_rules):
10+
with open(os.path.join(os.path.dirname(__file__), filename_message), encoding='utf-8') as msg_fh:
11+
message = email.message_from_file(msg_fh)
12+
with open(os.path.join(os.path.dirname(__file__), filename_rules), encoding='utf-8') as rule_fh:
13+
rules = sifter.parser.parse_file(rule_fh)
14+
return rules.evaluate(message)
15+
16+
17+
def test_evaulation():
1218

1319
EVAL_RESULTS = (
1420
("evaluation_1.msg", "evaluation_1.rules", [('redirect', 'acm@example.com')]),
@@ -20,24 +26,5 @@ class TestEvaluateRules(unittest.TestCase):
2026
("evaluation_3.msg", "evaluation_3.rules", [('keep', None)]),
2127
)
2228

23-
def setUp(self) -> None:
24-
self.messages = {}
25-
self.rules = {}
26-
for result in self.EVAL_RESULTS:
27-
with codecs.open(os.path.join(os.path.dirname(__file__), result[0]),
28-
encoding='utf-8') as msg_fh:
29-
self.messages.setdefault(result[0], email.message_from_file(msg_fh))
30-
with codecs.open(os.path.join(os.path.dirname(__file__), result[1]),
31-
encoding='utf-8') as rule_fh:
32-
self.rules.setdefault(result[1], sifter.parser.parse_file(rule_fh))
33-
34-
def test_msg_rule_cross_product(self) -> None:
35-
for result in self.EVAL_RESULTS:
36-
self.assertEqual(
37-
self.rules[result[1]].evaluate(self.messages[result[0]]),
38-
result[2]
39-
)
40-
41-
42-
if __name__ == '__main__':
43-
unittest.main()
29+
for messagefile, rulefile, evaluated_rules in EVAL_RESULTS:
30+
assert evaluated_rules == eval_rules(messagefile, rulefile)

tests/test_grammar.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
# type: ignore
22

3-
import unittest
3+
import pytest
44

55
import sifter.extension
66
from sifter.grammar.state import EvaluationState
77

88

9-
class TestEvaluationState(unittest.TestCase):
9+
def test_grammar():
10+
sifter.extension.register('ext1')
11+
sifter.extension.register('ext2')
12+
state = EvaluationState()
1013

11-
def setUp(self) -> None:
12-
sifter.extension.register('ext1')
13-
sifter.extension.register('ext2')
14-
self.state = EvaluationState()
14+
state.require_extension('ext1')
1515

16-
def test_require_extension(self) -> None:
17-
self.state.require_extension('ext1')
18-
self.assertTrue(self.state.check_required_extension('ext1', 'ext1'))
19-
self.assertRaises(
20-
RuntimeError,
21-
self.state.check_required_extension,
22-
'ext2',
23-
'ext2',
24-
)
16+
assert state.check_required_extension('ext1', 'ext1') is True
2517

26-
27-
if __name__ == '__main__':
28-
unittest.main()
18+
with pytest.raises(RuntimeError):
19+
state.check_required_extension('ext2', 'ext2')

tests/test_parser.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
# type: ignore
22

33
import os.path
4-
import unittest
5-
import codecs
64

75
import sifter.parser
86

97

10-
class TestParseFile(unittest.TestCase):
8+
def test_parser():
9+
in_filename = "parser_1.in"
10+
golden_filename = "parser_1.out"
1111

12-
def test_files(self) -> None:
13-
for in_filename, golden_filename in (("parser_1.in", "parser_1.out"),):
14-
with codecs.open(os.path.join(os.path.dirname(__file__), in_filename), encoding='utf-8') as in_fh:
15-
test_output = str(sifter.parser.parse_file(in_fh))
16-
with codecs.open(os.path.join(os.path.dirname(__file__), golden_filename),
17-
encoding='utf-8') as golden_fh:
18-
golden_output = golden_fh.read()
19-
self.assertEqual(test_output, golden_output)
12+
with open(os.path.join(os.path.dirname(__file__), in_filename), 'r', encoding='utf-8', newline='') as in_fh:
13+
test_output = str(sifter.parser.parse_file(in_fh))
14+
with open(os.path.join(os.path.dirname(__file__), golden_filename), 'r', encoding='utf-8', newline='') as golden_fh:
15+
golden_output = golden_fh.read()
2016

21-
22-
if __name__ == '__main__':
23-
unittest.main()
17+
assert test_output == golden_output

tests/test_validators.py

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# type: ignore
22

3-
import unittest
3+
4+
import pytest
45

56
from sifter.grammar.tag import Tag as GrammarTag
67
from sifter.grammar.rule import Rule, RuleSyntaxError
@@ -18,60 +19,39 @@ def __init__(self, arguments=None, tests=None):
1819
super(MockRule, self).__init__(arguments, tests)
1920

2021

21-
class TestValidationFn(unittest.TestCase):
22-
23-
def test_too_many_args(self) -> None:
24-
mock_rule = MockRule([GrammarTag('IS'), 13, ])
25-
self.assertRaises(
26-
RuleSyntaxError,
27-
mock_rule.validate_arguments,
28-
)
29-
30-
def test_not_enough_args(self) -> None:
31-
mock_rule = MockRule([13, ])
32-
self.assertRaises(
33-
RuleSyntaxError,
34-
mock_rule.validate_arguments,
35-
[
36-
Number(),
37-
StringList(),
38-
],
39-
)
40-
41-
42-
class TestTagValidator(unittest.TestCase):
43-
44-
def test_allowed_tag(self) -> None:
45-
mock_validator = TagValidator(['MOCK', 'IS', ])
46-
self.assertEqual(
47-
mock_validator.validate([GrammarTag('IS')], 0),
48-
1
49-
)
50-
51-
def test_allowed_single_tag(self) -> None:
52-
# test the case for a non-list single tag name
53-
mock_validator = TagValidator('IS')
54-
self.assertEqual(
55-
mock_validator.validate([GrammarTag('IS')], 0),
56-
1
57-
)
58-
59-
def test_not_allowed_tag(self) -> None:
60-
mock_validator = TagValidator(['MOCK', 'FOO', ])
61-
self.assertEqual(
62-
mock_validator.validate([GrammarTag('IS')], 0),
63-
0
64-
)
65-
66-
def test_not_allowed_single_tag(self) -> None:
67-
# test the case for a non-list single tag name. test when the tag is a
68-
# substring of the allowed tag.
69-
mock_validator = TagValidator('ISFOO')
70-
self.assertEqual(
71-
mock_validator.validate([GrammarTag('IS')], 0),
72-
0
73-
)
74-
75-
76-
if __name__ == '__main__':
77-
unittest.main()
22+
def test_too_many_args() -> None:
23+
mock_rule = MockRule([GrammarTag('IS'), 13, ])
24+
with pytest.raises(RuleSyntaxError):
25+
mock_rule.validate_arguments()
26+
27+
28+
def test_not_enough_args() -> None:
29+
mock_rule = MockRule([13, ])
30+
with pytest.raises(RuleSyntaxError):
31+
mock_rule.validate_arguments([
32+
Number(),
33+
StringList(),
34+
])
35+
36+
37+
def test_allowed_tag() -> None:
38+
mock_validator = TagValidator(['MOCK', 'IS', ])
39+
assert mock_validator.validate([GrammarTag('IS')], 0) == 1
40+
41+
42+
def test_allowed_single_tag() -> None:
43+
# test the case for a non-list single tag name
44+
mock_validator = TagValidator('IS')
45+
assert mock_validator.validate([GrammarTag('IS')], 0) == 1
46+
47+
48+
def test_not_allowed_tag() -> None:
49+
mock_validator = TagValidator(['MOCK', 'FOO', ])
50+
assert mock_validator.validate([GrammarTag('IS')], 0) == 0
51+
52+
53+
def test_not_allowed_single_tag() -> None:
54+
# test the case for a non-list single tag name. test when the tag is a
55+
# substring of the allowed tag.
56+
mock_validator = TagValidator('ISFOO')
57+
assert mock_validator.validate([GrammarTag('IS')], 0) == 0

0 commit comments

Comments
 (0)