示例#1
0
文件: rest.py 项目: 0x19/flask-peewee
 def setUp(self):
     super(RestApiResourceTestCase, self).setUp()
     FModel.delete().execute()
     EModel.delete().execute()
     CModel.delete().execute()
     BModel.delete().execute()
     AModel.delete().execute()
示例#2
0
 def setUp(self):
     super(RestApiResourceTestCase, self).setUp()
     FModel.delete().execute()
     EModel.delete().execute()
     CModel.delete().execute()
     BModel.delete().execute()
     AModel.delete().execute()
示例#3
0
 def setUp(self):
     super(RestApiResourceTestCase, self).setUp()
     db.create_tables([
         AModel,
         BModel,
         BDetails,
         CModel,
         DModel,
         EModel,
         FModel,
     ])
     FModel.delete().execute()
     EModel.delete().execute()
     DModel.delete().execute()
     CModel.delete().execute()
     BModel.delete().execute()
     BDetails.delete().execute()
     AModel.delete().execute()
示例#4
0
    def test_reverse_resources(self):
        self.create_test_models()
        CModel.delete().execute()

        # Test reverse resource serialization
        resp = self.app.get('/api/amodelv2')
        self.assertEqual(resp.get_json(), [
            {'a_field': 'a1', 'bmodel_set': [{'a': 1, 'b_field': 'b1', 'c': None}], 'id': 1},
            {'a_field': 'a2', 'bmodel_set': [{'a': 2, 'b_field': 'b2', 'c': None}], 'id': 2},
        ])

        # Test filter on reverse resources
        resp = self.app.get('/api/amodelv2?bmodel__b_field=b2')
        self.assertEqual(resp.get_json(), [
            {'a_field': 'a2', 'bmodel_set': [{'a': 2, 'b_field': 'b2', 'c': None}], 'id': 2},
        ])

        # Test unique reverse resources
        self.c2 = CModel.create(c_field='c2', b=self.b2)
        resp = self.app.get('/api/bmodelv2?b_field=b2')
        self.assertEqual(resp.get_json(), [
            {'a': 2, 'b_field': 'b2', 'c': {"c_field": "c2", "b": 2}},
        ])

        # Test empty reverse resources
        AModel.create(a_field='a3')
        resp = self.app.get('/api/amodelv2')
        resp_json = resp.get_json()
        self.assertEqual(resp_json[2]["bmodel_set"], [])

        # Test many to one reverse resources
        BModel.create(b_field='b11', a=self.a1)
        resp = self.app.get('/api/amodelv2')
        resp_json = resp.get_json()
        self.assertEqual(len(resp_json), 3)
        self.assertEqual(len(resp_json[0]["bmodel_set"]), 2)