Example #1
0
    def test_process_with_regex(self):
        group_by_attributes = ['b']
        aggregator = PropertyAggregator(properties=group_by_attributes, activity_key='b', activity_value=r"foo|bar")

        data_dict = [{'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}
        }, {'a': 3, 'b': 2,  'c': {'d': 5, 'e': 4}
        }, {'a': 4, 'b': "bar", 'c': {'d': 6, 'e': 4}
        }, {'a': 5, 'b': "bar", 'c': {'d': 6, 'e': 4}
        }]
        expected = [
            {'a': 1, 'c': {'e': 4, 'd': 3}, 'b': 2}, {'a': 3, 'c': {'e': 4, 'd': 5}, 'b': 2},
            {'a': [4, 5], 'c': [{'e': 4, 'd': 6}, {'e': 4, 'd': 6}], 'b': 'bar',
                'grouped_by_attributes': ['b'], 'grouped_by_values': ['bar']}
        ]
        actual = aggregator.process(data_dict, data_dict, [aggregator])
        eq_(actual, expected)
Example #2
0
    def test__aggregate_activities_with_activity_key_filter(self):
        aggregator = PropertyAggregator(activity_key='b', activity_value=3)
        group_by_attributes = ['b']

        data_dict = [{'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}
        }, {'a': 3, 'b': 2,  'c': {'d': 5, 'e': 4}
        }, {'a': 4, 'b': 3, 'c': {'d': 6, 'e': 4}
        }, {'a': 5, 'b': 3, 'c': {'d': 6, 'e': 4}
        }]
        expected = [
            {'a': 1, 'c': {'e': 4, 'd': 3}, 'b': 2},
            {'a': 3, 'c': {'e': 4, 'd': 5}, 'b': 2},
            {'a': [4, 5], 'c': [{'e': 4, 'd': 6}, {'e': 4, 'd': 6}], 'b': 3,
                'grouped_by_attributes': ['b'], 'grouped_by_values': [3]}
        ]

        _raw_group_actvities = groupby(data_dict, aggregator._group_by_aggregator(group_by_attributes))
        actual = aggregator._aggregate_activities(group_by_attributes=group_by_attributes,
            grouped_activities=_raw_group_actvities)
        eq_(actual, expected)
Example #3
0
    def test_get_activities_with_aggregation_pipline(self):
        activity_ids = [self.like_activity_id, self.reply_activity_id, self.activity_id, self.activity_id2, self.like_activity_id2, self.reply_activity_id2]

        activities = self._client.get_activities(activity_ids=activity_ids, aggregation_pipeline=[PropertyAggregator(properties=['verb', 'actor'])])

        eq_([{u'id': u'7779', u'verb': u'like', u'target': {u'objectType': u'something', u'id': u'31415', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'inReplyTo': [], u'objectType': u'like', u'id': u'6669', u'published': u'2012-08-05T12:00:00Z'}, u'actor': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}}, {u'id': u'8889', u'verb': u'reply', u'target': {u'objectType': u'something', u'id': u'31415', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'content': u'This is my first reply', u'inReplyTo': [], u'objectType': u'reply', u'id': u'9999', u'published': u'2012-08-05T12:00:00Z'}, u'actor': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}}, {'grouped_by_attributes': ['verb', 'actor'], u'title': [u'Stream Item', u'Stream Item'], u'object': [{u'objectType': u'something', u'id': u'4353', u'published': u'2012-07-05T12:00:00Z'}, {u'published': u'2012-07-05T12:00:00Z', u'id': u'4353', u'objectType': u'something'}], u'actor': {u'published': u'2012-07-05T12:00:00Z', u'id': u'4321', u'objectType': u'something'}, u'verb': u'post', u'replies': [{u'totalItems': 2, u'items': [{u'verb': u'reply', u'actor': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'target': {u'objectType': u'something', u'id': u'31415', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'content': u'This is my first reply', u'inReplyTo': [], u'objectType': u'reply', u'id': u'9999', u'published': u'2012-08-05T12:00:00Z'}, u'actor': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}, u'verb': u'reply', u'id': u'8889', u'objectType': u'activity'}}, {u'verb': u'reply', u'actor': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'target': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'content': u'This is my second reply', u'inReplyTo': [], u'objectType': u'reply', u'id': u'9998', u'published': u'2012-08-05T12:05:00Z'}, u'actor': {u'objectType': u'something', u'id': u'4321', u'published': u'2012-07-05T12:00:00Z'}, u'verb': u'reply', u'id': u'8888', u'objectType': u'activity'}}]}, {u'totalItems': 2, u'items': [{u'verb': u'reply', u'actor': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'target': {u'objectType': u'something', u'id': u'31415', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'content': u'This is my first reply', u'inReplyTo': [], u'objectType': u'reply', u'id': u'9999', u'published': u'2012-08-05T12:00:00Z'}, u'actor': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}, u'verb': u'reply', u'id': u'8889', u'objectType': u'activity'}}, {u'verb': u'reply', u'actor': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'target': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'content': u'This is my second reply', u'inReplyTo': [], u'objectType': u'reply', u'id': u'9998', u'published': u'2012-08-05T12:05:00Z'}, u'actor': {u'objectType': u'something', u'id': u'4321', u'published': u'2012-07-05T12:00:00Z'}, u'verb': u'reply', u'id': u'8888', u'objectType': u'activity'}}]}], u'id': [u'5555', u'5556'], 'grouped_by_values': [u'post', {u'published': u'2012-07-05T12:00:00Z', u'id': u'4321', u'objectType': u'something'}]}, {u'id': u'7778', u'verb': u'like', u'target': {u'objectType': u'something', u'id': u'31415', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'inReplyTo': [], u'objectType': u'like', u'id': u'6669', u'published': u'2012-08-05T12:00:00Z'}, u'actor': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}}, {u'id': u'8888', u'verb': u'reply', u'target': {u'objectType': u'something', u'id': u'1234', u'published': u'2012-07-05T12:00:00Z'}, u'object': {u'content': u'This is my second reply', u'inReplyTo': [], u'objectType': u'reply', u'id': u'9998', u'published': u'2012-08-05T12:05:00Z'}, u'actor': {u'objectType': u'something', u'id': u'4321', u'published': u'2012-07-05T12:00:00Z'}}], activities)
Example #4
0
class TestPropertyAggregator(object):
    def setUp(self):
        self._aggregator = PropertyAggregator()

    def test_process(self):
        group_by_attributes = ['b']
        aggregator = PropertyAggregator(properties=group_by_attributes, activity_key='b', activity_value=3)

        data_dict = [{'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}
        }, {'a': 3, 'b': 2,  'c': {'d': 5, 'e': 4}
        }, {'a': 4, 'b': 3, 'c': {'d': 6, 'e': 4}
        }, {'a': 5, 'b': 3, 'c': {'d': 6, 'e': 4}
        }]
        expected = [
            {'a': 1, 'c': {'e': 4, 'd': 3}, 'b': 2},
            {'a': 3, 'c': {'e': 4, 'd': 5}, 'b': 2},
            {'a': [4, 5], 'c': [{'e': 4, 'd': 6}, {'e': 4, 'd': 6}], 'b': 3,
                'grouped_by_attributes': ['b'], 'grouped_by_values': [3]}
        ]

        actual = aggregator.process(data_dict, data_dict, [aggregator])
        eq_(actual, expected)

    def test_process_with_regex(self):
        group_by_attributes = ['b']
        aggregator = PropertyAggregator(properties=group_by_attributes, activity_key='b', activity_value=r"foo|bar")

        data_dict = [{'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}
        }, {'a': 3, 'b': 2,  'c': {'d': 5, 'e': 4}
        }, {'a': 4, 'b': "bar", 'c': {'d': 6, 'e': 4}
        }, {'a': 5, 'b': "bar", 'c': {'d': 6, 'e': 4}
        }]
        expected = [
            {'a': 1, 'c': {'e': 4, 'd': 3}, 'b': 2}, {'a': 3, 'c': {'e': 4, 'd': 5}, 'b': 2},
            {'a': [4, 5], 'c': [{'e': 4, 'd': 6}, {'e': 4, 'd': 6}], 'b': 'bar',
                'grouped_by_attributes': ['b'], 'grouped_by_values': ['bar']}
        ]
        actual = aggregator.process(data_dict, data_dict, [aggregator])
        eq_(actual, expected)

    def test__aggregate_activities_with_activity_key_filter(self):
        aggregator = PropertyAggregator(activity_key='b', activity_value=3)
        group_by_attributes = ['b']

        data_dict = [{'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}
        }, {'a': 3, 'b': 2,  'c': {'d': 5, 'e': 4}
        }, {'a': 4, 'b': 3, 'c': {'d': 6, 'e': 4}
        }, {'a': 5, 'b': 3, 'c': {'d': 6, 'e': 4}
        }]
        expected = [
            {'a': 1, 'c': {'e': 4, 'd': 3}, 'b': 2},
            {'a': 3, 'c': {'e': 4, 'd': 5}, 'b': 2},
            {'a': [4, 5], 'c': [{'e': 4, 'd': 6}, {'e': 4, 'd': 6}], 'b': 3,
                'grouped_by_attributes': ['b'], 'grouped_by_values': [3]}
        ]

        _raw_group_actvities = groupby(data_dict, aggregator._group_by_aggregator(group_by_attributes))
        actual = aggregator._aggregate_activities(group_by_attributes=group_by_attributes,
            grouped_activities=_raw_group_actvities)
        eq_(actual, expected)

    def test__aggregate_activities(self):
        group_by_attributes = ['b', 'c.e']

        data_dict = [{'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}
        }, {'a': 3, 'b': 2,  'c': {'d': 5, 'e': 4}
        }, {'a': 4, 'b': 2, 'c': {'d': 6, 'e': 4}
        }, {'a': 5, 'b': 3, 'c': {'d': 6, 'e': 4}
        }]
        expected = [{'a': [1, 3, 4], 'c': {'e': 4, 'd': [3, 5, 6]}, 'b': 2, 'grouped_by_attributes': ['b', 'c.e'],
            'grouped_by_values': [2, 4]}, {'a': 5, 'c': {'e': 4, 'd': 6}, 'b': 3}]

        _raw_group_actvities = groupby(data_dict, self._aggregator._group_by_aggregator(group_by_attributes))
        actual = self._aggregator._aggregate_activities(group_by_attributes=group_by_attributes, grouped_activities=_raw_group_actvities)
        eq_(actual, expected)

    def test__listify_attributes(self):
        data_dict = {
            'a': 1,
            'b': 2,
            'c': {
                'd': 3,
                'e': 4
            }
        }
        group_by_attributes = ['a', 'a.c.f', 'c.e']
        expected = {
            'a': 1,
            'b': [2],
            'c': {
                'd': [3],
                'e': 4
            }
        }

        actual = self._aggregator._listify_attributes(group_by_attributes=group_by_attributes, activity=data_dict)
        eq_(actual, (['c'], expected,))

    def test_group_by_aggregator(self):
        data_dict = {
            'a': 1,
            'b': 2,
            'c': {
                'd': 3,
                'e': 4
            }
        }
        expected = [1, 2, 4]
        actual = self._aggregator._group_by_aggregator(group_by_attributes=['a', 'b', 'a.c.f', 'c.e'])(data_dict)
        eq_(expected, actual)
Example #5
0
 def setUp(self):
     self._aggregator = PropertyAggregator()