class TestWhatTheyReallyMean(unittest.TestCase): def setUp(self): proj = Project(title='bogus') self.bar = Entity(proj, title='bar') self.e = Entity(proj, title='bogus entity') self.e.allowed_types = { 'foo': Entity, 'i': int, 'foolist': [Entity]} def test_1(self): """ Verify we don't alter entities. """ assert self.bar == self.e.what_they_really_mean('foo', self.bar) def test_2(self): """ Verify we don't alter look ups that we can't map to entities. """ assert 'sniz' == self.e.what_they_really_mean('baz', 'sniz') def test_3(self): """ Convert title of an entity to an entity. """ assert self.bar == \ self.e.what_they_really_mean('foo', self.bar.title) def test_5(self): """ Verify we type-cast when appropriate. """ assert 99 == self.e.what_they_really_mean('i', '99') def test_6(self): """ Convert a list of titles to a list of entities. """ assert [self.bar] == self.e.what_they_really_mean('foo', ['bar']) def test_7(self): """ Pass a list of entities through. """ temp = self.e.what_they_really_mean('foo', [self.bar]) assert [self.bar] == temp, \ 'got %s and wanted %s!' % (temp, [self.bar]) def test_list_of_entities_2(self): """ Verify I don't mess up a list of entities. """ temp = self.e.what_they_really_mean('foolist', [self.bar]) assert [self.bar] == temp, \ 'got %s and wanted %s!' % (temp, [self.bar]) def test_list_of_entities_3(self): """ Verify I convert a list of titles. """ temp = self.e.what_they_really_mean('foolist', [self.bar.title]) assert [self.bar] == temp, \ 'got %s and wanted %s!' % (temp, [self.bar]) def test_invalid_title(self): assert 'fizzle' == self.e.what_they_really_mean('foo', 'fizzle') def test_list_of_invalid_titles(self): temp = self.e.what_they_really_mean('foo', ['fizzle']) assert ['fizzle'] == temp, \ 'got %s, expected %s!' % (temp, ['fizzle']) def test_by_uuid_1(self): """ Convert a UUID into an entity. """ temp = self.e.what_they_really_mean('foo', self.bar.uuid) assert self.bar == temp, 'got %r, expected %s!' % (temp, self.bar) def test_list_of_uuids(self): """ Convert a list of UUIDs to a list of entities. """ temp = self.e.what_they_really_mean('foo', [self.bar.uuid]) assert [self.bar] == temp, \ 'got %r, expected %s!' % (temp, [self.bar]) def test_by_frag(self): """ Convert a UUID fragment into an entity. """ assert self.bar == \ self.e.what_they_really_mean('foo', self.bar.frag) def test_list_of_frags(self): """ Convert a list of fragments to a list of entities. """ assert [self.bar] == \ self.e.what_they_really_mean('foo', [self.bar.frag])
class TestMatchesDict(unittest.TestCase): def setUp(self): self.p = Project(title="TestMatchesDict") self.important = Entity(self.p, title='Important!!!') Entity.allowed_types['priority'] = Entity Entity.allowed_types['components'] = [Entity] self.e = Entity(self.p, title='Clean cat box', creator='Matt', pscore=99, tags=['boring', 'chore'], priority=self.important) def test_matches_dict_1(self): """ Verify matches_dict handles scalars and list comparisons. """ assert self.e.matches_dict(creator='Matt') == self.e assert self.e.matches_dict(creator='Nobody') == None assert self.e.matches_dict(tags=['boring', 'chore']) == self.e assert self.e.matches_dict(tags='boring') == self.e assert self.e.matches_dict(tags='chore') == self.e assert self.e.matches_dict(creator=['Matt']) == self.e assert self.e.matches_dict(creator=['Matt', 'Nobody']) == self.e assert self.e.matches_dict(pscore=99) == self.e assert self.e.matches_dict( creator=['Matt', 'Nobody'], tags=['fun']) == None assert self.e.matches_dict( creator=['Matt', 'Nobody'], tags=['fun', 'boring']) == self.e def test_order_independence_of_query(self): """ Test two attributes with lists as values. """ assert not self.e.matches_dict(creator=['Matt'], title=['DO NOT MATCH']) assert not self.e.matches_dict(creator=['DO NOT MATCH'], title=['Clean cat box']) def test_order_independence_of_subquery(self): """ Test two attributes with lists, and one of the lists has a title as an element. """ assert not self.e.matches_dict( priority=[self.important.title], # this one matches tags=['DOES NOT MATCH']) # but not this one. def test_matches_dict_2(self): """ Verify we can match entities by using their UUID """ assert self.e.matches_dict(priority=self.important) == self.e print("got %s" % self.e.matches_dict(priority=self.important.uuid)) assert self.e.matches_dict(priority=self.important.uuid) == self.e frag = self.important.frag assert self.e.matches_dict(priority=frag) == self.e def test_matches_dict_3(self): """ Verify we can match entities by using their title. """ assert 'priority' in self.e.allowed_types print("self.e['priority'] is %(priority)s" % self.e) assert self.e.matches_dict( priority=self.important.title) == self.e, \ "Lookup using title failed" def test_matches_dict_4(self): """ Verify we can match with lists of titles. """ print("self.e['priority'] is %(priority)s" % self.e) assert self.e.matches_dict( priority=[self.important.title]) == self.e, \ "Lookup using list of titles failed" def test_matches_dict_5(self): """ Verify we can match with lists of titles. """ print('priority is %(priority)s' % self.e) print('what they really meant: %s' % self.e.what_they_really_mean('priority', ['bogus'])) temp = self.e.matches_dict(priority=["bogus"]) assert temp is None, "temp (%s) should be None!" % temp def test_matches_dict_6(self): """ Verify we can match with lists of frags. """ print("self.e['priority'] is %(priority)s" % self.e) assert self.e.matches_dict( priority=[self.important.frag]) == self.e, \ "Lookup using list of frags failed" def test_matches_dict_7(self): """ Test allowed_types['components'] = [Entity] """ e = Entity(self.p, title='Clean cat box', creator='Matt', components=[Entity(self.p, title='bogus component')], tags=['boring', 'chore'], priority=self.important) assert e.matches_dict( components='bogus component') == e, 'OH NOES'