-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assign fk/m2m on create and support delete
- Loading branch information
1 parent
c1a3337
commit 6eede3d
Showing
19 changed files
with
321 additions
and
284 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
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 |
---|---|---|
@@ -1,82 +1,65 @@ | ||
import pytest | ||
from uuid import UUID | ||
|
||
from uuid import uuid4 | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.test import RequestFactory | ||
|
||
from tests.models import DummyModel | ||
from tests.views import DummyAPI | ||
uuid = uuid4() | ||
email = "[email protected]" | ||
phone = "(555) 555-5555" | ||
|
||
|
||
test_uuid = "ce6a5b4f-599d-4442-8a74-d7a8d2b54854" | ||
test_email = "[email protected]" | ||
test_phone = "(555) 555-5555" | ||
@pytest.fixture(name="profile_view") | ||
def profile_view_fixture(db, profile_factory): | ||
from django.test import RequestFactory | ||
|
||
from tests.views import ProfileDetail | ||
|
||
@pytest.fixture | ||
@pytest.mark.django_db | ||
def view(): | ||
view = DummyAPI() | ||
profile_factory.create(email=email, phone=phone) | ||
view = ProfileDetail() | ||
view.bundle = { | ||
"id": test_uuid, | ||
"email": test_email, | ||
"phone": test_phone, | ||
"id": str(uuid), | ||
"email": email, | ||
"phone": phone, | ||
} | ||
DummyModel.objects.create(id=UUID(test_uuid), email=test_email, phone=test_phone) | ||
view.request = RequestFactory().patch(f"/{test_uuid}/") | ||
view.kwargs = dict(id=test_uuid) | ||
view.request = RequestFactory().patch(f"/{uuid}/") | ||
view.kwargs = dict(id=str(uuid)) | ||
view.serializer = None | ||
return view | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_validate_bundle(view): | ||
assert view.validate_bundle("id") | ||
assert view.validate_bundle("email") | ||
assert view.validate_bundle("phone") | ||
def test_validate_bundle(profile_view): | ||
assert profile_view.validate_bundle("id") | ||
assert profile_view.validate_bundle("email") | ||
assert profile_view.validate_bundle("phone") | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_validate_uuid_accepts_str(view): | ||
string = "ce6a5b4f-599d-4442-8a74-d7a8d2b54854" | ||
result = view.validate_uuid(string) | ||
assert result == UUID(string) | ||
def test_validate_uuid_accepts_str(profile_view): | ||
assert profile_view.validate_uuid(str(uuid)) == uuid | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_validate_uuid_accepts_uuid(view): | ||
uuid = UUID("ce6a5b4f-599d-4442-8a74-d7a8d2b54854") | ||
result = view.validate_uuid(uuid) | ||
assert result == uuid | ||
def test_validate_uuid_accepts_uuid(profile_view): | ||
assert profile_view.validate_uuid(uuid) == uuid | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_validate_uuid_raises_error(view): | ||
string = "not-a-uuid" | ||
def test_validate_uuid_raises_error(profile_view): | ||
with pytest.raises(ValidationError): | ||
view.validate_uuid(string) | ||
profile_view.validate_uuid("not-a-uuid") | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_validate_email_passes(view): | ||
email = "[email protected]" | ||
result = view.validate_email(email) | ||
assert email == result | ||
def test_validate_email_passes(profile_view): | ||
assert profile_view.validate_email(email) == email | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_validate_email_raises_error(view): | ||
email = "fake.example@com" | ||
def test_validate_email_raises_error(profile_view): | ||
with pytest.raises(ValidationError): | ||
view.validate_email(email) | ||
profile_view.validate_email("fake.example@com") | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_validate_custom_field_passes(view): | ||
phone = "(555) 555-5555" | ||
assert view.validate_phone(phone) == "+5555555555" | ||
def test_validate_custom_field_passes(profile_view): | ||
assert profile_view.validate_phone(phone) == "+5555555555" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_validate_custom_field_raises_error(view): | ||
phone = "invalid number" | ||
def test_validate_custom_field_raises_error(profile_view): | ||
with pytest.raises(ValidationError): | ||
view.validate_phone(phone) | ||
profile_view.validate_phone("invalid number") |
Oops, something went wrong.