예제 #1
0
def test_pluck_on_classes() -> None:
    class Class:
        def __init__(self, val: int) -> None:
            self.a = val

    collection = Collection([Class(1), Class(2)])
    assert collection.pluck("a").as_list() == [1, 2]
예제 #2
0
def test_choices() -> None:
    items = [
        {'item_id': 1, 'item_name': 'one'},
        {'item_id': 2, 'item_name': 'two'},
        {'item_id': 3, 'item_name': 'three'},
    ]
    assert Collection(items).choices('item_name', 'item_id') == [(1, 'one'), (2, 'two'), (3, 'three')]
예제 #3
0
def test_avg_by_key_over_classes() -> None:
    class Class:
        def __init__(self, val: int) -> None:
            self.a = val

    collection = Collection([Class(1), Class(2), Class(3)])
    assert collection.avg("a") == 2
예제 #4
0
def test_sort_by_key() -> None:
    collection = Collection([{"a": 2}, {"a": 3}, {"a": 1}])
    assert collection.sort(key=lambda x: x["a"]).as_list() == [
        {"a": 1},
        {"a": 2},
        {"a": 3},
    ]
예제 #5
0
def test_chunk() -> None:
    collection = Collection([1, 2, 3, 4, 5, 6])
    chunks = list(collection.chunk(2))
    assert len(chunks) == 3
    assert chunks[0] == [1, 2]
    assert chunks[1] == [3, 4]
    assert chunks[2] == [5, 6]
예제 #6
0
def test_key_value_by_string_key() -> None:
    collection = Collection(
        [
            {"id": 1},
            {"id": 2},
            {"id": 3},
            {"id": 3},
        ]
    )
    assert collection.key_value("id") == {1: {"id": 1}, 2: {"id": 2}, 3: {"id": 3}}
예제 #7
0
def test_each() -> None:
    call_count = 0

    def _each_fn(*args: Any) -> None:
        nonlocal call_count
        call_count += 1

    collection = Collection([1, 2, 3])
    assert collection.each(_each_fn).as_list() == [1, 2, 3]
    assert call_count == 3
예제 #8
0
def test_choices_dict() -> None:
    items = [
        {'item_id': 1, 'item_name': 'one'},
        {'item_id': 2, 'item_name': 'two'},
        {'item_id': 3, 'item_name': 'three'},
    ]
    assert Collection(items).choices_dict('item_name', 'item_id') == [
        {'value': 1, 'label': 'one'},
        {'value': 2, 'label': 'two'},
        {'value': 3, 'label': 'three'},
    ]
예제 #9
0
def test_sum_by_key_over_classes() -> None:
    class Class:
        def __init__(self, val: int) -> None:
            self.a = val

    obj1 = Class(1)
    obj2 = Class(2)
    obj3 = Class(3)

    collection = Collection([obj1, obj2, obj3])
    assert collection.sum("a") == 6
예제 #10
0
 async def choices_dict(
         self,
         label_column: str = 'name',
         value_column: str = 'id',
         label_key: str = 'label',
         value_key: str = 'value') -> list[t.Dict[t.Any, t.Any]]:
     result = await self._execute(self._stmt)
     return Collection(result.scalars().all()).choices_dict(
         label_col=label_column,
         value_col=value_column,
         label_key=label_key,
         value_key=value_key,
     )
예제 #11
0
def test_key_value() -> None:
    collection = Collection(
        [
            {"id": 1},
            {"id": 2},
            {"id": 3},
            {"id": 3},
        ]
    )
    assert collection.key_value(lambda x: x["id"]) == {
        1: {"id": 1},
        2: {"id": 2},
        3: {"id": 3},
    }
예제 #12
0
def test_group_by_string_key() -> None:
    collection = Collection(
        [
            {"id": 1},
            {"id": 2},
            {"id": 3},
            {"id": 3},
        ]
    )
    assert collection.group_by("id") == {
        1: [{"id": 1}],
        2: [{"id": 2}],
        3: [{"id": 3}, {"id": 3}],
    }
예제 #13
0
def test_group_by() -> None:
    collection = Collection(
        [
            {"id": 1},
            {"id": 2},
            {"id": 3},
            {"id": 3},
        ]
    )
    assert collection.group_by(lambda x: x["id"]) == {
        1: [{"id": 1}],
        2: [{"id": 2}],
        3: [{"id": 3}, {"id": 3}],
    }
예제 #14
0
def test_min_over_scalar() -> None:
    collection = Collection([1, 2, 3])
    assert collection.min() == 1
예제 #15
0
def test_sort_by_string_key() -> None:
    collection = Collection([{"a": 2}, {"a": 3}, {"a": 1}])
    assert collection.sort(key="a").as_list() == [{"a": 1}, {"a": 2}, {"a": 3}]
예제 #16
0
def test_avg_by_key_over_dicts() -> None:
    collection = Collection([{"a": 1}, {"a": 2}, {"a": 3}])
    assert collection.avg("a") == 2
예제 #17
0
def test_avg_over_scalar() -> None:
    collection = Collection([1, 2, 3])
    assert collection.avg() == 2
예제 #18
0
def test_first() -> None:
    collection = Collection([1, 2, 3])
    assert collection.first() == 1
예제 #19
0
def test_get_by_index() -> None:
    collection = Collection([1, 2, 3])
    assert collection[1] == 2
예제 #20
0
 async def choices(self,
                   label_column: str = 'name',
                   value_column: str = 'id') -> t.List[t.Tuple[str, t.Any]]:
     result = await self._execute(self._stmt)
     return Collection(result.scalars().all()).choices(
         label_col=label_column, value_col=value_column)
예제 #21
0
def test_stringable() -> None:
    collection = Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
    assert str(collection) == "<Collection: [1,2,3,4,5,6,7,8,9,10 and 1 items more]>"
예제 #22
0
def test_reverse() -> None:
    collection = Collection([1, 2, 3])
    assert collection.reverse() == Collection([3, 2, 1])
예제 #23
0
def test_countable() -> None:
    collection = Collection([1, 2, 3])
    assert len(collection) == 3
예제 #24
0
def test_delete_by_index() -> None:
    collection = Collection([1, 2, 3])
    del collection[1]
    assert collection.as_list() == [1, 3]
예제 #25
0
def test_get_slice() -> None:
    collection = Collection([1, 2, 3])
    assert collection[1:] == [2, 3]
예제 #26
0
def test_min_by_key_over_dicts() -> None:
    collection = Collection([{"a": 1}, {"a": 2}, {"a": 3}])
    assert collection.min("a") == 1
예제 #27
0
 async def all(self) -> Collection[M]:
     result = await self._execute(self._stmt)
     return Collection(result.scalars().all())
예제 #28
0
def test_pluck_on_dicts() -> None:
    collection = Collection([{"a": 1}, {"a": 2}])
    assert collection.pluck("a").as_list() == [1, 2]
예제 #29
0
def test_contains() -> None:
    collection = Collection([1, 2, 3])
    assert 2 in collection
예제 #30
0
def test_set_by_index() -> None:
    collection = Collection([1, 2, 3])
    collection[2] = 4
    assert collection.as_list() == [1, 2, 4, 3]