<s> import datetime <EOL> from decimal import Decimal <EOL> from dateutil . relativedelta import relativedelta <EOL> from django . utils import timezone <EOL> from django . test import TestCase <EOL> from timepiece import utils <EOL> from timepiece . tests import factories <EOL> from timepiece . tests . base import ViewTestMixin , LogTimeMixin <EOL> from . . models import Project <EOL> class TestProjectTimesheet ( ViewTestMixin , LogTimeMixin , TestCase ) : <EOL> url_name = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> super ( TestProjectTimesheet , self ) . setUp ( ) <EOL> self . user = factories . User ( ) <EOL> self . user2 = factories . User ( ) <EOL> self . superuser = factories . Superuser ( ) <EOL> self . p1 = factories . BillableProject ( name = '<STR_LIT:1>' ) <EOL> self . p2 = factories . NonbillableProject ( name = '<STR_LIT:2>' ) <EOL> self . p4 = factories . BillableProject ( name = '<STR_LIT:4>' ) <EOL> self . p3 = factories . NonbillableProject ( name = '<STR_LIT:1>' ) <EOL> self . url_args = ( self . p1 . pk , ) <EOL> self . devl_activity = factories . Activity ( billable = True ) <EOL> self . activity = factories . Activity ( ) <EOL> def make_entries ( self ) : <EOL> days = [ <EOL> utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ) , <EOL> utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT> ) ) , <EOL> utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT> ) ) , <EOL> utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:1> ) ) , <EOL> timezone . now ( ) , <EOL> ] <EOL> self . log_time ( project = self . p1 , start = days [ <NUM_LIT:0> ] , delta = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> self . log_time ( project = self . p2 , start = days [ <NUM_LIT:0> ] , delta = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> self . log_time ( project = self . p1 , start = days [ <NUM_LIT:1> ] , delta = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> self . log_time ( project = self . p3 , start = days [ <NUM_LIT:1> ] , delta = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> self . log_time ( project = self . p1 , user = self . user2 , start = days [ <NUM_LIT:2> ] , <EOL> delta = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> self . log_time ( project = self . p2 , start = days [ <NUM_LIT:2> ] , delta = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> self . log_time ( project = self . p1 , start = days [ <NUM_LIT:3> ] , delta = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> self . log_time ( project = self . p3 , start = days [ <NUM_LIT:3> ] , delta = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> self . log_time ( project = self . p1 , start = days [ <NUM_LIT:4> ] , delta = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> self . log_time ( project = self . p2 , start = days [ <NUM_LIT:4> ] , delta = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> def testNoPermission ( self ) : <EOL> self . login_user ( self . user ) <EOL> self . make_entries ( ) <EOL> response = self . _get ( ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def testNoProject ( self ) : <EOL> Project . objects . all ( ) . delete ( ) <EOL> self . login_user ( self . superuser ) <EOL> response = self . _get ( url_args = ( <NUM_LIT> , ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def testEmptyProjectTimesheet ( self ) : <EOL> """<STR_LIT>""" <EOL> self . login_user ( self . superuser ) <EOL> def verify_empty ( response ) : <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , self . p1 ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ ] ) <EOL> response = self . _get ( ) <EOL> verify_empty ( response ) <EOL> self . make_entries ( ) <EOL> data = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> } <EOL> response = self . _get ( data = data ) <EOL> verify_empty ( response ) <EOL> def testCurrentProjectTimesheet ( self ) : <EOL> self . login_user ( self . superuser ) <EOL> self . make_entries ( ) <EOL> response = self . _get ( ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , self . p1 ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Decimal ( response . context [ '<STR_LIT>' ] ) , Decimal ( <NUM_LIT:1> ) ) <EOL> user_entry = response . context [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( user_entry [ '<STR_LIT>' ] , self . user . last_name ) <EOL> self . assertEqual ( user_entry [ '<STR_LIT>' ] , self . user . first_name ) <EOL> self . assertEqual ( Decimal ( user_entry [ '<STR_LIT>' ] ) , Decimal ( <NUM_LIT:1> ) ) <EOL> def testOldProjectTimesheet ( self ) : <EOL> self . login_user ( self . superuser ) <EOL> self . make_entries ( ) <EOL> data = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } <EOL> response = self . _get ( data = data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , self . p1 ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( Decimal ( response . context [ '<STR_LIT>' ] ) , Decimal ( <NUM_LIT:3> ) ) <EOL> user_entry0 = response . context [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> user_entry1 = response . context [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> self . assertEqual ( user_entry0 [ '<STR_LIT>' ] , self . user . last_name ) <EOL> self . assertEqual ( user_entry0 [ '<STR_LIT>' ] , self . user . first_name ) <EOL> self . assertEqual ( Decimal ( user_entry0 [ '<STR_LIT>' ] ) , Decimal ( <NUM_LIT:2> ) ) <EOL> self . assertEqual ( user_entry1 [ '<STR_LIT>' ] , self . user2 . last_name ) <EOL> self . assertEqual ( user_entry1 [ '<STR_LIT>' ] , <EOL> self . user2 . first_name ) <EOL> self . assertEqual ( Decimal ( user_entry1 [ '<STR_LIT>' ] ) , Decimal ( <NUM_LIT:1> ) ) <EOL> def testOtherProjectTimesheet ( self ) : <EOL> self . login_user ( self . superuser ) <EOL> self . make_entries ( ) <EOL> response = self . _get ( url_args = ( self . p2 . pk , ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , self . p2 ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Decimal ( response . context [ '<STR_LIT>' ] ) , Decimal ( <NUM_LIT:1> ) ) <EOL> user_entry = response . context [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( user_entry [ '<STR_LIT>' ] , self . user . last_name ) <EOL> self . assertEqual ( user_entry [ '<STR_LIT>' ] , self . user . first_name ) <EOL> self . assertEqual ( Decimal ( user_entry [ '<STR_LIT>' ] ) , Decimal ( <NUM_LIT:1> ) ) <EOL> def test_project_csv ( self ) : <EOL> self . login_user ( self . superuser ) <EOL> self . make_entries ( ) <EOL> response = self . _get ( <EOL> url_name = '<STR_LIT>' , url_args = ( self . p1 . pk , ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> data = dict ( response . items ( ) ) <EOL> self . assertEqual ( data [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> disposition = data [ '<STR_LIT>' ] <EOL> self . assertTrue ( disposition . startswith ( '<STR_LIT>' ) ) <EOL> contents = response . content . decode ( '<STR_LIT:utf-8>' ) . splitlines ( ) <EOL> headers = contents [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) <EOL> self . assertTrue ( '<STR_LIT>' not in headers ) <EOL> def testRoundingConversions ( self ) : <EOL> """<STR_LIT>""" <EOL> xtime = timezone . now ( ) - relativedelta ( minutes = <NUM_LIT:10> ) <EOL> factories . Entry ( ** { <EOL> '<STR_LIT:user>' : self . superuser , <EOL> '<STR_LIT>' : self . p2 , <EOL> '<STR_LIT>' : xtime , <EOL> '<STR_LIT>' : xtime + relativedelta ( seconds = <NUM_LIT> ) <EOL> } ) <EOL> factories . Entry ( ** { <EOL> '<STR_LIT:user>' : self . superuser , <EOL> '<STR_LIT>' : self . p2 , <EOL> '<STR_LIT>' : xtime + relativedelta ( seconds = <NUM_LIT:30> ) , <EOL> '<STR_LIT>' : xtime + relativedelta ( seconds = <NUM_LIT> ) <EOL> } ) <EOL> self . login_user ( self . superuser ) <EOL> response = self . _get ( url_args = ( self . p2 . pk , ) ) <EOL> entries = response . context [ '<STR_LIT>' ] <EOL> self . assertEqual ( len ( entries ) , <NUM_LIT:2> ) <EOL> self . assertAlmostEqual ( sum ( Decimal ( e [ '<STR_LIT>' ] ) for e in entries ) , Decimal ( <NUM_LIT> ) , places = <NUM_LIT:2> ) </s>
