def test_was_published_recently_with_old_question(self): """ was_published_recently() should return False for questions whose pub_date is older than 1 day. """ time = timezone.now() - datetime.timedelta(days=30) old_question = Question(pub_date=time) self.assertEqual(old_question.was_published_recently(), False)
def test_was_published_recently_with_recent_question(self): """ was_published_recently() should return True for questions whose pub_date is within the last day. """ time = timezone.now() - datetime.timedelta(hours=1) recent_question = Question(pub_date=time) self.assertEqual(recent_question.was_published_recently(), True)
def test_was_published_recently_with_future_question(): """ was_published_recently() should return False for questions whose pub_date is in the future. """ time = timezone.now() + datetime.timedelta(days=30) future_question = Question(pub_date=time) assert not future_question.was_published_recently()
def test_was_published_recently_with_future_question(self): """ was_published_recently() should return False for questions whose pub_date is in the future. """ time = timezone.now() + datetime.timedelta(days=30) future_question = Question(pub_date=time) self.assertEqual(future_question.was_published_recently(), False)
def test_was_published_recently_with_recent_question(): """ was_published_recently() should return True for questions whose pub_date is within the last day. """ time = timezone.now() - datetime.timedelta(hours=1) recent_question = Question(pub_date=time) assert recent_question.was_published_recently()
def test_was_published_recently_with_old_question(): """ was_published_recently() should return False for questions whose pub_date is older than 1 day. """ time = timezone.now() - datetime.timedelta(days=30) old_question = Question(pub_date=time) assert not old_question.was_published_recently()
def create_question(self, text: str, exc: Optional[Exception] = None) -> Question: question = Question(question_text=text, publish_date=datetime.now(tz=utc)) if exc: raise exc question.save() return question
def test_expect_related_name_when_query(self, mocker): choices = mock_django_model(mocker, model_type=Choice) q1 = Question(pk=1, question_text='Test Question?') c1 = Choice(pk=1, question=q1, choice_text='Choice A') choices.add(c1) self._mock_reverse_many_to_one(mocker, choices) repo = ChoiceRepo() result = repo.get_question_choices(q1) assert result == [c1] del Question.choices.related_manager_cls
def test_expect_related_name_when_query_startswith(self, mocker): choices = mock_django_model(mocker, model_type=Choice) q1 = Question(pk=1, question_text='How do you feel?') c1 = Choice(pk=1, question=q1, choice_text='Good: excellent!') c2 = Choice(pk=2, question=q1, choice_text='Good: awesome!') c3 = Choice(pk=3, question=q1, choice_text='Bad: sick...') choices.add(c1, c2, c3) self._mock_reverse_many_to_one(mocker, choices) repo = ChoiceRepo() result = repo.get_question_choices_starting_with(q1, 'Bad') assert result == [c3] del Question.choices.related_manager_cls
from mysite.polls.models import Question, Choice from django.utils import timezone # Create q = Question(question_text="What's new?", pub_date=timezone.now()) q.save() # Read Question.objects.all() # Queryset collection 반환 Question.objects.filter( # 해당 조건은 포함 question_text_startswith='What' ).exclude( # 해당 조건은 제외 pub_date_gte=datetime.date.today() ).filter( pub_date_gte=datetime.date.today(2005, 1, 30) ) Question.objects.get(pk=1) # 한 개의 요소만 있는 것이 확실한 경우 한 개의 객체를 가져온다. Queryset collection(X) Question.objects.all()[:5] # list 반환 Queryset collection(X) 갯수 제한을 해서 가져온다. Question.objects.all()[5:10] Question.objects.all()[5:] # Update q.question_text = 'What is your favorite hoobby?' q.save() Question.objects.filter(pub_date_year=2007).update(question_text='Evrything is the same') # 여러 객체 Update # Delete Question.objects.filter(pub_date_year=2005).delete() Question.objects.all().delete() # 전체삭제