def test_update(self): category = Category_factory.build() data = Category_serializer(category).data category_result = Category_serializer(category, data=data) is_valid = category_result.is_valid() self.assertTrue(is_valid, category_result.errors) category = category_result.save() self.assertTrue(isinstance(category, Category_model))
def test_create(self): owner = User_factory.build() category = Category_factory.build() data = Category_serializer(category).data category_result = Category_serializer(data=data) is_valid = category_result.is_valid() self.assertTrue(is_valid, category_result.errors) category = category_result.save(owner=owner) self.assertTrue(isinstance(category, Category_model)) self.assertEqual(category.owner.username, owner.username)
def test_create_new_category(self, category_get): category_get.side_effect = Category_model.DoesNotExist owner = User_factory.build() category = Category_factory.build() category = Category_serializer(category).data category.pop("owner") item = Item_factory.build() data = Item_serializer(item).data result = Item_serializer(data=data) is_valid = result.is_valid() self.assertTrue(is_valid, result.errors) item = result.save(category=category, owner=owner) self.assertTrue(isinstance(item, Item_model))
def test_create(self, category_get): category = Category_factory.build() category_get.return_value = category category = Category_serializer(category).data item = Item_factory.build() data = Item_serializer(item).data result = Item_serializer(data=data) is_valid = result.is_valid() self.assertTrue(is_valid, result.errors) item = result.save(category=category) self.assertTrue(isinstance(item, Item_model)) self.assertEqual(category["name"], item.category.name) category_get.assert_called_with(**category)
def test_create_no_send_owner(self): """ Cuano no se manda el owner de la categorua deberia de fallar con una exception de KeyError porque los datos no traiana el owner """ category = Category_factory.build() data = Category_serializer(category).data category_result = Category_serializer(data=data) is_valid = category_result.is_valid() self.assertTrue(is_valid, category_result.errors) with self.assertRaises(KeyError): category_result.save()
def test_validate(self): category = Category_factory.build() data = Category_nested_serializer(category).data result = Category_nested_serializer(data=data) is_valid = result.is_valid() self.assertTrue(is_valid, result.errors)