<s> import re <EOL> import sys <EOL> from django . test import TestCase <EOL> try : <EOL> from unittest . mock import patch <EOL> except ImportError : <EOL> from mock import patch <EOL> from tracking . models import Visitor , Pageview <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> def _u ( s ) : <EOL> return s <EOL> else : <EOL> def _u ( s ) : <EOL> return unicode ( s ) <EOL> class MiddlewareTestCase ( TestCase ) : <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> def test_no_session ( self , mock_warnings ) : <EOL> tracking = '<STR_LIT>' <EOL> with self . settings ( MIDDLEWARE_CLASSES = [ tracking ] ) : <EOL> self . client . get ( '<STR_LIT:/>' ) <EOL> self . assertEqual ( Visitor . objects . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Pageview . objects . count ( ) , <NUM_LIT:0> ) <EOL> msg = '<STR_LIT>' <EOL> mock_warnings . warn . assert_called_once_with ( msg , RuntimeWarning ) <EOL> @ patch ( '<STR_LIT>' , False ) <EOL> def test_no_track_ajax ( self ) : <EOL> self . client . get ( '<STR_LIT:/>' , HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( Visitor . objects . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Pageview . objects . count ( ) , <NUM_LIT:0> ) <EOL> @ patch ( '<STR_LIT>' , [ <NUM_LIT> ] ) <EOL> def test_no_track_status ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( Visitor . objects . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Pageview . objects . count ( ) , <NUM_LIT:0> ) <EOL> @ patch ( '<STR_LIT>' , False ) <EOL> def test_no_track_pageviews ( self ) : <EOL> self . client . get ( '<STR_LIT:/>' ) <EOL> self . assertEqual ( Visitor . objects . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Pageview . objects . count ( ) , <NUM_LIT:0> ) <EOL> @ patch ( '<STR_LIT>' , True ) <EOL> def test_track_pageviews ( self ) : <EOL> self . client . get ( '<STR_LIT:/>' ) <EOL> self . assertEqual ( Visitor . objects . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Pageview . objects . count ( ) , <NUM_LIT:1> ) <EOL> def test_track_user_agent ( self ) : <EOL> self . client . get ( '<STR_LIT:/>' , HTTP_USER_AGENT = '<STR_LIT>' ) <EOL> self . assertEqual ( Visitor . objects . count ( ) , <NUM_LIT:1> ) <EOL> visitor = Visitor . objects . get ( ) <EOL> self . assertEqual ( visitor . user_agent , '<STR_LIT>' ) <EOL> def test_track_user_agent_unicode ( self ) : <EOL> self . client . get ( '<STR_LIT:/>' , HTTP_USER_AGENT = _u ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( Visitor . objects . count ( ) , <NUM_LIT:1> ) <EOL> visitor = Visitor . objects . get ( ) <EOL> self . assertEqual ( visitor . user_agent , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , False ) <EOL> def test_track_anonymous_users ( self ) : <EOL> self . client . get ( '<STR_LIT:/>' ) <EOL> self . assertEqual ( Visitor . objects . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Pageview . objects . count ( ) , <NUM_LIT:0> ) <EOL> def test_track_ignore_url ( self ) : <EOL> ignore_urls = [ re . compile ( '<STR_LIT:foo>' ) ] <EOL> with patch ( '<STR_LIT>' , ignore_urls ) : <EOL> self . client . get ( '<STR_LIT:/>' ) <EOL> self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( Visitor . objects . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Pageview . objects . count ( ) , <NUM_LIT:1> ) <EOL> @ patch ( '<STR_LIT>' , True ) <EOL> @ patch ( '<STR_LIT>' , True ) <EOL> @ patch ( '<STR_LIT>' , True ) <EOL> def test_track_referer_string ( self ) : <EOL> self . client . get ( '<STR_LIT>' , HTTP_REFERER = '<STR_LIT>' ) <EOL> self . assertEqual ( Visitor . objects . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Pageview . objects . count ( ) , <NUM_LIT:1> ) <EOL> view = Pageview . objects . get ( ) <EOL> self . assertEqual ( view . referer , '<STR_LIT>' ) <EOL> self . assertEqual ( view . query_string , '<STR_LIT>' ) </s>
