def test_key_region_equality(self): key_region_a = KeyRegion( name="foo", index=1, content='%key foo', ) key_region_b = KeyRegion( name="foo", index=1, content='%key foo', ) key_region_c = KeyRegion( name="bar", index=1, content='%key foo', ) key_region_d = KeyRegion( name="foo", index=2, content='%key foo', ) key_region_e = KeyRegion( name="foo", index=1, content='%key bar', ) self.assertIsNot(key_region_a, key_region_b) self.assertTrue(key_region_a == key_region_b) self.assertFalse(key_region_a == key_region_c) self.assertFalse(key_region_a == key_region_d) self.assertFalse(key_region_a == key_region_e) self.assertFalse(key_region_a == 42)
def test_extract_regions_text_last(self): contents = [ '%arg arg1', '%include "foo"', '%key key2', 'a', 'b', 'c', ] content = '\n'.join(contents) script = Script( name='foo', regions=[ ArgumentRegion( name='arg2', index=1, type_='string', content='%arg arg2', ), KeyRegion( name='key1', index=1, content='%key key1', ), ], ) get_script_by_name = MagicMock(return_value=script) regions = self.parser.parse_regions( content=content, current_path=".", get_script_by_name=get_script_by_name, ) self.maxDiff = None self.assertEqual( [ ArgumentRegion( name='arg1', index=1, type_='string', content=contents[0], ), ScriptRegion( script=script, content='%include "foo"', ), KeyRegion( name='key2', index=2, content=contents[2], ), TextRegion(content='\n'.join(contents[3:6])), ], regions, )
def test_key_region_as_string(self): name = 'foo' index = 1 key_region = KeyRegion( name=name, index=index, content='%key foo', ) render_context = MagicMock() result = key_region.render(context=render_context) render_context.render_key.assert_called_once_with(name=name) self.assertEqual(render_context.render_key.return_value, result)
def test_extract_regions_keys(self): contents = [ '%key key1', '%include "foo"', '%key key3', ] content = '\n'.join(contents) script = Script( name='foo', regions=[ KeyRegion( name='key2', index=1, content='%key key2', ), ], ) get_script_by_name = MagicMock(return_value=script) regions = self.parser.parse_regions( content=content, current_path=".", get_script_by_name=get_script_by_name, ) self.maxDiff = None self.assertEqual( [ KeyRegion( name='key1', index=1, content=contents[0], ), ScriptRegion( script=script, content='%include "foo"', ), KeyRegion( name='key3', index=3, content=contents[2], ), ], regions, )
def test_script_instanciation_with_incorrect_keys(self): name = 'foo' regions = [ KeyRegion( name="key1", index=1, content='%key key1', ), KeyRegion( name="key2", index=3, content='%key key2', ), ] with self.assertRaises(ValueError): Script( name=name, regions=regions, )
def test_script_instanciation_with_keys(self): name = 'foo' subregions = [ KeyRegion( name="key2", index=1, content='%key key2', ), TextRegion(content='local b = 0;'), ] regions = [ KeyRegion( name="key1", index=1, content='%key key1', ), TextRegion(content='a'), ScriptRegion( script=Script( name='bar', regions=subregions, ), content='%include "bar"', ), KeyRegion( name="key3", index=3, content='%key key3', ), ] script = Script( name=name, regions=regions, ) self.assertEqual( [ "key1", "key2", "key3", ], script.keys, )
def test_key_region_line_count(self): name = 'foo' index = 1 key_region = KeyRegion( name=name, index=index, content='%key foo', ) self.assertEqual(1, key_region.line_count) self.assertEqual(1, key_region.real_line_count)
def test_key_region_instanciation(self): name = 'foo' index = 1 key_region = KeyRegion( name=name, index=index, content='%key foo', ) self.assertEqual(name, key_region.name) self.assertEqual(index, key_region.index)
def test_key_region_representation(self): name = 'foo' index = 1 key_region = KeyRegion( name=name, index=index, content='%key foo', ) self.assertEqual( ("KeyRegion(line_count=1, name='foo', content='%key foo')"), repr(key_region), )
def test_script_region_keys(self): name = 'foo' regions = [ KeyRegion( name='key1', index=1, content='%key key1', ), TextRegion(content='b'), ] script = Script( name=name, regions=regions, ) script_region = ScriptRegion( script=script, content='%include "foo"', ) self.assertEqual(script.keys, script_region.keys)
def test_script_call_missing_key(self): name = 'foo' regions = [ KeyRegion( name='key1', index=1, content='%key key1', ), ArgumentRegion( name='arg1', index=1, type_='string', content='%arg arg1', ), ] script = Script( name=name, regions=regions, ) with self.assertRaises(TypeError): script.get_runner(client=None)(arg1='ARG')
def test_script_run(self, _): name = 'foo' regions = [ KeyRegion( name='key1', index=1, content='%key key1', ), ArgumentRegion( name='arg1', index=1, type_='string', content='%arg arg1', ), TextRegion(content='b'), KeyRegion( name='key2', index=2, content='%key key2', ), ArgumentRegion( name='arg2', index=2, type_='integer', content='%arg arg2', ), ArgumentRegion( name='arg3', index=3, type_='bool', content='%arg arg3', ), ArgumentRegion( name='arg4', index=4, type_='list', content='%arg arg4', ), ArgumentRegion( name='arg5', index=5, type_='dict', content='%arg arg5', ), ] script = Script( name=name, regions=regions, ) script._redis_script = MagicMock(return_value="result") result = script.get_runner(client=None)( arg1='ARG', arg2=2, arg3=False, arg4=(1, 2.5, None, 'a'), arg5={'b': None}, key1='KEY', key2='KEY 2', ) self.assertEqual("result", result) script._redis_script.assert_called_once_with( keys=['KEY', 'KEY 2'], args=[ 'ARG', 2, 0, jdumps([1, 2.5, None, 'a']), jdumps({'b': None}), ], client=None, )
def test_script_render_with_duplicate_includes_nested_keys(self): script_a = Script( name='a', regions=[ KeyRegion( name='key1', index=1, content='%key key1', ), ], ) script_b = Script( name='b', regions=[ KeyRegion( name='key2', index=1, content='%key key2', ), ], ) script_c = Script( name='c', regions=[ KeyRegion( name='key3', index=1, content='%key key3', ), ], ) script_d = Script( name='d', regions=[ ScriptRegion( script=script_c, content='%include "c"', ), KeyRegion( name='key4', index=2, content='%key key4', ), ], ) script = Script( name='abcd', regions=[ ScriptRegion( script=script_a, content='%include "a"', ), ScriptRegion( script=script_b, content='%include "b"', ), ScriptRegion( script=script_d, content='%include "d"', ), ], ) self.assertEqual( [ 'local key1 = KEYS[1]', 'local key2 = KEYS[2]', 'local key3 = KEYS[3]', 'local key4 = KEYS[4]', ], script.render().split('\n'), )