def setUpTestData(cls): cls.traveler = UserFactory() cls.unicef_staff = UserFactory(is_staff=True) cls.travel = TravelFactory(reference_number=make_travel_reference_number(), traveler=cls.traveler, supervisor=cls.unicef_staff, section=None)
def test_list_view(self): partner = self.travel.activities.first().partner partner_id = partner.id with self.assertNumQueries(4): response = self.forced_auth_req('get', reverse('t2f:travels:list:activities', kwargs={'partner_organization_pk': partner_id}), user=self.unicef_staff) response_json = json.loads(response.rendered_content) expected_keys = ['primary_traveler', 'travel_type', 'date', 'locations', 'status', 'reference_number', 'trip_id'] self.assertEqual(len(response_json), 1) self.assertKeysIn(expected_keys, response_json[0], exact=True) # add a new travel activity and make sure the number of queries remain the same travel2 = TravelFactory(reference_number=make_travel_reference_number(), traveler=self.traveler1, status=Travel.APPROVED, supervisor=self.unicef_staff) act = travel2.activities.first() act.partner = partner act.save() self.assertEqual(act.primary_traveler, act.travels.first().traveler) with self.assertNumQueries(4): response = self.forced_auth_req('get', reverse('t2f:travels:list:activities', kwargs={'partner_organization_pk': partner_id}), user=self.unicef_staff) response_json = json.loads(response.rendered_content) self.assertEqual(len(response_json), 2)
def test_dashboard_travels_list_view_no_section(self): travel = TravelFactory(reference_number=make_travel_reference_number(), traveler=self.traveler, supervisor=self.unicef_staff, sector=None, section=None) with self.assertNumQueries(10): response = self.forced_auth_req( 'get', reverse( 't2f:travels:list:dashboard', ), user=self.unicef_staff, data={ "office_id": travel.office.id, "year": travel.start_date.year, "months": '{month:02d}'.format(month=travel.start_date.month) } ) response_json = json.loads(response.rendered_content) self.assertEqual(response.status_code, status.HTTP_200_OK) expected_keys = ['travels_by_section', 'completed', 'planned', 'approved'] self.assertKeysIn(expected_keys, response_json) self.assertEqual(response_json['travels_by_section'][0]['section_name'], 'No Section selected') self.assertEqual(len(response_json['travels_by_section']), 1) self.assertEqual(response_json['planned'], 1)
def setUpTestData(cls): business_area = PublicsBusinessAreaFactory() cls.traveler = UserFactory(is_staff=True) cls.traveler.profile.vendor_number = 'usrvnd' cls.traveler.profile.save() workspace = cls.traveler.profile.country workspace.business_area_code = business_area.code workspace.save() cls.unicef_staff = UserFactory(is_staff=True) cls.travel = TravelFactory( reference_number=make_travel_reference_number(), traveler=cls.traveler, supervisor=cls.unicef_staff, start_date=datetime(2017, 4, 4, 12, 00, tzinfo=UTC), end_date=datetime(2017, 4, 14, 16, 00, tzinfo=UTC)) cls.travel.expenses.all().delete() ItineraryItemFactory(travel=cls.travel) ItineraryItemFactory(travel=cls.travel) cls.travel.submit_for_approval() cls.travel.approve() cls.travel.send_for_payment() cls.travel.save()
class TravelFactory(factory.DjangoModelFactory): traveler = factory.SubFactory(UserFactory) supervisor = factory.SubFactory(UserFactory) office = factory.SubFactory(OfficeFactory) sector = factory.SubFactory(SectorFactory) start_date = fuzzy.FuzzyDateTime(start_dt=_FUZZY_START_DATE, end_dt=timezone.now()) end_date = fuzzy.FuzzyDateTime(start_dt=timezone.now(), end_dt=_FUZZY_END_DATE) purpose = factory.Sequence(lambda n: 'Purpose #{}'.format(n)) international_travel = False ta_required = True reference_number = factory.Sequence( lambda n: models.make_travel_reference_number()) currency = factory.SubFactory(PublicsCurrencyFactory) mode_of_travel = [] itinerary = factory.RelatedFactory(ItineraryItemFactory, 'travel') expenses = factory.RelatedFactory(ExpenseFactory, 'travel') deductions = factory.RelatedFactory(DeductionFactory, 'travel') cost_assignments = factory.RelatedFactory(CostAssignmentFactory, 'travel') clearances = factory.RelatedFactory(ClearanceFactory, 'travel') action_points = factory.RelatedFactory(ActionPointFactory, 'travel') @factory.post_generation def populate_activities(self, create, extracted, **kwargs): ta = TravelActivityFactory(primary_traveler=self.traveler) ta.travels.add(self) class Meta: model = models.Travel
def _clone_model(self, model): from t2f.models import make_travel_reference_number new_instance = model.__class__.objects.get(pk=model.pk) new_instance.pk = None new_instance.id = None new_instance.reference_number = make_travel_reference_number() return new_instance
def setUpTestData(cls): cls.unicef_staff = UserFactory(is_staff=True) cls.traveler1 = UserFactory() cls.traveler2 = UserFactory() cls.travel = TravelFactory(reference_number=make_travel_reference_number(), traveler=cls.traveler1, status=Travel.APPROVED, supervisor=cls.unicef_staff) # to filter against cls.travel_activity = TravelActivityFactory(primary_traveler=cls.traveler1) cls.travel_activity.travels.add(cls.travel)
def test_completed_counts(self): currency = PublicsCurrencyFactory() expense_type = PublicsTravelExpenseTypeFactory() business_area = PublicsBusinessAreaFactory() dsa_region = PublicsDSARegionFactory() wbs = PublicsWBSFactory(business_area=business_area) grant = wbs.grants.first() fund = grant.funds.first() traveler = UserFactory(is_staff=True) traveler.profile.vendor_number = 'usrvend' traveler.profile.save() travel = TravelFactory(reference_number=make_travel_reference_number(), traveler=traveler, status=Travel.CERTIFIED, supervisor=self.unicef_staff) data = {'cost_assignments': [{'wbs': wbs.id, 'grant': grant.id, 'fund': fund.id, 'share': 100}], 'deductions': [{'date': '2016-11-03', 'breakfast': True, 'lunch': True, 'dinner': False, 'accomodation': True}], 'itinerary': [{'origin': 'Berlin', 'destination': 'Budapest', 'departure_date': '2017-04-14T17:06:55.821490', 'arrival_date': '2017-04-15T17:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': []}, {'origin': 'Budapest', 'destination': 'Berlin', 'departure_date': '2017-05-20T12:06:55.821490', 'arrival_date': '2017-05-21T12:06:55.821490', 'dsa_region': dsa_region.id, 'overnight_travel': False, 'mode_of_travel': ModeOfTravel.RAIL, 'airlines': []}], 'traveler': traveler.id, 'ta_required': True, 'report': 'Some report', 'currency': currency.id, 'supervisor': self.unicef_staff.id, 'expenses': [{'amount': '120', 'type': expense_type.id, 'currency': currency.id, 'document_currency': currency.id}]} act1 = TravelActivityFactory(travel_type=TravelType.PROGRAMME_MONITORING, primary_traveler=traveler) act2 = TravelActivityFactory(travel_type=TravelType.SPOT_CHECK, primary_traveler=traveler) act1.travels.add(travel) act2.travels.add(travel) partner_programmatic_visits = PartnerOrganization.objects.get(id=act1.partner.id) partner_spot_checks = PartnerOrganization.objects.get(id=act2.partner.id) response = self.forced_auth_req('post', reverse('t2f:travels:details:state_change', kwargs={'travel_pk': travel.id, 'transition_name': 'mark_as_completed'}), user=traveler, data=data) response_json = json.loads(response.rendered_content) partner_programmatic_visits_after_complete = PartnerOrganization.objects.get(id=act1.partner.id) partner_spot_checks_after_complete = PartnerOrganization.objects.get(id=act2.partner.id) self.assertEqual(response_json['status'], Travel.COMPLETED) self.assertEqual(partner_programmatic_visits.hact_values['programmatic_visits']['completed']['total'] + 1, partner_programmatic_visits_after_complete.hact_values[ 'programmatic_visits']['completed']['total']) self.assertEqual(partner_spot_checks.hact_values['spot_checks']['completed']['total'] + 1, partner_spot_checks_after_complete.hact_values['spot_checks']['completed']['total'])