def testTags(self): o = Object() o.uid = '3' self.db.add_resp(200, 'application/json', '{"tagPaths": []}') self.assertEqual(o.tag_paths, []) self.assertEqual(self.db.reqs[0], ('GET', '/objects/3', NO_CONTENT, { 'showAbout': False }, None)) self.db.add_resp(200, 'application/json', '{"tagPaths": []}') self.assertEquals(o.tags, []) self.assertEqual(self.db.reqs[1], ('GET', '/objects/3', NO_CONTENT, { 'showAbout': False }, None)) self.db.add_resp(200, 'application/json', '{"tagPaths": ["test\\/fomtest"]}') self.assertTrue('test/fomtest' in o.tag_paths) self.assertEqual(self.db.reqs[2], ('GET', '/objects/3', NO_CONTENT, { 'showAbout': False }, None)) self.db.add_resp(200, 'application/json', '{"tagPaths": ["test\\/fomtest"]}') self.assertEquals(o.tags[0].path, 'test/fomtest') self.assertEqual(self.db.reqs[3], ('GET', '/objects/3', NO_CONTENT, { 'showAbout': False }, None))
def testGet(self): o = Object() o.uid = '9' o.get(u'test/test') self.assertEqual( self.last, ('GET', u'/objects/9/test/test', NO_CONTENT, None, None))
def testSetPrimitive(self): o = Object() o.uid = '9' for i, value in enumerate((None, True, False, 123, 45.67, 'hey')): o.set(u'test/test', value) self.assertEqual(value, o._cache[u'test/test']) self.assertEqual( self.db.reqs[i], ('PUT', u'/objects/9/test/test', value, None, None))
def testCreate(self): """Creating an object works! """ o = Object() self.db.add_resp(201, 'application/json', '{"id": "9d4", "URI": "https"}') o.create() self.assert_(o.uid) self.assertEqual(self.last, ('POST', '/objects', {}, None, None))
def testSetOpaque(self): o = Object() o.uid = '7' o.set(u'test/test', 'xyz', 'application/bananas') self.assertEqual(self.last, ( 'PUT', u'/objects/7/test/test', 'xyz', None, 'application/bananas'))
def testGet(self): o = Object() o.uid = '9' o.get(u'test/test') self.assertEqual(self.last, ( 'GET', u'/objects/9/test/test', NO_CONTENT, None, None))
def testHas(self): o = Object() o.uid = '0' self.db.add_resp(404, 'application/json', 'TNoInstanceOnObject') self.assertFalse(o.has('test/fomtest')) self.assertEqual( self.db.reqs[0], ('HEAD', '/objects/0/test/fomtest', NO_CONTENT, None, None)) self.db.add_resp(200, 'application/vnd.fluiddb.value+json', '') self.assertTrue(o.has('test/fomtest')) self.assertEqual( self.db.reqs[1], ('HEAD', '/objects/0/test/fomtest', NO_CONTENT, None, None))
def testSetPrimitive(self): o = Object() o.uid = '9' for i, value in enumerate( (None, True, False, 123, 45.67, 'hey')): o.set(u'test/test', value) self.assertEqual(value, o._cache[u'test/test']) self.assertEqual(self.db.reqs[i], ( 'PUT', u'/objects/9/test/test', value, None, None))
def testCreate(self): """Creating an object works! """ o = Object() self.db.add_resp(201, 'application/json', '{"id": "9d4", "URI": "https"}') o.create() self.assert_(o.uid) self.assertEqual(self.last, ( 'POST', '/objects', {}, None, None))
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 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 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 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 get_object(uuid=None, about=None): """ Returns the referenced object or a new object if uuid or about are not given. """ logger.info("Getting object") if uuid: logger.info("Object with uuid %r" % uuid) return Object(uid=uuid) elif about: logger.info("Object with about tag value: %r" % about) return Object(about=about) else: o = Object() o.create() logger.info("New object with uuid %r" % o.uid) return o
def testHas(self): o = Object() o.uid = '0' self.db.add_resp(404, 'application/json', 'TNoInstanceOnObject') self.assertFalse(o.has('test/fomtest')) self.assertEqual(self.db.reqs[0], ( 'HEAD', '/objects/0/test/fomtest', NO_CONTENT, None, None)) self.db.add_resp(200, 'application/vnd.fluiddb.value+json', '') self.assertTrue(o.has('test/fomtest')) self.assertEqual(self.db.reqs[1], ( 'HEAD', '/objects/0/test/fomtest', NO_CONTENT, None, None))
def testCreateWithAbout(self): """Make sure creating an object with an about tag value works """ self.db.add_resp(201, 'application/json', '{"id": "9d4", "URI": "https"}') o = Object(about="foo") self.assert_(o.uid) self.assertEqual(self.last, ('POST', '/objects', { u'about': 'foo' }, None, None))
def testTags(self): o = Object() o.uid = '3' self.db.add_resp(200, 'application/json', '{"tagPaths": []}') self.assertEqual(o.tag_paths, []) self.assertEqual(self.db.reqs[0], ( 'GET', '/objects/3', NO_CONTENT, {'showAbout': False}, None)) self.db.add_resp(200, 'application/json', '{"tagPaths": []}') self.assertEquals(o.tags, []) self.assertEqual(self.db.reqs[1], ( 'GET', '/objects/3', NO_CONTENT, {'showAbout': False}, None)) self.db.add_resp(200, 'application/json', '{"tagPaths": ["test\\/fomtest"]}') self.assertTrue('test/fomtest' in o.tag_paths) self.assertEqual(self.db.reqs[2], ( 'GET', '/objects/3', NO_CONTENT, {'showAbout': False}, None)) self.db.add_resp(200, 'application/json', '{"tagPaths": ["test\\/fomtest"]}') self.assertEquals(o.tags[0].path, 'test/fomtest') self.assertEqual(self.db.reqs[3], ( 'GET', '/objects/3', NO_CONTENT, {'showAbout': False}, None))
def generateNamespaceData(username): """Generate random namespace, tag and tag value data. @param username: The username to connect as. """ rootNamespace = Namespace(username) name = 'namespace-%s' % ''.join( choice(ascii_letters) for i in xrange(randint(8, 12))) namespace = rootNamespace.create_namespace(name, 'A child namespace') logQueue.put(('CREATE_NAMESPACE path=%s/%s', (username, name))) countQueue.put(1) for tagName in WORDS: tag = namespace.create_tag(tagName, 'A tag', False) logQueue.put(('CREATE_TAG path=%s/%s/%s', (username, name, tagName))) countQueue.put(1) for i in range(randint(2, 20)): value = getRandomValue() about = 'namespace %s tag %s count %d' % (name, tagName, i) obj = Object(uuid4(), about=about) obj.set(tag.path, value) logQueue.put(('CREATE_TAG_VALUE value=%s tag=%s/%s/%s', (value, username, name, tagName))) countQueue.put(1)
def generateNamespaceData(username): """Generate random namespace, tag and tag value data. @param username: The username to connect as. """ rootNamespace = Namespace(username) name = 'namespace-%s' % ''.join(choice(ascii_letters) for i in xrange(randint(8, 12))) namespace = rootNamespace.create_namespace(name, 'A child namespace') logQueue.put(('CREATE_NAMESPACE path=%s/%s', (username, name))) countQueue.put(1) for tagName in WORDS: tag = namespace.create_tag(tagName, 'A tag', False) logQueue.put(('CREATE_TAG path=%s/%s/%s', (username, name, tagName))) countQueue.put(1) for i in range(randint(2, 20)): value = getRandomValue() about = 'namespace %s tag %s count %d' % (name, tagName, i) obj = Object(uuid4(), about=about) obj.set(tag.path, value) logQueue.put(('CREATE_TAG_VALUE value=%s tag=%s/%s/%s', (value, username, name, tagName))) countQueue.put(1)
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()
def testEquality(self): b = Object() b.uid = '7' a = Object(b.uid) self.assertEqual(a, b)
def testSetOpaque(self): o = Object() o.uid = '7' o.set(u'test/test', 'xyz', 'application/bananas') self.assertEqual(self.last, ('PUT', u'/objects/7/test/test', 'xyz', None, 'application/bananas'))
def testNew(self): """Make sure a new object is in the correct state """ o = Object() self.assertTrue(o.uid is None)