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

Commit

Permalink
implement TODO-marked tests regarding None, and defined the None token.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Frasca committed Jan 4, 2015
1 parent 9d751fc commit 9118156
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
14 changes: 13 additions & 1 deletion bauble/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ def search(text, session=None):
return list(results)


class NoneToken(object):
def __init__(self, t):
pass

def __repr__(self):
return '(None<NoneType>)'

def express(self):
return None


class ValueABC(object):
## abstract base class.

Expand Down Expand Up @@ -370,7 +381,8 @@ class SearchParser(object):
unquoted_string = Word(alphanums + alphas8bit + '%.-_*;:')
string_value = (unquoted_string | quotedString.setParseAction(removeQuotes)).setParseAction(StringToken)('string')

value = (numeric_value | string_value).setParseAction(ValueToken)('value')
none_token = Literal('None').setParseAction(NoneToken)
value = (numeric_value | string_value | none_token).setParseAction(ValueToken)('value')
value_list = Group(OneOrMore(string_value) ^ delimitedList(string_value)).setParseAction(ValueListAction)('value_list')

domain = Word(alphas, alphanums)
Expand Down
68 changes: 37 additions & 31 deletions bauble/test/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ def callback(self, *args, **kwargs):

parser = search.SearchParser()

# TODO: should we make these search tests independent of any plugins,
# we could use setup() to initialize a custom MapperSearch instead of
# expecting a plugin to set it up

class SearchParserTests(unittest.TestCase):
error_msg = lambda me, s, v, e: '%s: %s == %s' % (s, v, e)

Expand Down Expand Up @@ -391,10 +387,10 @@ def test_search_by_query22(self):
Family = self.Family
Genus = self.Genus
family2 = Family(family=u'family2')
genus2 = Genus(family=family2, genus=u'genus2')
f3 = Family(family=u'fam3')
g2 = Genus(family=family2, genus=u'genus2')
f3 = Family(family=u'fam3', qualifier=u's. lat.')
g3 = Genus(family=f3, genus=u'genus3')
self.session.add_all([family2, genus2, f3, g3])
self.session.add_all([family2, g2, f3, g3])
self.session.commit()

mapper_search = search.get_strategy('MapperSearch')
Expand All @@ -416,55 +412,65 @@ def test_search_by_query22(self):
r = list(results)
self.assertEqual(r, [])

# TODO: create a query to test the =None statement, can't use
# family.qualifier b/c its default value is ''
s = 'genus where family.family=fam3 and family.qualifier=""'
results = mapper_search.search(s, self.session)
self.assertEqual(results, set([g3]))
self.assertEqual(results, set([]))

# test the searching with the empty string does exactly that
# and does try to use None
s = 'genus where family.family=Orchidaceae and family.qualifier=""'
# sqlite3 stores None as the empty string.
s = 'genus where family.qualifier=""'
results = mapper_search.search(s, self.session)
r = list(results)
self.assertEqual(results, set([g2]))

# test where the column is ambiguous so make sure we choose
# the right one, in this case we want to make sure we get the
# qualifier on the family and not the genus
s = 'plant where accession.species.genus.family.family="Orchidaceae" '\
'and accession.species.genus.family.qualifier=""'
results = mapper_search.search(s, self.session)
self.assertEqual(results, set([]))

def test_search_by_query22None(self):
"query with MapperSearch, joined tables, predicates using None"
"""query with MapperSearch, joined tables, predicates using None
results are irrelevant, because sqlite3 uses the empty string to
represent None
"""

# test does not depend on plugin functionality
Family = self.Family
Genus = self.Genus
family2 = Family(family=u'family2')
genus2 = Genus(family=family2, genus=u'genus2')
f3 = Family(family=u'fam3')
g2 = Genus(family=family2, genus=u'genus2')
f3 = Family(family=u'fam3', qualifier=u's. lat.')
g3 = Genus(family=f3, genus=u'genus3')
self.session.add_all([family2, genus2, f3, g3])
self.session.add_all([family2, g2, f3, g3])
self.session.commit()

mapper_search = search.get_strategy('MapperSearch')
self.assertTrue(isinstance(mapper_search, search.MapperSearch))

s = 'genus where family.qualifier is None'
results = mapper_search.search(s, self.session)
self.assertEqual(results, set([]))

# make sure None isn't treated as the string 'None' and that
# the query picks up the is operator
s = 'genus where author is None'
results = mapper_search.search(s, self.session)
r = list(results)
self.assertEqual(results, set([]))

s = 'genus where author is not None'
results = mapper_search.search(s, self.session)
r = list(results)
resultsNone = mapper_search.search(s, self.session)
s = 'genus where not author = ""'
resultsEmptyString = mapper_search.search(s, self.session)
self.assertEqual(resultsNone, resultsEmptyString)

s = 'genus where author != None'
results = mapper_search.search(s, self.session)
r = list(results)

# test where the column is ambiguous so make sure we choose
# the right one, in this case we want to make sure we get the
# qualifier on the family and not the genus
s = 'plant where accession.species.genus.family.family="Orchidaceae" '\
'and accession.species.genus.family.qualifier=""'
results = mapper_search.search(s, self.session)
r = list(results)
resultsNone = mapper_search.search(s, self.session)
s = 'genus where not author = ""'
resultsEmptyString = mapper_search.search(s, self.session)
self.assertEqual(resultsNone, resultsEmptyString)

def test_search_by_query22id(self):
"query with MapperSearch, joined tables, test on id of dependent table"
Expand Down

0 comments on commit 9118156

Please sign in to comment.