-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(test): add 3 simple tests covering core usage
- all 3 are taken straight from the README as well, and cover whitelists, one-to-one relations, and foreign key relations - should add more tests for larger sets of relations, but these do cover the vast majority of functionality and most common use cases (that are even in the README) (env): gitignore pytest_cache directory
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import pytest | ||
from .models import User, Settings, Post | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_whitelist(): | ||
new_user = User.objects.create( | ||
name='John Doe', | ||
email='[email protected]', | ||
) | ||
|
||
assert new_user.serialize() == {'name': 'John Doe'} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_one_to_one(): | ||
new_user = User.objects.create( | ||
name='John Doe', | ||
email='[email protected]', | ||
) | ||
Settings.objects.create(user=new_user) | ||
|
||
new_user_refreshed = (User.objects.select_related('settings') | ||
.get(pk=new_user.pk)) | ||
|
||
assert new_user_refreshed.serialize() == {'name': 'John Doe'} | ||
# recursively serialize Settings object by passing the join in | ||
assert new_user_refreshed.serialize('settings') == \ | ||
{'name': 'John Doe', 'settings': {'email_notifications': False}} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_foreign_key(): | ||
new_user = User.objects.create( | ||
name='John Doe', | ||
email='[email protected]', | ||
) | ||
Post.objects.create(user=new_user, text='wat a nice post') | ||
Post.objects.create(user=new_user, text='another nice post') | ||
|
||
serialized_posts = [ | ||
{'id': 1, 'text': 'wat a nice post', 'user_id': 1}, | ||
{'id': 2, 'text': 'another nice post', 'user_id': 1} | ||
] | ||
|
||
# called on QuerySet | ||
# adds an _id to the foreign key name, just like when using `.values()` | ||
assert Post.objects.all().serialize() == serialized_posts | ||
|
||
# called on Manager | ||
assert User.objects.get(pk=new_user.pk).post_set.serialize() == \ | ||
serialized_posts | ||
|
||
# recursively serialize Post objects by passing the join in | ||
assert (User.objects.prefetch_related('post_set').get(pk=new_user.pk) | ||
.serialize('post_set')) == \ | ||
{'name': 'John Doe', 'post_set': serialized_posts} |