class Base(unittest.TestCase): def setUp(self): self.hash = DummyHash({}) self.hash_patch = mock.patch('stubo.cache.Hash', self.hash) self.hash_patch.start() from stubo.testing import DummyScenario self.scenario = DummyScenario() self.db_patch = mock.patch('stubo.cache.Scenario', self.scenario) self.db_patch.start() self.patch_module = mock.patch('stubo.ext.module.Module', DummyModule) self.patch_module.start() def tearDown(self): self.hash_patch.stop() self.db_patch.stop() self.patch_module.stop() def _get_cache(self): from stubo.cache import Cache return Cache('localhost') def _make_scenario(self, name, **kwargs): doc = dict(name=name, **kwargs) self.scenario.insert(**doc)
def setUp(self): self.hash = DummyHash({}) self.hash_patch = mock.patch('stubo.cache.Hash', self.hash) self.hash_patch.start() from stubo.testing import DummyScenario self.scenario = DummyScenario() self.db_patch = mock.patch('stubo.cache.Scenario', self.scenario) self.db_patch.start() self.patch_module = mock.patch('stubo.ext.module.Module', DummyModule) self.patch_module.start()
class Test_create_session_cache(unittest.TestCase): def setUp(self): self.hash = DummyHash({}) self.hash_patch = mock.patch('stubo.cache.Hash', self.hash) self.hash_patch.start() from stubo.testing import DummyScenario self.scenario = DummyScenario() self.db_patch = mock.patch('stubo.cache.Scenario', self.scenario) self.db_patch.start() self.patch_module = mock.patch('stubo.ext.module.Module', DummyModule) self.patch_module.start() def tearDown(self): self.hash_patch.stop() self.db_patch.stop() self.patch_module.stop() def _get_cache(self): from stubo.cache import Cache return Cache('localhost') def _make_scenario(self, name, **kwargs): doc = dict(name=name, **kwargs) self.scenario.insert(**doc) def test_no_stubs(self): from stubo.exceptions import HTTPServerError with self.assertRaises(HTTPServerError): self._get_cache().create_session_cache('foo', 'bar') def test_new_session(self): from stubo.cache import compute_hash scenario_name = 'foo' self._make_scenario('localhost:foo') from stubo.model.stub import create, Stub stub = Stub(create('<test>match this</test>', '<test>OK</test>'), 'localhost:foo') doc = dict(scenario='localhost:foo', stub=stub) self.scenario.insert_stub(doc, stateful=True) cache = self._get_cache() cache.create_session_cache('foo', 'bar') session = self.hash.get('localhost:foo', 'bar') self.assertEqual(session["status"], "playback") self.assertEqual(session['session'], 'bar') self.assertEqual(session["scenario"], "localhost:foo") self.assertTrue('stubs' in session) stubs = session['stubs'] self.assertEqual(len(stubs), 1) from stubo.model.stub import StubCache stub = StubCache(stubs[0], 'localhost:foo', 'bar') self.assertEqual(stub.contains_matchers(), ["<test>match this</test>"]) self.assertEqual(stub.response_ids(), [compute_hash('<test>OK</test>')]) self.assertEqual(self.hash.get_raw('localhost:sessions', 'bar'), 'foo') def test_new_session_with_state(self): from stubo.cache import compute_hash scenario_name = 'foo' self._make_scenario('localhost:foo') from stubo.model.stub import create, Stub stub = Stub(create('<test>match this</test>', '<test>OK</test>'), 'localhost:foo') doc = dict(scenario='localhost:foo', stub=stub) self.scenario.insert_stub(doc, stateful=True) stub2 = Stub(create('<test>match this</test>', '<test>BAD</test>'), 'localhost:foo') doc2 = dict(scenario='localhost:foo', stub=stub2) self.scenario.insert_stub(doc2, stateful=True) cache = self._get_cache() cache.create_session_cache('foo', 'bar') session = self.hash.get('localhost:foo', 'bar') self.assertEqual(session["status"], "playback") self.assertEqual(session['session'], 'bar') self.assertEqual(session["scenario"], "localhost:foo") self.assertTrue('stubs' in session) stubs = session['stubs'] self.assertEqual(len(stubs), 1) from stubo.model.stub import StubCache stub = StubCache(stubs[0], session["scenario"], session['session']) responses = stub.response_ids() self.assertEqual(len(responses), 2) self.assertEqual(stub.contains_matchers(), ["<test>match this</test>"]) self.assertEqual(responses, [ compute_hash('<test>OK</test>'), compute_hash('<test>BAD</test>') ]) self.assertEqual(self.hash.get_raw('localhost:sessions', 'bar'), 'foo') def test_update_dormant_session_with_stubs(self): self.hash.set('somehost:foo', 'bar', { 'status': 'dormant', 'session': 'bar', 'scenario': 'localhost:foo' }) from stubo.cache import compute_hash scenario_name = 'foo' self._make_scenario('localhost:foo') from stubo.model.stub import create, Stub stub = Stub(create('<test>match this</test>', '<test>OK</test>'), 'localhost:foo') doc = dict(scenario='localhost:foo', stub=stub) self.scenario.insert_stub(doc, stateful=True) cache = self._get_cache() cache.create_session_cache('foo', 'bar') session = self.hash.get('localhost:foo', 'bar') self.assertEqual(session["status"], "playback") self.assertEqual(session['session'], 'bar') self.assertEqual(session["scenario"], "localhost:foo") self.assertTrue('stubs' in session) stubs = session['stubs'] self.assertEqual(len(stubs), 1) from stubo.model.stub import StubCache stub = StubCache(stubs[0], session["scenario"], session['session']) self.assertEqual(stub.contains_matchers(), ["<test>match this</test>"]) self.assertEqual(stub.response_ids(), [compute_hash('<test>OK</test>')]) def test_update_dormat_session_with_stubs_and_delay(self): self.hash.set('localhost:foo', 'bar', { 'status': 'dormant', 'session': 'bar', 'scenario': 'localhost:foo' }) delay_policy = { "delay_type": "fixed", "name": "slow", "milliseconds": "500" } self.hash.set('localhost:delay_policy', 'slow', delay_policy) self._make_scenario('localhost:foo') from stubo.model.stub import create, Stub stub = Stub(create('<test>match this</test>', '<test>OK</test>'), 'localhost:foo') stub.set_delay_policy('slow') doc = dict(scenario='localhost:foo', stub=stub) self.scenario.insert_stub(doc, stateful=True) self._get_cache().create_session_cache('foo', 'bar') session = self.hash.get('localhost:foo', 'bar') self.assertTrue('stubs' in session) stubs = session['stubs'] self.assertEqual(len(stubs), 1) from stubo.model.stub import StubCache stub = StubCache(stubs[0], session["scenario"], session['session']) self.assertEqual(stub.delay_policy(), delay_policy) def test_update_dormat_session_with_stubs_and_module(self): self.hash.set('localhost:foo', 'bar', { 'status': 'dormant', 'session': 'bar', 'scenario': 'localhost:foo' }) module = {"system_date": "2013-09-24", "version": 1, "name": "funcky"} self._make_scenario('localhost:foo') from stubo.model.stub import create, Stub stub = Stub(create('<test>match this</test>', '<test>OK</test>'), 'localhost:foo') stub.set_module(module) doc = dict(scenario='localhost:foo', stub=stub) self.scenario.insert_stub(doc, stateful=True) self._get_cache().create_session_cache('foo', 'bar') session = self.hash.get('localhost:foo', 'bar') self.assertTrue('stubs' in session) stubs = session['stubs'] self.assertEqual(len(stubs), 1) from stubo.model.stub import StubCache stub = StubCache(stubs[0], session["scenario"], session['session']) self.assertEqual(stub.module(), module)