class DevicePositionInTimeTest(TransactionTestCase): """ test for DevicePositionInTime model """ def setUp(self): """ this method will automatically call for every single test """ # for testing requests inside the project self.factory = RequestFactory() self.userId = "067e6162-3b6f-4ae2-a171-2470b63dff00" # inital config for DevicePositionInTime self.time = timezone.now() DevicePositionInTime.objects.create( userId=self.userId, longitud=3.5, latitud=5.2, timeStamp=self.time) DevicePositionInTime.objects.create( userId=self.userId, longitud=3.4, latitud=5.2, timeStamp=self.time) # this should not be answered DevicePositionInTime.objects.create(userId=self.userId, longitud=3.3, latitud=4.2, timeStamp=self.time - timezone.timedelta(minutes=11)) # initial config for ActiveToken self.test = TestHelper(self) self.test.insertEventsOnDatabase() # add dummy bus Bus.objects.create( registrationPlate='AA1111', service='507', uuid='159fc6b7-7a20-477e-b5c7-af421e1e0e16') # add dummy bus stop busStop = BusStop.objects.create( code='PA459', name='bla', longitud=0, latitud=0) # add dummy service and its path # '#00a0f0'color_id = models.IntegerField(default = 0) Service.objects.create( service='507', origin='origin_test', destiny='destination_test') ServiceStopDistance.objects.create( busStop=busStop, service='507I', distance=5) ServiceLocation.objects.create( service='507I', distance=1, longitud=4, latitud=5) ServiceLocation.objects.create( service='507I', distance=2, longitud=5, latitud=5) ServiceLocation.objects.create( service='507I', distance=3, longitud=6, latitud=5) ServiceLocation.objects.create( service='507I', distance=4, longitud=7, latitud=5) ServiceLocation.objects.create( service='507I', distance=5, longitud=8, latitud=5) ServiceLocation.objects.create( service='507I', distance=6, longitud=9, latitud=5) def test_consistencyModelDevicePositionInTime(self): '''This method test the database for the DevicePositionInTime model''' longituds = [3.5, 3.4, 3.3] latituds = [5.2, 5.2, 4.2] timeStamps = [ self.time, self.time, self.time - timezone.timedelta( minutes=11)] for cont in range(3): devicePosition = DevicePositionInTime.objects.get( longitud=longituds[cont]) self.assertEqual(devicePosition.latitud, latituds[cont]) self.assertEqual(devicePosition.timeStamp, timeStamps[cont]) def test_consistencyModelActiveToken(self): '''This method test the database for the ActiveToken model''' service = '503' licencePlate = 'ZZZZ00' travelToken = self.test.getInBusWithLicencePlate( self.userId, service, licencePlate) # the created token is an active token self.assertEqual( ActiveToken.objects.filter( token=travelToken).exists(), True) # the created token exist in the table of token self.assertEqual( Token.objects.filter( token=travelToken).exists(), True) jsonResponse = self.test.endRoute(travelToken) self.assertEqual(jsonResponse['response'], 'Trip ended.') # activeToken has to be missing but token has to exists self.assertEqual( ActiveToken.objects.filter( token=travelToken).exists(), False) self.assertEqual( Token.objects.filter( token=travelToken).exists(), True) def test_consistencyModelPoseInTrajectoryOfToken(self): '''this method test the PoseInTrajectoryOfToken''' service = '503' licencePlate = 'ZZZZ00' testToken = self.test.getInBusWithLicencePlate( self.userId, service, licencePlate) jsonResponse = self.test.sendFakeTrajectoryOfToken(testToken) self.assertEqual(jsonResponse['response'], 'Poses were register.') # remove the token self.test.endRoute(testToken) jsonResponse = self.test.sendFakeTrajectoryOfToken(testToken) self.assertEqual(jsonResponse['response'], 'Token doesn\'t exist.') def test_EventsByBusStopReportNegativelyForFistTime(self): """ report stop event negatively for fist time """ busStopCode = 'PA459' eventCode = 'evn00001' jsonResponse = self.test.confirmOrDeclineStopEvent( self.userId, busStopCode, eventCode, 'decline') self.assertEqual(jsonResponse['codeBusStop'], busStopCode) self.assertEqual(jsonResponse['events'][0]['eventDecline'], 1) self.assertEqual(jsonResponse['events'][0]['eventConfirm'], 0) self.assertEqual(jsonResponse['events'][0]['eventcode'], eventCode) def test_EventsByBusStop(self): '''This method test two thing, the posibility to report an event and asking the events for the specific busStop''' busStopCode = 'PA459' eventCode = 'evn00001' # submitting some events to the server jsonResponse = self.test.reportStopEvent( self.userId, busStopCode, eventCode) # report one event, and confirm it self.assertEqual(jsonResponse['codeBusStop'], busStopCode) self.assertEqual(jsonResponse['events'][0]['eventDecline'], 0) self.assertEqual(jsonResponse['events'][0]['eventConfirm'], 1) self.assertEqual(jsonResponse['events'][0]['eventcode'], eventCode) # do event +1 to the event jsonResponse = self.test.confirmOrDeclineStopEvent( self.userId, busStopCode, eventCode, 'confirm') self.assertEqual(jsonResponse['codeBusStop'], busStopCode) self.assertEqual(jsonResponse['events'][0]['eventDecline'], 0) self.assertEqual(jsonResponse['events'][0]['eventConfirm'], 2) self.assertEqual(jsonResponse['events'][0]['eventcode'], eventCode) # do event -1 to the event jsonResponse = self.test.confirmOrDeclineStopEvent( self.userId, busStopCode, eventCode, 'decline') self.assertEqual(jsonResponse['codeBusStop'], busStopCode) self.assertEqual(jsonResponse['events'][0]['eventDecline'], 1) self.assertEqual(jsonResponse['events'][0]['eventConfirm'], 2) self.assertEqual(jsonResponse['events'][0]['eventcode'], eventCode) # change manualy the timeStamp to simulate an event that has expired busStop = BusStop.objects.get(code=busStopCode) event = Event.objects.get(id=eventCode) anEvent = EventForBusStop.objects.get(busStop=busStop, event=event) anEvent.timeStamp = anEvent.timeCreation - \ timezone.timedelta(minutes=event.lifespam) anEvent.save() # ask for ecents and the answere should be none jsonResponse = self.test.reportStopEvent( self.userId, busStopCode, eventCode) self.assertEqual(jsonResponse['events'][0]['eventDecline'], 0) self.assertEqual(jsonResponse['events'][0]['eventConfirm'], 1) self.assertEqual(jsonResponse['events'][0]['eventcode'], eventCode) def test_registerPose(self): request = self.factory.get('/android/userPosition') request.user = AnonymousUser() lat = 45 lon = 46 response = views.userPosition(request, self.userId, lat, lon) self.assertEqual(response.status_code, 200) self.assertEqual( DevicePositionInTime.objects.filter( longitud=lon, latitud=lat).exists(), True) def test_preferPositionOfPersonInsideABus(self): # Bus.objects.create(registrationPlate = 'AA1111', service = '507') thebus = Busv2.objects.create(registrationPlate='AA1111') Busassignment.objects.create(service='507', uuid=thebus) timeStampNow = str(timezone.localtime(timezone.now())) timeStampNow = timeStampNow[0:19] userLatitud = -33.458771 userLongitud = -70.676266 # first we test the position of the bus without passsangers # bus = Bus.objects.get(registrationPlate='AA1111', service='507') bus = Busv2.objects.get(registrationPlate='AA1111') busassignment = Busassignment.objects.get(service='507', uuid=bus) busPose = busassignment.getLocation() self.assertTrue(busPose['random']) self.assertEqual(busPose['latitude'], -500) self.assertEqual(busPose['longitude'], -500) self.assertEqual(busPose['passengers'], 0) # add the position of a passanger inside the bus service = '507' licencePlate = 'AA1111' testToken = self.test.getInBusWithLicencePlate( self.userId, service, licencePlate) testPoses = {"poses": [ {"latitud": userLatitud, "longitud": userLongitud, "timeStamp": str(timeStampNow), "inVehicleOrNot": "vehicle"}]} jsonResponse = self.test.sendFakeTrajectoryOfToken( testToken, testPoses) self.assertEqual(jsonResponse['response'], 'Poses were register.') # ask the position of the bus whit a passanger bus = Busv2.objects.get(registrationPlate=licencePlate) busassignment = Busassignment.objects.get(uuid=bus, service=service) busPose = busassignment.getLocation() self.assertEqual(busPose['latitude'], userLatitud) self.assertEqual(busPose['longitude'], userLongitud) self.assertEqual(busPose['random'], False) self.assertEqual(busPose['passengers'] > 0, True) self.test.endRoute(testToken)
class CronTasksTestCase(TransactionTestCase): """ test for cron-task actions """ def setUp(self): self.factory = RequestFactory() self.test = TestHelper(self) self.test.insertEventsOnDatabase() # create bus stop self.stop = "PI62" self.test.insertBusstopsOnDatabase([self.stop]) # define test events self.stopEventCode = "evn00010" self.busEventCode = "evn00200" self.userId = "067e6162-3b6f-4ae2-a171-2470b63dff00" self.service = "506" self.registrationPlate = "XXYY25" self.machineId = self.test.askForMachineId(self.registrationPlate) def test_does_not_have_the_minimum_number_of_declines_for_bus_stop(self): """ it does not have the minimum number of declines for bus stop """ self.test.reportStopEvent(self.userId, self.stop, self.stopEventCode) # decline event for index in range(0, cronTasks.MINIMUM_NUMBER_OF_DECLINES - 2): self.test.confirmOrDeclineStopEvent(self.userId, self.stop, self.stopEventCode, "decline") # decline isn't 100% over confirm cronTasks.clearEventsThatHaveBeenDecline() jsonResponse = self.test.requestEventsForBusStop(self.stop) # evaluate events self.assertEqual(len(jsonResponse["events"]), 1) self.assertEqual(jsonResponse["events"][0]["eventcode"], self.stopEventCode) def test_does_not_have_the_percentage_of_declines_for_bus_stop(self): """ it has the minimum number of declines but not the percentage of declines over confirms for bus stop""" self.test.reportStopEvent(self.userId, self.stop, self.stopEventCode) # report events for bus stop for index in range(0, cronTasks.MINIMUM_NUMBER_OF_DECLINES + 1): self.test.confirmOrDeclineStopEvent(self.userId, self.stop, self.stopEventCode, "confirm") # decline event for index in range(0, cronTasks.MINIMUM_NUMBER_OF_DECLINES + 1): self.test.confirmOrDeclineStopEvent(self.userId, self.stop, self.stopEventCode, "decline") # decline isn't 100% over confirm cronTasks.clearEventsThatHaveBeenDecline() jsonResponse = self.test.requestEventsForBusStop(self.stop) # evaluate events self.assertEqual(len(jsonResponse["events"]), 1) self.assertEqual(jsonResponse["events"][0]["eventcode"], self.stopEventCode) def test_have_the_percentage_of_declines_and_the_minimum_number_of_declines_over_confirm_for_bus_stop(self): """ it has the minimum number of declines and the percentage of declines over confirms for bus stop""" self.test.reportStopEvent(self.userId, self.stop, self.stopEventCode) # report events for bus stop for index in range(0, cronTasks.MINIMUM_NUMBER_OF_DECLINES): self.test.confirmOrDeclineStopEvent(self.userId, self.stop, self.stopEventCode, "confirm") # decline event for index in range(0, cronTasks.MINIMUM_NUMBER_OF_DECLINES * 3): self.test.confirmOrDeclineStopEvent(self.userId, self.stop, self.stopEventCode, "decline") # decline isn't 100% over confirm cronTasks.clearEventsThatHaveBeenDecline() jsonResponse = self.test.requestEventsForBusStop(self.stop) # evaluate events self.assertEqual(len(jsonResponse["events"]), 0) def test_does_not_have_the_minimum_number_of_declines_for_bus(self): """ it does not have the minimum number of declines for bus """ # create assignment self.test.createBusAndAssignmentOnDatabase(self.userId, self.service, self.registrationPlate) self.test.reportEventV2(self.userId, self.machineId, self.service, self.busEventCode) # decline event for index in range(0, cronTasks.MINIMUM_NUMBER_OF_DECLINES - 1): self.test.confirmOrDeclineEventV2(self.userId, self.machineId, self.service, self.busEventCode, "decline") # decline isn't 100% over confirm cronTasks.clearEventsThatHaveBeenDecline() jsonResponse = self.test.requestEventsForBusV2(self.machineId) # evaluate events self.assertEqual(len(jsonResponse["events"]), 1) self.assertEqual(jsonResponse["events"][0]["eventcode"], self.busEventCode) self.assertEqual(jsonResponse["uuid"], self.machineId) self.assertEqual(jsonResponse["registrationPlate"], self.registrationPlate) def test_does_not_have_the_percentage_of_declines_for_bus(self): """ it has the minimum number of declines but not the percentage of declines over confirms for bus """ # create assignment self.test.createBusAndAssignmentOnDatabase(self.userId, self.service, self.registrationPlate) # generate report events for bus for index in range(0, cronTasks.MINIMUM_NUMBER_OF_DECLINES + 1): self.test.confirmOrDeclineEventV2(self.userId, self.machineId, self.service, self.busEventCode, "confirm") # decline event for index in range(0, cronTasks.MINIMUM_NUMBER_OF_DECLINES + 1): self.test.confirmOrDeclineEventV2(self.userId, self.machineId, self.service, self.busEventCode, "decline") # decline is 100% over confirm cronTasks.clearEventsThatHaveBeenDecline() jsonResponse = self.test.requestEventsForBusV2(self.machineId) # evaluate events self.assertEqual(len(jsonResponse["events"]), 1) self.assertEqual(jsonResponse["events"][0]["eventcode"], self.busEventCode) self.assertEqual(jsonResponse["uuid"], self.machineId) self.assertEqual(jsonResponse["registrationPlate"], self.registrationPlate) def test_have_the_percentage_of_declines_and_the_minimum_number_of_declines_over_confirm_for_bus(self): """ it has the minimum number of declines and the percentage of declines over confirms for bus """ # create assignment self.test.createBusAndAssignmentOnDatabase(self.userId, self.service, self.registrationPlate) self.test.reportEventV2(self.userId, self.machineId, self.service, self.busEventCode) # generate report events for bus for index in range(0, cronTasks.MINIMUM_NUMBER_OF_DECLINES): self.test.confirmOrDeclineEventV2(self.userId, self.machineId, self.service, self.busEventCode, "confirm") # decline event for index in range(0, cronTasks.MINIMUM_NUMBER_OF_DECLINES * 3): self.test.confirmOrDeclineEventV2(self.userId, self.machineId, self.service, self.busEventCode, "decline") # decline is 100% over confirm jsonResponse = self.test.requestEventsForBusV2(self.machineId) self.assertEqual(len(jsonResponse["events"]), 1) self.assertEqual(jsonResponse["events"][0]["eventcode"], self.busEventCode) self.assertEqual(jsonResponse["uuid"], self.machineId) cronTasks.clearEventsThatHaveBeenDecline() jsonResponse = self.test.requestEventsForBusV2(self.machineId) # evaluate events self.assertEqual(len(jsonResponse["events"]), 0)