Skip to content

Commit

Permalink
Merge pull request #8 from swiss-seismological-service/krischer/deter…
Browse files Browse the repository at this point in the history
…ministic-order-of-origins

Force a deterministic order of event origins
  • Loading branch information
mrosskopf authored May 29, 2024
2 parents 4c5b73f + 9afbf27 commit a6d29bb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
5 changes: 4 additions & 1 deletion dug_seis/db/sqlite_db_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def to_str(v):

results = []

sql += " ORDER BY _oid ASC"

for row in self.cursor.execute(sql).fetchall():
result = {}
for name, r in zip(column_names, row):
Expand Down Expand Up @@ -588,7 +590,8 @@ def to_object(o_type: str, r):
# Origins are stored via origin references.
origin_refs = self.cursor.execute(
"SELECT _oid, _parent_oid, originID from OriginReference "
f"WHERE _parent_oid in ({','.join(['?'] * len(object_results))})",
f"WHERE _parent_oid in ({','.join(['?'] * len(object_results))}) "
"ORDER BY _oid ASC",
[o._object_id for o in object_results],
).fetchall()
if origin_refs:
Expand Down
12 changes: 4 additions & 8 deletions dug_seis/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
DATA_DIR = pathlib.Path(__file__).parent / "data"


def _sort_origins(event):
event.origins = sorted(event.origins, key=lambda x: str(x.resource_id))


def test_basic_db(tmp_path):
url = f"sqlite://{tmp_path / 'db.sqlite'}"
db = DB(url=url)
Expand Down Expand Up @@ -1213,8 +1209,8 @@ def test_write_and_read_same_picks_used_in_multiple_arrivals():
# unused pick.
del event.picks[0]

_sort_origins(event)
_sort_origins(events[0])
event
events[0]

# Now they are the same.
assert event == events[0]
Expand Down Expand Up @@ -1266,8 +1262,8 @@ def test_add_origin_to_event_and_change_preferred_origin():

# Adding to the database should change it.
db.update_event(event=event)
assert _sort_origins(db.get_objects(object_type="Event")[0]) == _sort_origins(event)
assert db.get_objects(object_type="Event")[0].preferred_origin() == event.origins[1]
assert db.get_objects(object_type="Event")[0] == event
assert db.get_objects(object_type="Event")[0].preferred_origin() == event.preferred_origin()

assert db.count("Event") == 1
assert db.count("Origin") == 2
Expand Down

0 comments on commit a6d29bb

Please sign in to comment.