diff --git a/python/crankshaft/crankshaft/poc/xyz.py b/python/crankshaft/crankshaft/poc/xyz.py index 4e32596..558c77c 100644 --- a/python/crankshaft/crankshaft/poc/xyz.py +++ b/python/crankshaft/crankshaft/poc/xyz.py @@ -2,4 +2,5 @@ import plpy def xyz(): plpy.notice('XYZ...') - return "xyz-result" + r = plpy.execute("SELECT * FROM table") + return r[0]['x'] diff --git a/python/crankshaft/test/mock_plpy.py b/python/crankshaft/test/mock_plpy.py new file mode 100644 index 0000000..63c88f6 --- /dev/null +++ b/python/crankshaft/test/mock_plpy.py @@ -0,0 +1,34 @@ +import re + +class MockPlPy: + def __init__(self): + self._reset() + + def _reset(self): + self.infos = [] + self.notices = [] + self.debugs = [] + self.logs = [] + self.warnings = [] + self.errors = [] + self.fatals = [] + self.executes = [] + self.results = [] + self.prepares = [] + self.results = [] + + def _define_result(self, query, result): + pattern = re.compile(query, re.IGNORECASE | re.MULTILINE) + self.results.append([pattern, result]) + + def notice(self, msg): + self.notices.append(msg) + + def info(self, msg): + self.infos.append(msg) + + def execute(self, query): # TODO: additional arguments + for result in self.results: + if result[0].match(query): + return result[1] + return [] diff --git a/python/crankshaft/test/test_poc.py b/python/crankshaft/test/test_poc.py index 06fc3d6..7307285 100644 --- a/python/crankshaft/test/test_poc.py +++ b/python/crankshaft/test/test_poc.py @@ -3,14 +3,17 @@ import unittest -class MockPlPy: - def notice(self, msg): - print msg +from mock_plpy import MockPlPy +plpy = MockPlPy() import sys -sys.modules['plpy'] = MockPlPy() +sys.modules['plpy'] = plpy + import crankshaft class TestPoc(unittest.TestCase): def test_should_have_xyz(self): - assert crankshaft.poc.xyz() == "xyz-result" + plpy._reset() + plpy._define_result('select\s+\*\s+from\s+table', [{'x': 111}]) + assert crankshaft.poc.xyz() == 111 + assert plpy.notices[0] == 'XYZ...'