<s> from django . contrib import messages <EOL> from django . contrib . messages . middleware import MessageMiddleware <EOL> from django . contrib . sessions . middleware import SessionMiddleware <EOL> from django . core import mail <EOL> from django . http import HttpResponse <EOL> from django . test import TestCase <EOL> from django . views import generic <EOL> from djet import assertions , testcases <EOL> from tests import models <EOL> class MockView ( generic . View ) : <EOL> def get ( self , * args , ** kwargs ) : <EOL> return HttpResponse ( ) <EOL> class StatusCodeAssertionsMixinTest ( assertions . StatusCodeAssertionsMixin , testcases . ViewTestCase ) : <EOL> view_class = MockView <EOL> def test_assert_not_redirect_should_pass_when_view_not_redirect ( self ) : <EOL> request = self . factory . get ( ) <EOL> response = self . view ( request ) <EOL> self . assert_not_redirect ( response ) <EOL> def test_assert_redirect_should_pass_when_view_redirect ( self ) : <EOL> view = generic . RedirectView . as_view ( url = '<STR_LIT:/>' , permanent = True ) <EOL> request = self . factory . get ( ) <EOL> response = view ( request ) <EOL> self . assert_redirect ( response ) <EOL> def test_assert_status_code_should_pass_when_code_is_200 ( self ) : <EOL> request = self . factory . get ( ) <EOL> response = self . view ( request ) <EOL> self . assert_status_equal ( response , <NUM_LIT:200> ) <EOL> self . assert_status_in ( response , [ <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> def test_assert_status_code_should_pass_when_responses_have_equal_codes ( self ) : <EOL> request = self . factory . get ( ) <EOL> response = self . view ( request ) <EOL> self . assert_status_equal ( response , HttpResponse ) <EOL> self . assert_status_in ( response , [ HttpResponse ] ) <EOL> def test_assert_status_code_should_raise_assertion_error_when_code_is_not_200 ( self ) : <EOL> request = self . factory . get ( ) <EOL> response = self . view ( request ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . assert_status_equal ( response , <NUM_LIT> ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . assert_status_in ( response , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> class EmailMockView ( generic . View ) : <EOL> def get ( self , * args , ** kwargs ) : <EOL> for _ in range ( int ( self . request . GET . get ( '<STR_LIT>' ) ) ) : <EOL> mail . send_mail ( '<STR_LIT:test>' , '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> return HttpResponse ( ) <EOL> class EmailAssertionsMixinTest ( assertions . EmailAssertionsMixin , testcases . ViewTestCase ) : <EOL> view_class = EmailMockView <EOL> def test_assert_no_email_sent_when_not_sent ( self ) : <EOL> request = self . factory . get ( data = { '<STR_LIT>' : '<STR_LIT:0>' } ) <EOL> self . view ( request ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . assert_emails_in_mailbox ( <NUM_LIT:3> ) <EOL> def test_assert_email_sent_when_really_sent ( self ) : <EOL> request = self . factory . get ( data = { '<STR_LIT>' : '<STR_LIT:3>' } ) <EOL> self . view ( request ) <EOL> self . assert_emails_in_mailbox ( <NUM_LIT:3> ) <EOL> def test_assert_email_exists_raising_assertion_error_when_no_matching_email ( self ) : <EOL> request = self . factory . get ( data = { '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> self . view ( request ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . assert_email_exists ( subject = '<STR_LIT>' ) <EOL> def test_assert_email_exists_passes_when_matched_email ( self ) : <EOL> request = self . factory . get ( data = { '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> self . view ( request ) <EOL> self . assert_email_exists ( subject = '<STR_LIT:test>' ) <EOL> class MessagesMockView ( generic . View ) : <EOL> def get ( self , * args , ** kwargs ) : <EOL> for _ in range ( int ( self . request . GET . get ( '<STR_LIT>' ) ) ) : <EOL> messages . success ( self . request , '<STR_LIT>' ) <EOL> return HttpResponse ( ) <EOL> class MessagesAssertionsMixin ( assertions . MessagesAssertionsMixin , testcases . ViewTestCase ) : <EOL> view_class = MessagesMockView <EOL> middleware_classes = [ <EOL> SessionMiddleware , <EOL> MessageMiddleware , <EOL> ] <EOL> def test_assert_messages_sent_when_not_sent ( self ) : <EOL> request = self . factory . get ( data = { '<STR_LIT>' : '<STR_LIT:0>' } ) <EOL> self . view ( request ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . assert_messages_sent ( request , <NUM_LIT:3> ) <EOL> def test_assert_messages_sent_when_really_sent ( self ) : <EOL> request = self . factory . get ( data = { '<STR_LIT>' : '<STR_LIT:3>' } ) <EOL> self . view ( request ) <EOL> self . assert_messages_sent ( request , <NUM_LIT:3> ) <EOL> def test_assert_messages_raise_assertion_error_when_no_matching_message ( self ) : <EOL> request = self . factory . get ( data = { '<STR_LIT>' : '<STR_LIT:0>' } ) <EOL> self . view ( request ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . assert_message_exists ( request , messages . SUCCESS , '<STR_LIT>' ) <EOL> def test_assert_messages_passes_when_matched_message ( self ) : <EOL> request = self . factory . get ( data = { '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> self . view ( request ) <EOL> self . assert_message_exists ( request , messages . SUCCESS , '<STR_LIT>' ) <EOL> class InstanceAssertionsMixinTest ( assertions . InstanceAssertionsMixin , TestCase ) : <EOL> def test_assert_instance_exists_passes_for_existing_instances ( self ) : <EOL> models . MockModel . objects . create ( field = '<STR_LIT:value>' ) <EOL> self . assert_instance_exists ( models . MockModel , field = '<STR_LIT:value>' ) <EOL> def test_assert_instance_exists_raises_assertion_error_when_no_instance ( self ) : <EOL> with self . assertRaisesRegexp ( AssertionError , '<STR_LIT>' ) : <EOL> self . assert_instance_exists ( models . MockModel , field = '<STR_LIT:value>' ) <EOL> def test_assert_instance_does_not_exist_passes_for_nonexisting_instances ( self ) : <EOL> self . assert_instance_does_not_exist ( models . MockModel , field = '<STR_LIT:value>' ) <EOL> def test_assert_instance_does_not_exist_raises_assertion_error_when_instance_found ( self ) : <EOL> models . MockModel . objects . create ( field = '<STR_LIT:value>' ) <EOL> with self . assertRaisesRegexp ( AssertionError , '<STR_LIT>' ) : <EOL> self . assert_instance_does_not_exist ( models . MockModel , field = '<STR_LIT:value>' ) <EOL> def test_assert_instance_created_passes_when_instance_created ( self ) : <EOL> with self . assert_instance_created ( models . MockModel , field = '<STR_LIT:value>' ) : <EOL> models . MockModel . objects . create ( field = '<STR_LIT:value>' ) <EOL> def test_assert_instance_created_raises_assertion_error_when_not_created ( self ) : <EOL> with self . assertRaisesRegexp ( AssertionError , '<STR_LIT>' ) : <EOL> with self . assert_instance_created ( models . MockModel , field = '<STR_LIT:value>' ) : <EOL> pass <EOL> def test_assert_instance_deleted_passes_when_instance_deleted ( self ) : <EOL> models . MockModel . objects . create ( field = '<STR_LIT:value>' ) <EOL> with self . assert_instance_deleted ( models . MockModel , field = '<STR_LIT:value>' ) : <EOL> models . MockModel . objects . get ( field = '<STR_LIT:value>' ) . delete ( ) <EOL> def test_assert_instance_deleted_raises_assertion_error_when_not_deleted ( self ) : <EOL> models . MockModel . objects . create ( field = '<STR_LIT:value>' ) <EOL> with self . assertRaisesRegexp ( AssertionError , '<STR_LIT>' ) : <EOL> with self . assert_instance_deleted ( models . MockModel , field = '<STR_LIT:value>' ) : <EOL> pass </s>
