def test_delete_invalid_columns(): t = Table() t.create_column("a") t.create_column("b") with pytest.raises(ColumnDoesNotExist): del t["c"]
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"]
def test_equal_tables(): t1 = Table(name="foo") t2 = Table(name="bar") t3 = Table(name="foo") assert t1 == t3 assert t1 != t2
def test_delete_columns(): t = Table(primary_id="primary_key") t.create_column("a") t.create_column("b") del t["b"] assert t.columns == ["primary_key", "a"]
def test_find_one_no_result(): t = Table(primary_id="a") t.insert(dict(a=1, b=2)) row = t.find_one(a=2) assert row == None
def test_unique_constraint(): t = Table() t.create_column("a", unique=True) t.insert({"a": 1, "b": 2}) t.insert({"a": 2}) with pytest.raises(UniqueConstraintError): t.insert({"a": 1})
def test_error_on_overwrite(): t1 = Table() t2 = Table() db = Database() db["mytable"] = t1 exp_msg = "Table 'mytable' already exists in the Database!" with pytest.raises(TableAlreadyExists, match=exp_msg): db["mytable"] = t2
def test_number_of_rows(): table = Table(primary_id="pk") rows = [] for i in range(167): row = {"pk": i, "a": 1, "b": 2, "c": 0} table.insert(row) rows.append(row) assert list(table.all()) == rows
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
def test_invalid_delete(): t = Table(primary_id="a") row = {"a": 1, "foo": "bar"} t.insert(row) t.delete(a=5, ignore_errors=True) assert list(t.all()) == [row] with pytest.raises(KeyError): t.delete(a=5, ignore_errors=False) assert t.delete(a=1, ignore_errors=False) == 1 assert len(t) == 0
def create_table(self, name: str, primary_id: str = "id") -> Table: if name in self._tables: raise TableAlreadyExists(name) self._tables[name] = Table(name, primary_id=primary_id) return self._tables[name]
def from_dataset(cls, db: dataset.Database) -> Database: pymemdb_database = cls() for tablename in db.tables: pymemdb_database[tablename] = Table.from_dataset(db[tablename]) return pymemdb_database
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)]
def test_get_all_ordered_descending(): t = Table(primary_id="a") t.insert({"a": 1, "b": 2}) t.insert({"a": 2, "b": 5}) t.insert({"a": 3, "b": 6}) results = t.all(ordered="descending") assert [r["b"] for r in results] == [6, 5, 2]
def test_len_of_column(): t = Table() t.insert({"a": 1}) t.insert({"a": 1}) t.insert({"a": 1}) assert len(t["a"]) == 3
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)
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 }]
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
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
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
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
def test_invalid_colname(): t1 = Table() with pytest.raises(ColumnDoesNotExist): t1["g"]
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]
def test_update_no_vals(): t = Table(primary_id="a") row = {"a": 1, "b": 2} t.insert({"a": 1, "b": 2}) t.update(where={"a": 2}, b=5) assert list(t.all()) == [row]
def test_single_insert_given_primary(): table = Table(primary_id="pk") row = {"pk": 1, "a": 1, "b": 2, "c": 0} table.insert(row) assert list(table.all()) == [{**row}]
def test_assign_new_table(): t = Table() db = Database() db["test"] = t assert "test" in db.tables
def test_add_columns(): t = Table(primary_id="id") t.create_column("a") t.create_column("b") assert t.columns == ["id", "a", "b"]
def test_insert_pk_twice(): t = Table(primary_id="pk") t.insert(dict(pk=1, b=2)) with pytest.raises(UniqueConstraintError): t.insert(dict(pk=1, b=3))
def test_invalid_arg_ordered(): t = Table(primary_id="a") t.insert({"a": 1}) with pytest.raises(ValueError): list(t.all(ordered="foo"))
def big_table(): t = Table(primary_id="normal") for i in range(100): t.insert({"normal": i, "squared": i**2, "cubed": i**3}) return t