Skip to content

Commit

Permalink
attempt to fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nboyse committed Nov 15, 2023
1 parent c86f64c commit 5ca30e0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
11 changes: 7 additions & 4 deletions commodities/models/dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def get_dependent_measures(
measure_qs = Measure.objects.filter(goods_sid_query)

if self.moment.clock_type.is_transaction_clock:
measure_qs = measure_qs.current()
measure_qs = measure_qs.approved_up_to_transaction(self.moment.transaction)
else:
measure_qs = measure_qs.latest_approved()

Expand Down Expand Up @@ -823,7 +823,7 @@ def get_snapshot(
date=snapshot_date,
)

commodities = self._get_snapshot_commodities(snapshot_date)
commodities = self._get_snapshot_commodities(transaction, snapshot_date)

return CommodityTreeSnapshot(
moment=moment,
Expand All @@ -832,6 +832,7 @@ def get_snapshot(

def _get_snapshot_commodities(
self,
transaction: Transaction,
snapshot_date: date,
) -> List[Commodity]:
"""
Expand All @@ -852,10 +853,12 @@ def _get_snapshot_commodities(
that match the latest_version goods.
"""
item_ids = {c.item_id for c in self.commodities if c.obj}
goods = GoodsNomenclature.objects.filter(
goods = GoodsNomenclature.objects.approved_up_to_transaction(
transaction,
).filter(
item_id__in=item_ids,
valid_between__contains=snapshot_date,
).current()
)

latest_versions = get_latest_versions(goods)
pks = {good.pk for good in latest_versions}
Expand Down
12 changes: 6 additions & 6 deletions common/business_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def get_linked_models(
related_instances = [getattr(model, field.name)]
for instance in related_instances:
try:
yield instance.version_at()
yield instance.version_at(transaction)
except TrackedModel.DoesNotExist:
# `related_instances` will contain all instances, even
# deleted ones, and `version_at` will return
Expand Down Expand Up @@ -278,8 +278,8 @@ def validate(self, model):

if (
type(model)
.objects.current()
.filter(**query)
.objects.filter(**query)
.approved_up_to_transaction(self.transaction)
.exclude(version_group=model.version_group)
.exists()
):
Expand All @@ -305,8 +305,8 @@ def validate(self, model):
query["valid_between__overlap"] = model.valid_between

if (
model.__class__.objects.current()
.filter(**query)
model.__class__.objects.filter(**query)
.approved_up_to_transaction(self.transaction)
.exclude(version_group=model.version_group)
.exists()
):
Expand Down Expand Up @@ -573,7 +573,7 @@ def validate(self, exclusion):
Membership = geo_group._meta.get_field("members").related_model

if (
not Membership.objects.current()
not Membership.objects.approved_up_to_transaction(self.transaction)
.filter(
geo_group__sid=geo_group.sid,
member__sid=exclusion.excluded_geographical_area.sid,
Expand Down
12 changes: 6 additions & 6 deletions common/models/trackedmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,15 @@ def current_version(self: Cls) -> Cls:
raise self.__class__.DoesNotExist("Object has no current version")
return current_version

def version_at(self: Cls) -> Cls:
def version_at(self: Cls, transaction) -> Cls:
"""
The latest version of this model that was approved as of the given
transaction.
:param transaction Transaction: Limit versions to this transaction
:rtype TrackedModel:
"""
return self.get_versions().current().get()
return self.get_versions().approved_up_to_transaction(transaction).get()

@classproperty
def copyable_fields(cls):
Expand Down Expand Up @@ -504,12 +504,12 @@ def copy(
kwargs.update(nested_fields)

if not ignore:
for model in queryset.current():
for model in queryset.approved_up_to_transaction(transaction):
model.copy(transaction, **kwargs)

return new_object

def in_use_by(self, via_relation: str) -> QuerySet[TrackedModel]:
def in_use_by(self, via_relation: str, transaction=None) -> QuerySet[TrackedModel]:
"""
Returns all of the models that are referencing this one via the
specified relation and exist as of the passed transaction.
Expand All @@ -526,7 +526,7 @@ def in_use_by(self, via_relation: str) -> QuerySet[TrackedModel]:

return remote_model.objects.filter(
**{f"{remote_field_name}__version_group": self.version_group}
).current()
).approved_up_to_transaction(transaction)

def in_use(self, transaction=None, *relations: str) -> bool:
"""
Expand Down Expand Up @@ -572,7 +572,7 @@ def in_use(self, transaction=None, *relations: str) -> bool:

# If we find any objects for any relation, then the model is in use.
for relation_name in using_models:
relation_queryset = self.in_use_by(relation_name)
relation_queryset = self.in_use_by(relation_name, transaction)
if relation_queryset.exists():
return True

Expand Down
2 changes: 1 addition & 1 deletion measures/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def duty_sentence(
# Components with the greatest transaction_id that is less than
# or equal to component_parent's transaction_id, are considered 'current'.
component_qs = component_parent.components.approved_up_to_transaction(
transaction=component_parent.transaction
component_parent.transaction,
)
if not component_qs:
return ""
Expand Down
2 changes: 1 addition & 1 deletion measures/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Meta:
excluded_origin_descriptions = factory.LazyAttribute(
lambda m: random.choice(MeasureSheetRow.separators).join(
e.excluded_geographical_area.descriptions.approved_up_to_transaction(
transaction=e.excluded_geographical_area
transaction=e.excluded_geographical_area.transaction,
)
.last()
.description
Expand Down
6 changes: 5 additions & 1 deletion workbaskets/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ def workbasket(self) -> WorkBasket:

def get_queryset(self):
qs = super().get_queryset()
transaction = None
current = self.workbasket
if current:
transaction = current.transactions.last()

return qs.current()
return qs.approved_up_to_transaction(transaction)

0 comments on commit 5ca30e0

Please sign in to comment.