<s> import datetime <EOL> from decimal import Decimal <EOL> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib . auth . models import Permission , User <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 timepiece . entries . models import Entry <EOL> from timepiece . reports . utils import find_overtime <EOL> class PayrollTest ( ViewTestMixin , LogTimeMixin , TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( PayrollTest , self ) . setUp ( ) <EOL> self . user = factories . User ( ) <EOL> self . user2 = factories . User ( ) <EOL> self . superuser = factories . Superuser ( ) <EOL> self . devl_activity = factories . Activity ( billable = True ) <EOL> self . activity = factories . Activity ( ) <EOL> self . sick = factories . Project ( name = '<STR_LIT>' ) <EOL> self . vacation = factories . Project ( name = '<STR_LIT>' ) <EOL> settings . TIMEPIECE_PAID_LEAVE_PROJECTS = { <EOL> '<STR_LIT>' : self . sick . pk , '<STR_LIT>' : self . vacation . pk <EOL> } <EOL> self . next = utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:1> ) ) <EOL> self . overtime_before = utils . add_timezone ( <EOL> datetime . datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) ) <EOL> self . first = utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:1> ) ) <EOL> self . first_week = utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:2> ) ) <EOL> self . middle = utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT> ) ) <EOL> self . last_billable = utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT> ) ) <EOL> self . last = utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT> ) ) <EOL> self . dates = [ <EOL> self . overtime_before , self . first , self . first_week , self . middle , <EOL> self . last , self . last_billable , self . next <EOL> ] <EOL> self . url = reverse ( '<STR_LIT>' ) <EOL> self . args = { <EOL> '<STR_LIT>' : self . first . year , <EOL> '<STR_LIT>' : self . first . month , <EOL> } <EOL> def make_entry ( self , user , start , delta , status = '<STR_LIT>' , billable = True , <EOL> project = None ) : <EOL> self . log_time ( start = start , delta = delta , user = user , status = status , <EOL> billable = billable , project = project ) <EOL> def make_logs ( self , day = None , user = None , billable_project = None , <EOL> nonbillable_project = None ) : <EOL> if not user : <EOL> user = self . user <EOL> if not day : <EOL> day = self . first <EOL> self . make_entry ( user , day , ( <NUM_LIT:3> , <NUM_LIT:30> ) , project = billable_project ) <EOL> self . make_entry ( user , day , ( <NUM_LIT:2> , <NUM_LIT:0> ) , project = nonbillable_project ) <EOL> self . make_entry ( user , day , ( <NUM_LIT:5> , <NUM_LIT:30> ) , status = Entry . INVOICED , project = billable_project ) <EOL> self . make_entry ( user , day , ( <NUM_LIT:6> , <NUM_LIT:0> ) , status = Entry . VERIFIED , project = billable_project ) <EOL> self . make_entry ( user , day , ( <NUM_LIT:8> , <NUM_LIT:0> ) , project = self . sick ) <EOL> self . make_entry ( user , day , ( <NUM_LIT:4> , <NUM_LIT:0> ) , project = self . vacation ) <EOL> def all_logs ( self , user = None , billable_project = None , <EOL> nonbillable_project = None ) : <EOL> if not user : <EOL> user = self . user <EOL> for day in self . dates : <EOL> self . make_logs ( day , user , billable_project , nonbillable_project ) <EOL> def testLastBillable ( self ) : <EOL> """<STR_LIT>""" <EOL> months = range ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> first_days = [ utils . add_timezone ( datetime . datetime ( <NUM_LIT> , month , <NUM_LIT:1> ) ) <EOL> for month in months ] <EOL> last_billable = [ utils . get_last_billable_day ( day ) . day <EOL> for day in first_days ] <EOL> self . assertEqual ( last_billable , <EOL> [ <NUM_LIT:30> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:30> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> def testFindOvertime ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( round ( find_overtime ( [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) , <NUM_LIT:2> ) , <EOL> <NUM_LIT> ) <EOL> def testWeeklyTotals ( self ) : <EOL> self . all_logs ( self . user ) <EOL> self . all_logs ( self . user2 ) <EOL> self . login_user ( self . superuser ) <EOL> response = self . client . get ( self . url , self . args ) <EOL> weekly_totals = response . context [ '<STR_LIT>' ] <EOL> self . assertEqual ( weekly_totals [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] , [ <EOL> Decimal ( '<STR_LIT>' ) , <EOL> Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> Decimal ( '<STR_LIT>' ) , <EOL> Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def testWeeklyTotalsSameLastName ( self ) : <EOL> """<STR_LIT>""" <EOL> User . objects . all ( ) . update ( last_name = '<STR_LIT>' ) <EOL> self . all_logs ( self . user ) <EOL> self . all_logs ( self . user2 ) <EOL> self . login_user ( self . superuser ) <EOL> response = self . client . get ( self . url , self . args ) <EOL> weekly_totals = response . context [ '<STR_LIT>' ] <EOL> self . assertEqual ( weekly_totals [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] , [ <EOL> Decimal ( '<STR_LIT>' ) , <EOL> Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> Decimal ( '<STR_LIT>' ) , <EOL> Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def testWeeklyTotalsSameFirstLastName ( self ) : <EOL> """<STR_LIT>""" <EOL> User . objects . all ( ) . update ( last_name = '<STR_LIT>' , first_name = '<STR_LIT>' ) <EOL> self . all_logs ( self . user ) <EOL> self . all_logs ( self . user2 ) <EOL> self . login_user ( self . superuser ) <EOL> response = self . client . get ( self . url , self . args ) <EOL> weekly_totals = response . context [ '<STR_LIT>' ] <EOL> self . assertEqual ( weekly_totals [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] , [ <EOL> Decimal ( '<STR_LIT>' ) , <EOL> Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> Decimal ( '<STR_LIT>' ) , <EOL> Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def testWeeklyOvertimes ( self ) : <EOL> """<STR_LIT>""" <EOL> dates = self . dates <EOL> for day_num in range ( <NUM_LIT> , <NUM_LIT> ) : <EOL> dates . append ( utils . add_timezone ( <EOL> datetime . datetime ( <NUM_LIT> , <NUM_LIT:4> , day_num ) <EOL> ) ) <EOL> for day_num in range ( <NUM_LIT:5> , <NUM_LIT:9> ) : <EOL> dates . append ( utils . add_timezone ( <EOL> datetime . datetime ( <NUM_LIT> , <NUM_LIT:5> , day_num ) <EOL> ) ) <EOL> for day in dates : <EOL> self . make_logs ( day ) <EOL> def check_overtime ( week0 = Decimal ( '<STR_LIT>' ) , week1 = Decimal ( '<STR_LIT>' ) , <EOL> overtime = Decimal ( '<STR_LIT>' ) ) : <EOL> self . login_user ( self . superuser ) <EOL> response = self . client . get ( self . url , self . args ) <EOL> weekly_totals = response . context [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> self . assertEqual ( weekly_totals [ <NUM_LIT:0> ] , week0 ) <EOL> self . assertEqual ( weekly_totals [ <NUM_LIT:1> ] , week1 ) <EOL> self . assertEqual ( weekly_totals [ <NUM_LIT:5> ] , overtime ) <EOL> check_overtime ( ) <EOL> self . make_logs ( utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:9> ) ) ) <EOL> check_overtime ( ) <EOL> self . make_logs ( utils . add_timezone ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) ) ) <EOL> check_overtime ( ) <EOL> self . make_logs ( utils . add_timezone ( <EOL> datetime . datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> ) ) <EOL> check_overtime ( Decimal ( '<STR_LIT>' ) , Decimal ( '<STR_LIT>' ) , Decimal ( '<STR_LIT>' ) ) <EOL> def _setupMonthlyTotals ( self ) : <EOL> """<STR_LIT>""" <EOL> self . billable_project = factories . BillableProject ( ) <EOL> self . nonbillable_project = factories . NonbillableProject ( ) <EOL> self . all_logs ( self . user , self . billable_project , self . nonbillable_project ) <EOL> self . all_logs ( self . user2 , self . billable_project , self . nonbillable_project ) <EOL> self . login_user ( self . superuser ) <EOL> self . response = self . client . get ( self . url , self . args ) <EOL> self . rows = self . response . context [ '<STR_LIT>' ] <EOL> self . labels = self . response . context [ '<STR_LIT>' ] <EOL> def testMonthlyPayrollLabels ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _setupMonthlyTotals ( ) <EOL> self . assertEquals ( <EOL> self . labels [ '<STR_LIT>' ] , [ self . billable_project . type . label ] ) <EOL> self . assertEquals ( <EOL> self . labels [ '<STR_LIT>' ] , [ self . nonbillable_project . type . label ] ) <EOL> self . assertEquals ( len ( self . labels [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( self . sick . name in self . labels [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( self . vacation . name in self . labels [ '<STR_LIT>' ] ) <EOL> def testMonthlyPayrollRows ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _setupMonthlyTotals ( ) <EOL> self . assertEquals ( len ( self . rows ) , <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> for row in self . rows [ : - <NUM_LIT:1> ] : <EOL> work_total = Decimal ( '<STR_LIT>' ) <EOL> self . assertEquals ( row [ '<STR_LIT>' ] , work_total ) <EOL> self . assertEquals ( len ( row [ '<STR_LIT>' ] ) , <NUM_LIT:1> + <NUM_LIT:1> ) <EOL> for entry in row [ '<STR_LIT>' ] : <EOL> self . assertEquals ( entry [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( entry [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) / work_total * <NUM_LIT:100> ) <EOL> self . assertEquals ( len ( row [ '<STR_LIT>' ] ) , <NUM_LIT:1> + <NUM_LIT:1> ) <EOL> for entry in row [ '<STR_LIT>' ] : <EOL> self . assertEquals ( entry [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( entry [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) / work_total * <NUM_LIT:100> ) <EOL> self . assertEquals ( len ( row [ '<STR_LIT>' ] ) , <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> sick_index = self . labels [ '<STR_LIT>' ] . index ( self . sick . name ) <EOL> vacation_index = self . labels [ '<STR_LIT>' ] . index ( self . vacation . name ) <EOL> self . assertEquals ( <EOL> row [ '<STR_LIT>' ] [ sick_index ] [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( <EOL> row [ '<STR_LIT>' ] [ sick_index ] [ '<STR_LIT>' ] , <EOL> Decimal ( '<STR_LIT>' ) / Decimal ( '<STR_LIT>' ) * <NUM_LIT:100> ) <EOL> self . assertEquals ( <EOL> row [ '<STR_LIT>' ] [ vacation_index ] [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( <EOL> row [ '<STR_LIT>' ] [ vacation_index ] [ '<STR_LIT>' ] , <EOL> Decimal ( '<STR_LIT>' ) / Decimal ( '<STR_LIT>' ) * <NUM_LIT:100> ) <EOL> self . assertEquals ( row [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( row [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( row [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> def testMonthlyPayrollTotals ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _setupMonthlyTotals ( ) <EOL> totals = self . rows [ - <NUM_LIT:1> ] <EOL> work_total = Decimal ( '<STR_LIT>' ) <EOL> self . assertEquals ( totals [ '<STR_LIT>' ] , work_total ) <EOL> self . assertEquals ( len ( totals [ '<STR_LIT>' ] ) , <NUM_LIT:1> + <NUM_LIT:1> ) <EOL> for entry in totals [ '<STR_LIT>' ] : <EOL> self . assertEquals ( entry [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( entry [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) / work_total * <NUM_LIT:100> ) <EOL> self . assertEquals ( len ( totals [ '<STR_LIT>' ] ) , <NUM_LIT:1> + <NUM_LIT:1> ) <EOL> for entry in totals [ '<STR_LIT>' ] : <EOL> self . assertEquals ( entry [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( entry [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) / work_total * <NUM_LIT:100> ) <EOL> self . assertEquals ( len ( totals [ '<STR_LIT>' ] ) , <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> sick_index = self . labels [ '<STR_LIT>' ] . index ( self . sick . name ) <EOL> vacation_index = self . labels [ '<STR_LIT>' ] . index ( self . vacation . name ) <EOL> self . assertEquals ( <EOL> totals [ '<STR_LIT>' ] [ sick_index ] [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( <EOL> totals [ '<STR_LIT>' ] [ sick_index ] [ '<STR_LIT>' ] , <EOL> Decimal ( '<STR_LIT>' ) / Decimal ( '<STR_LIT>' ) * <NUM_LIT:100> ) <EOL> self . assertEquals ( <EOL> totals [ '<STR_LIT>' ] [ vacation_index ] [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( <EOL> totals [ '<STR_LIT>' ] [ vacation_index ] [ '<STR_LIT>' ] , <EOL> Decimal ( '<STR_LIT>' ) / Decimal ( '<STR_LIT>' ) * <NUM_LIT:100> ) <EOL> self . assertEquals ( totals [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( totals [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( totals [ '<STR_LIT>' ] , Decimal ( '<STR_LIT>' ) ) <EOL> def testNoPermission ( self ) : <EOL> """<STR_LIT>""" <EOL> self . login_user ( self . user ) <EOL> response = self . client . get ( self . url , self . args ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def testSuperUserPermission ( self ) : <EOL> """<STR_LIT>""" <EOL> self . login_user ( self . superuser ) <EOL> response = self . client . get ( self . url , self . args ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def testPayrollPermission ( self ) : <EOL> """<STR_LIT>""" <EOL> self . login_user ( self . user ) <EOL> payroll_perm = Permission . objects . get ( codename = '<STR_LIT>' ) <EOL> self . user . user_permissions . add ( payroll_perm ) <EOL> self . user . save ( ) <EOL> response = self . client . get ( self . url , self . args ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) </s>
