def test_push_to_fluiddb(self): # set stuff up... name = str(uuid.uuid4()) root_path = 'test/%s' % name tags = create_schema(TEMPLATE, root_path, 'flimp_test', 'flimp unit-test suite') self.assertEqual(4, len(tags)) fom_class = create_class(tags) # the good case push_to_fluiddb(TEMPLATE, root_path, fom_class, 'foo', 'flimp_test') # check an object was created result = Object.filter("has %s/foo" % root_path) self.assertEqual(1, len(result)) # lets try the other good case where we don't have an about tag field push_to_fluiddb(TEMPLATE, root_path, fom_class, None, 'flimp_test') # we should have *two* objects now result = Object.filter("has %s/foo" % root_path) self.assertEqual(2, len(result)) # check we have all the expected tags on the objects for obj in result: tag_paths = obj.tag_paths for k in tags: self.assertTrue(k in tag_paths) # ok... lets make sure that the allowEmpty flag is handled properly template = [ { 'foo-x': None, 'bar-x': { 'baz-x': '' }, 'bof-x': 'Hello', 'qux-x': False }, ] tags = create_schema(template, root_path, 'flimp_test', 'flimp unit-test suite') self.assertEqual(4, len(tags)) fom_class = create_class(tags) push_to_fluiddb(template, root_path, fom_class, None, 'flimp_test', allowEmpty=False) # check an object was created result = Object.filter("has %s/bof-x" % root_path) self.assertEqual(1, len(result)) tag_paths = result[0].tag_paths self.assertEqual(3, len(tag_paths))
def test_process(self): filename = GOOD_JSON name = str(uuid.uuid4()) root_path = 'test/%s' % name desc = 'flimp unit-test suite' about = None output = process(filename, root_path, name, desc, about) self.assertEqual("Processed 2 records", output) # check we have two objects each with three tags attached result = Object.filter('has %s/foo' % root_path) self.assertEqual(2, len(result)) for obj in result: self.assertEqual(1, len(obj.tag_paths)) # make sure the function returns expected text # preview preview = process(filename, root_path, name, desc, about, preview=True) expected = """Preview of processing %r The following namespaces/tags will be generated. test/%s/foo 2 records will be imported into FluidDB """ % (filename, name) self.assertEqual(expected, preview) # check check = process(filename, root_path, name, desc, about, check=True) self.assertEqual("Validation passed ok", check)
def test_process_data_list(self): # setup raw_data = [ { 'foo': 'a', 'bar': { 'baz': 'b' }, 'bof': 'c' }, { 'foo': 'x', 'bar': { 'baz': 'y' }, 'bof': None }, ] name = str(uuid.uuid4()) root_path = 'test/%s' % name desc = 'flimp unit-test suite' about = None allowEmpty = True process_data_list(raw_data, root_path, name, desc, about, allowEmpty) # check we have two objects each with three tags attached result = Object.filter('has %s/foo' % root_path) self.assertEqual(2, len(result)) for obj in result: self.assertEqual(3, len(obj.tag_paths))
def testFilterResultType(self): class UserClass(Object): username = tag_value('fluiddb/users/username') name = tag_value('fluiddb/users/name') response = {'results': {'id': {'05eee31e-fbd1-43cc-9500-0469707a9bc3': { 'fluiddb/users/username':{'value': 'ntoll'}, 'fluiddb/users/name':{'value': 'Nicholas'}, 'fluiddb/about': {'value': 'Object about the user ntoll'} }}}} self.db.add_resp(200, 'application/json', json.dumps(response)) results = Object.filter('fluiddb/users/username = "******"', result_type=UserClass) self.assertEquals(1, len(results)) # the _dirty_fields have not been added to self.assertEquals(0, len(results[0]._dirty_fields)) self.assertEquals(self.db.reqs[0], ( 'GET', '/values', NO_CONTENT, (('query', 'fluiddb/users/username = "******"'), ('tag', 'fluiddb/about'), ('tag', 'fluiddb/users/username'), ('tag', 'fluiddb/users/name')), None)) user = results[0] self.assertTrue('fluiddb/about' in user._cache) self.assertTrue('fluiddb/users/name' in user._cache) self.assertTrue('fluiddb/users/username' in user._cache) self.assertEquals('Object about the user ntoll', user.about) self.assertEquals('Nicholas', user.name) self.assertEquals('ntoll', user.username)
def testFilter(self): self.db.add_resp(200, 'application/json', '{"ids": ["466"]}') results = Object.filter('fluiddb/users/username = "******"') self.assertEquals(1, len(results)) # the _dirty_fields have not been added to self.assertEquals(0, len(results[0]._dirty_fields)) self.assertEquals(self.db.reqs[0], ('GET', '/objects', NO_CONTENT, { 'query': 'fluiddb/users/username = "******"' }, None)) self.db.add_resp(200, 'application/vnd.fluiddb.value+json', u'test') self.assertEquals(u'test', results[0].about) self.assertEquals( self.db.reqs[1], ('GET', u'/objects/466/fluiddb/about', NO_CONTENT, None, None))
def testFilterResultType(self): class UserClass(Object): username = tag_value('fluiddb/users/username') name = tag_value('fluiddb/users/name') response = { 'results': { 'id': { '05eee31e-fbd1-43cc-9500-0469707a9bc3': { 'fluiddb/users/username': { 'value': 'ntoll' }, 'fluiddb/users/name': { 'value': 'Nicholas' }, 'fluiddb/about': { 'value': 'Object about the user ntoll' } } } } } self.db.add_resp(200, 'application/json', json.dumps(response)) results = Object.filter('fluiddb/users/username = "******"', result_type=UserClass) self.assertEquals(1, len(results)) # the _dirty_fields have not been added to self.assertEquals(0, len(results[0]._dirty_fields)) self.assertEquals(self.db.reqs[0], ('GET', '/values', NO_CONTENT, (('query', 'fluiddb/users/username = "******"'), ('tag', 'fluiddb/about'), ('tag', 'fluiddb/users/username'), ('tag', 'fluiddb/users/name')), None)) user = results[0] self.assertTrue('fluiddb/about' in user._cache) self.assertTrue('fluiddb/users/name' in user._cache) self.assertTrue('fluiddb/users/username' in user._cache) self.assertEquals('Object about the user ntoll', user.about) self.assertEquals('Nicholas', user.name) self.assertEquals('ntoll', user.username)
def testFilter(self): self.db.add_resp(200, 'application/json', '{"ids": ["466"]}') results = Object.filter('fluiddb/users/username = "******"') self.assertEquals(1, len(results)) # the _dirty_fields have not been added to self.assertEquals(0, len(results[0]._dirty_fields)) self.assertEquals(self.db.reqs[0], ( 'GET', '/objects', NO_CONTENT, {'query': 'fluiddb/users/username = "******"'}, None)) self.db.add_resp(200, 'application/vnd.fluiddb.value+json', u'test') self.assertEquals(u'test', results[0].about) self.assertEquals(self.db.reqs[1], ( 'GET', u'/objects/466/fluiddb/about', NO_CONTENT, None, None))
def testQueryWithUnicodeTagName(self): """ Make sure that a query using a tag whose name contains non-ASCII unicode characters works correctly. See https://bugs.edge.launchpad.net/fluiddb/+bug/681354 for the reason this is tested. """ # Use the testuser1 root namespace. ns = Namespace('testuser1') # Umlauts FTW! tag_name = u'C\xfc\xe4h' # Create the tag. tag = ns.create_tag(tag_name, 'Used for testing purposes', False) try: # Run a query that uses the tag. If we haven't fixed the bug, # FluidDB will hang at this point. result = Object.filter('has testuser1/%s' % tag_name) # Check the result is an empty list (i.e., no results found). self.assertEqual([], result) finally: # Clean up the tag. tag.delete()