Skip to content

Commit

Permalink
feat: ensure user_fk gets set on save
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperhodge committed Feb 25, 2025
1 parent de32ef1 commit f9d02a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions enterprise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,8 @@ def save(self, *args, **kwargs):
enterprise_customer=self.enterprise_customer,
)

self.user_fk = self.user_id

return super().save(*args, **kwargs)

@property
Expand Down
23 changes: 23 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,29 @@ def test_user_email_property_user_missing(self):
enterprise_customer_user = factories.EnterpriseCustomerUserFactory(user_id=42)
assert enterprise_customer_user.user_email is None

def test_on_create_should_set_user_fk_to_user_id(self):
"""Test that user_fk is set to user_id when creating a record."""
user_id = 123
enterprise_customer_user = factories.EnterpriseCustomerUserFactory(user_id=user_id)
enterprise_customer_user.refresh_from_db()

assert enterprise_customer_user.user_fk == user_id, (
f"Expected user_fk to be {user_id}, but got {enterprise_customer_user.user_fk}"
)

def test_on_update_should_set_user_fk_to_user_id(self):
"""Test that user_fk updates when user_id is modified and saved."""
enterprise_customer_user = factories.EnterpriseCustomerUserFactory(user_id=100)

new_user_id = 200
enterprise_customer_user.user_id = new_user_id
enterprise_customer_user.save()
enterprise_customer_user.refresh_from_db()

assert enterprise_customer_user.user_fk == new_user_id, (
f"Expected user_fk to update to {new_user_id}, but got {enterprise_customer_user.user_fk}"
)

@ddt.data("alberteinstein", "richardfeynman", "leosusskind")
def test_username_property_user_exists(self, username):
user_instance = factories.UserFactory(username=username)
Expand Down

0 comments on commit f9d02a6

Please sign in to comment.