def test_recommended_going_query(self): # Creates the available journeys with driver... user1 = UserFactory() residence1 = ResidenceFactory(user=user1, position=Point(883877.34, 547084.05, srid=DEFAULT_PROJECTED_SRID)) user2 = UserFactory() residence2 = ResidenceFactory(user=user2, position=Point(882823.07, 545542.48, srid=DEFAULT_PROJECTED_SRID)) campus = CampusFactory() user3 = UserFactory() residence3 = ResidenceFactory(user=user3, position=Point(865621.24, 545274.90, srid=DEFAULT_PROJECTED_SRID)) template = JourneyTemplateFactory(user=user1, driver=user1, residence=residence1, campus=campus) JourneyFactory(template=template) template = JourneyTemplateFactory(user=user2, driver=user2, residence=residence2, campus=campus) JourneyFactory(template=template) template = JourneyTemplateFactory(user=user3, driver=user3, residence=residence3, campus=campus) JourneyFactory(template=template) user4 = UserFactory() residence4 = ResidenceFactory( user=user4, position=Point(882532.74, 545437.43, srid=DEFAULT_PROJECTED_SRID), distance=2500 ) template = JourneyTemplateFactory(user=user4, residence=residence4, campus=campus) JourneyFactory(template=template) self.assertEquals(Journey.objects.recommended( user=user4, kind=GOING ).count(), 2)
def test_search_form(self): user = UserFactory() template = JourneyTemplateFactory( departure=make_aware(datetime.datetime.now() + datetime.timedelta(days=2)), user=user, driver=user) JourneyFactory(template=template, departure=template.departure) template = JourneyTemplateFactory(user=user, driver=user) journey = JourneyFactory(template=template) data = { "departure_date": journey.departure.date(), "departure_time": journey.departure.time(), "distance": 1000, "time_window": 30, "position": six.text_type( make_point(journey.residence.position, origin_coord_srid=DEFAULT_PROJECTED_SRID, destiny_coord_srid=DEFAULT_GOOGLE_MAPS_SRID)), } form = SearchJourneyForm(data) self.assertTrue(form.is_valid()) results = form.search(UserFactory()) self.assertEquals(1, results.count())
def test_recommended_journeys(self): # Creates the available journeys with driver... user1 = UserFactory() residence1 = ResidenceFactory(user=user1, position=Point( 883877.34, 547084.05, srid=DEFAULT_PROJECTED_SRID)) user2 = UserFactory() residence2 = ResidenceFactory(user=user2, position=Point( 882823.07, 545542.48, srid=DEFAULT_PROJECTED_SRID)) campus = CampusFactory() user3 = UserFactory() residence3 = ResidenceFactory(user=user3, position=Point( 865621.24, 545274.90, srid=DEFAULT_PROJECTED_SRID)) template = JourneyTemplateFactory(user=user1, driver=user1, residence=residence1, campus=campus) JourneyFactory(template=template) template = JourneyTemplateFactory(user=user2, driver=user2, residence=residence2, campus=campus) JourneyFactory(template=template) template = JourneyTemplateFactory(user=user3, driver=user3, residence=residence3, campus=campus) JourneyFactory(template=template) user4 = UserFactory() residence4 = ResidenceFactory(user=user4, position=Point( 882532.74, 545437.43, srid=DEFAULT_PROJECTED_SRID), distance=2500) template = JourneyTemplateFactory(user=user4, residence=residence4, campus=campus) journey = JourneyFactory(template=template) # Make query... url = "/api/v1/journeys/{}/recommended/".format(journey.pk) self.client.force_authenticate(user=user4) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) response_data = json.loads(response.content.decode('utf-8')) self.assertEquals(2, len(response_data['results']))
def test_destination(self): user = UserFactory() origin = ResidenceFactory(user=user) destination = CampusFactory() template = JourneyTemplateFactory(user=user, residence=origin, campus=destination, kind=GOING) journey = JourneyFactory(template=template) self.assertEquals(journey.destination, destination)
def test_available_query(self): # Creates the available journeys with driver... user1 = UserFactory() residence1 = ResidenceFactory(user=user1, position=Point(883877.34, 547084.05, srid=DEFAULT_PROJECTED_SRID)) user2 = UserFactory() residence2 = ResidenceFactory(user=user2, position=Point(882823.07, 545542.48, srid=DEFAULT_PROJECTED_SRID)) campus = CampusFactory() template = JourneyTemplateFactory(user=user1, driver=user1, residence=residence1, campus=campus, kind=RETURN) JourneyFactory(template=template) template = JourneyTemplateFactory(user=user2, driver=user2, residence=residence2, campus=campus) JourneyFactory(template=template) # Creates a journey without driver user3 = UserFactory() residence3 = ResidenceFactory(user=user3, position=Point(865621.24, 545274.90, srid=DEFAULT_PROJECTED_SRID)) template = JourneyTemplateFactory(user=user3, residence=residence3, campus=campus, kind=RETURN) JourneyFactory(template=template) self.assertEquals(Journey.objects.available(user=user3).count(), 2)
def _make_journey(user=None, kind=GOING): user = UserFactory() if user is None else user origin = ResidenceFactory(user=user) destination = CampusFactory() template = JourneyTemplateFactory(user=user, residence=origin, campus=destination, kind=kind) return JourneyFactory(template=template)
def test_expand(self): journey = JourneyTemplateFactory() journey.departure = journey.departure.replace(day=1, month=1) journey.save() pattern = rrule(dtstart=make_naive(journey.departure), until=make_naive(journey.departure) + datetime.timedelta(days=20), freq=DAILY) journey.recurrence = str(pattern) journey.save() journeys = expand(journey) self.assertEquals(21, len(journeys))
def _make_journey(kind=GOING): user = UserFactory() origin = ResidenceFactory(user=user) destination = CampusFactory() template = JourneyTemplateFactory(user=user, residence=origin, campus=destination, kind=kind) return JourneyFactory(template=template, departure=template.departure, arrival=template.arrival)
def test_nearby_query(self): # Creates the available journeys with driver... user1 = UserFactory() residence1 = ResidenceFactory(user=user1, position=Point(883877.34, 547084.05, srid=DEFAULT_PROJECTED_SRID)) user2 = UserFactory() residence2 = ResidenceFactory(user=user2, position=Point(882823.07, 545542.48, srid=DEFAULT_PROJECTED_SRID)) campus = CampusFactory() user3 = UserFactory() residence3 = ResidenceFactory(user=user3, position=Point(865621.24, 545274.90, srid=DEFAULT_PROJECTED_SRID)) template = JourneyTemplateFactory(user=user1, driver=user1, residence=residence1, campus=campus) JourneyFactory(template=template) template = JourneyTemplateFactory(user=user2, driver=user2, residence=residence2, campus=campus) JourneyFactory(template=template) template = JourneyTemplateFactory(user=user3, driver=user3, residence=residence3, campus=campus) JourneyFactory(template=template) point = Point(882532.74, 545437.43, srid=DEFAULT_PROJECTED_SRID) self.assertEquals(Journey.objects.nearby( geometry=point, distance=D(m=2500) ).count(), 2)
def test_get_messages_journey(self): user = UserFactory() origin = ResidenceFactory(user=user) destination = CampusFactory() template = JourneyTemplateFactory(user=user, residence=origin, campus=destination) journey = JourneyFactory(template=template) [ MessageFactory( user=UserFactory(), journey=JourneyFactory(template=JourneyTemplateFactory( user=UserFactory(), residence=origin, campus=destination))) for _ in range(2) ] [MessageFactory(user=user, journey=journey) for _ in range(5)] [MessageFactory(user=UserFactory(), journey=journey) for _ in range(5)] url = "/api/v1/journeys/{}/messages/".format(journey.pk) self.client.force_authenticate(user=user) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) response_data = json.loads(response.content.decode('utf-8')) self.assertEquals(10, len(response_data['results']))
def test_create_message_owner_user(self): user = UserFactory() origin = ResidenceFactory(user=user) destination = CampusFactory() template = JourneyTemplateFactory(user=user, residence=origin, campus=destination) journey = JourneyFactory(template=template) self.assertEquals(0, journey.messages.count()) data = { "content": "Hello!", "journey": journey.pk, } self.client.force_authenticate(user=user) url = "/api/v1/messages/" response = self.client.post(url, data=data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEquals(1, journey.messages.count())