Skip to content

Commit

Permalink
Assign fk/m2m on create and support delete
Browse files Browse the repository at this point in the history
  • Loading branch information
stevelacey committed Nov 12, 2021
1 parent e0090c0 commit 1ce6e82
Show file tree
Hide file tree
Showing 19 changed files with 320 additions and 265 deletions.
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[pytest]
addopts =
--cov
--cov-fail-under 75
--cov-fail-under 78
--cov-report term:skip-covered
--cov-report html
--no-cov-on-fail
Expand Down
38 changes: 27 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import pytest
from pytest_factoryboy import register

import django
from django.conf import settings
from django.utils import timezone


def pytest_configure():
"""Initialize Django settings."""
import django
from django.conf import settings

settings.configure(
SECRET_KEY="secret",
DEBUG=True,
Expand Down Expand Up @@ -61,16 +60,33 @@ def pytest_configure():
register(UserFactory, "user")


@pytest.fixture(name="admin_client")
def admin_client_fixture(db, admin_user):
from worf.testing import ApiClient

client = ApiClient()
client.force_login(admin_user)
return client


@pytest.fixture(name="client")
def client_fixture():
from worf.testing import ApiClient

return ApiClient()


@pytest.fixture(name="now")
def now_fixture():
return timezone.now()
from django.utils import timezone

return timezone.now()

@pytest.fixture(name="profile_url")
def profile_url_fixture(profile):
return f"/profiles/{profile.pk}/"

@pytest.fixture(name="user_client")
def user_client_fixture(db, user):
from worf.testing import ApiClient

@pytest.fixture(name="user_url")
def user_url_fixture(user):
return f"/users/{user.pk}/"
client = ApiClient()
client.force_login(user)
return client
21 changes: 11 additions & 10 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
from uuid import uuid4

from django.db import models
from django.contrib.auth.models import User


class DummyModel(models.Model):
id = models.CharField(max_length=64, primary_key=True)
class Profile(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4)

email = models.CharField(max_length=64)
phone = models.CharField(max_length=64)

user = models.OneToOneField(User, on_delete=models.CASCADE)
role = models.ForeignKey("Role", on_delete=models.CASCADE)
team = models.ForeignKey("Team", blank=True, null=True, on_delete=models.SET_NULL)
skills = models.ManyToManyField("Skill", through="RatedSkill")
tags = models.ManyToManyField("Tag")

def api(self):
return dict(id=self.id, email=self.email, phone=self.phone)

Expand Down Expand Up @@ -72,11 +81,3 @@ def api(self):

class Team(models.Model):
name = models.CharField(max_length=64)


class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
role = models.ForeignKey(Role, on_delete=models.CASCADE)
team = models.ForeignKey(Team, blank=True, null=True, on_delete=models.SET_NULL)
skills = models.ManyToManyField(Skill, through=RatedSkill)
tags = models.ManyToManyField(Tag)
89 changes: 36 additions & 53 deletions tests/test_validators.py
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")
Loading

0 comments on commit 1ce6e82

Please sign in to comment.