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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#!/usr/bin/env python
# vim: set ts=4 sw=4 et sts=4 ai:
#
# Test some basic functionality.
#
import os
import re
import sys
import unittest
qpath = os.path.abspath(os.path.join(os.path.split(__file__)[0], '..'))
sys.path.insert(0, qpath)
class TestQBasic(unittest.TestCase):
def setUp(self):
if os.path.exists('/tmp/q'):
os.remove('/tmp/q')
def tearDown(self):
self.setUp()
def assertInQLog(self, string):
# Check the log file exists.
self.assertTrue(os.path.exists('/tmp/q'))
# Read in the data.
f = open('/tmp/q', 'r')
logdata = f.read()
f.close()
# Check the string is found in the log file.
# We can't use self.assertRegexpMatches as we need re.DOTALL
expected_regexp = re.compile('.*%s.*' % string, re.DOTALL)
if not expected_regexp.search(logdata):
msg = '%s: %r not found in\n%s\n%s\n%s' % (
"Regexp didn't match",
expected_regexp.pattern,
"-"*75,
logdata,
"-"*75,
)
raise self.failureException(msg)
def test_q_log_message(self):
import q
q.q('Test message')
self.assertInQLog('Test message')
def test_q_function_call(self):
import q
@q.t
def test(arg):
return 'RetVal'
self.assertEqual('RetVal', test('ArgVal'))
self.assertInQLog('ArgVal')
self.assertInQLog('RetVal')
def test_q_argument_order_arguments(self):
import q
q.writer.color = False
class A:
def __init__(self, two, three, four):
q(two, three, four)
A("ArgVal1", "ArgVal2", "ArgVal3")
self.assertInQLog(".*".join([
"__init__:",
"two='ArgVal1'",
"three='ArgVal2'",
"four='ArgVal3'",
]))
def test_q_argument_order_attributes1(self):
import q
q.writer.color = False
class A:
def __init__(self, two, three, four):
self.attrib1 = 'Attrib1'
self.attrib2 = 'Attrib2'
q(self.attrib1, self.attrib2)
A("ArgVal1", "ArgVal2", "ArgVal3")
self.assertInQLog(".*".join([
"__init__:",
"self.attrib1='Attrib1',",
"self.attrib2='Attrib2'",
]))
def test_q_argument_order_attributes2(self):
import q
q.writer.color = False
class A:
def __init__(s, two, three, four):
s.attrib1 = 'Attrib1'
s.attrib2 = 'Attrib2'
q(s.attrib1, s.attrib2)
A("ArgVal1", "ArgVal2", "ArgVal3")
self.assertInQLog(".*".join([
"__init__:",
"s.attrib1='Attrib1',",
"s.attrib2='Attrib2'",
]))
def test_q_argument_order_attributes_and_arguments(self):
import q
q.writer.color = False
class A:
def __init__(self, two, three, four):
self.attrib1 = 'Attrib1'
self.attrib2 = 'Attrib2'
q(two, three, self.attrib1, four, self.attrib2)
A("ArgVal1", "ArgVal2", "ArgVal3")
self.assertInQLog(".*".join([
"__init__:",
"two='ArgVal1'",
"three='ArgVal2'",
"self.attrib1='Attrib1'",
"four='ArgVal3'",
"self.attrib2='Attrib2'",
]))
def test_q_trace(self):
import q
q.writer.color = False
@q
def log1(msg='default'):
return msg
@q.t
def log2(msg='default'):
return msg
log1('log1 message')
log2('log2 message')
self.assertInQLog("log1\\('log1 message'\\)")
self.assertInQLog("log2\\('log2 message'\\)")
def test_q_nested_bad_wrapper(self):
# See http://micheles.googlecode.com/hg/decorator/documentation.html#statement-of-the-problem # noqa
import q
q.writer.color = False
def wrapper(func):
def do_nothing(*args, **kwargs):
return func(*args, **kwargs)
return do_nothing
@wrapper
@q
@wrapper
def decorated_log_bad(msg='default'):
return msg
decorated_log_bad('decorated bad message')
self.assertInQLog("do_nothing\\('decorated bad message'\\)")
self.assertInQLog("-> 'decorated bad message'")
def test_q_nested_good_wrappers(self):
import q
q.writer.color = False
import functools
def wrapper(func):
def do_nothing(*args, **kwargs):
return func(*args, **kwargs)
return functools.update_wrapper(do_nothing, func)
@wrapper
@q
@wrapper
def decorated_log_good(msg='default'):
return msg
decorated_log_good('decorated good message')
self.assertInQLog("decorated_log_good\\('decorated good message'\\)")
self.assertInQLog("-> 'decorated good message'")
unittest.main()
|