Skip to content

refactor(likes): make like/unlike operations atomic and enhance like action tests #60

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

Merged
merged 2 commits into from
Jul 17, 2025

Conversation

RandomProgramm3r
Copy link
Owner

Wrap the like and unlike logic in django.db.transaction.atomic() to ensure that the creation/deletion of Like records and the corresponding update of the promo’s like_count occur as a single, indivisible operation. This prevents race conditions and data inconsistencies. After updating like_count using F(), call promo.refresh_from_db() to reflect the latest database state in the model instance.

Refactor the TestUserPromoLikeActions test suite for clarity and maintainability:

  • Introduce helper methods (_create_user_and_get_token, _create_promo, _auth, _get_user_promo, _get_business_promo, _like, _unlike) to eliminate duplication.
  • Consolidate granular tests into comprehensive cases (test_initial_promo_state, test_like_action, test_like_idempotency, etc.).
  • Remove reliance on ordered execution by cleaning up test data in tearDown(), ensuring test isolation and robustness.

Wrap the like and unlike logic in `django.db.transaction.atomic()` to ensure that the creation/deletion of the like and the update of the promo's `like_count` are performed as a single, indivisible operation. This prevents potential race conditions and data  inconsistency.

Add `promo.refresh_from_db()` after the `F()` expression update to ensure the promo object reflects the latest database state.
Refactor the `TestUserPromoLikeActions` test case to be more concise and readable.

- Introduce helper methods (`_create_user_and_get_token`, `_create_promo`, `_auth`, `_get_user_promo`, `_get_business_promo`, `_like`, `_unlike`) to reduce code duplication and improve clarity.
- Consolidate multiple, granular tests into fewer, more comprehensive tests (`test_initial_promo_state`, `test_like_action`, `test_like_idempotency`, etc.).
- Remove the dependency on a strict, numbered execution order, making the test suite more robust and easier to maintain.
- Add a `tearDown` method to clean up credentials after each test.
@RandomProgramm3r RandomProgramm3r merged commit a2bf351 into main Jul 17, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant