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: Use old cycles during unmerge #4452

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 2 additions & 17 deletions seed/tests/test_property_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1709,21 +1709,6 @@ def test_properties_unmerge_without_losing_labels(self):
label_names = list(new_view.labels.values_list("name", flat=True))
self.assertCountEqual(label_names, [label_1.name, label_2.name])

def test_unmerging_assigns_new_canonical_records_to_each_resulting_records(self):
# Capture old property_ids
view = PropertyView.objects.first() # There's only one PropertyView
existing_property_ids = [
view.property_id,
self.property_1.id,
self.property_2.id,
]

# Unmerge the properties
url = reverse("api:v3:properties-unmerge", args=[view.id]) + f"?organization_id={self.org.pk}"
self.client.put(url, content_type="application/json")

self.assertFalse(PropertyView.objects.filter(property_id__in=existing_property_ids).exists())

def test_unmerging_two_properties_with_meters_gives_meters_to_both_of_the_resulting_records(self):
# Unmerge the properties
view_id = PropertyView.objects.first().id # There's only one PropertyView
Expand Down Expand Up @@ -1762,7 +1747,7 @@ def test_unmerge_results_in_the_use_of_new_canonical_records_and_deletion_of_old
url = reverse("api:v3:properties-unmerge", args=[view.id]) + f"?organization_id={self.org.pk}"
self.client.put(url, content_type="application/json")

self.assertFalse(Property.objects.filter(pk=property_id).exists())
self.assertTrue(Property.objects.filter(pk=property_id).exists())
self.assertEqual(Property.objects.count(), 2)

def test_unmerge_results_in_the_persistence_of_old_canonical_state_if_related_to_any_views(self):
Expand All @@ -1779,7 +1764,7 @@ def test_unmerge_results_in_the_persistence_of_old_canonical_state_if_related_to
self.client.put(url, content_type="application/json")

self.assertTrue(Property.objects.filter(pk=view.property_id).exists())
self.assertEqual(Property.objects.count(), 3)
self.assertEqual(Property.objects.count(), 2)


class PropertyViewExportTests(DataMappingBaseTestCase):
Expand Down
12 changes: 2 additions & 10 deletions seed/views/v3/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,23 +573,15 @@ def unmerge(self, request, pk=None):

# Clone the property record twice, then copy over meters
old_property = old_view.property
new_property = old_property
new_property.id = None
new_property.save()

new_property_2 = Property.objects.get(pk=new_property.id)
new_property_2 = Property.objects.get(pk=old_property.id)
new_property_2.id = None
new_property_2.save()

Property.objects.get(pk=new_property.id).copy_meters(old_view.property_id)
Property.objects.get(pk=new_property_2.id).copy_meters(old_view.property_id)

# If canonical Property is NOT associated to a different -View, delete it
if not PropertyView.objects.filter(property_id=old_view.property_id).exclude(id=old_view.id).exists():
Property.objects.get(pk=old_view.property_id).delete()

# Create the views
new_view1 = PropertyView(cycle_id=cycle_id, property_id=new_property.id, state=state1)
new_view1 = PropertyView(cycle_id=cycle_id, property_id=old_property.id, state=state1)
new_view2 = PropertyView(cycle_id=cycle_id, property_id=new_property_2.id, state=state2)

# Mark the merged state as deleted
Expand Down