def setUp(self): super(TestTimeAccounting, self).setUp() # this project has no allotments! self.project = Project.objects.order_by('pcode').all()[0] # setup some periods self.start = datetime(2000, 1, 1, 0) self.end = self.start + timedelta(hours = 12) times = [(datetime(2000, 1, 1, 0), 5.0, "one") , (datetime(2000, 1, 1, 5), 3.0, "two") , (datetime(2000, 1, 1, 8), 4.0, "three") ] self.ps = [] state = Period_State.objects.get(abbreviation = 'P') for start, dur, name in times: # each session has grade 4, time = 3 s = create_sesshun() s.name = name s.save() pa = Period_Accounting(scheduled = dur) pa.save() p = Period( session = s , start = start , duration = dur , state = state , accounting = pa ) p.save() self.ps.append(p) self.ta = TimeAccounting()
def setUp(self): super(TestProject, self).setUp() self.project = Project() self.project_adapter = ProjectHttpAdapter(self.project) pdata = {"semester" : "09A" , "type" : "science" , "total_time" : "10.0" , "PSC_time" : "10.0" , "sem_time" : "10.0" , "grade" : "4.0" , "notes" : "notes" , "schd_notes" : "scheduler's notes" } self.project_adapter.update_from_post(pdata) # Create the first user (on project) self.user1 = User(sanctioned = True ) self.user1.save() self.investigator1 = Investigator(project = self.project , user = self.user1 , observer = True) self.investigator1.save() # Create the second user (on project) self.user2 = User(sanctioned = True ) self.user2.save() self.investigator2 = Investigator(project = self.project , user = self.user2 , observer = True) self.investigator2.save() # Create the third unsanctioned user (on project) self.user3 = User(sanctioned = False ) self.user3.save() self.investigator3 = Investigator(project = self.project , user = self.user3 , observer = True) self.investigator3.save() # make a session self.sesshun = create_sesshun() self.sesshun.project = self.project self.sesshun.save() # make a period for it fdata = {'session' : self.sesshun.id , 'date' : '2009-06-01' , 'time' : '10:00' , 'duration' : 1.0 , 'backup' : False} self.period = Period() self.period_adapter = PeriodHttpAdapter(self.period) self.period_adapter.init_from_post(fdata, 'UTC')
def setUp(self): super(TestPeriodChanges, self).setUp() self.ps = [] #now = datetime.now() #s = Sesshun.objects.all()[0] s = create_sesshun() pending = Period_State.get_state("P") #start = now + timedelta(hours = 1) #start = start.replace(minute = 0, second = 0, microsecond = 0) start = datetime(2011, 2, 9, 11) self.start = start for i in range(1): duration = 4.0 # hrs start = start + timedelta(hours = duration) pa = Period_Accounting(scheduled = duration) pa.save() p = Period(session = s , start = start , duration = duration , state = pending , accounting = pa , score = 0.0 , forecast = datetime.now() ) p.save() self.ps.append(p)
def setUp(self): super(TestWindowRange, self).setUp() # create a session self.sesshun = create_sesshun() self.sesshun.session_type = Session_Type.get_type("windowed") self.sesshun.save() # create window self.w = Window(session = self.sesshun) self.w.save()
def setUp(self): super(TestScheduleTools, self).setUp() # setup some periods self.start = datetime(2000, 1, 1, 0) self.end = self.start + timedelta(hours = 12) times = [(datetime(2000, 1, 1, 0), 5.0, "one") , (datetime(2000, 1, 1, 5), 3.0, "two") , (datetime(2000, 1, 1, 8), 4.0, "three") ] self.ps = [] # init them as Scheduled, so that 'deleting' them just changes state state = Period_State.objects.get(abbreviation = 'S') for start, dur, name in times: s = create_sesshun() s.name = name s.save() rg = Receiver_Group(session = s) rg.save() rcvr = Receiver.objects.get(abbreviation = "L") rg.receivers.add(rcvr) rg.save() pa = Period_Accounting(scheduled = dur) pa.save() p = Period( session = s , start = start , duration = dur , state = state , accounting = pa ) p.save() pr = Period_Receiver(receiver = rcvr, period = p) pr.save() self.ps.append(p) self.backup = create_sesshun() self.backup.name = "backup" self.backup.status.backup = True self.backup.save()
def test_delete(self): s = create_sesshun() period = Period(session = s , duration = 180 , start = datetime(2011, 10, 12, 12, 0, 0) , state = Period_State.get_state('S') ) period.save() # Make sure we can not delete a session with children self.assertRaises(Exception, s.delete) # Force the deletion of the session. s.delete(force = True) self.assertRaises(Sesshun.DoesNotExist, Sesshun.objects.get, id = s.id)
def test_get_prescheduled_times(self): start = datetime(2009, 6, 1) end = datetime(2009, 6, 2) # No periods. times = self.project.get_prescheduled_times(start, end) self.assertEquals(0, len(times)) # Now add a project w/ prescheduled times. otherproject = Project() pdata = {"semester" : "09A" , "pcode" : "otherProject" , "type" : "science" , "total_time" : "10.0" , "PSC_time" : "10.0" , "sem_time" : "10.0" , "grade" : "4.0" , "notes" : "notes" , "schd_notes" : "scheduler's notes" } adapter = ProjectHttpAdapter(otherproject) adapter.update_from_post(pdata) othersesshun = create_sesshun() othersesshun.project = otherproject othersesshun.save() fdata = {'session' : othersesshun.id , 'date' : '2009-06-01' , 'time' : '13:00' , 'duration' : 1.0 , 'backup' : False} otherperiod = Period() adapter = PeriodHttpAdapter(otherperiod) adapter.init_from_post(fdata, 'UTC') otherperiod.state = Period_State.objects.filter(abbreviation = 'S')[0] otherperiod.save() # Test again times = self.project.get_prescheduled_times(start, end) self.assertEquals(1, len(times)) self.assertEquals(times[0][0], datetime(2009, 6, 1, 13)) self.assertEquals(times[0][1], datetime(2009, 6, 1, 14)) # Clean up otherperiod.delete() othersesshun.delete(force = True) otherproject.delete(force = True)
def setUp(self): super(TestSesshun, self).setUp() self.sesshun = create_sesshun()
def test_get_time_not_schedulable(self): "Test a number of overlapping bad things" # First bad thing: a receiver schedule that leaves out our rx # Schedule = 4/01/2009: 450, 600, 800 # 4/06/2009: 600, 800, 1070 # 4/11/2009: 800, 1070, 1_2 # 4/16/2009: 1070, 1_2, 2_3 # 4/21/2009: 1_2, 2_3, 4_6 # 4/26/2009: 2_3, 4_6, 8_10 # 5/01/2009: 4_6, 8_10, 12_18 # 5/06/2009: 8_10, 12_18, 18_26 # 5/11/2009: 12_18, 18_26, 26_40 start = datetime(2009, 4, 1, 0) # April 1 end = datetime(2009, 6, 1, 0) # June 1 rcvr_id = 3 for i in range(9): start_date = start + timedelta(5*i) for j in range(1, 4): rcvr_id = rcvr_id + 1 rs = Receiver_Schedule() rs.start_date = start_date rs.receiver = Receiver.objects.get(id = rcvr_id) rs.save() rcvr_id = rcvr_id - 2 # Now add some receivers to this session SessionHttpAdapter(self.sesshun).save_receivers('L | (X & S)') blackouts = self.sesshun.get_time_not_schedulable(start, end) # No available receivers at these times: exp = [(datetime(2009, 4, 1), datetime(2009, 4, 11)) , (datetime(2009, 5, 1), end) ] expected = set(exp) self.assertEquals(exp, blackouts) # Now add a project w/ prescheduled times. otherproject = Project() pdata = {"semester" : "09A" , "type" : "science" , "total_time" : "10.0" , "PSC_time" : "10.0" , "sem_time" : "10.0" , "grade" : "4.0" , "notes" : "notes" , "schd_notes" : "scheduler's notes" } adapter = ProjectHttpAdapter(otherproject) adapter.update_from_post(pdata) othersesshun = create_sesshun() othersesshun.project = otherproject othersesshun.save() fdata = {'session' : othersesshun.id , 'date' : '2009-04-20' , 'time' : '13:00' , 'duration' : 1.0 , 'backup' : False} otherperiod = Period() adapter = PeriodHttpAdapter(otherperiod) adapter.init_from_post(fdata, 'UTC') otherperiod.state = Period_State.objects.filter(abbreviation = 'S')[0] otherperiod.save() #exp.append((datetime(2009, 4, 20, 13, 0), datetime(2009, 4, 20, 14, 0))) exp = [(datetime(2009, 4, 1), datetime(2009, 4, 11)) , (datetime(2009, 4, 20, 13), datetime(2009, 4, 20, 14)) , (datetime(2009, 5, 1), end) ] blackouts = self.sesshun.get_time_not_schedulable(start, end) self.assertEquals(exp, blackouts) # how much time is that? hrsNotSchedulable = sum([TimeAgent.timedelta2minutes(b[1] - b[0])/60.0\ for b in blackouts]) self.assertEquals(985.0, hrsNotSchedulable) # how does this work when limiting the range? newEnd = datetime(2009, 4, 3) blackouts = self.sesshun.get_time_not_schedulable(start, newEnd) self.assertEquals([(start, newEnd)], blackouts) # extend this with a Project Blackout blackout = create_blackout(project = self.sesshun.project, start = datetime(2009, 4, 18, 12), end = datetime(2009, 4, 23, 12), repeat = 'Once') exp = [(datetime(2009, 4, 1), datetime(2009, 4, 11)) , (datetime(2009, 4, 18, 12), datetime(2009, 4, 23, 12)) , (datetime(2009, 5, 1), end) ] blackouts = self.sesshun.get_time_not_schedulable(start, end) self.assertEquals(exp, blackouts) # test the time available blacked out calculations # Calculate the expected result: # it turns out that the project blackout overlaps with all # schedulable time, except for one hour on 4/20 hrsBlackedOut = (TimeAgent.timedelta2minutes(blackout.getEndDate() - blackout.getStartDate()) / 60.0) - 1.0 totalTime = TimeAgent.timedelta2minutes(end - start) / 60.0 hrsSchedulable = totalTime - hrsNotSchedulable s, b, ss, bb = self.sesshun.getBlackedOutSchedulableTime(start, end) self.assertEquals(hrsSchedulable, s) self.assertEquals(hrsBlackedOut, b) # test it some more, but in different ranges start = datetime(2009, 5, 1) s, b, ss, bb = self.sesshun.getBlackedOutSchedulableTime(start, end) self.assertEquals(0.0, b) start = datetime(2009, 4, 22) end = datetime(2009, 4, 26) totalTime = TimeAgent.timedelta2minutes(end - start) / 60.0 s, b, ss, bb = self.sesshun.getBlackedOutSchedulableTime(start, end) self.assertEquals(totalTime, s) self.assertEquals(36.0, b) # cleanup self.sesshun.receiver_group_set.all().delete()
def test_get_prescheduled_days(self): start = datetime(2009, 6, 1) end = datetime(2009, 6, 3) # No periods. days = self.project.get_prescheduled_days(start, end) self.assertEquals(0, len(days)) # Now add a project w/ prescheduled times but not for whole day. otherproject = Project() pdata = {"semester" : "09A" , "pcode" : "otherProject" , "type" : "science" , "total_time" : "10.0" , "PSC_time" : "10.0" , "sem_time" : "10.0" , "grade" : "4.0" , "notes" : "notes" , "schd_notes" : "scheduler's notes" } project_adapter = ProjectHttpAdapter(otherproject) project_adapter.update_from_post(pdata) otherproject.save() othersesshun = create_sesshun() othersesshun.project = otherproject othersesshun.save() fdata = {'session' : othersesshun.id , 'date' : '2009-06-01' , 'time' : '13:00' , 'duration' : 1.0 , 'backup' : False} otherperiod = Period() period_adapter = PeriodHttpAdapter(otherperiod) period_adapter.init_from_post(fdata, 'UTC') otherperiod.state = Period_State.objects.filter(abbreviation = 'S')[0] otherperiod.save() # Test again days = self.project.get_prescheduled_days(start, end) self.assertEquals(0, len(days)) days = self.project.get_prescheduled_days(start, start + timedelta(days = 1)) self.assertEquals(0, len(days)) # Now add a period w/ prescheduled times for whole day. fdata = {'session' : othersesshun.id , 'date' : '2009-05-31' , 'time' : '23:00' , 'duration' : 30.0 , 'backup' : False} anotherperiod = Period() period_adapter.load(anotherperiod) period_adapter.init_from_post(fdata, 'UTC') anotherperiod.state = Period_State.objects.filter(abbreviation = 'S')[0] anotherperiod.save() # Test again days = self.project.get_prescheduled_days(start, end) self.assertEquals(1, len(days)) self.assertEquals(days[0], datetime(2009, 6, 1)) days = self.project.get_prescheduled_days(start, start + timedelta(days = 1)) self.assertEquals(1, len(days)) self.assertEquals(days[0], datetime(2009, 6, 1)) days = self.project.get_prescheduled_days(start + timedelta(days=1) , start + timedelta(days=2)) self.assertEquals(0, len(days)) # Now make that whole day prescheduled period part of the project. anotherperiod.session = self.sesshun anotherperiod.save() # Test again days = self.project.get_prescheduled_days(start, end) self.assertEquals(0, len(days)) days = self.project.get_prescheduled_days(start, start + timedelta(days = 1)) self.assertEquals(0, len(days)) # Clean up anotherperiod.delete() otherperiod.delete() othersesshun.delete(force = True) otherproject.delete(force = True)