예제 #1
0
 def test_member_name_override(self):
     self.output.write('class Approval(Object):\n')
     writer = ClassWriter('Approval', 'TEST', self.output)
     writer.write_simple_members(
         'Field', {'fields': {'URL': {}, 'url': {}}}, 'fields'
     )
     self.check_output("""\
     class Approval(Object):
         url = Field('URL')
         url_ = Field('url')
     """)
     # no error log:
     self.log.check()
예제 #2
0
 def test_member_name_override(self):
     self.output.write('class Approval(Object):\n')
     writer = ClassWriter('Approval', 'TEST', self.output)
     writer.write_simple_members('Field',
                                 {'fields': {
                                     'URL': {},
                                     'url': {}
                                 }}, 'fields')
     self.check_output("""\
     class Approval(Object):
         url = Field('URL')
         url_ = Field('url')
     """)
     # no error log:
     self.log.check()
예제 #3
0
    def test_method_override(self):
        writer = ClassWriter('Update', '???', self.output)
        writer.write_header()
        writer.write_members(self.writer.format_action,
                             {'actions': {
                                 "update_obj": {}
                             }}, 'actions')
        writer.write_footer()
        self.check_output('''
        class Update(Object):
            code = '???'

            @property
            def update_obj(self):
                """
                The object referenced by this update.
                """
                return self.session.api.from_data(
                    self.session, dict(
                        ID=self.update_obj_id,
                        objCode=self.update_obj_code
                    ))
        api.register(Update)
        ''')
예제 #4
0
    def test_method_override(self):
        writer = ClassWriter('Update', '???', self.output)
        writer.write_header()
        writer.write_members(self.writer.format_action,
                             {'actions': {"update_obj": {}}}, 'actions')
        writer.write_footer()
        self.check_output('''
        class Update(Object):
            code = '???'

            @property
            def update_obj(self):
                """
                The object referenced by this update.
                """
                return self.session.api.from_data(
                    self.session, dict(
                        ID=self.update_obj_id,
                        objCode=self.update_obj_code
                    ))
        api.register(Update)
        ''')
예제 #5
0
 def setUp(self):
     self.output = StringIO()
     self.writer = ClassWriter('FooBar', 'FOOB', self.output)
     self.log = LogCapture()
     self.addCleanup(self.log.uninstall)
예제 #6
0
class TestClassWriter(TestCase):

    def setUp(self):
        self.output = StringIO()
        self.writer = ClassWriter('FooBar', 'FOOB', self.output)
        self.log = LogCapture()
        self.addCleanup(self.log.uninstall)

    def check_output(self, expected):
        compare(expected=dedent(expected), actual=self.output.getvalue(),
                trailing_whitespace=False)

    def test_write_header(self):
        self.writer.write_header()
        self.check_output("""

        class FooBar(Object):
            code = 'FOOB'
        """)

    def test_write_members(self):
        self.output.write('class TestClass(Object):\n')
        self.writer.write_simple_members(
            'Field', {'fields': {'fooBar': {}, 'BazBob': {}}}, 'fields'
        )
        self.check_output("""\
        class TestClass(Object):
            baz_bob = Field('BazBob')
            foo_bar = Field('fooBar')
        """)

    def test_write_duplicate_members(self):
        self.output.write('class TestClass(Object):\n')
        self.writer.write_simple_members(
            'Field', {'fields': {'fooBar': {}, 'FooBAR': {}}},'fields'
        )
        self.check_output("""\
        class TestClass(Object):
            foo_bar = Field('FooBAR')
            foo_bar = Field('fooBar')
        """)
        self.log.check(
            ('workfront.generate', 'ERROR',
             "FooBar (FOOB) has duplicate member name: "
             "'foo_bar', first from 'FooBAR', current from 'fooBar'")
        )

    def test_member_name_override(self):
        self.output.write('class Approval(Object):\n')
        writer = ClassWriter('Approval', 'TEST', self.output)
        writer.write_simple_members(
            'Field', {'fields': {'URL': {}, 'url': {}}}, 'fields'
        )
        self.check_output("""\
        class Approval(Object):
            url = Field('URL')
            url_ = Field('url')
        """)
        # no error log:
        self.log.check()

    def test_write_footer(self):
        self.output.write('class FooBar(Object):\n    foo = "bar"\n')
        self.writer.write_footer()
        self.check_output("""\
        class FooBar(Object):
            foo = "bar"

        api.register(FooBar)
        """)

    def test_action_no_arguments(self):
        self.output.write('class FooBar(Object):\n    foo = "bar"\n')
        self.writer.write_members(self.writer.format_action, {
            'actions': {"doSomething": {
                    "resultType": "string",
                }
            }}, 'actions')
        self.check_output('''\
        class FooBar(Object):
            foo = "bar"
            def do_something(self):
                """
                The ``doSomething`` action.

                :return: ``string``
                """
                params = {}
                data = self.session.put(self.api_url()+'/doSomething', params)
                return data['result']
        ''')

    def test_action_no_returns(self):
        self.output.write('class FooBar(Object):\n    foo = "bar"\n')
        self.writer.write_members(self.writer.format_action, {
            'actions': {"doSomething": {
                    "arguments": [
                        {
                            "name": "anOption",
                            "type": "Task"
                        },
                    ],
                }
            }}, 'actions')
        self.check_output('''\
        class FooBar(Object):
            foo = "bar"

            def do_something(self, an_option=None):
                """
                The ``doSomething`` action.

                :param an_option: anOption (type: ``Task``)
                """
                params = {}
                if an_option is not None: params['anOption'] = an_option
                data = self.session.put(self.api_url()+'/doSomething', params)
        ''')

    def test_method_override(self):
        writer = ClassWriter('Update', '???', self.output)
        writer.write_header()
        writer.write_members(self.writer.format_action,
                             {'actions': {"update_obj": {}}}, 'actions')
        writer.write_footer()
        self.check_output('''
        class Update(Object):
            code = '???'

            @property
            def update_obj(self):
                """
                The object referenced by this update.
                """
                return self.session.api.from_data(
                    self.session, dict(
                        ID=self.update_obj_id,
                        objCode=self.update_obj_code
                    ))
        api.register(Update)
        ''')
예제 #7
0
 def setUp(self):
     self.output = StringIO()
     self.writer = ClassWriter('FooBar', 'FOOB', self.output)
     self.log = LogCapture()
     self.addCleanup(self.log.uninstall)
예제 #8
0
class TestClassWriter(TestCase):
    def setUp(self):
        self.output = StringIO()
        self.writer = ClassWriter('FooBar', 'FOOB', self.output)
        self.log = LogCapture()
        self.addCleanup(self.log.uninstall)

    def check_output(self, expected):
        compare(expected=dedent(expected),
                actual=self.output.getvalue(),
                trailing_whitespace=False)

    def test_write_header(self):
        self.writer.write_header()
        self.check_output("""

        class FooBar(Object):
            code = 'FOOB'
        """)

    def test_write_members(self):
        self.output.write('class TestClass(Object):\n')
        self.writer.write_simple_members(
            'Field', {'fields': {
                'fooBar': {},
                'BazBob': {}
            }}, 'fields')
        self.check_output("""\
        class TestClass(Object):
            baz_bob = Field('BazBob')
            foo_bar = Field('fooBar')
        """)

    def test_write_duplicate_members(self):
        self.output.write('class TestClass(Object):\n')
        self.writer.write_simple_members(
            'Field', {'fields': {
                'fooBar': {},
                'FooBAR': {}
            }}, 'fields')
        self.check_output("""\
        class TestClass(Object):
            foo_bar = Field('FooBAR')
            foo_bar = Field('fooBar')
        """)
        self.log.check(
            ('workfront.generate', 'ERROR',
             "FooBar (FOOB) has duplicate member name: "
             "'foo_bar', first from 'FooBAR', current from 'fooBar'"))

    def test_member_name_override(self):
        self.output.write('class Approval(Object):\n')
        writer = ClassWriter('Approval', 'TEST', self.output)
        writer.write_simple_members('Field',
                                    {'fields': {
                                        'URL': {},
                                        'url': {}
                                    }}, 'fields')
        self.check_output("""\
        class Approval(Object):
            url = Field('URL')
            url_ = Field('url')
        """)
        # no error log:
        self.log.check()

    def test_write_footer(self):
        self.output.write('class FooBar(Object):\n    foo = "bar"\n')
        self.writer.write_footer()
        self.check_output("""\
        class FooBar(Object):
            foo = "bar"

        api.register(FooBar)
        """)

    def test_action_no_arguments(self):
        self.output.write('class FooBar(Object):\n    foo = "bar"\n')
        self.writer.write_members(
            self.writer.format_action,
            {'actions': {
                "doSomething": {
                    "resultType": "string",
                }
            }}, 'actions')
        self.check_output('''\
        class FooBar(Object):
            foo = "bar"
            def do_something(self):
                """
                The ``doSomething`` action.

                :return: ``string``
                """
                params = {}
                data = self.session.put(self.api_url()+'/doSomething', params)
                return data['result']
        ''')

    def test_action_no_returns(self):
        self.output.write('class FooBar(Object):\n    foo = "bar"\n')
        self.writer.write_members(
            self.writer.format_action, {
                'actions': {
                    "doSomething": {
                        "arguments": [
                            {
                                "name": "anOption",
                                "type": "Task"
                            },
                        ],
                    }
                }
            }, 'actions')
        self.check_output('''\
        class FooBar(Object):
            foo = "bar"

            def do_something(self, an_option=None):
                """
                The ``doSomething`` action.

                :param an_option: anOption (type: ``Task``)
                """
                params = {}
                if an_option is not None: params['anOption'] = an_option
                data = self.session.put(self.api_url()+'/doSomething', params)
        ''')

    def test_method_override(self):
        writer = ClassWriter('Update', '???', self.output)
        writer.write_header()
        writer.write_members(self.writer.format_action,
                             {'actions': {
                                 "update_obj": {}
                             }}, 'actions')
        writer.write_footer()
        self.check_output('''
        class Update(Object):
            code = '???'

            @property
            def update_obj(self):
                """
                The object referenced by this update.
                """
                return self.session.api.from_data(
                    self.session, dict(
                        ID=self.update_obj_id,
                        objCode=self.update_obj_code
                    ))
        api.register(Update)
        ''')