Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_querylayer_size_defaults(self):
"""layer.QueryLayer gets defaults for options not passed"""
qlayer = QueryLayer(self.query, size='cold_brew')
qlayer.geom_type = 'point'
size_col_ans = {
'column': 'cold_brew',
'range': [5, 25],
'bins': 5,
'bin_method': 'quantiles'
}
self.assertDictEqual(qlayer.size, size_col_ans,
msg='size column should receive defaults')
qlayer = QueryLayer(self.query,
size={
'column': 'cold_brew',
'range': [4, 15],
'bin_method': 'equal'
})
def test_querylayer_get_cartocss(self):
"""layer.QueryLayer._get_cartocss"""
qlayer = QueryLayer(self.query, size=dict(column='cold_brew', min=10,
max=20))
qlayer.geom_type = 'point'
self.assertRegexpMatches(
qlayer._get_cartocss(BaseMap()), # pylint: disable=protected-access
(r'.*marker-width:\sramp\(\[cold_brew\],\srange\(10,20\),\s'
r'quantiles\(5\)\).*')
)
# test line cartocss
qlayer = QueryLayer(self.query)
qlayer.geom_type = 'line'
self.assertRegexpMatches(
qlayer._get_cartocss(BaseMap()), # pylint: disable=protected-access
r'^\#layer.*line\-width.*$'
)
# test point, line, polygon
def test_querylayer_time_numeric(self):
"""layer.QueryLayer time with quantitative classification"""
querylayer = QueryLayer(self.query,
time='timecol',
color='colorcol')
# category type
querylayer.style_cols['colorcol'] = 'number'
querylayer.style_cols['timecol'] = 'date'
querylayer.geom_type = 'point'
# normal behavior for point geometries
querylayer._setup([BaseMap(), querylayer], 1) # pylint: disable=protected-access
self.assertDictEqual(querylayer.scheme,
styling.mint(5))
# expect category maps query
self.assertRegexpMatches(querylayer.query.strip(),
r'^SELECT \*, colorcol as value '
r'.*_wrap$')
# cartocss should have cdb math mode
def test_line_styling(self): # pylint: disable=too-many-statements
"""layer.QueryLayer line styling"""
linelayer = QueryLayer(
'select * from lines',
size=5
)
linelayer.geom_type = 'line'
linelayer._setup([BaseMap(), linelayer], 1) # pylint: disable=protected-access
self.assertTrue(
'line-width: 5' in linelayer.cartocss
)
size = 'size_col'
color = 'mag'
linelayer = QueryLayer('select * from lines', size=size, color=color)
linelayer.geom_type = 'line'
qlayer.style_cols[color] = 'string'
qlayer._setup([BaseMap(), qlayer], 1) # pylint: disable=protected-access
self.assertEqual(qlayer.color, str_colors_ans[idx])
self.assertEqual(qlayer.scheme, str_scheme_ans[idx])
with self.assertRaises(ValueError,
msg='styling value cannot be a date'):
qlayer = QueryLayer(self.query, color='datetime_column')
qlayer.style_cols['datetime_column'] = 'date'
qlayer._setup([BaseMap(), qlayer], 1) # pylint: disable=protected-access
# Exception testing
# color column cannot be a geometry column
with self.assertRaises(ValueError,
msg='color clumn cannot be a geometry column'):
QueryLayer(self.query, color='the_geom')
# color dict must have a 'column' key
with self.assertRaises(ValueError,
msg='color dict must have a `column` key'):
QueryLayer(self.query, color={'scheme': styling.vivid(10)})
# test point, line, polygon
for geom in ('point', 'line', 'polygon', ):
styles = {'point': r'marker\-fill',
'line': r'line\-color',
'polygon': r'polygon\-fill'}
qlayer = QueryLayer(self.query, color='colname')
qlayer.geom_type = geom
self.assertRegexpMatches(
qlayer._get_cartocss(BaseMap()), # pylint: disable=protected-access
r'^\#layer.*{}.*\}}$'.format(styles[geom])
)
# geometry type should be defined
with self.assertRaises(ValueError,
msg='invalid geometry type'):
querylayer = QueryLayer(self.query, color='red')
querylayer.geom_type = 'notvalid'
querylayer._get_cartocss(BaseMap()) # pylint: disable=protected-access
with self.assertRaises(ValueError,
msg='time column cannot be `the_geom`'):
QueryLayer(self.query, time='the_geom')
# time dict must have a 'column' key
with self.assertRaises(ValueError,
msg='time dict must have a `column` key'):
QueryLayer(self.query, time={'scheme': styling.armyRose(10)})
# pass an int as the time column
with self.assertRaises(ValueError,
msg='`time` key has to be a str or dict'):
QueryLayer(self.query, time=7)
with self.assertRaises(ValueError):
querylayer = QueryLayer('select * from watermelon', time='seeds')
querylayer.style_cols['seeds'] = 'string'
querylayer.geom_type = 'point'
querylayer._setup([BaseMap(), querylayer], 1) # pylint: disable=protected-access
with self.assertRaises(ValueError):
querylayer = QueryLayer('select * from watermelon', time='seeds')
querylayer.style_cols['seeds'] = 'date'
querylayer.geom_type = 'polygon'
querylayer._setup([BaseMap(), querylayer], 1) # pylint: disable=protected-access
def test_querylayer_colors(self):
"""layer.QueryLayer color options tests"""
# no color options passed
basic = QueryLayer(self.query)
self.assertEqual(basic.color, None)
# check valid dict color options
dict_colors = [{'column': 'mandrill', 'scheme': styling.armyRose(7)},
{'column': 'mercxx', 'scheme': {'bin_method': 'equal',
'bins': 7,
'name': 'Temps'}},
{'column': 'elephant',
'scheme': styling.redOr(10, bin_method='jenks')}]
dict_colors_ans = ['mandrill', 'mercxx', 'elephant']
dict_colors_scheme = [{'name': 'ArmyRose',
'bins': 7,
'bin_method': 'quantiles'},
{'name': 'Temps',
'bins': 7,
'bin_method': 'equal'},
def test_querylayer_time_category(self):
"""layer.QueryLayer time with categories"""
querylayer = QueryLayer(self.query,
time='timecol',
color='colorcol')
# category type
querylayer.style_cols['colorcol'] = 'string'
querylayer.style_cols['timecol'] = 'date'
# if non-point geoms are present (or None), raise an error
with self.assertRaises(
ValueError,
msg='cannot make torque map with non-point geometries'):
querylayer._setup([BaseMap(), querylayer], 1) # pylint: disable=protected-access
querylayer.geom_type = 'point'
# normal behavior for point geometries
querylayer._setup([BaseMap(), querylayer], 1) # pylint: disable=protected-access
self.assertDictEqual(querylayer.scheme,
def test_querylayer_size_default(self):
"""layer.QueryLayer size to have default value"""
qlayer = QueryLayer(self.query)
self.assertEqual(qlayer.size, 10,
msg='should return default styling of 10')