Ejemplo n.º 1
0
    def testGetFromObjectsWithBinaryValue(self):
        """
        L{RecentActivityAPI.getForObjects} returns binary values using the
        expected format.
        """
        tagValues = TagValueAPI(self.user)
        objectID = uuid4()

        values = {
            objectID: {
                u'user/tag': {
                    u'mime-type': u'text/plain',
                    u'contents': 'Hello, world!'
                }
            }
        }
        tagValues.set(values)
        self.store.commit()

        value = {u'value-type': u'text/plain', u'size': 13}
        expected = [(u'user/tag', objectID, None, value, u'user')]

        result = self.recentActivity.getForObjects([objectID])

        # Remove the creation times from the result, with the order is enough.
        result = [(path, objectID, about, value, username)
                  for path, objectID, about, value, username, time in result]
        self.assertEqual(expected, result)
Ejemplo n.º 2
0
def extractTrendingHashtags(store, limit=10, duration=None):
    """Extract information about trending hashtags and store it in FluidDB.

    @param store: The storm store to query and to save our result to.
    @param limit: Optionally, the number of objects to retrieve.
    @param duration: Optionally, the recent time period to look at when
        determining which hashtags are trending.  Default is 28 days.

    The storm query below results in SQL like:

        SELECT COUNT(DISTINCT comments.object_id) AS count,
               about_tag_values.value,
               array_agg(ROW(comments.username, comments.creation_time))
        FROM about_tag_values, comment_object_link, comments
        WHERE about_tag_values.value LIKE '#%' AND
              about_tag_values.object_id = comment_object_link.object_id AND
              comments.object_id = comment_object_link.comment_id AND
              comments.creation_time >= '2012-11-09 07:42:40'::TIMESTAMP AND
              CHAR_LENGTH(about_tag_values.value) >= 2
        GROUP BY about_tag_values.value
        ORDER BY count DESC
        LIMIT 10
    """
    duration = timedelta(days=28) if duration is None else duration
    startTime = datetime.utcnow() - duration
    count = Alias(Count(Comment.objectID, distinct=True))
    result = store.find(
        (count, AboutTagValue.value,
         Func('array_agg', Row(Comment.username, Comment.creationTime))),
        Like(AboutTagValue.value,
             u'#%'), AboutTagValue.objectID == CommentObjectLink.objectID,
        Comment.objectID == CommentObjectLink.commentID,
        Comment.creationTime >= startTime,
        Func('CHAR_LENGTH', AboutTagValue.value) >= 2)
    result.group_by(AboutTagValue.value)
    result.order_by(Desc(count))
    result.config(limit=limit)

    data = [{
        'count': count,
        'usernames': _sortUsernames(usernames),
        'value': hashtag
    } for count, hashtag, usernames in result]

    user = getUser(u'fluidinfo.com')
    tagValues = TagValueAPI(user)
    objectID = ObjectAPI(user).create(u'fluidinfo.com')
    tagValues.set(
        {objectID: {
            u'fluidinfo.com/trending-hashtags': json.dumps(data)
        }})
    store.commit()
Ejemplo n.º 3
0
    def testDeleteTagWithData(self):
        """
        L{FacadeTagMixin.deleteTag} removes L{TagValue}s associated with the
        deleted L{Tag}.
        """
        objectID = uuid4()
        TagAPI(self.user).create([(u'username/tag', u'A tag.')])
        tagValues = TagValueAPI(self.user)
        tagValues.set({objectID: {u'username/tag': 42}})
        self.store.commit()
        with login(u'username', uuid4(), self.transact) as session:
            yield self.facade.deleteTag(session, u'username/tag')

        self.store.rollback()
        self.assertEqual({}, tagValues.get(objectIDs=[objectID],
                                           paths=[u'username/tag']))
Ejemplo n.º 4
0
class CachingTagValueAPI(object):
    """The public API to cached L{TagValue}-related logic in the model.

    @param user: The L{User} to perform operations on behalf of.
    """

    def __init__(self, user):
        self._api = TagValueAPI(user, factory=CachingAPIFactory())
        self._user = user

    def get(self, objectIDs, paths=None):
        """See L{TagValueAPI.get}."""
        return self._api.get(objectIDs, paths)

    def set(self, values):
        """See L{TagValueAPI.set}."""
        result = self._api.set(values)
        RecentObjectActivityCache().clear(values.keys())
        RecentUserActivityCache().clear([self._user.username])
        return result

    def delete(self, values):
        """See L{TagValueAPI.delete}."""
        if isgenerator(values):
            values = list(values)
        result = self._api.delete(values)
        objectIDs = [objectID for objectID, path in values]
        RecentObjectActivityCache().clear(objectIDs)
        RecentUserActivityCache().clear([self._user.username])
        return result
Ejemplo n.º 5
0
    def testGetFromObjectsWithBinaryValue(self):
        """
        L{RecentActivityAPI.getForObjects} returns binary values using the
        expected format.
        """
        tagValues = TagValueAPI(self.user)
        objectID = uuid4()

        values = {objectID: {u'user/tag': {u'mime-type': u'text/plain',
                                           u'contents': 'Hello, world!'}}}
        tagValues.set(values)
        self.store.commit()

        value = {u'value-type': u'text/plain',
                 u'size': 13}
        expected = [(u'user/tag', objectID, None, value, u'user')]

        result = self.recentActivity.getForObjects([objectID])

        # Remove the creation times from the result, with the order is enough.
        result = [(path, objectID, about, value, username)
                  for path, objectID, about, value, username, time in result]
        self.assertEqual(expected, result)
Ejemplo n.º 6
0
    def testGetRecentUserActivityForQueryWithObjectsNotUsers(self):
        """
        L{FacadeRecentActivityMixin.getRecentUserActivityForQuery} returns an
        empty result if the objects returned by the query are not users.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/following': u'A'}})
        self.store.commit()

        runDataImportHandler(self.client.url)

        with login(self.user.username, uuid4(), self.transact) as session:
            result = yield self.facade.getRecentUserActivityForQuery(
                session, u'has user/following')

            # Remove the creation times from the result.
            for item in result:
                del item['updated-at']

            self.assertEqual([], result)
Ejemplo n.º 7
0
    def testGetRecentActivityForQuery(self):
        """
        L{FacadeRecentActivityMixin.getRecentActivityForQuery} returns a
        C{dict} with information about the recent tag values on the objects
        returned by the given query.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/following': u'A'}})
        self.store.commit()

        runDataImportHandler(self.client.url)

        expected = [{'about': u'object1',
                     'id': str(objectID1),
                     'tag': u'user/following',
                     'username': u'username',
                     'value': u'A'},
                    {'about': u'object1',
                     'id': str(objectID1),
                     'tag': u'fluiddb/about',
                     'username': u'fluiddb',
                     'value': u'object1'}]

        with login(self.user.username, uuid4(), self.transact) as session:
            result = yield self.facade.getRecentActivityForQuery(
                session, u'has user/following')

            # Remove the creation times from the result.
            for item in result:
                del item['updated-at']

            self.assertEqual(expected, result)
Ejemplo n.º 8
0
"""Creates fluiddb/users/role for all the users."""
from fluiddb.application import setConfig, setupConfig
from fluiddb.scripts.commands import setupStore
from fluiddb.data.user import getUsers
from fluiddb.model.user import getUser
from fluiddb.model.value import TagValueAPI

if __name__ == '__main__':
    store = setupStore('postgres:///fluidinfo', 'main')
    setConfig(setupConfig(None))
    print __doc__

    tagValues = TagValueAPI(getUser(u'fluiddb'))

    for user in list(getUsers()):
        print 'Adding role for', user.username
        values = {user.objectID: {u'fluiddb/users/role': unicode(user.role)}}
        tagValues.set(values)
        store.commit()
Ejemplo n.º 9
0
    def testGetForUsersReturnsOnlyAllowedTags(self):
        """
        L{SecureRecentActivityAPI.getForUser} returns all the tags for the
        superuser.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')
        objectID2 = uuid4()

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag1': u'A'}})
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag2': u'B'}})
        self.store.commit()

        UserAPI().create([(u'user2', u'secret', u'User', u'*****@*****.**')])
        tagValues = TagValueAPI(getUser(u'user2'))

        tagValues.set({objectID1: {u'user2/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user2/tag2': u'D'}})
        self.store.commit()

        UserAPI().create([(u'user3', u'secret', u'User', u'*****@*****.**')])
        tagValues = TagValueAPI(getUser(u'user3'))

        tagValues.set({objectID1: {u'user3/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user3/tag2': u'D'}})
        self.store.commit()

        self.permissions.set([(u'user/tag2', Operation.READ_TAG_VALUE,
                               Policy.OPEN, [u'user']),
                              (u'user2/tag2', Operation.READ_TAG_VALUE,
                               Policy.CLOSED, [])])

        expected = [
            (u'user2/tag2', objectID2, None, u'D', u'user2'),
            (u'user2/tag1', objectID1, u'object1', u'C', u'user2'),
            (u'user/tag2', objectID1, u'object1', u'B', u'user'),
            (u'user/tag1', objectID1, u'object1', u'A', u'user')]

        result = self.recentActivity.getForUsers([u'user', u'user2'])
        # Remove the creation times from the result, with the order is enough.
        result = [(path, objectID, about, value, username)
                  for path, objectID, about, value, username, time in result]
        self.assertEqual(expected, result)
Ejemplo n.º 10
0
    def testGetForObjectsReturnsOnlyAllowedTags(self):
        """
        L{SecureRecentActivityAPI.getForObjects} will return all the tags for
        which the user has C{Operation.READ_TAG_VALUE} permissions, but not
        those for which the user doesn't have.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')
        objectID2 = uuid4()

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag1': u'A'}})
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag2': u'B'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user/tag2': u'D'}})
        self.store.commit()
        tagValues.set({uuid4(): {u'user/tag1': u'E'}})
        self.store.commit()
        tagValues.set({uuid4(): {u'user/tag2': u'F'}})
        self.store.commit()

        self.permissions.set([(u'user/tag2', Operation.READ_TAG_VALUE,
                               Policy.OPEN, [u'user'])])

        expected = [
            (u'user/tag1', objectID2, None, u'C', u'user'),
            (u'user/tag1', objectID1, u'object1', u'A', u'user'),
            (u'fluiddb/about', objectID1, u'object1', u'object1', u'fluiddb')]

        result = self.recentActivity.getForObjects([objectID1, objectID2])

        # Remove the creation times from the result, with the order is enough.
        result = [(path, objectID, about, value, username)
                  for path, objectID, about, value, username, time in result]
        self.assertEqual(expected, result)
Ejemplo n.º 11
0
            if value is not None:
                print 'ERROR about tag value already exists. Deleting it.'
                # Wipe all the tag values for that objectID
                result2 = store.find(TagValue,
                                     TagValue.objectID == value.objectID)
                result2.remove()
                result.remove()
            # Update the about value.
            value = getAboutTagValues(objectIDs=[tag.objectID]).one()
            if value is not None:
                value.value = newAbout
            else:
                createAboutTagValue(tag.objectID, newAbout)

        print 'Fixing system values'
        tagValues.set(systemValues)

        print 'Touching objects.'
        result = store.find(TagValue.objectID,
                            TagValue.tagID.is_in(currentIDs))
        touchObjects(list(result.config(distinct=True)))
        touchObjects(systemValues.keys())

        i += BATCH_SIZE
        currentIDs = tagsToFix[i:i + BATCH_SIZE]

    print 'Fixing namespaces.'
    i = 0
    currentIDs = namespacesToFix[i:i + BATCH_SIZE]
    while currentIDs:
        systemValues = {}
Ejemplo n.º 12
0
    def testGetForObjectsReturnsOnlyAllowedTags(self):
        """
        L{SecureRecentActivityAPI.getForObjects} will return all the tags for
        which the user has C{Operation.READ_TAG_VALUE} permissions, but not
        those for which the user doesn't have.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')
        objectID2 = uuid4()

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag1': u'A'}})
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag2': u'B'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user/tag2': u'D'}})
        self.store.commit()
        tagValues.set({uuid4(): {u'user/tag1': u'E'}})
        self.store.commit()
        tagValues.set({uuid4(): {u'user/tag2': u'F'}})
        self.store.commit()

        self.permissions.set([(u'user/tag2', Operation.READ_TAG_VALUE,
                               Policy.OPEN, [u'anon'])])

        expected = [(u'user/tag1', objectID2, None, u'C', u'user'),
                    (u'user/tag1', objectID1, u'object1', u'A', u'user'),
                    (u'fluiddb/about', objectID1, u'object1', u'object1',
                     u'fluiddb')]

        result = self.recentActivity.getForObjects([objectID1, objectID2])

        # Remove the creation times from the result, with the order is enough.
        result = [(path, objectID, about, value, username)
                  for path, objectID, about, value, username, time in result]
        self.assertEqual(expected, result)
Ejemplo n.º 13
0
    def testGetForUsersReturnsOnlyAllowedTags(self):
        """
        L{SecureRecentActivityAPI.getForUser} returns all the tags for the
        superuser.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')
        objectID2 = uuid4()

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag1': u'A'}})
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag2': u'B'}})
        self.store.commit()

        UserAPI().create([(u'user2', u'secret', u'User', u'*****@*****.**')])
        tagValues = TagValueAPI(getUser(u'user2'))

        tagValues.set({objectID1: {u'user2/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user2/tag2': u'D'}})
        self.store.commit()

        UserAPI().create([(u'user3', u'secret', u'User', u'*****@*****.**')])
        tagValues = TagValueAPI(getUser(u'user3'))

        tagValues.set({objectID1: {u'user3/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user3/tag2': u'D'}})
        self.store.commit()

        self.permissions.set([
            (u'user/tag2', Operation.READ_TAG_VALUE, Policy.OPEN, [u'user']),
            (u'user2/tag2', Operation.READ_TAG_VALUE, Policy.CLOSED, [])
        ])

        expected = [(u'user2/tag2', objectID2, None, u'D', u'user2'),
                    (u'user2/tag1', objectID1, u'object1', u'C', u'user2'),
                    (u'user/tag2', objectID1, u'object1', u'B', u'user'),
                    (u'user/tag1', objectID1, u'object1', u'A', u'user')]

        result = self.recentActivity.getForUsers([u'user', u'user2'])
        # Remove the creation times from the result, with the order is enough.
        result = [(path, objectID, about, value, username)
                  for path, objectID, about, value, username, time in result]
        self.assertEqual(expected, result)
Ejemplo n.º 14
0
class DataImportHandlerTest(FluidinfoTestCase):

    resources = [('client', IndexResource()),
                 ('config', ConfigResource()),
                 ('store', DatabaseResource())]

    def setUp(self):
        super(DataImportHandlerTest, self).setUp()
        createSystemData()
        UserAPI().create([(u'user', u'secret', u'User', u'*****@*****.**')])
        self.user = getUser(u'user')
        self.objects = ObjectAPI(self.user)
        self.values = TagValueAPI(self.user)
        self.index = ObjectIndex(self.client)

    @inlineCallbacks
    def assertQuery(self, objectIDs, query):
        """Asserts if a query to fluidinfo returns the expected results.

        @param objectIDs: A sequence with the expected object IDs for the
            query.
        @param query: The fluidinfo query to check.
        """
        query = parseQuery(query)
        results = yield self.index.search(query)
        self.assertEqual(set(objectIDs), results)

    @inlineCallbacks
    def testImportObjectWithStringValue(self):
        """The Data Import Handler correctly imports C{string} values."""
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': u'string'}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag = "string"')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithStringValueInvalidXML(self):
        """
        The Data Import Handler correctly imports C{string} values, including
        those that contain invalid XML characters.
        """
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': u'foo \uFFFE'}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag = "foo \uFFFE"')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithEmptyStringValue(self):
        """The Data Import Handler correctly imports empty C{string} values."""
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': u''}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag = ""')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithQuotesValue(self):
        """
        The Data Import Handler correctly imports C{string} values with single
        quotes.
        """
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': u'hello\'world'}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag = "hello\'world"')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithDoubleQuotesValue(self):
        """
        The Data Import Handler correctly imports C{string} values with double
        quotes.
        """
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': u'hello\"world'}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag = "hello\\\"world"')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithCommaAndParens(self):
        """
        The Data Import Handler correctly imports C{string} values with comma
        and parens.
        """
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': u'),'}})
        runDataImportHandler(self.client.url)

        yield self.assertQuery([objectID], u'user/tag = "),"')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithStringValueWithSpecialCharacters(self):
        """
        The Data Import Handler correctly imports C{string} values with special
        unicode characters.
        """
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': u'\xe1\xe9\xed\xf3'}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag = "\xe1\xe9\xed\xf3"')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithListValue(self):
        """The Data Import Handler correctly imports C{list} values."""
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': [u'one', u'two', u'three']}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag contains "one" '
                                           u'and user/tag contains "two" '
                                           u'and user/tag contains "three"')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithListValueWithSpecialCharacters(self):
        """
        The Data Import Handler correctly imports C{list} values with special
        unicode characters.
        """
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': [u'one', u'\xe1\xe9']}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag contains "\xe1\xe9"')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithListValueWithEmptyString(self):
        """
        The Data Import Handler correctly imports C{list} values with an empty
        string in them.
        """
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': [u'']}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag contains ""')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithIntValue(self):
        """The Data Import Handler correctly imports C{int} values."""
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': 5}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag = 5')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithIntSixDigitValue(self):
        """The Data Import Handler correctly imports C{int} values."""
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': 123456}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag = 123456')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithFloatValue(self):
        """The Data Import Handler correctly imports C{float} values."""
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': 5.5}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag = 5.5')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithBoolValue(self):
        """The Data Import Handler correctly imports C{boolean} values."""
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': False}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'user/tag = false')
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithNoneValue(self):
        """The Data Import Handler correctly imports C{null} values."""
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag': None}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithBinaryValue(self):
        """The Data Import Handler correctly imports C{binary} values."""
        objectID = self.objects.create()
        self.values.set({objectID: {
            u'user/tag': {
                'mime-type': 'text/plain',
                'contents': 'file contents'}}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'has user/tag')

    @inlineCallbacks
    def testImportObjectWithBinaryValueDoesNotCreateOtherValues(self):
        """
        The Data Import Handler does not import binary tag values as other Solr
        fields. This is a test for issue #1447.
        """
        objectID = self.objects.create()
        self.values.set({objectID: {
            u'user/tag': {
                'mime-type': 'text/plain',
                'contents': 'file contents'}}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID], u'has user/tag')
        yield self.assertQuery([], u'user/tag = 13')
        yield self.assertQuery([], u'user/tag = "size"')

    @inlineCallbacks
    def testImportObjectWithMultipleValues(self):
        """The Data Import Handler import documents with multiple values."""
        objectID = self.objects.create()
        self.values.set({objectID: {
            u'user/tag1': u'string',
            u'user/tag2': 5,
            u'user/tag3': 5.5,
            u'user/tag4': True,
            u'user/tag5': None,
            u'user/tag6': {
                'mime-type': 'text/plain',
                'contents': 'file contents'}}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID],
                               'user/tag1 = "string" and user/tag2 = 5 '
                               'and user/tag3 = 5.5 and user/tag4 = true')

        yield self.assertQuery([objectID],
                               'has user/tag1 and has user/tag2 '
                               'and has user/tag3 and has user/tag4 '
                               'and has user/tag5 and has user/tag6')

    @inlineCallbacks
    def testImportMultipleObjects(self):
        """The Data Import Handler imports multiple objects."""
        objectID1 = self.objects.create()
        objectID2 = self.objects.create()
        objectID3 = self.objects.create()
        objectID4 = self.objects.create()
        self.values.set({objectID1: {u'user/tag1': u'string'},
                         objectID2: {u'user/tag1': u'string'},
                         objectID3: {u'user/tag1': 5},
                         objectID4: {u'user/tag2': True}})
        runDataImportHandler(self.client.url)

        yield self.assertQuery([objectID1, objectID2], 'user/tag1 = "string"')
        yield self.assertQuery([objectID1, objectID2, objectID3],
                               'has user/tag1')

    @inlineCallbacks
    def testImportDeletedObject(self):
        """The Data Import Handler removes all fields from deleted objects"""
        objectID1 = self.objects.create()
        objectID2 = self.objects.create()
        self.values.set({objectID1: {u'user/tag1': u'string'},
                         objectID2: {u'user/tag1': u'string'}})
        self.values.delete([(objectID1, u'user/tag1')])
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID2], 'has user/tag1')
        yield self.assertQuery([objectID2], 'user/tag1 = "string"')

    @inlineCallbacks
    def testImportDeletedAndCreatedAgainObject(self):
        """
        The Data Import Handler correctly imports values deleted and created
        again.
        """
        objectID1 = self.objects.create()
        objectID2 = self.objects.create()
        self.values.set({objectID1: {u'user/tag1': u'string'},
                         objectID2: {u'user/tag1': u'string'}})
        self.values.delete([(objectID1, u'user/tag1')])
        self.values.set({objectID1: {u'user/tag1': u'string'}})
        runDataImportHandler(self.client.url)
        yield self.assertQuery([objectID1, objectID2], 'has user/tag1')
        yield self.assertQuery([objectID1, objectID2], 'user/tag1 = "string"')

    @inlineCallbacks
    def testDeltaImport(self):
        """
        When using a C{clean=false} option, the Data Import Handler only
        imports values modified since the last run.
        """
        objectID1 = self.objects.create()
        objectID2 = self.objects.create()

        self.values.set({objectID1: {
            u'user/tag1': u'string',
            u'user/tag2': 5,
            u'user/tag3': 5.5}})

        getDirtyObjects().remove()

        self.values.set({objectID2: {
            u'user/tag1': u'string',
            u'user/tag2': 5,
            u'user/tag3': 5.5}})

        runDataImportHandler(self.client.url, clean=False)

        yield self.assertQuery(
            [objectID2], 'has user/tag1 and has user/tag2 and has user/tag3 '
                         'and user/tag1="string" and user/tag2=5 and '
                         'user/tag3=5.5')

    @inlineCallbacks
    def testDeltaImportWithDeletedTag(self):
        """
        L{TagValue}s deleted via delete cascade triggered by a L{Tag} deletion
        should be deleted in the index too.
        """
        objectID = self.objects.create()
        self.values.set({objectID: {u'user/tag1': u'string',
                                    u'user/tag2': u'text'}})
        time.sleep(1)
        runDataImportHandler(self.client.url)
        tags = TagAPI(self.user)
        tags.delete([u'user/tag1'])
        self.store.commit()
        values = self.values.get([objectID], [u'user/tag1'])
        self.assertNotIn(u'user/tag1', values[objectID])
        runDataImportHandler(self.client.url, clean=False)
        yield self.assertQuery([], 'has user/tag1')
        yield self.assertQuery([], 'user/tag1 = "string"')
Ejemplo n.º 15
0
    def testGetRecentAboutActivity(self):
        """
        L{FacadeRecentActivityMixin.getRecentAboutActivity} returns a C{dict}
        with information about the recent tag values on the given object.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')
        objectID2 = uuid4()

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag1': u'A'}})
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag2': u'B'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user/tag2': u'D'}})
        self.store.commit()
        tagValues.set({uuid4(): {u'user/tag1': u'E'}})
        self.store.commit()
        tagValues.set({uuid4(): {u'user/tag2': u'F'}})
        self.store.commit()

        expected = [
            {'tag': u'user/tag2',
             'id': str(objectID1),
             'about': u'object1',
             'value': u'B',
             'username': u'username'},

            {'tag': u'user/tag1',
             'id': str(objectID1),
             'about': u'object1',
             'value': u'A',
             'username': u'username'},

            {'tag': u'fluiddb/about',
             'id': str(objectID1),
             'about': u'object1',
             'value': u'object1',
             'username': u'fluiddb'}]

        with login(self.user.username, uuid4(), self.transact) as session:
            result = yield self.facade.getRecentAboutActivity(
                session, 'object1')

            # Remove the creation times from the result.
            for item in result:
                del item['updated-at']

            self.assertEqual(expected, result)