Skip to content

Commit

Permalink
Made more readable (pep8) (#10)
Browse files Browse the repository at this point in the history
* made more readable

* made more readable

* made more readable

* made more readable
  • Loading branch information
hkey0 authored Dec 31, 2023
1 parent 8e31bb1 commit c3aa593
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/sinker/sinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


class Sinker:
def __init__(self, view, index):
def __init__(self, view: str, index: str):
"""
:param view: Postgres materialized view name
:param index: Elasticsearch index name
Expand Down Expand Up @@ -83,7 +83,7 @@ def recreate_index(self) -> None:
settings=index_body["settings"],
)

def setup_pg(self):
def setup_pg(self) -> None:
"""
Creates materialized views with supporting functions and triggers, and sets the parent table that drives the
materialized view
Expand Down
10 changes: 6 additions & 4 deletions tests/test_bulk_action_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@


@pytest.fixture
def bulk_action_generator():
def bulk_action_generator() -> BulkActionGenerator:
v2i = dict(foo_mv="foo_index")
pt2i = dict(foo_table="foo_index")
return BulkActionGenerator(views_to_indices=v2i, parent_tables_to_indices=pt2i)
return BulkActionGenerator(
views_to_indices=v2i,
parent_tables_to_indices=pt2i)


def test_index_action(bulk_action_generator):
def test_index_action(bulk_action_generator: BulkActionGenerator) -> None:
doc = '{"name" : "Foo Bar"}'
slot_dict = dict(table="foo_mv", id="a-1")
expected = {"_index": "foo_index", "_id": "a-1", "_source": doc}
assert bulk_action_generator.index_action(doc, slot_dict) == expected


def test_delete_action(bulk_action_generator):
def test_delete_action(bulk_action_generator: BulkActionGenerator) -> None:
slot_dict = dict(table="foo_table", id="a-1")
expected = {"_op_type": "delete", "_index": "foo_index", "_id": "a-1"}
assert bulk_action_generator.delete_action(slot_dict) == expected
24 changes: 16 additions & 8 deletions tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,25 @@ def wait_for_pg_es():

def test_exception_propagation_during_refresh(wait_for_pg_es):
runner = Runner()
# Update a record in Postgres but remove the materialized view so it errors when we iterate
# Update a record in Postgres but remove the materialized view so it
# errors when we iterate
conn = psycopg.connect(autocommit=True)
cur = conn.cursor()
cur.execute("update public.person set name = 'Uh oh' where id = 'p-1'")
cur.execute("drop materialized view sinker.person_mv")
# make sure the exception gets raised from the thread versus silently consuming it
# make sure the exception gets raised from the thread versus silently
# consuming it
with pytest.raises(psycopg.errors.UndefinedTable):
runner.iterate()


def test_exception_propagation_during_setup(wait_for_pg_es, mocker):
# mock es.get_client() to raise an exception in the thread that sets up the sinker
mocker.patch("sinker.sinker.get_client", side_effect=elasticsearch.exceptions.ConnectionError('Boom!'))
# make sure the exception gets raised from the thread versus silently consuming it
# mock es.get_client() to raise an exception in the thread that sets up
# the sinker
mocker.patch("sinker.sinker.get_client",
side_effect=elasticsearch.exceptions.ConnectionError('Boom!'))
# make sure the exception gets raised from the thread versus silently
# consuming it
with pytest.raises(elasticsearch.exceptions.ConnectionError):
Runner()

Expand Down Expand Up @@ -92,12 +97,14 @@ def test_end_to_end(wait_for_pg_es):
assert doc["_source"] == expected

# Update a record in Postgres
psycopg.connect(autocommit=True).execute("update public.person set name = 'Jane' where id = 'p-1'")
psycopg.connect(autocommit=True).execute(
"update public.person set name = 'Jane' where id = 'p-1'")
runner.iterate()
# Verify the update made it into the people index
doc = es.get(index="people", id="p-1")
assert doc["_source"]["name"] == "Jane"
# Verify the update made it into the courses index through person->student->enrollment->course.
# Verify the update made it into the courses index through
# person->student->enrollment->course.
doc = es.get(index="courses", id="c-1")
expected = {
"name": "Reth",
Expand Down Expand Up @@ -137,7 +144,8 @@ def test_end_to_end(wait_for_pg_es):
# Verify the doc got deleted from Elasticsearch
with pytest.raises(elasticsearch.NotFoundError):
es.get(index="people", id="p-1")
# Verify the doc got deleted from Elasticsearch through person->student->enrollment->course.
# Verify the doc got deleted from Elasticsearch through
# person->student->enrollment->course.
doc = es.get(index="courses", id="c-1")
expected = {
"name": "Reth",
Expand Down

0 comments on commit c3aa593

Please sign in to comment.