Ejemplo n.º 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]
Ejemplo n.º 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')]
Ejemplo n.º 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
Ejemplo n.º 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},
    ]
Ejemplo n.º 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]
Ejemplo n.º 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}}
Ejemplo n.º 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
Ejemplo n.º 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'},
    ]
Ejemplo n.º 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
Ejemplo n.º 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,
     )
Ejemplo n.º 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},
    }
Ejemplo n.º 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}],
    }
Ejemplo n.º 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}],
    }
Ejemplo n.º 14
0
def test_min_over_scalar() -> None:
    collection = Collection([1, 2, 3])
    assert collection.min() == 1
Ejemplo n.º 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}]
Ejemplo n.º 16
0
def test_avg_by_key_over_dicts() -> None:
    collection = Collection([{"a": 1}, {"a": 2}, {"a": 3}])
    assert collection.avg("a") == 2
Ejemplo n.º 17
0
def test_avg_over_scalar() -> None:
    collection = Collection([1, 2, 3])
    assert collection.avg() == 2
Ejemplo n.º 18
0
def test_first() -> None:
    collection = Collection([1, 2, 3])
    assert collection.first() == 1
Ejemplo n.º 19
0
def test_get_by_index() -> None:
    collection = Collection([1, 2, 3])
    assert collection[1] == 2
Ejemplo n.º 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)
Ejemplo n.º 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]>"
Ejemplo n.º 22
0
def test_reverse() -> None:
    collection = Collection([1, 2, 3])
    assert collection.reverse() == Collection([3, 2, 1])
Ejemplo n.º 23
0
def test_countable() -> None:
    collection = Collection([1, 2, 3])
    assert len(collection) == 3
Ejemplo n.º 24
0
def test_delete_by_index() -> None:
    collection = Collection([1, 2, 3])
    del collection[1]
    assert collection.as_list() == [1, 3]
Ejemplo n.º 25
0
def test_get_slice() -> None:
    collection = Collection([1, 2, 3])
    assert collection[1:] == [2, 3]
Ejemplo n.º 26
0
def test_min_by_key_over_dicts() -> None:
    collection = Collection([{"a": 1}, {"a": 2}, {"a": 3}])
    assert collection.min("a") == 1
Ejemplo n.º 27
0
 async def all(self) -> Collection[M]:
     result = await self._execute(self._stmt)
     return Collection(result.scalars().all())
Ejemplo n.º 28
0
def test_pluck_on_dicts() -> None:
    collection = Collection([{"a": 1}, {"a": 2}])
    assert collection.pluck("a").as_list() == [1, 2]
Ejemplo n.º 29
0
def test_contains() -> None:
    collection = Collection([1, 2, 3])
    assert 2 in collection
Ejemplo n.º 30
0
def test_set_by_index() -> None:
    collection = Collection([1, 2, 3])
    collection[2] = 4
    assert collection.as_list() == [1, 2, 4, 3]