Ejemplo n.º 1
0
    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,
        )
Ejemplo n.º 2
0
    def test_argument_region_as_string_bool_type(self):
        name = 'bar'
        index = 2
        argument_region = ArgumentRegion(
            name=name,
            index=index,
            type_='boolean',
            content='%arg bar',
        )
        render_context = MagicMock()
        result = argument_region.render(context=render_context)

        render_context.render_arg.assert_called_once_with(
            name=name,
            type_=bool,
        )
        self.assertEqual(render_context.render_arg.return_value, result)
Ejemplo n.º 3
0
    def test_argument_region_as_string_bool_type(self):
        name = 'bar'
        index = 2
        argument_region = ArgumentRegion(
            name=name,
            index=index,
            type_='boolean',
            content='%arg bar',
        )
        render_context = MagicMock()
        result = argument_region.render(context=render_context)

        render_context.render_arg.assert_called_once_with(
            name=name,
            type_=bool,
        )
        self.assertEqual(render_context.render_arg.return_value, result)
Ejemplo n.º 4
0
    def test_argument_region_as_string(self):
        name = 'foo'
        index = 1
        argument_region = ArgumentRegion(
            name=name,
            index=index,
            type_='string',
            content='%arg foo',
        )
        render_context = MagicMock()
        result = argument_region.render(context=render_context)

        render_context.render_arg.assert_called_once_with(
            name=name,
            type_=str,
        )
        self.assertEqual(render_context.render_arg.return_value, result)
Ejemplo n.º 5
0
    def test_script_instanciation_with_args(self):
        name = 'foo'
        subregions = [
            ArgumentRegion(
                name="arg2",
                index=1,
                type_='string',
                content='%arg arg2',
            ),
            TextRegion(content='local b = 0;'),
        ]
        regions = [
            ArgumentRegion(
                name="arg1",
                index=1,
                type_='string',
                content='%arg arg1',
            ),
            TextRegion(content='a'),
            ScriptRegion(
                script=Script(
                    name='bar',
                    regions=subregions,
                ),
                content='%include "bar"',
            ),
            ArgumentRegion(
                name="arg3",
                index=3,
                type_='integer',
                content='%arg arg3 integer',
            ),
        ]
        script = Script(
            name=name,
            regions=regions,
        )

        self.assertEqual(
            [
                ("arg1", str),
                ("arg2", str),
                ("arg3", int),
            ],
            script.args,
        )
Ejemplo n.º 6
0
    def test_argument_region_as_string(self):
        name = 'foo'
        index = 1
        argument_region = ArgumentRegion(
            name=name,
            index=index,
            type_='string',
            content='%arg foo',
        )
        render_context = MagicMock()
        result = argument_region.render(context=render_context)

        render_context.render_arg.assert_called_once_with(
            name=name,
            type_=str,
        )
        self.assertEqual(render_context.render_arg.return_value, result)
Ejemplo n.º 7
0
    def test_argument_region_invalid_instanciation(self):
        name = 'foo'
        index = 1

        with self.assertRaises(ValueError):
            ArgumentRegion(
                name=name,
                index=index,
                type_='unknown',
                content='%arg foo',
            )
Ejemplo n.º 8
0
    def test_argument_region_instanciation(self):
        name = 'foo'
        index = 1
        argument_region = ArgumentRegion(
            name=name,
            index=index,
            type_='string',
            content='%arg foo',
        )

        self.assertEqual(name, argument_region.name)
        self.assertEqual(index, argument_region.index)
Ejemplo n.º 9
0
 def test_script_instanciation_with_incorrect_args(self):
     name = 'foo'
     regions = [
         ArgumentRegion(
             name="arg1",
             type_='string',
             index=1,
             content='%arg arg1',
         ),
         ArgumentRegion(
             name="arg2",
             type_='string',
             index=3,
             content='%arg arg2',
         ),
     ]
     with self.assertRaises(ValueError):
         Script(
             name=name,
             regions=regions,
         )
Ejemplo n.º 10
0
    def test_argument_region_line_count(self):
        name = 'foo'
        index = 1
        argument_region = ArgumentRegion(
            name=name,
            index=index,
            type_='string',
            content='%arg foo',
        )

        self.assertEqual(1, argument_region.line_count)
        self.assertEqual(1, argument_region.real_line_count)
Ejemplo n.º 11
0
    def test_argument_region_representation(self):
        name = 'foo'
        index = 1
        argument_region = ArgumentRegion(
            name=name,
            index=index,
            type_='string',
            content='%arg foo',
        )

        self.assertEqual(
            "ArgumentRegion(line_count=1, name='foo', content='%arg foo')",
            repr(argument_region),
        )
Ejemplo n.º 12
0
    def test_script_instanciation_with_return_statement(self):
        name = 'foo'
        regions = [
            ArgumentRegion(
                name="arg1",
                type_='string',
                index=1,
                content='%arg arg1',
            ),
            ReturnRegion(
                type_='integer',
                content='%return integer',
            ),
        ]
        script = Script(
            name=name,
            regions=regions,
        )

        self.assertIs(int, script.return_type)
Ejemplo n.º 13
0
    def test_script_region_arguments(self):
        name = 'foo'
        regions = [
            ArgumentRegion(
                name='arg1',
                index=1,
                type_='string',
                content='%arg arg1',
            ),
            TextRegion(content='b'),
        ]
        script = Script(
            name=name,
            regions=regions,
        )
        script_region = ScriptRegion(
            script=script,
            content='%include "foo"',
        )

        self.assertEqual(script.args, script_region.args)
Ejemplo n.º 14
0
    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')
Ejemplo n.º 15
0
    def test_argument_region_equality(self):
        argument_region_a = ArgumentRegion(
            name="foo",
            index=1,
            type_='string',
            content='%arg foo',
        )
        argument_region_b = ArgumentRegion(
            name="foo",
            index=1,
            type_='string',
            content='%arg foo',
        )
        argument_region_c = ArgumentRegion(
            name="bar",
            index=1,
            type_='string',
            content='%arg foo',
        )
        argument_region_d = ArgumentRegion(
            name="foo",
            index=2,
            type_='string',
            content='%arg foo',
        )
        argument_region_e = ArgumentRegion(
            name="foo",
            index=2,
            type_='integer',
            content='%arg foo',
        )
        argument_region_f = ArgumentRegion(
            name="foo",
            index=1,
            type_='string',
            content='%arg bar',
        )

        self.assertIsNot(argument_region_a, argument_region_b)
        self.assertTrue(argument_region_a == argument_region_b)
        self.assertFalse(argument_region_a == argument_region_c)
        self.assertFalse(argument_region_a == argument_region_d)
        self.assertFalse(argument_region_a == argument_region_e)
        self.assertFalse(argument_region_a == argument_region_f)
        self.assertFalse(argument_region_a == 42)
Ejemplo n.º 16
0
    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,
        )
Ejemplo n.º 17
0
    def test_script_render_with_duplicate_includes_nested_args(self):
        script_a = Script(
            name='a',
            regions=[
                ArgumentRegion(
                    name='arg1',
                    type_='string',
                    index=1,
                    content='%arg arg1',
                ),
            ],
        )
        script_b = Script(
            name='b',
            regions=[
                ArgumentRegion(
                    name='arg2',
                    type_='string',
                    index=1,
                    content='%arg arg2',
                ),
            ],
        )
        script_c = Script(
            name='c',
            regions=[
                ArgumentRegion(
                    name='arg3',
                    type_='string',
                    index=1,
                    content='%arg arg3',
                ),
            ],
        )
        script_d = Script(
            name='d',
            regions=[
                ScriptRegion(
                    script=script_c,
                    content='%include "c"',
                ),
                ArgumentRegion(
                    name='arg4',
                    type_='string',
                    index=2,
                    content='%arg arg4',
                ),
            ],
        )
        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 arg1 = ARGV[1]',
                'local arg2 = ARGV[2]',
                'local arg3 = ARGV[3]',
                'local arg4 = ARGV[4]',
            ],
            script.render().split('\n'),
        )
Ejemplo n.º 18
0
    def test_extract_regions_arguments(self):
        contents = [
            '%arg arg1',
            '%include "foo"',
            '%arg arg3 string',
            '%arg arg4 integer',
            '%arg arg5 boolean',
        ]
        content = '\n'.join(contents)
        script = Script(
            name='foo',
            regions=[
                ArgumentRegion(
                    name='arg2',
                    index=1,
                    type_='string',
                    content='%arg arg2',
                ),
            ],
        )
        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"',
                ),
                ArgumentRegion(
                    name='arg3',
                    index=3,
                    type_='string',
                    content=contents[2],
                ),
                ArgumentRegion(
                    name='arg4',
                    index=4,
                    type_='integer',
                    content=contents[3],
                ),
                ArgumentRegion(
                    name='arg5',
                    index=5,
                    type_='boolean',
                    content=contents[4],
                ),
            ],
            regions,
        )