def create_test_models(self): self.a1 = AModel.create(a_field='a1') self.a2 = AModel.create(a_field='a2') self.b1 = BModel.create(b_field='b1', a=self.a1) self.b2 = BModel.create(b_field='b2', a=self.a2) self.c1 = CModel.create(c_field='c1', b=self.b1) self.c2 = CModel.create(c_field='c2', b=self.b2) self.e1 = EModel.create(e_field='e1') self.e2 = EModel.create(e_field='e2') self.f1 = FModel.create(f_field='f1', e=self.e1) self.f2 = FModel.create(f_field='f2')
def create_models(self): for i in range(1, 4): a = AModel.create(a_field='a%d' % i) b = BModel.create(b_field='b%d' % i, a=a) c = CModel.create(c_field='c%d' % i, b=b) d = DModel.create(d_field='d%d' % i, c=c) if i % 2 == 0: bd = BDetails.create(b=b)
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)
def test_model_admin_recursive_delete(self): self.create_users() m1 = Message.create(user=self.normal, content='test1') m2 = Message.create(user=self.normal, content='test2') m3 = Message.create(user=self.admin, content='test3') n1 = Note.create(user=self.normal, message='test1') n2 = Note.create(user=self.normal, message='test2') n3 = Note.create(user=self.admin, message='test3') a1 = AModel.create(a_field='a1') a2 = AModel.create(a_field='a2') b1 = BModel.create(b_field='b1', a=a1) b2 = BModel.create(b_field='b2', a=a2) bd1= BDetails.create(b=b1) bd2= BDetails.create(b=b2) c1 = CModel.create(c_field='c1', b=b1) c2 = CModel.create(c_field='c2', b=b2) d1 = DModel.create(d_field='d1', c=c1) d2 = DModel.create(d_field='d2', c=c2) with self.flask_app.test_client() as c: self.login(c) resp = c.get('/admin/amodel/delete/?id=%d' % (a1.id)) self.assertEqual(resp.status_code, 200) collected = self.get_context('collected') self.assertEqual(collected, { a1.id: [ (1, BModel, 'a', [b1]), (2, BDetails, 'a', [bd1]), (2, CModel, 'a', [c1]), (3, DModel, 'a', [d1]), ] }) resp = c.post('/admin/amodel/delete/', data={'id': a1.id}) self.assertEqual(resp.status_code, 302) self.assertEqual(AModel.select().count(), 1) self.assertEqual(BModel.select().count(), 1) self.assertEqual(BDetails.select().count(), 1) self.assertEqual(CModel.select().count(), 1) self.assertEqual(DModel.select().count(), 1) # send it a single id resp = c.get('/admin/user/delete/?id=%d' % (self.normal.id)) self.assertEqual(resp.status_code, 200) query = self.get_context('query') self.assertEqual(list(query), [self.normal]) collected = self.get_context('collected') self.assertEqual(len(collected), 1) u_k = collected[self.normal.id] self.assertEqual(len(u_k), 2) self.assertEqual(u_k, [ (1, Message, 'user', [m1, m2]), (1, Note, 'user', [n1, n2]), ]) # post to it, get a redirect on success resp = c.post('/admin/user/delete/', data={'id': self.normal.id}) self.assertEqual(resp.status_code, 302) self.assertEqual(User.select().count(), 2) self.assertEqual(Message.select().count(), 1) self.assertEqual(Note.select().count(), 1) resp = c.get('/admin/user/delete/?id=%d&id=%d' % (self.admin.id, self.inactive.id)) self.assertEqual(resp.status_code, 200) collected = self.get_context('collected') self.assertEqual(len(collected), 2) u_k = collected[self.admin.id] self.assertEqual(len(u_k), 2) self.assertEqual(u_k, [ (1, Message, 'user', [m3]), (1, Note, 'user', [n3]), ]) u_k = collected[self.inactive.id] self.assertEqual(len(u_k), 0) # post to it, get a redirect on success resp = c.post('/admin/user/delete/', data={'id': [self.admin.id, self.inactive.id]}) self.assertEqual(resp.status_code, 302) self.assertEqual(User.select().count(), 0) self.assertEqual(Message.select().count(), 0) self.assertEqual(Note.select().count(), 0)