async def test_fk(self): # create a developer dev = Developer(name="developboy", age=24) await dev.save() # a client that has assigned developer client = Client(name="devman", dev=dev.id) await client.save() # get all of the developer clients clients_returned = dev.client_set() client_set = await clients_returned[0] # the client has the dev correctly set self.assertTrue(client.dev == dev.id) # and is correct comming back for the other model self.assertTrue(client_set.dev == dev.id)
async def test_arrayfield_empty_array(self): dev = Developer(name="walkie", age=43) await dev.save() skill = await Skill.objects.create(dev=dev.id, name="C/CPP", specialization=[]) self.assertIsInstance(skill.specialization, list) self.assertEqual(skill.specialization, [])
async def test_arrayfield_correct(self): dev = Developer(name="oldscholl", age=38) await dev.save() skill = await Skill.objects.create( dev=dev.id, name="Python", specialization=["backend", "frontend"]) self.assertIsInstance(skill.specialization, list) self.assertIn("backend", skill.specialization) self.assertIn("frontend", skill.specialization) self.assertEqual(2, len(skill.specialization))
async def test_textfield_correct(self): dev = Developer(name="talkie", age=33) await dev.save() skill = await Skill.objects.create( dev=dev.id, name="Ruby", specialization=["Rails"], notes="Wish I could help you developing something cool") self.assertIsInstance(skill.notes, str)
async def test_m2m(self): # new organization org_list = [] for _ in range(1, 6): org = Organization(name="kool org") await org.save() org_list.append(org.id) # create a developer dev = Developer(name="developer", age=55, org=org_list) await dev.save() # and the relation comes back # the method exists devs_returned = org.developer_set() developer_set = await devs_returned[0] orgs_returned = dev.organization_set() organization_set = await orgs_returned[0] # and they are correct self.assertEqual(developer_set.id, dev.id) self.assertIn(organization_set.id, org_list)
async def test_arrayfield_multidimensional(self): dev = Developer(name="multitalent", age=22) await dev.save() skill = await Skill.objects.create(dev=dev.id, name="Rust", specialization=[["backend", "web"], ["sql", "postgres"]]) self.assertIsInstance(skill.specialization, list) self.assertIsInstance(skill.specialization[0], list) self.assertIn("backend", skill.specialization[0]) self.assertIn("web", skill.specialization[0]) self.assertIn("sql", skill.specialization[1]) self.assertIn("postgres", skill.specialization[1])