示例#1
0
 def setUp(self):
     self.timetable = TimetableFactory()
     self.another_timetable = Timetable(
         name='timetable',
         code='FT7871',
         api_key='TF78993jTA',
         cycle_length=self.timetable.cycle_length,
         ref_cycle_day=self.timetable.ref_cycle_day,
         ref_cycle_date=self.timetable.ref_cycle_date,
         description=self.timetable.description)
示例#2
0
 def setUp(self):
     self.timetable = TimetableFactory()
     self.another_timetable = Timetable(
         name='timetable',
         code='FT7871',
         api_key='TF78993jTA',
         cycle_length=self.timetable.cycle_length,
         ref_cycle_day=self.timetable.ref_cycle_day,
         ref_cycle_date=self.timetable.ref_cycle_date,
         description=self.timetable.description
     )
示例#3
0
class TimetableTest(TestCase):
    """Tests the Timetable model."""
    def setUp(self):
        self.timetable = TimetableFactory()
        self.another_timetable = Timetable(
            name='timetable',
            code='FT7871',
            api_key='TF78993jTA',
            cycle_length=self.timetable.cycle_length,
            ref_cycle_day=self.timetable.ref_cycle_day,
            ref_cycle_date=self.timetable.ref_cycle_date,
            description=self.timetable.description)

    def test_duplicate_timetable_name_cannot_be_saved(self):
        self.another_timetable.name = 'fellows timetable'

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_duplicate_timetable_code_cannot_be_saved(self):
        self.another_timetable.code = self.timetable.code

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_duplicate_api_key_cannot_be_saved(self):
        self.another_timetable.api_key = self.timetable.api_key

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_ref_cycle_day_greater_than_cycle_length_cannot_be_saved(self):
        self.another_timetable.ref_cycle_day = self.timetable.cycle_length + 1

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_cycle_length_and_ref_cycle_day_of_zero_cant_be_saved(self):
        # test for cycle_length == 0
        self.another_timetable.cycle_length = 0
        self.assertRaises(ValidationError, self.another_timetable.save)

        # test for ref_cycle_day == 0
        self.another_timetable.ref_cycle_day = 0
        self.another_timetable.cycle_length = self.timetable.cycle_length

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_cycle_length_and_ref_cycle_day_of_negative_value_cant_be_saved(
            self):
        # test for cycle_length < 0
        self.another_timetable.cycle_length = -3
        self.assertRaises(ValidationError, self.another_timetable.save)

        # test for ref_cycle_day < 0
        self.another_timetable.ref_cycle_day = -3
        self.another_timetable.cycle_length = self.timetable.cycle_length

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_calculate_cycle_day(self):
        test_date = datetime.date(2016, 11, 23)
        self.assertEqual(13, self.timetable.calculate_cycle_day(test_date))

        test_date = datetime.date(2016, 11, 24)
        self.assertEqual(14, self.timetable.calculate_cycle_day(test_date))

        test_date = datetime.date(2016, 11, 25)
        self.assertEqual(1, self.timetable.calculate_cycle_day(test_date))

        # test_date equal to ref_cycle_date
        test_date = self.timetable.ref_cycle_date
        cycle_day = self.timetable.ref_cycle_day
        self.assertEqual(cycle_day,
                         self.timetable.calculate_cycle_day(test_date))

        # test_date earlier than ref_cycle_date cannot be resolved to a valid cycle_day
        test_date = datetime.date(2016, 9, 25)
        self.assertRaises(ValidationError, self.timetable.calculate_cycle_day,
                          test_date)

    def test_get_vendors(self):
        vendor_service = VendorServiceFactory(timetable=self.timetable)

        new_vendors = [
            VendorFactory(name=x) for x in ['Spicy Foods', 'Tantalizer']
        ]
        start_date = datetime.date(2016, 10, 1)
        end_date = datetime.date(2016, 11, 30)

        for new_vendor in new_vendors:
            VendorServiceFactory(timetable=self.timetable,
                                 vendor=new_vendor,
                                 start_date=start_date,
                                 end_date=end_date)

        test_date = datetime.date(2016, 11, 25)
        self.assertEqual(new_vendors, self.timetable.get_vendors(test_date))

        test_date = datetime.date(2008, 2, 23)
        self.assertEqual([vendor_service.vendor],
                         self.timetable.get_vendors(test_date))

        test_date = datetime.date(2000, 2, 23)
        self.assertEqual([], self.timetable.get_vendors(test_date))
示例#4
0
class TimetableTest(TestCase):
    """Tests the Timetable model."""

    def setUp(self):
        self.timetable = TimetableFactory()
        self.another_timetable = Timetable(
            name='timetable',
            code='FT7871',
            api_key='TF78993jTA',
            cycle_length=self.timetable.cycle_length,
            ref_cycle_day=self.timetable.ref_cycle_day,
            ref_cycle_date=self.timetable.ref_cycle_date,
            description=self.timetable.description
        )

    def test_duplicate_timetable_name_cannot_be_saved(self):
        self.another_timetable.name = 'fellows timetable'

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_duplicate_timetable_code_cannot_be_saved(self):
        self.another_timetable.code = self.timetable.code

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_duplicate_api_key_cannot_be_saved(self):
        self.another_timetable.api_key = self.timetable.api_key

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_ref_cycle_day_greater_than_cycle_length_cannot_be_saved(self):
        self.another_timetable.ref_cycle_day = self.timetable.cycle_length + 1

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_cycle_length_and_ref_cycle_day_of_zero_cant_be_saved(self):
        # test for cycle_length == 0
        self.another_timetable.cycle_length = 0
        self.assertRaises(ValidationError, self.another_timetable.save)

        # test for ref_cycle_day == 0
        self.another_timetable.ref_cycle_day = 0
        self.another_timetable.cycle_length = self.timetable.cycle_length

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_cycle_length_and_ref_cycle_day_of_negative_value_cant_be_saved(self):
        # test for cycle_length < 0
        self.another_timetable.cycle_length = -3
        self.assertRaises(ValidationError, self.another_timetable.save)

        # test for ref_cycle_day < 0
        self.another_timetable.ref_cycle_day = -3
        self.another_timetable.cycle_length = self.timetable.cycle_length

        self.assertRaises(ValidationError, self.another_timetable.save)

    def test_calculate_cycle_day(self):
        test_date = timezone.make_aware(timezone.datetime(2016, 11, 23, 12, 30, 0))
        self.assertEqual(13, self.timetable.calculate_cycle_day(test_date))

        test_date = timezone.make_aware(timezone.datetime(2016, 11, 24, 12, 30, 0))
        self.assertEqual(14, self.timetable.calculate_cycle_day(test_date))

        test_date = timezone.make_aware(timezone.datetime(2016, 11, 25, 12, 30, 0))
        self.assertEqual(1, self.timetable.calculate_cycle_day(test_date))

        # test_date earlier than ref_cycle_date cannot be resolved to a valid cycle_day
        test_date = timezone.make_aware(timezone.datetime(2016, 9, 25, 12, 30, 0))
        self.assertEqual(None, self.timetable.calculate_cycle_day(test_date))

    def test_get_vendors(self):
        vendor_service = VendorServiceFactory(timetable=self.timetable)

        new_vendors = [VendorFactory(name=x) for x in ['Spicy Foods', 'Tantalizer']]
        start_date = timezone.make_aware(timezone.datetime(2016, 10, 1, 0, 0, 0))
        end_date = timezone.make_aware(timezone.datetime(2016, 11, 30, 0, 0, 0))

        for new_vendor in new_vendors:
            VendorServiceFactory(
                timetable=self.timetable,
                vendor=new_vendor,
                start_date=start_date,
                end_date=end_date
            )

        test_date = timezone.make_aware(timezone.datetime(2016, 11, 25, 12, 30, 0))
        self.assertEqual(new_vendors, self.timetable.get_vendors(test_date))

        test_date = timezone.make_aware(timezone.datetime(2008, 2, 23, 0, 0, 0))
        self.assertEqual([vendor_service.vendor], self.timetable.get_vendors(test_date))

        test_date = timezone.make_aware(timezone.datetime(2000, 2, 23, 0, 0, 0))
        self.assertEqual([], self.timetable.get_vendors(test_date))