Exemplo n.º 1
0
    def test_project_cannot_be_modified_when_inactive(self):
        project = Project(name='Test', active=False).validate_and_save()

        # Should raise a generic validation error if attempting to
        # save on a inactive project
        with self.assertRaises(ValidationError) as context_manager:
            project.validate_and_save()

        self.assertValidationMessagePresent(
            context_manager.exception.error_dict,
            field='__all__',
            error_code='cannot_modify_when_inactive')

        # But should be able to save the change if it is
        # re-opening the project
        project.active = True
        project.validate_and_save()
        self.assertEqual(project.active, True)
Exemplo n.º 2
0
class ChargeModelAdminTestCase(TestCase):
    def setUp(self):
        self.project = Project(name='Test').validate_and_save()
        self.model_admin = ChargeAdmin(model=Charge, admin_site=admin_site)

    def test_charge_admin_date_hierarchy_is_set_to_start_time(self):
        # This property configures the admin to support browsing
        # through Charge records via a more granular date picker.
        self.assertEqual(self.model_admin.date_hierarchy, 'start_time')

    def test_charge_admin_queryset_is_annotated_with_time_charged(self):
        Charge(project=self.project,
               start_time=timezone.now()).validate_and_save()
        Charge(project=self.project,
               start_time=timezone.now()).validate_and_save()

        queryset = self.model_admin.get_queryset(HttpRequest())

        for charge in queryset:
            self.assertTrue(hasattr(charge, 'db_time_charged'))

    def test_charge_admin_list_display_contains_time_charged(self):
        self.assertTrue('time_spent' in self.model_admin.list_display)
        obj = types.SimpleNamespace()
        obj.db_time_charged = 'Test'
        self.assertEqual(self.model_admin.time_spent(obj), 'Test')

    def test_charge_admin_time_charged_field_is_sortable_in_list(self):
        self.assertTrue(
            hasattr(self.model_admin.time_spent, 'admin_order_field'))
        self.assertEqual(self.model_admin.time_spent.admin_order_field,
                         'db_time_charged')

    def test_charge_admin_has_all_fields_editable_when_creating_new_charge(
            self):
        readonly_fields = self.model_admin.get_readonly_fields(
            HttpRequest, None)

        self.assertEqual(len(readonly_fields), 0)

    def test_charge_admin_has_all_fields_editable_when_project_is_active_and_charge_is_not_closed(
            self):
        self.project.active = True
        self.project.validate_and_save()

        charge = Charge(project=self.project,
                        start_time=timezone.now(),
                        closed=False).validate_and_save()

        readonly_fields = self.model_admin.get_readonly_fields(
            HttpRequest(), charge)

        self.assertEqual(len(readonly_fields), 0)

    def test_charge_admin_has_read_only_start_and_end_time_when_project_not_active_and_charge_closed(
            self):
        charge = Charge(project=self.project,
                        start_time=timezone.now(),
                        end_time=timezone.now() + timedelta(hours=1),
                        closed=True).validate_and_save()

        self.project.active = False
        self.project.validate_and_save()

        readonly_fields = self.model_admin.get_readonly_fields(
            HttpRequest(), charge)

        self.assertSequenceEqual(readonly_fields, (
            'start_time',
            'end_time',
        ))

    def test_charge_admin_has_read_only_start_time_end_time_and_project_when_charge_is_closed(
            self):
        self.project.active = True
        self.project.validate_and_save()

        charge = Charge(project=self.project,
                        start_time=timezone.now(),
                        end_time=timezone.now() + timedelta(hours=1),
                        closed=True).validate_and_save()

        readonly_fields = self.model_admin.get_readonly_fields(
            HttpRequest(), charge)

        self.assertSequenceEqual(readonly_fields, (
            'start_time',
            'end_time',
            'project',
        ))

    def test_charge_admin_has_read_only_start_time_end_time_and_closed_status_when_project_not_active(
            self):
        charge = Charge(project=self.project,
                        start_time=timezone.now(),
                        closed=False).validate_and_save()

        self.project.active = False
        self.project.validate_and_save()

        readonly_fields = self.model_admin.get_readonly_fields(
            HttpRequest(), charge)

        self.assertSequenceEqual(readonly_fields, (
            'start_time',
            'end_time',
            'closed',
        ))