-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
88 lines (63 loc) · 2.59 KB
/
test_utils.py
File metadata and controls
88 lines (63 loc) · 2.59 KB
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
# cd c:\Docs\Code\lawn
# kr *.py -r -c "python manage.py test test_utils"
import logging
import os
from django.test import TestCase
from utils import LoggingFilterContext, TemporaryFileContext
class TestLoggingFilter(TestCase):
def test_filtering(self):
calls = list()
def test_fn(record):
calls.append( record )
# Outter filters are checked first.
with LoggingFilterContext( lambda record: False ):
with LoggingFilterContext( test_fn ):
logging.error('pass')
with LoggingFilterContext( lambda record: True ):
with LoggingFilterContext( test_fn ):
logging.error('fail')
self.assertEquals( ['fail'], [x.msg for x in calls])
def test_filtering_annotate(self):
calls = list()
@LoggingFilterContext.annotate(lambda rec: rec.msg=='pass')
def test_fn(record):
calls.append( record )
with test_fn.logging_filter:
with LoggingFilterContext( test_fn ):
logging.warn('pass')
with test_fn.logging_filter:
with LoggingFilterContext( test_fn ):
logging.warn('fail')
self.assertEquals( ['pass'], [x.msg for x in calls])
def test_filtering_annotate_regex(self):
calls = list()
@LoggingFilterContext.annotate_regex('.*pass$')
def test_fn(record):
calls.append( record )
with test_fn.logging_filter:
with LoggingFilterContext( test_fn ):
logging.warn('pass')
with test_fn.logging_filter:
with LoggingFilterContext( test_fn ):
logging.warn('fail')
self.assertEquals( ['pass'], [x.msg for x in calls])
def test_filtering_annotate_not_regex(self):
calls = list()
@LoggingFilterContext.annotate_not_regex('.*fail$')
def test_fn(record):
calls.append( record )
with test_fn.logging_filter:
with LoggingFilterContext( test_fn ):
logging.warn('pass')
with test_fn.logging_filter:
with LoggingFilterContext( test_fn ):
logging.warn('fail')
self.assertEquals( ['pass'], [x.msg for x in calls])
class TestTemporaryFileContext(TestCase):
def test_file_is_removed(self):
contents = 'asd'
with TemporaryFileContext(contents) as tempfile:
fname = tempfile.fileName()
self.assertTrue( os.access(fname, os.R_OK) )
self.assertEquals( contents, open(fname).read() )
self.assertFalse( os.access(fname, os.R_OK) )