def test_two_level_grouping_with_collect(self):
        data = [
            datum(name='Jill', place='Kettering', age=[34, 36], count=2),
            datum(name='Jack', place='Kennington', age=[23], count=1),
            datum(name='James', place='Keswick', age=[10, 21, 32], count=3),
            datum(name='James', place='Kettering', age=[43, 87], count=2),
            datum(name='Jill', place='Keswick', age=[76, 32], count=2),
        ]
        results = nested_merge([['name'], ['place']], [('age', 'mean')], data)

        assert_that(
            results,
            contains(
                has_entries({
                    'name':
                    'Jack',
                    'age:mean':
                    23,
                    '_subgroup':
                    contains(
                        has_entries({
                            'place': 'Kennington',
                            'age:mean': 23
                        }))
                }),
                has_entries({
                    'name':
                    'James',
                    'age:mean':
                    38.6,
                    '_subgroup':
                    contains(
                        has_entries({
                            'place': 'Keswick',
                            'age:mean': 21.0
                        }),
                        has_entries({
                            'place': 'Kettering',
                            'age:mean': 65.0
                        }))
                }),
                has_entries({
                    'name':
                    'Jill',
                    'age:mean':
                    44.5,
                    '_subgroup':
                    contains(
                        has_entries({
                            'place': 'Keswick',
                            'age:mean': 54.0
                        }),
                        has_entries({
                            'place': 'Kettering',
                            'age:mean': 35.0
                        }))
                }),
            ))
Пример #2
0
    def test_two_level_grouping_with_collect(self):
        data = [
            datum(name='Jill', place='Kettering', age=[34, 36], count=2),
            datum(name='Jack', place='Kennington', age=[23], count=1),
            datum(name='James', place='Keswick', age=[10, 21, 32], count=3),
            datum(name='James', place='Kettering', age=[43, 87], count=2),
            datum(name='Jill', place='Keswick', age=[76, 32], count=2),
        ]
        results = nested_merge([['name'], ['place']], [('age', 'mean')], data)

        assert_that(results,
                    contains(
                        has_entries({
                            'name': 'Jack',
                            'age:mean': 23,
                            '_subgroup': contains(
                                has_entries({
                                    'place': 'Kennington',
                                    'age:mean': 23
                                })
                            )}),
                        has_entries({
                            'name': 'James',
                            'age:mean': 38.6,
                            '_subgroup': contains(
                                has_entries({
                                    'place': 'Keswick',
                                    'age:mean': 21.0
                                }),
                                has_entries({
                                    'place': 'Kettering',
                                    'age:mean': 65.0
                                })
                            )
                        }),
                        has_entries({
                            'name': 'Jill',
                            'age:mean': 44.5,
                            '_subgroup': contains(
                                has_entries({
                                    'place': 'Keswick',
                                    'age:mean': 54.0
                                }),
                                has_entries({
                                    'place': 'Kettering',
                                    'age:mean': 35.0
                                })
                            )
                        }),
                    ))
Пример #3
0
    def test_one_level_grouping_with_collect(self):
        data = [
            datum(name='Jill', age=[12, 45]),
            datum(name='Jack', age=[34, 34]),
            datum(name='John', age=[56, 65])
        ]
        results = nested_merge([['name']], [('age', 'mean')], data)

        assert_that(results,
                    contains(
                        has_entries({'name': 'Jack', 'age:mean': 34}),
                        has_entries({'name': 'Jill', 'age:mean': 28.5}),
                        has_entries({'name': 'John', 'age:mean': 60.5}),
                    ))
Пример #4
0
    def _group(self, keys, query, sort=None, limit=None, collect=None):
        collect_fields = unique_collect_fields(collect)
        results = self._mongo_driver.group(keys, query, list(collect_fields))

        results = nested_merge(keys, collect, results)

        if sort:
            sorters = {
                "ascending": lambda a, b: cmp(a, b),
                "descending": lambda a, b: cmp(b, a)
            }
            sorter = sorters[sort[1]]
            try:
                results.sort(cmp=sorter, key=lambda a: a[sort[0]])
            except KeyError:
                raise InvalidSortError('Invalid sort key {0}'.format(sort[0]))
        if limit:
            results = results[:limit]

        return results
    def test_one_level_grouping_with_collect(self):
        data = [
            datum(name='Jill', age=[12, 45]),
            datum(name='Jack', age=[34, 34]),
            datum(name='John', age=[56, 65])
        ]
        results = nested_merge([['name']], [('age', 'mean')], data)

        assert_that(
            results,
            contains(
                has_entries({
                    'name': 'Jack',
                    'age:mean': 34
                }),
                has_entries({
                    'name': 'Jill',
                    'age:mean': 28.5
                }),
                has_entries({
                    'name': 'John',
                    'age:mean': 60.5
                }),
            ))
    def test_two_level_grouping_combination_of_keys(self):
        data = [
            datum(name='IE',
                  version='6',
                  place='England',
                  age=[13, 12],
                  count=2),
            datum(name='IE', version='6', place='Wales', age=[13, 14],
                  count=2),
            datum(name='IE', version='7', place='England', age=[8, 7],
                  count=2),
            datum(name='IE', version='7', place='Wales', age=[8, 9], count=2),
            datum(name='IE', version='8', place='England', age=[5, 4],
                  count=2),
            datum(name='IE', version='8', place='Wales', age=[5, 6], count=2),
            datum(name='Chrome',
                  version='20',
                  place='England',
                  age=[2, 1],
                  count=2),
            datum(name='Chrome',
                  version='20',
                  place='Wales',
                  age=[2, 3],
                  count=2),
        ]
        results = nested_merge([['name', 'version'], ['place']],
                               [('age', 'mean')], data)

        assert_that(
            results,
            contains(
                has_entries({
                    'name':
                    'Chrome',
                    'version':
                    '20',
                    'age:mean':
                    2,
                    '_subgroup':
                    contains(
                        has_entries({
                            'place': 'England',
                            'age:mean': 1.5
                        }), has_entries({
                            'place': 'Wales',
                            'age:mean': 2.5
                        }))
                }),
                has_entries({
                    'name':
                    'IE',
                    'version':
                    '6',
                    'age:mean':
                    13,
                    '_subgroup':
                    contains(
                        has_entries({
                            'place': 'England',
                            'age:mean': 12.5
                        }), has_entries({
                            'place': 'Wales',
                            'age:mean': 13.5
                        }))
                }),
                has_entries({
                    'name':
                    'IE',
                    'version':
                    '7',
                    'age:mean':
                    8,
                    '_subgroup':
                    contains(
                        has_entries({
                            'place': 'England',
                            'age:mean': 7.5
                        }), has_entries({
                            'place': 'Wales',
                            'age:mean': 8.5
                        }))
                }),
                has_entries({
                    'name':
                    'IE',
                    'version':
                    '8',
                    'age:mean':
                    5,
                    '_subgroup':
                    contains(
                        has_entries({
                            'place': 'England',
                            'age:mean': 4.5
                        }), has_entries({
                            'place': 'Wales',
                            'age:mean': 5.5
                        }))
                }),
            ))
Пример #7
0
    def test_two_level_grouping_combination_of_keys(self):
        data = [
            datum(name='IE', version='6', place='England', age=[13, 12], count=2),
            datum(name='IE', version='6', place='Wales', age=[13, 14], count=2),
            datum(name='IE', version='7', place='England', age=[8, 7], count=2),
            datum(name='IE', version='7', place='Wales', age=[8, 9], count=2),
            datum(name='IE', version='8', place='England', age=[5, 4], count=2),
            datum(name='IE', version='8', place='Wales', age=[5, 6], count=2),
            datum(name='Chrome', version='20', place='England', age=[2, 1], count=2),
            datum(name='Chrome', version='20', place='Wales', age=[2, 3], count=2),
        ]
        results = nested_merge([['name', 'version'], ['place']], [('age', 'mean')], data)

        assert_that(results,
                    contains(
                        has_entries({
                            'name': 'Chrome',
                            'version': '20',
                            'age:mean': 2,
                            '_subgroup': contains(
                                has_entries({
                                    'place': 'England',
                                    'age:mean': 1.5
                                }),
                                has_entries({
                                    'place': 'Wales',
                                    'age:mean': 2.5
                                })
                            )
                        }),
                        has_entries({
                            'name': 'IE',
                            'version': '6',
                            'age:mean': 13,
                            '_subgroup': contains(
                                has_entries({
                                    'place': 'England',
                                    'age:mean': 12.5
                                }),
                                has_entries({
                                    'place': 'Wales',
                                    'age:mean': 13.5
                                })
                            )
                        }),
                        has_entries({
                            'name': 'IE',
                            'version': '7',
                            'age:mean': 8,
                            '_subgroup': contains(
                                has_entries({
                                    'place': 'England',
                                    'age:mean': 7.5
                                }),
                                has_entries({
                                    'place': 'Wales',
                                    'age:mean': 8.5
                                })
                            )
                        }),
                        has_entries({
                            'name': 'IE',
                            'version': '8',
                            'age:mean': 5,
                            '_subgroup': contains(
                                has_entries({
                                    'place': 'England',
                                    'age:mean': 4.5
                                }),
                                has_entries({
                                    'place': 'Wales',
                                    'age:mean': 5.5
                                })
                            )
                        }),
                    ))