def test_add_human_obj_with_home_obj(self): # add related cats objects to the human object serializer = self.serializer_class(data=self.data, context=self.context) self.assertTrue(serializer.is_valid(), serializer.errors) human_obj = serializer.save() # Created related cats objects CatFactory.create_batch(10, owner=human_obj) self.assertDictEqual(self.sort_hyperlinks(serializer.data), self.obtain_expected_result(self.data, human_obj))
def test_remove_not_existing_cat_obj(self): CatFactory.create_batch(10) num_of_obj = self.get_num_of_obj(self.list_url) response = self.remove_obj(url=self.detail_url, data=None, pk=99, token=get_valid_token_key()) self.assertNotEqual(response.status_code, status.HTTP_204_NO_CONTENT, "#TCV-D05: Delete operation accidentally succeed") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND, "#TCV-D05: Non-existing page was somehow reached") new_num_of_obj = self.get_num_of_obj(self.list_url) self.assertEqual( new_num_of_obj, num_of_obj, "#TCV-D05: Total no. of objects is unexpectedly reduced")
def test_retrieve_multiple_cat_obj(self): cat_objs = CatFactory.create_batch(10) response = self.retrieve_obj(url=self.list_url) self.assertEqual(response.status_code, status.HTTP_200_OK, "#TCV-R02: Retrieve cat objects failed") json_data = response.json() self.assertEqual(json_data['count'], 10, "#TCV-R02: Total count of cat object is not correct")
def test_retrieve_human_obj_one_by_one(self): # Create multiple humans object to retrieve human_objs = HumanFactory.create_batch(10) serializer = self.serializer_class(instance=human_objs, many=True, context=self.context) # Check the length of the generated serializer data self.assertEqual(len(serializer.data), 10) # Retrieve each object one by one for human_obj in human_objs: # Create cat objects to the breed objects CatFactory.create_batch(3, owner=human_obj) serializer = self.serializer_class(instance=human_obj, context=self.context) # Compare all the fields self.assertDictEqual( self.sort_hyperlinks(serializer.data), self.obtain_expected_result(self.data, human_obj, read=True))
def test_retrieve_cat_obj_one_by_one(self): # Create multiple cats object to retrieve cat_objs = CatFactory.create_batch(10) for cat_obj in cat_objs: # Retrieve each object one by one serializer = self.serializer_class( instance=cat_obj, context=self.context ) # Compare all the fields self.assertDictEqual( serializer.data, self.obtain_expected_result(self.data, cat_obj, read=True) )
def create_cat_obj(self): data = self.data.copy() data['breed'] = self.breed data['owner'] = self.owner return CatFactory.create(**data)