Example #1
0
def test_find_no_results():
    t = Table(primary_id="a")
    t.insert(dict(a=1, b=2))

    with pytest.raises(StopIteration):
        next(t.find(a=3))

    assert next(t.find(a=1)) == dict(a=1, b=2)
Example #2
0
def test_find_with_multiple_default_vals():
    t = Table(primary_id="b")
    t.create_column("firstname", default="John")
    t.create_column("lastname", default="Smith")
    t.insert(dict(b=1))
    t.insert(dict(b=2))
    t.insert(dict(b=3, lastname="Doe"))

    result = t.find(lastname="Smith")
    result2 = t.find(b=2)
    result3 = t.find(firstname="John", lastname="Smith")

    assert [r["b"] for r in result] == [1, 2]
    assert list(result2) == [dict(b=2, firstname="John", lastname="Smith")]
    assert [r["b"] for r in result3] == [1, 2]
Example #3
0
def test_update():
    t = Table(primary_id="id")
    t.insert({"a": 1, "b": 2})
    t.insert({"a": 2, "b": 5})
    t.insert({"a": 3, "b": 6})
    t.insert({"a": 3, "b": 7})
    t.insert({"a": 3, "b": 8})

    t.update(where={"a": 2}, b=5)
    result = next(t.find(a=2))
    assert result["b"] == 5
    t.update(dict(a=3), b=10)

    result = list(t.find(a=3))
    assert all(row["b"] == 10 for row in result)
Example #4
0
def test_find_with_default_val():
    t = Table(primary_id="b")
    t.insert(dict(b=1))
    t.insert(dict(b=2))
    t.insert(dict(a=1, b=3))
    results = list(t.find(a=None))
    assert results == [dict(b=1, a=None), dict(b=2, a=None)]
Example #5
0
def test_no_insert_columns_on_find():
    t = Table(primary_id="a")
    t.insert({"a": 1})
    t.insert({"a": 2})

    assert list(t.find(a=1, b=2, ignore_errors=True)) == [{"a": 1}]
    assert t.columns == ["a"]
Example #6
0
def test_delete_simple():
    t = Table()
    t.insert(dict(b=2))
    t.insert(dict(a="John"))
    t.insert(dict(a="John"))
    t.delete(a="John")

    assert list(t.find(a="John")) == []
    assert len(list(t.all())) == 1
Example #7
0
def test_update_replace():
    t = Table()

    row1 = dict(vorname="rainer", nachname="greiff")
    row2 = dict(vorname="rainer", nachname="smith")
    row3 = dict(vorname="rainer", nachname="erhard")

    for row in [row1, row2, row3]:
        t.insert(row)

    t.update_replace(where={"nachname": "smith"}, nachname="greiff")

    assert len(t) == 2
    assert len(list(t.find(nachname="greiff"))) == 1
Example #8
0
def test_delete_find_delete():
    t = Table(primary_id="a")
    t.insert({"a": 1, "b": 2})
    t.insert({"a": 2, "b": 5})
    t.insert({"a": 3, "b": 6})

    result = list(t.find(a=1))
    assert len(result) == 1
    assert len(t) == 3

    n_delete = t.delete(a=1)

    result = list(t.find(a=1))
    print(result)
    assert n_delete == 1
    assert len(result) == 0
    assert len(t) == 2
    assert list(t.all(ordered="ascending")) == [{
        "a": 2,
        "b": 5
    }, {
        "a": 3,
        "b": 6
    }]
Example #9
0
def test_update_replace_no_replace():
    t = Table()

    row1 = dict(vorname="rainer", nachname="greiff", address="123street")
    row2 = dict(vorname="rainer", nachname="smith", address="123street")
    row3 = dict(vorname="rainer", nachname="smith", address="456street")
    row4 = dict(vorname="luke", nachname="doe", address="23Street")

    for row in [row1, row2, row3, row4]:
        t.insert(row)

    t.update_replace(where={"nachname": "doe"}, nachname="greiff")

    assert len(t) == 4
    assert len(list(t.find(nachname="greiff"))) == 2
Example #10
0
def test_update_single():
    t = Table()

    row1 = dict(vorname="rainer", nachname="greiff")
    row2 = dict(vorname="rainer", nachname="greiff")
    row3 = dict(vorname="rainer", nachname="erhard")

    for row in [row1, row2, row3]:
        t.insert(row)

    row_vals_before = set(t["vorname"].values["rainer"])
    t.update(dict(vorname="rainer", nachname="erhard"), n_values=45)

    row_vals_after = set(t["vorname"].values["rainer"])
    result = list(t.find(n_values=45))
    assert len(result) == 1
    assert row_vals_before == row_vals_after
Example #11
0
def test_update_replace_multiple_replace():
    t = Table()

    rows = [
        dict(vorname="rainer", nachname="greiff", address="123street"),
        dict(vorname="rainer", nachname="greiff", address="123street"),
        dict(vorname="rainer", nachname="smith", address="123street"),
        dict(vorname="rainer", nachname="smith", address="123street"),
        dict(vorname="rainer", nachname="smith", address="123street"),
        dict(vorname="rainer", nachname="smith", address="456street"),
        dict(vorname="luke", nachname="doe", address="23Street"),
    ]

    for row in rows:
        t.insert(row)

    t.update_replace(where={"nachname": "smith"}, nachname="greiff")

    assert len(t) == 3
    assert len(list(t.find(nachname="greiff"))) == 2