Skip to content

Commit

Permalink
LinkedInLearning#1 added noteFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Shamaine committed Feb 16, 2024
1 parent 88320c7 commit c4d0cfd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 12 additions & 1 deletion notes/factories.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
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

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'))
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)
6 changes: 3 additions & 3 deletions notes/test_notes_views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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('<h3>')

0 comments on commit c4d0cfd

Please sign in to comment.