Skip to content

Commit

Permalink
馃帹 Apply black to ingest_runs common files
Browse files Browse the repository at this point in the history
  • Loading branch information
gsantia committed Jun 1, 2021
1 parent fd0e11b commit e87f5ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
34 changes: 15 additions & 19 deletions creator/ingest_runs/common/model.py
Expand Up @@ -20,7 +20,7 @@ def camel_to_snake(camel_str):
"""
Convert CamelCase to snake_case
"""
return re.sub(r'(?<!^)(?=[A-Z])', '_', camel_str).lower()
return re.sub(r"(?<!^)(?=[A-Z])", "_", camel_str).lower()


def hash_versions(version_kf_ids):
Expand All @@ -45,26 +45,23 @@ class IngestProcess(models.Model):
Common model functionality for ingest processes
(e.g. ingest run, validation run)
"""

__queue__ = settings.INGEST_QUEUE

class Meta:
abstract = True

id = models.UUIDField(
primary_key=True, default=uuid.uuid4, editable=False
)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
created_at = models.DateTimeField(
auto_now_add=True,
null=True,
help_text="Time when the ingest process was created",
)
started_at = models.DateTimeField(
null=True,
help_text="Time when ingest process started running"
null=True, help_text="Time when ingest process started running"
)
stopped_at = models.DateTimeField(
null=True,
help_text="Time when ingest process stopped running"
null=True, help_text="Time when ingest process stopped running"
)
creator = models.ForeignKey(
User,
Expand All @@ -87,7 +84,7 @@ class Meta:
)
state = FSMField(
default=State.NOT_STARTED,
help_text="The current state of the ingest process"
help_text="The current state of the ingest process",
)
job_log = models.OneToOneField(
JobLog,
Expand Down Expand Up @@ -135,7 +132,7 @@ def complete(self):
@transition(
field=state,
source=[State.NOT_STARTED, State.RUNNING],
target=State.CANCELED
target=State.CANCELED,
)
def cancel(self):
"""
Expand All @@ -149,7 +146,7 @@ def cancel(self):
@transition(
field=state,
source=[State.NOT_STARTED, State.RUNNING, State.CANCELED],
target=State.FAILED
target=State.FAILED,
)
def fail(self):
"""
Expand All @@ -166,32 +163,31 @@ def compute_input_hash(self):
This will be used to determine whether an ingest process is already
running for a set of file versions
"""
return hash_versions(
self.versions.values_list("pk", flat=True)
)
return hash_versions(self.versions.values_list("pk", flat=True))

def _save_event(self, event_type):
""" Create and save an event for an ingest process state transition """
"""Create and save an event for an ingest process state transition"""
from creator.events.models import Event

snake_name = camel_to_snake(self.__class__.__name__)
name = camel_to_snake(self.__class__.__name__).replace("_", " ")
prefix = "".join([w[0] for w in snake_name.split("_")]).upper()
msgs = {
State.RUNNING: (
f"{prefix}_STA",
f"{self.creator.username} started {name} {self.pk}"
f"{self.creator.username} started {name} {self.pk}",
),
State.COMPLETED: (
f"{prefix}_COM",
f"{name.title()} {self.pk} completed"
f"{name.title()} {self.pk} completed",
),
State.CANCELED: (
f"{prefix}_CAN",
f"{self.creator.username} canceled {name} {self.pk}"
f"{self.creator.username} canceled {name} {self.pk}",
),
State.FAILED: (
f"{prefix}_FAI",
f"{name.title()} {self.pk} failed"
f"{name.title()} {self.pk} failed",
),
}
event_name, message = msgs[event_type]
Expand Down
24 changes: 12 additions & 12 deletions tests/ingest_runs/test_common.py
Expand Up @@ -9,7 +9,7 @@
IngestProcess,
)
from creator.ingest_runs.common.mutations import (
cancel_duplicate_ingest_processes
cancel_duplicate_ingest_processes,
)
from creator.files.models import Version
from creator.ingest_runs.models import (
Expand Down Expand Up @@ -70,18 +70,20 @@ def test_hash_versions(db, clients, prep_file):
[
ValidationRun,
IngestRun,
]
],
)
def test_ingest_process_states(
db, mocker, ingest_process_cls, start_state, state_transition_method,
expected_msg
db,
mocker,
ingest_process_cls,
start_state,
state_transition_method,
expected_msg,
):
"""
Test that correct events are fired on ingest process state transitions
"""
mock_stop_job = mocker.patch(
"creator.ingest_runs.common.model.stop_job"
)
mock_stop_job = mocker.patch("creator.ingest_runs.common.model.stop_job")
creator = User.objects.first()
obj = ingest_process_cls(creator=creator, state=start_state)
obj.save()
Expand Down Expand Up @@ -121,7 +123,7 @@ def test_ingest_process_states(
(IngestRunFactory, cancel_ingest, State.RUNNING),
(ValidationRunFactory, cancel_validation, State.NOT_STARTED),
(ValidationRunFactory, cancel_validation, State.RUNNING),
]
],
)
def test_cancel_duplicate_ingest_processes(
db, mocker, clients, prep_file, factory, cancel_task, state
Expand All @@ -146,14 +148,12 @@ def test_cancel_duplicate_ingest_processes(
version_ids[0:1], process.__class__, cancel_task
)
assert canceled_any
mock_queue.enqueue.assert_called_with(
cancel_task, args=(process.id,)
)
mock_queue.enqueue.assert_called_with(cancel_task, args=(process.id,))
mock_queue.reset_mock()

# Ingest processes with different versions won't be canceled
canceled_any = cancel_duplicate_ingest_processes(
version_ids, process.__class__, cancel_task
)
mock_queue.enqueue.call_count == 0
assert (not canceled_any)
assert not canceled_any

0 comments on commit e87f5ac

Please sign in to comment.