Exemplo n.º 1
0
class TestRuleGeneration(unittest.TestCase):
    def setUp(self):
        self.dispatcher = BaseDispatcher()

    def test_add_resource(self):
        def endpoint(self, params):
            pass

        resource = BaseResource(name='resource')
        resource.on_list = endpoint

        self.dispatcher.add_resource(resource)

        rules = list(self.dispatcher.url_map.iter_rules())

        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0].endpoint, endpoint)


    def test_add_nested_resource(self):
        def endpoint(self, params):
            pass

        def nested_endpoint(self, params):
            pass

        resource = BaseResource(name='resource')
        resource.on_detail = endpoint

        nested_resource = BaseResource(name='nested_resource')
        nested_resource.on_detail = nested_endpoint

        self.dispatcher.add_resource(resource)
        self.dispatcher.add_resource(nested_resource, resource)

        rules = list(self.dispatcher.url_map.iter_rules(nested_endpoint))

        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0].rule, '/resource/<int:resource_id>/nested_resource/<int:nested_resource_id>')

    def test_add_nested_resource_without_details(self):
        def endpoint(self, params):
            pass

        def nested_endpoint(self, params):
            pass

        resource = BaseResource(name='resource')
        resource.on_list = endpoint

        nested_resource = BaseResource(name='nested_resource')
        nested_resource.on_detail = nested_endpoint

        self.dispatcher.add_resource(resource)

        with self.assertRaises(AttributeError):
            self.dispatcher.add_resource(nested_resource, resource)
Exemplo n.º 2
0
 def setUp(self):
     self.dispatcher = BaseDispatcher()