Add plpy mockup

This commit is contained in:
Javier Goizueta 2016-02-16 08:34:12 +01:00
parent 8f3ef6210d
commit 669aa0119e
3 changed files with 44 additions and 6 deletions

View File

@ -2,4 +2,5 @@ import plpy
def xyz():
plpy.notice('XYZ...')
return "xyz-result"
r = plpy.execute("SELECT * FROM table")
return r[0]['x']

View File

@ -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 []

View File

@ -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...'