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 deleteItemFromDB(self, item: PhoneData): try: _ = self.table.delete_item(Key={ 'BRAND': item.getBrand(), 'MODEL': item.getDBModel() }, ) except ClientError as e: print(e.response['Error']['Message']) else: pass
def test_equal(self): phone1 = PhoneData(brand="test", model="Decepticon", price=300.123, vendor="unitTest", info={ "url": "https:/testURL.com", "currency": "VND" }) phone2 = PhoneData(brand="test", model="Decepticon", price="300.123 VND", vendor="unitTest") self.assertEqual(phone1, phone2)
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 parseData(self, content, url): listMobile = [] listProduct = content.find('ul', attrs={'class': 'homeproduct'}) temp = listProduct.findAll('li') allProducts = [x.find('a', href=True) for x in temp] if len(allProducts) == 0: raise NoProductFoundException for a in allProducts: try: image_html = ScrapEngine.hideInvalidTag( a.find('img'), ['strike']) name_html = ScrapEngine.hideInvalidTag(a.find('h3'), ['strike']) price_html = ScrapEngine.hideInvalidTag( a.find('div', attrs={'class': 'price'}), ['strike', 'span']) image_src = "NA" if 'src' in image_html.attrs: image_src = image_html['src'] elif 'data-original' in image_html.attrs: image_src = image_html['data-original'] name = ScrapEngine.processString(name_html.getText(), self.ignoreTerm) name_idx = name.find(" ") price = ScrapEngine.processString(price_html.getText(), self.ignoreTerm) href = "n.a" href = urljoin(url, a['href']) try: listMobile.append( PhoneData(brand=name, model="", price=price, vendor="thegioididong", info={ "url": href, "img": image_src })) except PhoneDataInvalidException as error: print("Unable to parse: " + name + ": " + price + ". Error:" + str(error)) pass except Exception as e: print("Error: " + str(e)) pass print("Done with: " + url) print("Found {} items".format(str(len(listMobile)))) return listMobile
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 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 convertDBDataToPhone(item): try: temp = item['MODEL'] idx = temp.find('_') if idx < 0: phone_model = temp else: phone_model = temp[:idx] return PhoneData(brand=item['BRAND'], model=phone_model, price=item['PRICE'], vendor=item['VENDOR'], name=item['NAME'], info=item['INFO']) except PhoneDataInvalidException as error: print("Phone data invalid: {}: {}. Error: {}".format( item['NAME'], item['PRICE'], str(error)))
def parseData(self, content, url): listMobile = [] listProduct = content.find('div', attrs={'class': 'product-list'}) allProducts = listProduct.findAll('div', attrs={'class': 'list-item'}) if len(allProducts) == 0: raise NoProductFoundException for a in allProducts: image_html = ScrapEngine.hideInvalidTag(a.find('img'), ['strike']) name_html = ScrapEngine.hideInvalidTag( a.find('div', attrs={'class': 'product-name'}), ['strike']) price_html = ScrapEngine.hideInvalidTag( a.find('div', attrs={'class': 'product-price'}), ['strike']) try: image_src = image_html['src'] name = ScrapEngine.processString(name_html.getText(), self.ignoreTerm) name_idx = name.find(" ") price = ScrapEngine.processString(price_html.getText(), self.ignoreTerm) href = "n.a" temp = name_html.find('a', href=True) href = urljoin(url, temp['href']) try: listMobile.append( PhoneData(brand=name, model="", price=price, vendor="hoanghaMobile", info={ "url": href, "img": image_src })) except PhoneDataInvalidException as error: print("Unable to parse: " + name + ": " + price + ". Error:" + str(error)) pass except Exception as e: print("Error: " + str(e)) pass print("Done with: " + url) return listMobile
def test_PhoneName(self): phone = PhoneData(brand="test Megatron", model="", price=500000, vendor="unitTest", info={"currency": "SGD"}) self.assertEqual(phone.getBrand(), "test") self.assertEqual(phone.getModel(), "Megatron") phone = PhoneData(brand="iPhone6XR", model="", price=500000, vendor="unitTest", info={"currency": "SGD"}) self.assertEqual(phone.getBrand(), "iPhone") self.assertEqual(phone.getModel(), "6XR") phone = PhoneData(brand="iPhone6 XR", model="", price=500000, vendor="unitTest", info={"currency": "SGD"}) self.assertEqual(phone.getBrand(), "iPhone") self.assertEqual(phone.getModel(), "6 XR")
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 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 __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))