Skip to content

Commit

Permalink
Removes nesting of get_next_submission_offset
Browse files Browse the repository at this point in the history
- Moves get_next_submission_offset to top level of data module
- Refactors test_data to account for this change
  • Loading branch information
aidanlw17 committed May 20, 2019
1 parent ec75157 commit 973c285
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
36 changes: 19 additions & 17 deletions db/data.py
Expand Up @@ -189,22 +189,6 @@ def _insert_lowlevel_json(connection, ll_id, data_json, data_sha256, version_id)
"data_sha256": data_sha256,
"version": version_id})

def _get_submission_offset(connection, mbid):
""" Get highest existing submission offset for mbid, then increment """
query = text("""
SELECT MAX(submission_offset) as max_offset
FROM lowlevel
WHERE gid = :mbid
""")
result = connection.execute(query, {"mbid": mbid})

row = result.fetchone()
if row["max_offset"] is not None:
return row["max_offset"] + 1
else:
# No previous submission
return 0

is_lossless_submit = data['metadata']['audio_properties']['lossless']
version = data['metadata']['version']
build_sha1 = version['essentia_build_sha']
Expand All @@ -218,7 +202,7 @@ def _get_submission_offset(connection, mbid):
return

try:
submission_offset = _get_submission_offset(connection, mbid)
submission_offset = get_next_submission_offset(connection, mbid)
ll_id = _insert_lowlevel(connection, mbid, build_sha1, is_lossless_submit, is_mbid, submission_offset)
version_id = insert_version(connection, version, VERSION_TYPE_LOWLEVEL)
_insert_lowlevel_json(connection, ll_id, data_json, data_sha256, version_id)
Expand All @@ -227,6 +211,24 @@ def _get_submission_offset(connection, mbid):
raise db.exceptions.BadDataException(
"data is badly formed")


def get_next_submission_offset(connection, mbid):
""" Get highest existing submission offset for mbid, then increment """
query = text("""
SELECT MAX(submission_offset) as max_offset
FROM lowlevel
WHERE gid = :mbid
""")
result = connection.execute(query, {"mbid": mbid})

row = result.fetchone()
if row["max_offset"] is not None:
return row["max_offset"] + 1
else:
# No previous submission
return 0


def add_model(model_name, model_version, model_status=STATUS_HIDDEN):
if model_status not in MODEL_STATUSES:
raise Exception("model_status must be one of %s" % ",".join(MODEL_STATUSES))
Expand Down
23 changes: 8 additions & 15 deletions db/test/test_data.py
Expand Up @@ -77,32 +77,25 @@ def test_submit_low_level_data_missing_keys(self, clean, write, sanity):
db.data.submit_low_level_data(self.test_mbid, self.test_lowlevel_data, gid_types.GID_TYPE_MBID)


def test_write_low_level_submission_offset(self):

def _get_submission_offset(connection, mbid):
query = text("""
SELECT MAX(submission_offset) as max_offset
FROM lowlevel
WHERE gid = :mbid
""")
result = connection.execute(query, {"mbid": mbid})
row = result.fetchone()
return row["max_offset"]

def test_get_next_submission_offset(self):
# Check that next max offset is returned
with db.engine.connect() as connection:
one = {"data": "one", "metadata": {"audio_properties": {"lossless": True}, "version": {"essentia_build_sha": "x"}}}
two = {"data": "two", "metadata": {"audio_properties": {"lossless": True}, "version": {"essentia_build_sha": "x"}}}
three = {"data": "three", "metadata": {"audio_properties": {"lossless": True}, "version": {"essentia_build_sha": "x"}}}

db.data.write_low_level(self.test_mbid, one, gid_types.GID_TYPE_MBID)
self.assertEqual(0, _get_submission_offset(connection, self.test_mbid))
self.assertEqual(1, db.data.get_next_submission_offset(connection, self.test_mbid))

# Adding second submission, max offset is incremented
db.data.write_low_level(self.test_mbid, two, gid_types.GID_TYPE_MBID)
self.assertEqual(1, _get_submission_offset(connection, self.test_mbid))
self.assertEqual(2, db.data.get_next_submission_offset(connection, self.test_mbid))

# Before any submissions exist, returns 0
self.assertEqual(0, db.data.get_next_submission_offset(connection, self.test_mbid_two))

db.data.write_low_level(self.test_mbid_two, three, gid_types.GID_TYPE_MBID)
self.assertEqual(0, _get_submission_offset(connection, self.test_mbid_two))
self.assertEqual(1, db.data.get_next_submission_offset(connection, self.test_mbid_two))


def test_write_load_low_level(self):
Expand Down

0 comments on commit 973c285

Please sign in to comment.