From 1312cc1955eff36a6af498ef14471fdf07c3faf9 Mon Sep 17 00:00:00 2001 From: mehak-sachdeva Date: Thu, 3 Aug 2017 16:56:54 -0400 Subject: [PATCH] adding the utils test --- test/test_context.py | 4 +- test/test_utils.py | 89 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 test/test_utils.py diff --git a/test/test_context.py b/test/test_context.py index a743a0e15..0d0c58e88 100644 --- a/test/test_context.py +++ b/test/test_context.py @@ -17,7 +17,7 @@ def setUp(self): os.environ.get('USERNAME') is None): try: creds = json.loads(open('test/secret.json').read()) - except FileNotFoundError: + except OSError: print("Create a `secret.json` file by renaming " "`secret.json.sample` to `secret.json` and updating " "the credentials to match your environment.") @@ -104,7 +104,7 @@ def test_cartocontext_write(self): # check if table exists resp = self.sql_client.send(''' - SELECT * + SELECT * FROM {table} LIMIT 0 '''.format(table=self.test_write_table)) diff --git a/test/test_utils.py b/test/test_utils.py new file mode 100644 index 000000000..3f899e88c --- /dev/null +++ b/test/test_utils.py @@ -0,0 +1,89 @@ +"""Unit tests for cartoframes.utils""" +import unittest +from cartoframes.utils import dict_items, cssify +from cartoframes import styling +from collections import OrderedDict +# from cartoframes import styling + +class TestUtils(unittest.TestCase): + """Tests for functions in utils module""" + def setUp(self): + self.point_style = OrderedDict([( + "#layer['mapnik::geometry_type'=1]", OrderedDict([ + ('marker-width', "6"), + ('marker-fill', "yellow"), + ('marker-fill-opacity', "1"), + ('marker-allow-overlap', "true"), + ('marker-line-width', "0.5"), + ('marker-line-color', "black"), + ("marker-line-opacity", "1")]) + )]) + self.point_style_dict = dict_items(self.point_style) + self.point_stylecss = cssify(self.point_style) + self.point_values = [] + for value in self.point_style_dict: + self.point_values.append(value) + self.polygon_style = OrderedDict([( + "#layer['mapnik::geometry_type'=3]", OrderedDict([ + ('polygon-fill', 'ramp([column], (#ffc6c4, #ee919b, #cc607d, #9e3963, #672044), quantiles)'), + ('polygon-opacity', '0.9'), + ('polygon-gamma', '0.5'), + ('line-color', '#FFF'), + ('line-width', '0.5'), + ('line-opacity', '0.25'), + ('line-comp-op', 'hard-light')]) + )]) + self.polygon_style_dict = dict_items(self.polygon_style) + self.polygon_values = [] + for value in self.polygon_style_dict: + self.polygon_values.append(value) + + self.polygon_stylecss = cssify(self.polygon_style) + + self.complex_style = OrderedDict([ + ("#layer['mapnik::geometry_type'=1]", OrderedDict([ + ('marker-width', "5"), + ('marker-fill', "yellow"), + ('marker-fill-opacity', '1'), + ('marker-allow-overlap', 'true'), + ('marker-line-width', '0.5'), + ('marker-line-color', "black"), + ('marker-line-opacity', '1')])), + ("#layer['mapnik::geometry_type'=2]", OrderedDict([ + ('line-width', '1.5'), + ('line-color', "black")])), + ("#layer['mapnik::geometry_type'=3]", OrderedDict([ + ('polygon-fill', "blue"), + ('polygon-opacity', '0.9'), + ('polygon-gamma', '0.5'), + ('line-color', '#FFF'), + ('line-width', '0.5'), + ('line-opacity', '0.25'), + ('line-comp-op', 'hard-light')])) + ]) + + self.complex_style_dict = dict_items(self.complex_style) + self.complex_values = [] + for value in self.complex_style_dict: + self.complex_values.append(value) + + self.complex_stylecss = cssify(self.complex_style) + + def test_dict_items(self): + """utils.dict_items""" + # ensure correct formation of dict items from provided styling + self.assertEqual(self.polygon_values, + self.polygon_style.items()) + self.assertEqual(self.point_values, + self.point_style.items()) + self.assertEqual(self.complex_values, + self.complex_style.items()) + + def test_cssify(self): + """utils.cssify""" + self.assertEqual(self.point_stylecss, + "#layer['mapnik::geometry_type'=1] { marker-width: 6; marker-fill: yellow; marker-fill-opacity: 1; marker-allow-overlap: true; marker-line-width: 0.5; marker-line-color: black; marker-line-opacity: 1;} ") + self.assertEqual(self.polygon_stylecss, + "#layer['mapnik::geometry_type'=3] { polygon-fill: ramp([column], (#ffc6c4, #ee919b, #cc607d, #9e3963, #672044), quantiles); polygon-opacity: 0.9; polygon-gamma: 0.5; line-color: #FFF; line-width: 0.5; line-opacity: 0.25; line-comp-op: hard-light;} ") + self.assertEqual(self.complex_stylecss, + "#layer['mapnik::geometry_type'=1] { marker-width: 5; marker-fill: yellow; marker-fill-opacity: 1; marker-allow-overlap: true; marker-line-width: 0.5; marker-line-color: black; marker-line-opacity: 1;} #layer['mapnik::geometry_type'=2] { line-width: 1.5; line-color: black;} #layer['mapnik::geometry_type'=3] { polygon-fill: blue; polygon-opacity: 0.9; polygon-gamma: 0.5; line-color: #FFF; line-width: 0.5; line-opacity: 0.25; line-comp-op: hard-light;} ")