def test_b_002_01_1_ProductPost(self): toValidate = {"name": " 123 ", "price": 789, "in_stock": True} product = ProductPost(**toValidate) #print(product.dict()) self.assertEqual(product.dict(), { 'name': '123', 'price': 789.0, 'in_stock': True }) print("Test b_2_1_1:ProductPost Successful")
def test_b_001_01_1_ProductPost(self): toValidate = { "name": " abcdef ", "price": 123.1, "quantity": 400.12, "code": 798 } product = ProductPost(**toValidate) self.assertEqual(product.dict(), { "name": "abcdef", "price": 123.1, "quantity": 400.12, "code": 798 }) print("Test b_1_1_1:ProductPost Successful")
def test_b_001_01_3_ProductPost(self): toValidate = {"name": {}, "price": {}, "quantity": {}, "code": {}} try: product = ProductPost(**toValidate) self.assertEqual(True, False) except Exception as e: #print(json.loads(e.json())) self.assertEqual(json.loads(e.json()), [{ 'loc': ['name'], 'msg': 'str type expected', 'type': 'type_error.str' }, { 'loc': ['price'], 'msg': 'value is not a valid float', 'type': 'type_error.float' }, { 'loc': ['quantity'], 'msg': 'value is not a valid float', 'type': 'type_error.float' }, { 'loc': ['code'], 'msg': 'value is not a valid integer', 'type': 'type_error.integer' }]) print("Test b_1_1_3:ProductPost:Fail:not string")
def test_b_001_01_2_ProductPost(self): toValidate = {"code": 789456611} try: product = ProductPost(**toValidate) self.assertEqual(True, False) except Exception as e: #print(json.loads(e.json())) self.assertEqual(json.loads( e.json()), [{ 'loc': ['name'], 'msg': 'field required', 'type': 'value_error.missing' }, { 'loc': ['price'], 'msg': 'field required', 'type': 'value_error.missing' }, { 'loc': ['quantity'], 'msg': 'field required', 'type': 'value_error.missing' }, { 'loc': ['code'], 'msg': 'this code is already related to another product', 'type': 'value_error' }]) print("Test b_1_1_2:ProductPost:Fail:all missing required, " + "code not unique")
def test_b_002_01_5_ProductPost(self): # long product name # Very expensive price toValidate = { "name": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "price": 1000000000000000000000000000000000000000, "in_stock": True } try: product = ProductPost(**toValidate) self.assertEqual(True, False) except Exception as e: #print(json.loads(e.json())) self.assertEqual(json.loads(e.json()), [{ 'loc': ['name'], 'msg': 'ensure this value has at most 100 characters', 'type': 'value_error.any_str.max_length', 'ctx': { 'limit_value': 100 } }, { 'loc': ['price'], 'msg': 'ensure this value is less than or equal to 1000000', 'type': 'value_error.number.not_le', 'ctx': { 'limit_value': 1000000 } }]) print("Test b_2_1_5:ProductPost:Fail:long name, expensive price")
def test_b_002_01_4_ProductPost(self): # Short product name # Very cheap price toValidate = {"name": " a ", "price": .01, "in_stock": True} try: product = ProductPost(**toValidate) self.assertEqual(True, False) except Exception as e: #print(json.loads(e.json())) self.assertEqual(json.loads(e.json()), [{ 'loc': ['name'], 'msg': 'ensure this value has at least 3 characters', 'type': 'value_error.any_str.min_length', 'ctx': { 'limit_value': 3 } }, { 'loc': ['price'], 'msg': 'ensure this value is greater than or equal to 0.1', 'type': 'value_error.number.not_ge', 'ctx': { 'limit_value': 0.1 } }]) print("Test b_2_1_4:ProductPost:Fail:short name, cheap price")
def test_b_001_01_6_ProductPost(self): # adding unknown attribute # This attribute will not be returned # Testing White spaces # Code default value toValidate = { "name": " abacdegfgh ", "price": 100, "quantity": 10, "bla bla": "jgashgj" } product = ProductPost(**toValidate) #print(product.dict()) self.assertEqual(product.dict(), { 'name': 'abacdegfgh', 'price': 100.0, 'quantity': 10.0, 'code': None }) print("Test b_1_1_6:ProductPost:Added unknown value:Cleaned")
def test_b_001_01_5_ProductPost(self): # name : long # price : very expensive # quantity : very big # code: very big toValidate = { "name": "a" * 700000, "price": 10000000000000000000, "quantity": 1000000000, "code": 1000000000000000000000000000000000 } try: product = ProductPost(**toValidate) self.assertEqual(True, False) except Exception as e: #print(json.loads(e.json())) self.assertEqual(json.loads(e.json()), [{ 'loc': ['name'], 'msg': 'ensure this value has at most 300 characters', 'type': 'value_error.any_str.max_length', 'ctx': { 'limit_value': 300 } }, { 'loc': ['price'], 'msg': 'ensure this value is less than or equal to 1000000', 'type': 'value_error.number.not_le', 'ctx': { 'limit_value': 1000000 } }, { 'loc': ['quantity'], 'msg': 'ensure this value is less than or equal to 100000000', 'type': 'value_error.number.not_le', 'ctx': { 'limit_value': 100000000 } }, { 'loc': ['code'], 'msg': 'ensure this value is less than or equal to 10000000000000' + '0000000000000', 'type': 'value_error.number.not_le', 'ctx': { 'limit_value': 100000000000000000000000000 } }]) print("Test b_1_1_5:ProductPost:Fail:big data")
def test_b_001_01_4_ProductPost(self): # name : short # price : very cheap # quantity : very small # code: very small toValidate = { "name": " a ", "price": .00000000000001, "quantity": .000000001, "code": 1 } try: product = ProductPost(**toValidate) self.assertEqual(True, False) except Exception as e: #print(json.loads(e.json())) self.assertEqual(json.loads(e.json()), [{ 'loc': ['name'], 'msg': 'ensure this value has at least 3 characters', 'type': 'value_error.any_str.min_length', 'ctx': { 'limit_value': 3 } }, { 'loc': ['price'], 'msg': 'ensure this value is greater than or equal to 0.1', 'type': 'value_error.number.not_ge', 'ctx': { 'limit_value': 0.1 } }, { 'loc': ['quantity'], 'msg': 'ensure this value is greater than or equal to 0.001', 'type': 'value_error.number.not_ge', 'ctx': { 'limit_value': 0.001 } }, { 'loc': ['code'], 'msg': 'ensure this value is greater than or equal to 2', 'type': 'value_error.number.not_ge', 'ctx': { 'limit_value': 2 } }]) print("Test b_1_1_4:ProductPost:Fail:short data")
def test_b_002_01_2_ProductPost(self): toValidate = {} try: product = ProductPost(**toValidate) self.assertEqual(True, False) except Exception as e: # print(json.loads(e.json())) self.assertEqual(json.loads(e.json()), [{ 'loc': ['name'], 'msg': 'field required', 'type': 'value_error.missing' }, { 'loc': ['price'], 'msg': 'field required', 'type': 'value_error.missing' }]) print("Test b_2_1_2:ProductPost:Fail:all missing required")
def test_b_002_01_3_ProductPost(self): # Wrong data types toValidate = {"name": {}, "price": {}, "in_stock": {}} try: product = ProductPost(**toValidate) self.assertEqual(True, False) except Exception as e: #print(json.loads(e.json())) self.assertEqual(json.loads(e.json()), [{ 'loc': ['name'], 'msg': 'str type expected', 'type': 'type_error.str' }, { 'loc': ['price'], 'msg': 'value is not a valid float', 'type': 'type_error.float' }, { 'loc': ['in_stock'], 'msg': 'value could not be parsed to a boolean', 'type': 'type_error.bool' }]) print("Test b_2_1_3:ProductPost:Fail: Wrong data types")
def post_products(request, product: ProductPost): product = Product(**product.dict()) product.insert() return {"suucess": True, "product": product.deep()}