class TestHelpers(unittest.TestCase): def setUp(self): self.test_suite = WhiskytonTest() self.app = self.test_suite.set_app(app) def tearDown(self): self.test_suite.unset_app() # test methods from Whisky (whiskyton/models.py) def test_slug(self): whisky = self.test_suite.get_whisky(2) self.assertEqual(whisky.get_slug(), "glendeveronmacduff") def test_get_tastes(self): whisky = self.test_suite.get_whisky(2) tastes = ["1", "1", "1", "1", "1", "3", "2", "1", "0", "2", "0", "2"] self.assertEqual(whisky.get_tastes(), tastes) # test methods from Chart (whiskyton/helpers/charts.py) def test_cache_path(self): base_dir = app.config["BASEDIR"] cache_path = base_dir.child("whiskyton", "static", "charts") chart = Chart() self.assertEqual(str(cache_path), chart.cache_path()) def test_cache_name(self): whisky_1, whisky_2 = self.test_suite.get_whiskies() chart = Chart(reference=whisky_1, comparison=whisky_2) cache_dir_path = chart.cache_path() cache_file_path = chart.cache_name(True) cache_name = chart.cache_name() self.assertEqual(cache_name, "110113221101x111113210202.svg") self.assertEqual(cache_file_path, cache_dir_path.child(cache_name).absolute()) def test_create_and_cache(self): base_dir = app.config["BASEDIR"] whisky_1, whisky_2 = self.test_suite.get_whiskies() chart = Chart(reference=whisky_1, comparison=whisky_2) contents = chart.create() cached = chart.cache() sample = base_dir.child("whiskyton", "tests", "chart_sample.svg") self.assertEqual(contents, cached.read_file()) self.assertEqual(contents, sample.read_file()) # test methods from whiskyton/helpers/sitemap.py def test_recursive_listdir(self): sample_dir = app.config["BASEDIR"].child("whiskyton") files = sitemap.recursive_listdir(sample_dir) self.assertIsInstance(files, list) for file_path in files: self.assertTrue(file_path.exists()) self.assertTrue(file_path.isfile()) def test_most_recent_update(self): output = sitemap.most_recent_update() dt = datetime.strptime(output, "%Y-%m-%d") self.assertIsInstance(dt, datetime)
class TestRoutes(TestCase): def setUp(self): self.test_suite = WhiskytonTest() self.app = self.test_suite.set_app(app, db) def tearDown(self): self.test_suite.unset_app(db) # test routes from whiskyton/blueprint/site.py def test_index(self): resp = self.app.get("/") pq = PyQuery(resp.data) random = pq(".jumbotron strong").html() self.assertEqual(resp.status_code, 200) self.assertIn(random, ["Isle of Arran", "Glen Deveron / MacDuff"]) def test_successful_search(self): resp = self.app.get("/search?s=Glen+Deveron+%2F+MacDuff") self.assertEqual(resp.status_code, 302) def test_unsuccessful_search(self): resp = self.app.get("/search?s=Bowm") pq = PyQuery(resp.data) title = pq("#whiskies h2").html() random = pq("#whiskies p a.label").html() self.assertEqual(resp.status_code, 200) self.assertIn("Bowm", title) self.assertIn(random, ["Isle of Arran", "Glen Deveron / MacDuff"]) def test_valid_whisky_page(self): resp = self.app.get("/isleofarran") pq = PyQuery(resp.data) title = pq("#header h1").html() subtitle = pq("#whiskies h2 span.label").html() charts = pq("div.chart") self.assertEqual(resp.status_code, 200) self.assertIn("Isle of Arran", title) self.assertIn("Isle of Arran", subtitle) self.assertTrue(charts) def test_invalid_whisky_page(self): resp = self.app.get("/jackdaniels") pq = PyQuery(resp.data) form = pq("form") error_message = pq("#whiskies").html() self.assertEqual(resp.status_code, 404) self.assertIn("404", error_message) self.assertGreater(len(form), 0) def test_successful_search_id(self): whisky = Whisky.query.first() resp = self.app.get("/w/{}".format(whisky.id)) self.assertEqual(resp.status_code, 302) def test_unsuccessful_search_id(self): resp = self.app.get("/w/{}".format(6.02e23)) self.assertEqual(resp.status_code, 404) # test routes from whiskyton/blueprints/files.py def test_valid_chart(self): whisky_1, whisky_2 = self.test_suite.get_whiskies() chart = Chart(reference=whisky_1, comparison=whisky_2) cache_name = chart.cache_name(True) if cache_name.exists(): cache_name.remove() svg = "{}-{}.svg".format(whisky_1.slug, whisky_2.slug) resp = self.app.get("/charts/{}".format(svg)) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.data.decode("utf-8").count("<polygon "), 6) self.assertEqual(resp.data.decode("utf-8").count("<text "), 12) self.assertEqual(resp.data.decode("utf-8").count("<g "), 4) self.assertEqual(resp.data.decode("utf-8").count('id="grid"'), 1) self.assertEqual(resp.data.decode("utf-8").count('id="label"'), 1) self.assertEqual(resp.data.decode("utf-8").count('id="reference"'), 1) self.assertEqual(resp.data.decode("utf-8").count('id="whisky"'), 1) def test_invalid_chart(self): resp = self.app.get("/charts/jackdaniels-jameson.svg") self.assertEqual(resp.status_code, 404) def test_whisky_json(self): whisky_1, whisky_2 = self.test_suite.get_whiskies() resp = self.app.get("/whiskyton.json") json_data = loads(resp.data) self.assertEqual(resp.status_code, 200) self.assertIn("whiskies", json_data.keys()) self.assertIn(whisky_1.distillery, json_data["whiskies"]) self.assertIn(whisky_2.distillery, json_data["whiskies"]) def test_robots(self): resp = self.app.get("/robots.txt") self.assertIn(resp.status_code, [200, 304]) def test_favicon(self): resp = self.app.get("/favicon.ico") self.assertIn(resp.status_code, [200, 304]) def test_sitemap(self): resp = self.app.get("/sitemap.xml") self.assertIn(resp.status_code, [200, 304])
class TestRoutes(TestCase): def setUp(self): self.test_suite = WhiskytonTest() self.app = self.test_suite.set_app(app, db) def tearDown(self): self.test_suite.unset_app(db) # test routes from whiskyton/blueprint/site.py def test_index(self): resp = self.app.get('/') pq = PyQuery(resp.data) random = pq('.jumbotron strong').html() self.assertEqual(resp.status_code, 200) self.assertIn(random, ['Isle of Arran', 'Glen Deveron / MacDuff']) def test_successful_search(self): resp = self.app.get('/search?s=Glen+Deveron+%2F+MacDuff') self.assertEqual(resp.status_code, 302) def test_unsuccessful_search(self): resp = self.app.get('/search?s=Bowm') pq = PyQuery(resp.data) title = pq('#whiskies h2').html() random = pq('#whiskies p a.label').html() self.assertEqual(resp.status_code, 200) self.assertIn('Bowm', title) self.assertIn(random, ['Isle of Arran', 'Glen Deveron / MacDuff']) def test_valid_whisky_page(self): resp = self.app.get('/isleofarran') pq = PyQuery(resp.data) title = pq('#header h1').html() subtitle = pq('#whiskies h2 span.label').html() charts = pq('div.chart') self.assertEqual(resp.status_code, 200) self.assertIn('Isle of Arran', title) self.assertIn('Isle of Arran', subtitle) self.assertTrue(charts) def test_invalid_whisky_page(self): resp = self.app.get('/jackdaniels') pq = PyQuery(resp.data) form = pq('form') error_message = pq('#whiskies').html() self.assertEqual(resp.status_code, 404) self.assertIn('404', error_message) self.assertGreater(len(form), 0) def test_successful_search_id(self): whisky = Whisky.query.first() resp = self.app.get('/w/{}'.format(whisky.id)) self.assertEqual(resp.status_code, 302) def test_unsuccessful_search_id(self): resp = self.app.get('/w/{}'.format(6.02e+23)) self.assertEqual(resp.status_code, 404) # test routes from whiskyton/blueprints/files.py def test_valid_chart(self): whisky_1, whisky_2 = self.test_suite.get_whiskies() chart = Chart(reference=whisky_1, comparison=whisky_2) cache_name = chart.cache_name(True) if cache_name.exists(): cache_name.remove() svg = '{}-{}.svg'.format(whisky_1.slug, whisky_2.slug) resp = self.app.get('/charts/{}'.format(svg)) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.data.count('<polygon '), 6) self.assertEqual(resp.data.count('<text '), 12) self.assertEqual(resp.data.count('<g '), 4) self.assertEqual(resp.data.count('id="grid"'), 1) self.assertEqual(resp.data.count('id="label"'), 1) self.assertEqual(resp.data.count('id="reference"'), 1) self.assertEqual(resp.data.count('id="whisky"'), 1) def test_invalid_chart(self): resp = self.app.get('/charts/jackdaniels-jameson.svg') self.assertEqual(resp.status_code, 404) def test_bootstrap_fonts(self): base_url = '/static/fonts/glyphicons-halflings-regular.' extensions = ['eot', 'svg', 'ttf', 'woff', 'py'] for ext in extensions: resp = self.app.get(base_url + ext) if ext is not 'py': self.assertEqual(resp.status_code, 200) else: self.assertEqual(resp.status_code, 404) def test_whisky_json(self): whisky_1, whisky_2 = self.test_suite.get_whiskies() resp = self.app.get('/whiskyton.json') json_data = loads(resp.data) self.assertEqual(resp.status_code, 200) self.assertIn('whiskies', json_data.keys()) self.assertIn(whisky_1.distillery, json_data['whiskies']) self.assertIn(whisky_2.distillery, json_data['whiskies']) def test_robots(self): resp = self.app.get('/robots.txt') self.assertIn(resp.status_code, [200, 304]) def test_favicon(self): resp = self.app.get('/favicon.ico') self.assertIn(resp.status_code, [200, 304]) def test_sitemap(self): resp = self.app.get('/sitemap.xml') self.assertIn(resp.status_code, [200, 304])