Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
implement typed tests from the query builder. close #237.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrasca committed Dec 25, 2015
1 parent 4fe13df commit 5f9a510
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
30 changes: 25 additions & 5 deletions bauble/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, t=None):
pass

def __repr__(self):
return '(Empty<Set>)'
return 'Empty'

def express(self):
return set()
Expand Down Expand Up @@ -853,6 +853,24 @@ def _get_prop_menuitems(self, mapper):
return items


def parse_typed_value(value):
"""parse the input string and return the corresponding typed value
handles integers, floats, None, Empty, and falls back to string.
"""
try:
new_val = value
new_val = float(value)
new_val = int(value)
except:
if value == 'None':
new_val = None
if value == 'Empty':
new_val = EmptyToken()
value = new_val
return value


class ExpressionRow(object):
"""
"""
Expand Down Expand Up @@ -973,7 +991,7 @@ def get_widgets(self):

def get_expression(self):
"""
Return the expression represented but this ExpressionRow. If
Return the expression represented by this ExpressionRow. If
the expression is not valid then return None.
:param self:
Expand All @@ -991,12 +1009,14 @@ def get_expression(self):
else:
# assume it's a gtk.Entry or other widget with a text property
value = self.value_widget.props.text.strip()
value = parse_typed_value(value)
and_or = ''
if self.and_or_combo:
and_or = self.and_or_combo.get_active_text()
return ' '.join([and_or, self.prop_button.props.label,
self.cond_combo.get_active_text(),
'"%s"' % value]).strip()
result = ' '.join([and_or, self.prop_button.props.label,
self.cond_combo.get_active_text(),
repr(value)]).strip()
return result


class QueryBuilder(gtk.Dialog):
Expand Down
26 changes: 26 additions & 0 deletions bauble/test/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,3 +933,29 @@ def test_between_just_parse(self):
results = sp.parse_string('species where id between 0 and 1')
self.assertEqual(str(results.statement),
"SELECT * FROM species WHERE (BETWEEN id 0.0 1.0)")


class ParseTypedValue(BaubleTestCase):
def test_parse_typed_value_floats(self):
result = search.parse_typed_value('0.0')
self.assertEquals(result, 0.0)
result = search.parse_typed_value('-4.0')
self.assertEquals(result, -4.0)

def test_parse_typed_value_int(self):
result = search.parse_typed_value('0')
self.assertEquals(result, 0)
result = search.parse_typed_value('-4')
self.assertEquals(result, -4)

def test_parse_typed_value_None(self):
result = search.parse_typed_value('None')
self.assertEquals(result, None)

def test_parse_typed_value_empty_set(self):
result = search.parse_typed_value('Empty')
self.assertEquals(type(result), search.EmptyToken)

def test_parse_typed_value_fallback(self):
result = search.parse_typed_value('whatever else')
self.assertEquals(result, 'whatever else')

0 comments on commit 5f9a510

Please sign in to comment.