Example #1
0
 def test_sell_to_not_allowed_raises_exception(self):
     # This is not ideal, as we're duplicating testing for rules.py
     def fake_rule(*args, **kwargs):
         return False
     with temporary_settings({'RULE_CAN_BUY_CAR': fake_rule}):
         with self.assertRaises(Car.NotAllowedException):
             self.close.sell_to(self.user)
Example #2
0
    def test_ride(self):
        fare = 50
        offset = 10
        profile = self.user.get_profile()
        profile.balance = fare + offset
        owner_profile = self.user2.get_profile()
        self.close.owner_fares = FareInfo()
        self.close.owner = owner_profile

        original_riders = 1
        original_revenue = 15
        self.close.total_fares = FareInfo(riders=original_riders,
                                          revenue=original_revenue)

        def fake_fare(*args, **kwargs):
            return fare

        expected_rider_balance = profile.balance - fare
        expected_owner_balance = owner_profile.balance + fare
        with temporary_settings({'RULE_FIND_FARE': fake_fare}):
            self.assertEquals(self.close.ride(self.user,
                                   self.bathurst_station,
                                   self.bathurst_and_king),
                               fare)

        self.assertEqual(self.user.get_profile().balance,
            expected_rider_balance)
        self.assertEqual(self.user2.get_profile().balance,
            expected_owner_balance)
        self.assertEqual(self.close.owner_fares.riders, 1)
        self.assertEqual(self.close.owner_fares.revenue, fare)
        self.assertEqual(self.close.total_fares.riders, original_riders + 1)
        self.assertEqual(self.close.total_fares.revenue,
                        original_revenue + fare)
Example #3
0
    def test_sell_insufficient_funds_returns_403(self):
        profile = self.user.get_profile()
        profile.balance = 0
        profile.save()

        def fake_price(*args, **kwargs):
            return 100
        with temporary_settings({'RULE_GET_STREETCAR_PRICE': fake_price}):
            self.assertStatusCode(self.sell_name, self.car.number, code=403)
Example #4
0
    def setUp(self):

        # Construct the zip manually each time, to make debugging
        # slightly less painful
        with ZipFile(THIS_DIR + GTFS_ZIP, 'w') as gtfszip:
            for txt in glob.glob(THIS_DIR + GTFS_SUBDIR + '/*.txt'):
                gtfszip.write(txt, os.path.basename(txt))
        with temporary_settings({'GTFS_URL': THIS_DIR + GTFS_ZIP}):
            management.call_command('updatestops', stdout=NullStream())
Example #5
0
    def setUp(self):

        # Construct the zip manually each time, to make debugging
        # slightly less painful
        with ZipFile(THIS_DIR + GTFS_ZIP, 'w') as gtfszip:
            for txt in glob.glob(THIS_DIR + GTFS_SUBDIR + '/*.txt'):
                gtfszip.write(txt, os.path.basename(txt))
        with temporary_settings({'GTFS_URL': THIS_DIR + GTFS_ZIP}):
            management.call_command('updatestops', stdout=NullStream())
Example #6
0
    def test_find_nearby_limits_by_setting(self):
        api_url = reverse('stop-find', args=(self.loc[1], self.loc[0]))

        limit = 2
        with temporary_settings({'STOP_SEARCH_LIMIT': limit}):
            response = self.client.get(api_url)
        self.assertEquals(response.status_code, 200)
        data = json.loads(response.content)

        self.assertEquals(len(data), limit)
Example #7
0
    def test_checkout_cant_buy_has_no_purchase(self):
        self.user.get_profile().check_in(self.car, self.stop)

        def fake_fare(*args, **kwargs):
            return 0

        def fake_can_purchase(*args, **kwargs):
            return False
        with temporary_settings({'RULE_FIND_FARE': fake_fare,
                                 'RULE_CAN_BUY_CAR': fake_can_purchase}):
            data = self._get_checkout_data()
        self.assertNotIn('purchase', data)
Example #8
0
    def test_checkout_returns_fare(self):
        fare = 100
        profile = self.user.get_profile()
        profile.balance = fare * 10
        profile.save()

        def fake_fare(*args, **kwargs):
            return fare

        profile.check_in(self.car, self.bathurst_station)
        with temporary_settings({'RULE_FIND_FARE': fake_fare}):
            self.assertEquals(profile.check_out(self.bathurst_and_king),
                              fare)
Example #9
0
    def test_checkout_returns_fare(self):
        fare = 100
        profile = self.user.get_profile()
        profile.balance = fare * 100
        profile.save()
        profile.check_in(self.car, self.stop)

        def fake_fare(*args, **kwargs):
            return fare
        with temporary_settings({'RULE_FIND_FARE': fake_fare}):
            data = self._get_checkout_data()
        self.assertIn('fare', data)
        self.assertEquals(data['fare'], fare)
Example #10
0
    def test_sell_transfers_ownership(self):
        self.car.owner = None
        self.car.save()

        def fake_rule(*args, **kwargs):
            return True

        def fake_price(*args, **kwargs):
            return 0
        with temporary_settings({'RULE_CAN_BUY_CAR': fake_rule,
                                 'RULE_GET_STREETCAR_PRICE': fake_price}):
            self.assertStatusCode(self.sell_name, self.car.number)
        self.car = Car.objects.get(number=self.car.number)
        self.assertEquals(self.car.owner, self.user.get_profile())
Example #11
0
    def test_buy_back_credits_account(self):
        price = 65
        profile = self.user.get_profile()
        expected_balance = profile.balance + price

        def fake_price(*args, **kwargs):
            return price
        with temporary_settings({'RULE_GET_STREETCAR_PRICE': fake_price}):
            self.close.owner = profile
            self.close.save()
            self.close.buy_back(self.user)

            self.assertEqual(profile.balance, expected_balance)
            self.assertEqual(self.close.owner_fares.riders, 0)
            self.assertEqual(self.close.owner_fares.revenue, 0)
            self.assertEqual(self.close.owner, None)
Example #12
0
    def test_sell_to(self):
        price = 65
        difference = 50

        def fake_price(*args, **kwargs):
            return price
        with temporary_settings({'RULE_GET_STREETCAR_PRICE': fake_price}):
            profile = self.user.get_profile()
            new_balance = price + difference
            profile.balance = new_balance
            profile.save()

            self.close.owner_fares = FareInfo(riders=10, revenue=15)
            self.close.sell_to(self.user)
            self.assertEqual(profile.balance, difference)
            self.assertEqual(self.close.owner_fares.riders, 0)
            self.assertEqual(self.close.owner_fares.revenue, 0)
            self.assertEqual(self.close.owner, profile)
Example #13
0
    def test_checkout_can_buy_has_purchase(self):
        self.user.get_profile().check_in(self.car, self.stop)
        price = 1000

        def fake_fare(*args, **kwargs):
            return 0

        def fake_can_purchase(*args, **kwargs):
            return True

        def fake_price(*args, **kwargs):
            return price
        with temporary_settings({'RULE_FIND_FARE': fake_fare,
                                 'RULE_CAN_BUY_CAR': fake_can_purchase,
                                 'RULE_GET_STREETCAR_PRICE': fake_price}):
            data = self._get_checkout_data()
        self.assertIn('purchase', data)
        self.assertEquals(data['purchase']['price'], price)
        self.assertEquals(data['purchase']['url'],
                          reverse('car-sell', args=(self.car.number,)))
Example #14
0
    def test_get_list_data_correct(self):
        def fake_price(*args, **kwargs):
            return 0

        with temporary_settings({"RULE_GET_STREETCAR_PRICE": fake_price}):
            self.car1.sell_to(self.user)
            self.car3.sell_to(self.user)

        expected_cars = {car.number: car for car in (self.car1, self.car3)}
        response = self._make_get()
        self.assertEquals(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEquals(len(data), 2)
        for car in data:
            self.assertIn(str(car["number"]), expected_cars)
            expected = expected_cars[str(car["number"])]
            self.assertSequenceEqual(car["location"], expected.location)
            self.assertEquals(car["timeline_url"], reverse("car-timeline", args=(car["number"],)))
            self.assertEquals(car["stats_url"], reverse("user-car", args=(car["number"],)))
Example #15
0
    def test_get_list_data_correct(self):
        def fake_price(*args, **kwargs):
            return 0

        with temporary_settings({'RULE_GET_STREETCAR_PRICE': fake_price}):
            self.car1.sell_to(self.user)
            self.car3.sell_to(self.user)

        expected_cars = {car.number: car for car in (self.car1, self.car3)}
        response = self._make_get()
        self.assertEquals(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEquals(len(data), 2)
        for car in data:
            self.assertIn(str(car['number']), expected_cars)
            expected = expected_cars[str(car['number'])]
            self.assertSequenceEqual(car['location'], expected.location)
            self.assertEquals(car['timeline_url'],
                              reverse('car-timeline', args=(car['number'], )))
            self.assertEquals(car['stats_url'],
                              reverse('user-car', args=(car['number'], )))
Example #16
0
 def setUp(self):
     self.temporary_settings = temporary_settings(
                             {'NEXTBUS_API_URL': XML_FILE,
                              'NEXTBUS_ROUTE_LIST': ("501", "511")})
     with self.temporary_settings:
         management.call_command('updatecars', stdout=NullStream())
Example #17
0
 def test_sell_not_allowed_returns_403(self):
     def fake_rule(*args, **kwargs):
         return False
     with temporary_settings({'RULE_CAN_BUY_CAR': fake_rule}):
         self.assertStatusCode(self.sell_name, self.car.number, code=403)
Example #18
0
 def test_sell_to_creates_event(self):
     def fake_price(*args, **kwargs):
         return 0
     with temporary_settings({'RULE_GET_STREETCAR_PRICE': fake_price}):
         self.close.sell_to(self.user)
         self.assertEventCreated('car_bought', self.close)