Content-Length: 389911 | pFad | http://github.com/Donnype/xtdb-py/commit/c20143a38495dd7687ef80db5532affc469a5386

30 Test soft delete and test+fix evict · Donnype/xtdb-py@c20143a · GitHub
Skip to content

Commit

Permalink
Test soft delete and test+fix evict
Browse files Browse the repository at this point in the history
  • Loading branch information
Donnype committed May 17, 2023
1 parent 3ee4748 commit c20143a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Iterator

import pytest
Expand Down Expand Up @@ -33,6 +34,11 @@ class FourthEntity(Base):
value: float = field(default_factory=float)


@pytest.fixture
def valid_time() -> datetime:
return datetime.now(timezone.utc)


@pytest.fixture
def xtdb_http_client() -> XTDBHTTPClient:
client = XTDBHTTPClient(base_url=os.environ["XTDB_URI"])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from datetime import datetime

import pytest

Expand Down Expand Up @@ -45,6 +46,32 @@ def test_query_simple_filter(xtdb_session: XTDBSession):
xtdb_session.commit()


def test_deleted_and_evicted(xtdb_session: XTDBSession, valid_time: datetime):
entity = TestEntity(name="test")

xtdb_session.put(entity, valid_time)
xtdb_session.commit()

xtdb_session.delete(entity)
xtdb_session.commit()

query = Query(TestEntity).where(TestEntity, name="test")
result = xtdb_session.client.query(query)
assert result == []

result = xtdb_session.client.query(query, valid_time)
assert result == [[{"TestEntity/name": "test", "type": "TestEntity", "xt/id": entity._pk}]]

xtdb_session.evict(entity)
xtdb_session.commit()

result = xtdb_session.client.query(query)
assert result == []

result = xtdb_session.client.query(query, valid_time)
assert result == []


def test_query_not_empty_on_reference_filter_for_entity(xtdb_session: XTDBSession):
test = TestEntity(name="test")
second1 = SecondEntity(test_entity=test, age=1)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def test_invalid_fields_name():
with pytest.raises(InvalidField) as ctx:
Query(TestEntity).where(TestEntity, wrong=TestEntity)

assert ctx.exconly() == 'xtdb.query.InvalidField: "wrong" is not a field of TestEntity'
assert ctx.exconly() == 'xtdb.exceptions.InvalidField: "wrong" is not a field of TestEntity'

with pytest.raises(InvalidField) as ctx:
Query(TestEntity).where(TestEntity, abc="def")

assert ctx.exconly() == 'xtdb.query.InvalidField: "abc" is not a field of TestEntity'
assert ctx.exconly() == 'xtdb.exceptions.InvalidField: "abc" is not a field of TestEntity'


def test_escaping_quotes():
Expand All @@ -71,12 +71,12 @@ def test_invalid_field_types():
with pytest.raises(InvalidField) as ctx:
Query(TestEntity).where(SecondEntity, test=InvalidField)

assert ctx.exconly() == 'xtdb.query.InvalidField: "test" is not a field of SecondEntity'
assert ctx.exconly() == 'xtdb.exceptions.InvalidField: "test" is not a field of SecondEntity'

with pytest.raises(InvalidField) as ctx:
Query(TestEntity).where(TestEntity, name=TestEntity)

assert ctx.exconly() == 'xtdb.query.InvalidField: "name" is not a relation of TestEntity'
assert ctx.exconly() == 'xtdb.exceptions.InvalidField: "name" is not a relation of TestEntity'


def test_allow_string_for_foreign_keys():
Expand Down
8 changes: 8 additions & 0 deletions xtdb/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
class XTDBException(Exception):
"""Base exception for XTDB errors."""


class InvalidField(XTDBException):
pass


class InvalidPath(XTDBException):
pass
9 changes: 1 addition & 8 deletions xtdb/query.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
from dataclasses import dataclass, field
from typing import List, Optional, Type, Union

from xtdb.exceptions import InvalidField
from xtdb.orm import TYPE_FIELD, Base


class InvalidField(ValueError):
pass


class InvalidPath(ValueError):
pass


@dataclass
class Query:
"""Object representing an XTDB query.
Expand Down
2 changes: 1 addition & 1 deletion xtdb/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def evict(self, document: Base, valid_time: Optional[datetime] = None) -> None:
if not valid_time:
valid_time = datetime.now(timezone.utc)

self._transaction.add(Operation(type=OperationType.EVICT, value=document.dict(), valid_time=valid_time))
self._transaction.add(Operation(type=OperationType.EVICT, value=document._pk, valid_time=valid_time))

def fn(self, document: Base, valid_time: Optional[datetime] = None) -> None:
if not valid_time:
Expand Down

0 comments on commit c20143a

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/Donnype/xtdb-py/commit/c20143a38495dd7687ef80db5532affc469a5386

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy