diff --git a/notes/factories.py b/notes/factories.py index 979be7fd..ab1392e1 100644 --- a/notes/factories.py +++ b/notes/factories.py @@ -1,7 +1,10 @@ import factory from django.contrib.auth.models import User +from notes.models import Notes +from factory import fuzzy from django.contrib.auth.hashers import make_password + class UserFactory(factory.django.DjangoModelFactory): class Meta: model = User @@ -9,4 +12,12 @@ class Meta: username= factory.Sequence(lambda n: f"user_{n:04}") #user_0000 email=factory.LazyAttribute(lambda user: f"{user.username}@example.com") #encrypt the password in the database - password=factory.LazyAttribute(lambda p: make_password('password')) \ No newline at end of file + password=factory.LazyAttribute(lambda p: make_password('password')) + +class NoteFactory(factory.django.DjangoModelFactory): + class Meta: + model = Notes + title=fuzzy.FuzzyText(length =20) + text =fuzzy.FuzzyText(length =200) + #create a brand new user for you id u do not have one while creating the note + user = factory.SubFactory(UserFactory) \ No newline at end of file diff --git a/notes/test_notes_views.py b/notes/test_notes_views.py index 54aa48bc..c941ec1d 100644 --- a/notes/test_notes_views.py +++ b/notes/test_notes_views.py @@ -1,7 +1,7 @@ import pytest from django.contrib.auth.models import User from notes.models import Notes -from .factories import UserFactory +from .factories import UserFactory,NoteFactory #use pytest.fixture to create reusable code @pytest.fixture def logged_user(client): @@ -12,9 +12,9 @@ def logged_user(client): @pytest.mark.django_db def test_list_endpoint_return_user_notes(client,logged_user): - note = Notes.objects.create(title='An interesting title', text='',user=logged_user) + note = NoteFactory(user=logged_user) response = client.get(path='/smart/notes') assert 200 == response.status_code content=str(response.content) - assert "An interesting title" in content + assert note.title in content assert 1 ==content.count('

')