Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix modify and save test, add "updated" to modify #416

Open
wants to merge 37 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/execution_engine2/db/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,13 @@ class Job(Document):
meta = {"collection": "ee2_jobs"}

def save(self, *args, **kwargs):
self.updated = time.time()
self.updated = datetime.utcnow().timestamp()
return super(Job, self).save(*args, **kwargs)

def modify(self, query=None, **update):
update["updated"] = datetime.utcnow().timestamp()
return super(Job, self).modify(query=None, **update)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this just pass the query as is, not pass None?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming I understand this I guess no code uses the query field otherwise tests should've broken

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea it should pass the query good call

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did the tests pass with this bug?


def __repr__(self):
return self.to_json()

Expand Down
42 changes: 40 additions & 2 deletions test/tests_for_db/ee2_MongoUtil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import unittest
from datetime import datetime

import json
from bson.objectid import ObjectId

from execution_engine2.db.MongoUtil import MongoUtil
Expand Down Expand Up @@ -58,17 +58,55 @@ def test_init_ok(self):
mongo_util = self.getMongoUtil()
self.assertTrue(set(class_attri) <= set(mongo_util.__dict__.keys()))

def test_save_and_modify_updates_timestamp(self):
job = get_example_job(status=Status.created.value)
updated_ts = job.updated
job.save()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its seems a bit odd that saving a job updates the timestamp even if there are no changes, is that what you want?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, updated tracks the last time the job was saved or modified, so seems ok to me. What do you suggest?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean updated to me means that the job was updated, e.g. changed in some way. It doesn't make sense to me for the updated timestamp to change if the job hasn't changed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, so if we make sure to only save jobs with changes, then we should be alright?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if its possible to know if a record has a different value or not when saving unless we reload the record or do two saves?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, so if we make sure to only save jobs with changes, then we should be alright?

I think so, but we should document the behavior clearly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if its possible to know if a record has a different value or not when saving unless we reload the record or do two saves?

You can do a query on the entire record, but that's probably overkill. It'd be nice if Mongo had a feature where a field would only be updated if at least one of a set of provided fields changed

job.reload()
new_updated_ts = job.updated
assert new_updated_ts > updated_ts
# Now check modify
job.modify(add_to_set__child_jobs=["a"])
assert job.updated > new_updated_ts

def test_insert_jobs(self):
"""Check to see that jobs are inserted into mongo"""
job = get_example_job(status=Status.created.value)
job2 = get_example_job(status=Status.created.value)
job3 = get_example_job(status=Status.created.value)
# job3.updated = datetime.utcnow().timestamp()

json_repr = [job.to_json(), job2.to_json(), job3.to_json()]
mongo_repr = [job.to_mongo(), job2.to_mongo(), job3.to_mongo()]

jobs_to_insert = [job, job2]
job_ids = self.getMongoUtil().insert_jobs(jobs_to_insert)
assert len(job_ids) == len(jobs_to_insert)
retrieved_jobs = self.getMongoUtil().get_jobs(job_ids=job_ids)

for i, retrieved_job in enumerate(retrieved_jobs):
assert jobs_to_insert[i].to_json() == retrieved_job.to_json()
original_job_json = json.loads(json_repr[i])
original_job_mongo = mongo_repr[i]

retrieved_job_json = json.loads(retrieved_job.to_json())
retrieved_job_mongo = retrieved_job.to_mongo()

# Cannot compare `id` or `updated` for a job that wasn't yet saved,
# and save/updates over-write the updated timestamp
# so we should remove them before comparison

for item in [
original_job_json,
original_job_mongo,
retrieved_job_mongo,
retrieved_job_json,
]:
for field in ["updated", "_id"]:
if field in item:
del item[field]

assert original_job_json == retrieved_job_json
assert original_job_mongo == retrieved_job_mongo

def test_update_jobs_enmasse(self):
"""Check to see that created jobs get updated to queued"""
Expand Down