Commit af9a61a3 authored by Neil Spink's avatar Neil Spink

get solution to pass all tests in python3

parent 93f354a4
Pipeline #41150321 (#95) failed with stages
in 2 minutes and 13 seconds
#!/bin/bash #!/bin/bash
pip install -r ../source/requirements.txt pip3 install -r ../source/requirements.txt
pip install -r ../source/requirements_unit_test.txt pip3 install -r ../source/requirements_unit_test.txt
\ No newline at end of file \ No newline at end of file
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
"FunctionName": "grab-price", "FunctionName": "grab-price",
"Description": "Reading the price off the online shop.", "Description": "Reading the price off the online shop.",
"Handler": "grab_price.lambda_handler", "Handler": "grab_price.lambda_handler",
"Runtime": "python2.7", "Runtime": "python3.6",
"Role": "arn:aws:iam::385753165070:role/price-grabber-role" "Role": "arn:aws:iam::385753165070:role/price-grabber-role"
} }
}, },
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
"FunctionName": "grab-invoke", "FunctionName": "grab-invoke",
"Description": "Invokes the price grabber using list from S3 bucket.", "Description": "Invokes the price grabber using list from S3 bucket.",
"Handler": "grab_invoke.lambda_handler", "Handler": "grab_invoke.lambda_handler",
"Runtime": "python2.7", "Runtime": "python3.6",
"Role": "arn:aws:iam::385753165070:role/price-grabber-role" "Role": "arn:aws:iam::385753165070:role/price-grabber-role"
} }
} }
......
import urllib2 from urllib.request import urlopen
import lxml.html import lxml.html
import re import re
...@@ -12,8 +12,7 @@ class Crawler(object): ...@@ -12,8 +12,7 @@ class Crawler(object):
@staticmethod @staticmethod
def get_web_page(url): def get_web_page(url):
req = urllib2.Request(url) response = urlopen(url)
response = urllib2.urlopen(req)
return response.read() return response.read()
......
...@@ -9,7 +9,7 @@ def lambda_handler(event, context): ...@@ -9,7 +9,7 @@ def lambda_handler(event, context):
result = job.grab(event) result = job.grab(event)
print result print(result)
return result return result
......
...@@ -20,7 +20,7 @@ def lambda_handler(event, context): ...@@ -20,7 +20,7 @@ def lambda_handler(event, context):
result = json.dumps(data) result = json.dumps(data)
print result print(result)
return result return result
......
...@@ -18,7 +18,7 @@ class Invoker(object): ...@@ -18,7 +18,7 @@ class Invoker(object):
stream = obj.get()['Body'].read().decode('utf-8') stream = obj.get()['Body'].read().decode('utf-8')
else: else:
stream = file('./deployment/website-monitor-list.yml', 'r') stream = open('./deployment/website-monitor-list.yml', 'r')
yml_data = yaml.load(stream) yml_data = yaml.load(stream)
......
import unittest import unittest
import crawler as bot from . crawler import Crawler
class TestCrawlerMethods(unittest.TestCase): class TestCrawlerMethods(unittest.TestCase):
def test_get_shirt_html(self): def test_get_shirt_html(self):
try: try:
with open('../test-data/shirt.html', 'r') as myfile: #dev server with open('../test-data/shirt.html', 'r') as my_file: # dev server
result = myfile.read().replace('\n', '') result = my_file.read().replace('\n', '')
except IOError: except IOError:
with open('./test-data/shirt.html', 'r') as myfile: #ci server with open('./test-data/shirt.html', 'r') as my_file: # ci server
result = myfile.read().replace('\n', '') result = my_file.read().replace('\n', '')
self.assertIsNotNone(result) self.assertIsNotNone(result)
return result return result
def test_parse_price(self): def test_parse_price(self):
web = bot.Crawler() web = Crawler()
price = web.parse_price('CHF 90.00') price = web.parse_price('CHF 90.00')
self.assertEqual(price, '90.00') self.assertEqual(price, '90.00')
def test_parse_price_failing(self): def test_parse_price_failing(self):
web = bot.Crawler() web = Crawler()
with self.assertRaises(Exception): with self.assertRaises(Exception):
web.parse_price('abcdef') web.parse_price('abcdef')
def test_parse_html_way1(self): def test_parse_html_way1(self):
web = bot.Crawler() web = Crawler()
page = self.test_get_shirt_html() page = self.test_get_shirt_html()
data = web.parse_html(page, "//div[@class='h-text h-color-black title-typo h-p-top-m']/text()") data = web.parse_html(page, "//div[@class='h-text h-color-black title-typo h-p-top-m']/text()")
self.assertEqual(data, u'CHF\xa0105.00') self.assertEqual(data, u'CHF\xa0105.00')
def test_parse_html_way2(self): def test_parse_html_way2(self):
web = bot.Crawler() web = Crawler()
page = self.test_get_shirt_html() page = self.test_get_shirt_html()
data = web.parse_html(page, "//div[contains(@class, 'h-product-price')]/div/text()") data = web.parse_html(page, "//div[contains(@class, 'h-product-price')]/div/text()")
self.assertEqual(data, u'CHF\xa0105.00') self.assertEqual(data, u'CHF\xa0105.00')
def test_parse_html_failing(self): def test_parse_html_failing(self):
web = bot.Crawler() web = Crawler()
page = self.test_get_shirt_html() page = self.test_get_shirt_html()
with self.assertRaises(Exception): with self.assertRaises(Exception):
......
import unittest import unittest
from invoker import Invoker from . invoker import Invoker
import json import json
from mock import patch from mock import patch
......
...@@ -6,10 +6,10 @@ class TestWebsiteMonitorList(unittest.TestCase): ...@@ -6,10 +6,10 @@ class TestWebsiteMonitorList(unittest.TestCase):
def test_sites_information_valid(self, yml_file='../deployment/website-monitor-list.yml'): def test_sites_information_valid(self, yml_file='../deployment/website-monitor-list.yml'):
try: try:
stream = file(yml_file, 'r') # dev server stream = open(yml_file, 'r') # dev server
except IOError: except IOError:
stream = file('./deployment/website-monitor-list.yml', 'r') # ci server stream = open('./deployment/website-monitor-list.yml', 'r') # ci server
ymldata = yaml.load(stream) ymldata = yaml.load(stream)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment