Skip to content

Commit

Permalink
Fix failing unit test due to sqlalchemy object not initialized
Browse files Browse the repository at this point in the history
Use duck typing check instead of isinstance.
Inside the test I can't instantiate a proper type object with
all the needed mocked properties.
  • Loading branch information
majamassarini committed Nov 27, 2024
1 parent 85ad870 commit 097a3fc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
8 changes: 4 additions & 4 deletions packit_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ def get_submitted_time_from_model(
) -> datetime:
# TODO: unify `submitted_name` (or better -> create for both models `task_accepted_time`)
# to delete this mess plz
if isinstance(model, CoprBuildTargetModel):
return model.build_submitted_time

return model.submitted_time
try:
return model.build_submitted_time # type: ignore[union-attr]
except AttributeError:
return model.submitted_time # type: ignore[union-attr]


@overload
Expand Down
23 changes: 10 additions & 13 deletions tests/unit/events/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ def mock_config():
def tf_models():
time = datetime(2000, 4, 28, 14, 9, 33, 860293)
latest_time = datetime.utcnow()
fake_tf = flexmock(pipeline_id="1", submitted_time=time, target="target")
flexmock(TFTTestRunTargetModel).new_instances(fake_tf)
tf = TFTTestRunTargetModel()
tf.__class__ = TFTTestRunTargetModel

another_fake_tf = flexmock(
pipeline_id="2",
submitted_time=latest_time,
target="target",
)
flexmock(TFTTestRunTargetModel).new_instances(another_fake_tf)
another_tf = TFTTestRunTargetModel()
another_tf.__class__ = TFTTestRunTargetModel
tf = flexmock(TFTTestRunTargetModel).new_instances().mock()
tf.pipeline_id = "1"
tf.submitted_time = time
tf.target = "target"

another_tf = flexmock(TFTTestRunTargetModel).new_instances().mock()
another_tf.pipeline_id = "2"
another_tf.submitted_time = latest_time
another_tf.target = "target"

yield [tf, another_tf]
22 changes: 9 additions & 13 deletions tests/unit/events/test_copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@ def copr_build_results_end():
def copr_models():
time = datetime(2000, 4, 28, 14, 9, 33, 860293)
latest_time = datetime.utcnow()
fake_copr = flexmock(build_id="1", build_submitted_time=time, target="target")
flexmock(CoprBuildTargetModel).new_instances(fake_copr)
copr = CoprBuildTargetModel()
copr.__class__ = CoprBuildTargetModel

another_fake_copr = flexmock(
build_id="2",
build_submitted_time=latest_time,
target="target",
)
flexmock(CoprBuildTargetModel).new_instances(another_fake_copr)
another_copr = CoprBuildTargetModel()
another_copr.__class__ = CoprBuildTargetModel
copr = flexmock(CoprBuildTargetModel).new_instances().mock()
copr.build_id = "1"
copr.build_submitted_time = time
copr.target = "target"

another_copr = flexmock(CoprBuildTargetModel).new_instances().mock()
another_copr.build_id = "2"
another_copr.build_submitted_time = latest_time
another_copr.target = "target"

yield [copr, another_copr]

Expand Down

0 comments on commit 097a3fc

Please sign in to comment.