def testUniqueFileIDAndValueID(self): """ An C{IntegrityError} is raised if an L{OpaqueValueLink} with the same fileID and valueID is added to the database. Duplicated C{tag_id} or C{file_id} can be added as long as the pair is unique. """ user = createUser(u'name', u'password', u'User', u'*****@*****.**') user.namespaceID = createNamespace(user, user.username, None).id tag = createTag(user, user.namespace, u'tag') value1 = TagValue(user.id, tag.id, uuid4(), None) value2 = TagValue(user.id, tag.id, uuid4(), None) self.store.add(value1) self.store.add(value2) self.store.flush() fileID1 = 'f' * 64 fileID2 = '0' * 64 self.store.add(OpaqueValue(fileID1, 'content1')) self.store.add(OpaqueValue(fileID2, 'content2')) # Add an initial link self.store.add(OpaqueValueLink(value1.id, fileID1)) # Add link with the same fileID but different valueID. It should work. self.store.add(OpaqueValueLink(value2.id, fileID1)) # Add link with the same valueID but different fileID. It should work. self.store.add(OpaqueValueLink(value1.id, fileID2)) self.store.flush() # Add link with same fileID and valueID. It should fail. self.store.add(OpaqueValueLink(value1.id, fileID1)) self.assertRaises(IntegrityError, self.store.flush) self.store.rollback()
def testGetTagValues(self): """ L{getTagValues} returns all L{TagValue}s in the database, by default. """ objectID1 = uuid4() objectID2 = uuid4() user = createUser(u'username', u'password', u'User', u'*****@*****.**') user.namespaceID = createNamespace(user, user.username, None).id tag = createTag(user, user.namespace, u'name') value1 = self.store.add(TagValue(user.id, tag.id, objectID1, None)) value2 = self.store.add(TagValue(user.id, tag.id, objectID2, 42)) self.assertEqual(sorted([value1, value2]), sorted(getTagValues()))
def testUniqueTagAndObjectID(self): """ An C{IntegrityError} is raised if a L{TagValue} with duplicate L{Tag.id} and object ID values is added to the database. """ user = createUser(u'name', u'password', u'User', u'*****@*****.**') user.namespaceID = createNamespace(user, user.username, None).id tag = createTag(user, user.namespace, u'tag') objectID = uuid4() self.store.add(TagValue(user.id, tag.id, objectID, None)) self.store.flush() self.store.add(TagValue(user.id, tag.id, objectID, None)) self.assertRaises(IntegrityError, self.store.flush) self.store.rollback()
def testGetTagValuesWithTagIDsAndObjectIDs(self): """ When C{(Tag.id, object ID)} 2-tuples are provided L{getTagValues} returns matching L{TagValue}s. """ objectID1 = uuid4() objectID2 = uuid4() user = createUser(u'username', u'password', u'User', u'*****@*****.**') user.namespaceID = createNamespace(user, user.username, None).id tag = createTag(user, user.namespace, u'name') value = self.store.add(TagValue(user.id, tag.id, objectID1, None)) self.store.add(TagValue(user.id, tag.id, objectID2, 42)) self.assertEqual(value, getTagValues([(objectID1, tag.id)]).one())
def testUnicodeSetValue(self): """A L{TagValue} can store a C{list} of C{unicode} values.""" objectID = uuid4() user = createUser(u'username', u'password', u'User', u'*****@*****.**') namespace = createNamespace(user, u'name') tag = createTag(user, namespace, u'tag') self.store.add(TagValue(user.id, tag.id, objectID, [u'foo', u'bar']))
def testFloatValue(self): """A L{TagValue} can store a C{float} value.""" objectID = uuid4() user = createUser(u'username', u'password', u'User', u'*****@*****.**') namespace = createNamespace(user, u'name') tag = createTag(user, namespace, u'tag') self.store.add(TagValue(user.id, tag.id, objectID, 42.1))
def testValuesWithObjectIDs(self): """ A L{TagValueCollection} filtered by object ID only contains values for L{Tag}s with a matching object ID. """ objectID1 = uuid4() objectID2 = uuid4() user = createUser(u'username', u'password', u'User', u'*****@*****.**') namespace = createNamespace(user, u'name') tag = createTag(user, namespace, u'tag') tagValue1 = TagValue(user.id, tag.id, objectID1, None) tagValue2 = TagValue(user.id, tag.id, objectID2, 42) self.store.add(tagValue1) self.store.add(tagValue2) collection = TagValueCollection(objectIDs=[objectID1]) self.assertEqual([(tag, tagValue1)], list(collection.values()))
def testValuesWithPaths(self): """ A L{TagValueCollection} filtered by path only contains values for L{Tag}s with a matching path. """ objectID = uuid4() user = createUser(u'username', u'password', u'User', u'*****@*****.**') namespace = createNamespace(user, u'name') tag1 = createTag(user, namespace, u'tag1') tag2 = createTag(user, namespace, u'tag2') self.store.add(TagValue(user.id, tag1.id, objectID, None)) self.store.add(TagValue(user.id, tag2.id, objectID, 42)) collection = TagValueCollection(paths=[u'name/tag1']) (tag, tagValue) = collection.values().one() self.assertEqual(objectID, tagValue.objectID) self.assertEqual(u'name/tag1', tag.path) self.assertEqual(None, tagValue.value)
def testValues(self): """ L{TagValueCollection.values} returns a L{ResultSet} that yields all available L{Tag} values, when no filtering has been applied. """ objectID = uuid4() user = createUser(u'username', u'password', u'User', u'*****@*****.**') namespace = createNamespace(user, u'name') tag1 = createTag(user, namespace, u'tag1') tag2 = createTag(user, namespace, u'tag2') tagValue1 = TagValue(user.id, tag1.id, objectID, 42) tagValue2 = TagValue(user.id, tag2.id, objectID, u'foo') self.store.add(tagValue1) self.store.add(tagValue2) collection = TagValueCollection() self.assertEqual(sorted([(tag1, tagValue1), (tag2, tagValue2)]), sorted(collection.values()))
def testUnicodeValueWithIllegalXMLCharacter(self): """ A L{TagValue} can store a C{unicode} value even if it contains illegal XML characters. """ objectID = uuid4() user = createUser(u'username', u'password', u'User', u'*****@*****.**') namespace = createNamespace(user, u'name') tag = createTag(user, namespace, u'tag') self.store.add(TagValue(user.id, tag.id, objectID, u'foo \uFFFE'))
def testValuesWithCreatedBeforeTime(self): """ A L{TagValueCollection} filtered by creation time only contains values for L{TagValue}s created before the specified time. """ objectID = uuid4() user = createUser(u'username', u'password', u'User', u'*****@*****.**') namespace = createNamespace(user, u'name') tag1 = createTag(user, namespace, u'tag1') tag2 = createTag(user, namespace, u'tag2') self.store.add(TagValue(user.id, tag1.id, objectID, None)) value = self.store.add(TagValue(user.id, tag2.id, objectID, 42)) value.creationTime = datetime.utcnow() - timedelta(hours=24) collection = TagValueCollection(createdBeforeTime=datetime.utcnow() - timedelta(hours=12)) (tag, tagValue) = collection.values().one() self.assertEqual(objectID, tagValue.objectID) self.assertEqual(u'name/tag2', tag.path) self.assertEqual(42, tagValue.value)
def testBinaryValue(self): """A L{TagValue} can store a binary value.""" objectID = uuid4() user = createUser(u'username', u'password', u'User', u'*****@*****.**') namespace = createNamespace(user, u'name') tag = createTag(user, namespace, u'tag') self.store.add( TagValue(user.id, tag.id, objectID, { 'mime-type': 'text/html', 'size': 123 }))
def testRemoveOpaqueValueRaisesError(self): """ If an L{OpaqueValue} is removed while there is an existent L{OpaqueValueLink} entry in the database, an L{IntegrityError} is raised. """ user = createUser(u'name', u'password', u'User', u'*****@*****.**') user.namespaceID = createNamespace(user, user.username, None).id tag = createTag(user, user.namespace, u'tag') value = TagValue(user.id, tag.id, uuid4(), None) self.store.add(value) self.store.flush() fileID = 'f' * 64 opaque = self.store.add(OpaqueValue(fileID, 'content')) self.store.add(OpaqueValueLink(value.id, fileID)) self.store.remove(opaque) self.assertRaises(IntegrityError, self.store.flush) self.store.rollback()
def testRemoveTagValueRemovesLink(self): """ If a L{TagValue} referenced by a L{OpaqueValueLink} is deleted, the referenced link is removed too. """ user = createUser(u'name', u'password', u'User', u'*****@*****.**') user.namespaceID = createNamespace(user, user.username, None).id tag = createTag(user, user.namespace, u'tag') value = TagValue(user.id, tag.id, uuid4(), None) self.store.add(value) self.store.flush() fileID = 'f' * 64 self.store.add(OpaqueValue(fileID, 'content')) self.store.add(OpaqueValueLink(value.id, fileID)) self.store.remove(value) self.store.flush() result = self.store.find(OpaqueValueLink, OpaqueValueLink.fileID == fileID).one() self.assertIdentical(None, result)