def test_init_decimal(self): phone = PhoneData(brand="test", model="Megatron", price=500000, vendor="unitTest", info={"currency": "SGD"}) self.assertEqual(phone.getPrice(), 500000) self.assertEqual(phone.getName(), "test Megatron") phone2 = PhoneData(brand="test", model="Decepticon", price=300.123, vendor="unitTest", info={ "url": "https:/testURL.com", "currency": "VND" }) self.assertEqual(phone2.getPrice(), 300.123) self.assertEqual(phone2.getName(), "test Decepticon") self.assertEqual(phone2.getInfo()["url"], "https:/testURL.com") self.assertEqual(phone2.getInfo()["currency"], "VND") with self.assertRaises(PhoneDataInvalidException): phone3 = PhoneData(brand="test", model="Decepticon", price=-300.123, vendor="unitTest", info={ "url": "https:/testURL.com", "currency": "VND" })
def test_init_string(self): phone = PhoneData(brand="test", model="Megatron", price="500000 VND", vendor="unitTest") self.assertEqual(phone.getPrice(), 500000) self.assertEqual(phone.getName(), "test Megatron") phone2 = PhoneData(brand="test", model="Megatron XL", price="1000000 VND", vendor="unitTest", info={"url": "https:/testURL.com"}) self.assertEqual(phone2.getPrice(), 1000000) self.assertEqual(phone2.getName(), "test Megatron XL") self.assertEqual(phone2.getInfo()["url"], "https:/testURL.com") phone3 = PhoneData(brand="test 1", model="Decepticon", price="$SGD 300000", vendor="unitTest", info={"url": "https:/testURL.com"}) self.assertEqual(phone3.getPrice(), 300000) self.assertEqual(phone3.getName(), "test 1 Decepticon") self.assertEqual(phone3.getInfo()["currency"], "SGD") with self.assertRaises(PhoneDataInvalidException): phone4 = PhoneData(brand="test 1", model="Decepticon", price="$SGD abc 300000 ", vendor="unitTest", info={"url": "https:/testURL.com"})
def addIfBelongToCollection(self, phone: PhoneData): if self.brand == phone.getBrand() and self.model == phone.getModel(): self.phoneList.append(phone) self.minPrice = min(self.minPrice, phone.getPrice()) return True else: return False
def convertPhoneToDBData(phone: PhoneData): return { 'BRAND': phone.getBrand(), 'MODEL': phone.getDBModel(), 'NAME': phone.getName(), 'TYPE': 'Mobile', 'PRICE': phone.getPrice(), 'VENDOR': phone.getVendor(), 'INFO': phone.getInfo() }
def updateItemToDB(self, item: PhoneData): try: _ = self.table.update_item( Key={ 'BRAND': item.getBrand(), 'MODEL': item.getDBModel() }, UpdateExpression="set INFO = :i, PRICE = :p", ExpressionAttributeValues={ ':p': item.getPrice(), ':i': item.getInfo() }, ReturnValues="UPDATED_NEW") except ClientError as e: print(e.response['Error']['Message']) else: pass
def __init__(self, firstPhone: PhoneData): self.brand = firstPhone.getBrand() self.model = firstPhone.getModel() self.name = firstPhone.getName() self.minPrice = firstPhone.getPrice() self.phoneList = [firstPhone]
class TestDatabaseFunctional(unittest.TestCase): def setUp(self) -> None: self.phone = PhoneData(brand="test", model="Megatron", price="500000 VND", vendor="unitTest") self.phone2 = PhoneData(brand="test", model="Megatron XL", price="1000000 VND", vendor="unitTest", info={"url": "https:/testURL.com"}) self.phone3 = PhoneData(brand="test 1", model="Decepticon", price="$SGD 300000", vendor="unitTest", info={"url": "https:/testURL.com"}) self.data = [self.phone, self.phone2, self.phone3] self.tableName = "test_table" self.pElements = constants.phonePrimaryElements self.sElements = constants.phoneSecondaryElements self.dynamoElements = [DynamoElement('DeviceName', 'HASH', 'S')] DatabaseEngine.createTable(self.tableName, self.pElements, self.sElements) self.db = phoneDBEngine(self.tableName) def tearDown(self) -> None: DatabaseEngine.deleteTable(self.tableName) def test_insert_update_deleteData(self): # Insert self.db.pushAllDataToDB(self.data) retriveData = self.db.getAllDataFromTable() ok = True for item in self.data: if item not in retriveData: ok = False break self.assertTrue(ok, "Push data failed") # Update self.phone.price = 12345 self.phone.info["currency"] = "Yolo Dollar" self.db.updateItemToDB(self.phone) try: temp = self.db.getSpecificItemFromDB(brand=self.phone.getBrand(), model=self.phone.getModel(), vendor=self.phone.getVendor()) result = phoneDBEngine.convertAllDataToPhone(temp) self.assertEqual(result.getPrice(), self.phone.getPrice()) self.assertEqual(result.getInfo().get("currency"), self.phone.getInfo().get("currency")) except ClientError as e: self.fail("update Data failed") # Delete self.db.deleteItemFromDB(self.phone) temp = self.db.getSpecificItemFromDB(brand=self.phone.getBrand(), model=self.phone.getModel(), vendor=self.phone.getVendor()) self.assertIsNone(temp) def test_getAllPhones_withBrands(self): phoneAdapter = phoneDBEngine(constants.dynamoDBTableName) data = phoneAdapter.getItemsWithBrandAndModel(brand="Samsung") phoneData = phoneDBEngine.convertAllDataToPhone(data) for item in phoneData: if "Samsung" not in item.getBrand(): self.fail("Phones not from correct brand." + str(item)) def test_getAllPhones_withBrands_andModels(self): phoneAdapter = phoneDBEngine(constants.dynamoDBTableName) data = phoneAdapter.getItemsWithBrandAndModel(brand="Samsung", model="Galaxy A20s") phoneData = phoneDBEngine.convertAllDataToPhone(data) for item in phoneData: if "Samsung" not in item.getBrand(): self.fail("Phones not from correct brand." + str(item)) else: print(item) def test_getAllPhones(self): phoneAdapter = phoneDBEngine(constants.dynamoDBTableName) data = phoneAdapter.getAllDevicesWithType('Mobile') self.assertGreater(len(data), 30) def test_getPhonesWithinPriceRange(self): lowerLim = 5000000 higherLim = 10000000 phoneAdapter = phoneDBEngine(constants.dynamoDBTableName) data = phoneAdapter.getPhonesInPriceRange(lowerLim, higherLim) self.assertGreater(len(data), 0) for item in data: if item.getPrice() < lowerLim or item.getPrice() > higherLim: self.fail("Price not within range. " + str(item)) def test_getPhonesWithinPriceRangeWithBrand(self): lowerLim = 3000000 higherLim = 10000000 brand = "Samsung" phoneAdapter = phoneDBEngine(constants.dynamoDBTableName) data = phoneAdapter.getItemWithBrandAndPriceAndType( devType='Mobile', brand=brand, lowerLim=lowerLim, higherLim=higherLim) self.assertGreater(len(data), 0) for item in data: phone = phoneDBEngine.convertDBDataToPhone(item) if phone.getPrice() < lowerLim or phone.getPrice( ) > higherLim or phone.getBrand() != brand: self.fail( "Price not within range or Item not from from correct brand. " + str(item))