Example #1
0
    def test_sequence_choices_num_enum(self):
        # Only the first state is valid when field is null because it is the initial state
        assert_is_none(self.inst.state_graph)
        assert_raises(PersistenceException,
                      change_and_save,
                      self.inst,
                      state_graph=TestFieldsModel.GRAPH.SECOND)
        assert_raises(PersistenceException,
                      change_and_save,
                      self.inst,
                      state_graph=TestFieldsModel.GRAPH.THIRD)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.FIRST)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.FIRST)

        # Than one can switch only to second state
        assert_raises(PersistenceException,
                      change_and_save,
                      self.inst,
                      state_graph=TestFieldsModel.GRAPH.THIRD)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.SECOND)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.SECOND)

        # We cannot go back to first but we can go to the third state
        assert_raises(PersistenceException,
                      change_and_save,
                      self.inst,
                      state_graph=TestFieldsModel.GRAPH.FIRST)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.THIRD)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.THIRD)
Example #2
0
 def test_subchoices_field_value_should_be_empty(self):
     self.inst.state = 4  # setting an invalid value
     try:
         TestFieldsModel._meta.get_field('state_reason').validate(  # pylint: disable=W0212
             TestFieldsModel.STATE_REASON.SUB_NOT_OK_2, self.inst)  # pylint: disable=W0212
         assert_true(False, 'Field validation should raise an error')
     except ValidationError as ex:
         assert_equal(['Value must be empty'], ex.messages)
     assert_is_none(TestFieldsModel._meta.get_field('state_reason').clean(  # pylint: disable=W0212
         TestFieldsModel.STATE_REASON.SUB_NOT_OK_2, self.inst))  # pylint: disable=W0212
Example #3
0
 def test_subchoices_field_value_should_be_empty(self):
     self.inst.state = 4  # setting an invalid value
     try:
         TestFieldsModel._meta.get_field('state_reason').validate(  # pylint: disable=W0212
             TestFieldsModel.STATE_REASON.SUB_NOT_OK_2, self.inst)  # pylint: disable=W0212
         assert_true(False, 'Field validation should raise an error')
     except ValidationError as ex:
         assert_equal(['Value must be empty'], ex.messages)
     assert_is_none(
         TestFieldsModel._meta.get_field('state_reason').clean(  # pylint: disable=W0212
             TestFieldsModel.STATE_REASON.SUB_NOT_OK_2, self.inst))  # pylint: disable=W0212
Example #4
0
 def test_enum_should_contain_only_defined_values(self):
     enum = Enum('A', 'B')
     assert_equal(enum.A, 'A')
     assert_equal(enum.B, 'B')
     assert_equal(list(enum), ['A', 'B'])
     assert_equal(enum.all, ('A', 'B'))
     assert_equal(enum.get_name('A'), 'A')
     with assert_raises(AttributeError):
         enum.C  # pylint: disable=W0104
     assert_is_none(enum.get_name('C'))
     assert_in('A', enum)
     assert_in(enum.A, enum)
     assert_not_in('C', enum)
Example #5
0
 def test_auto_gemerated_num_enum_should_contain_only_defined_values(self):
     enum = NumEnum('A', 'B')
     assert_equal(enum.A, 1)
     assert_equal(enum.B, 2)
     assert_equal(list(enum), [1, 2])
     assert_equal(enum.all, (1, 2))
     assert_equal(enum.get_name(1), 'A')
     with assert_raises(AttributeError):
         enum.C  # pylint: disable=W0104
     assert_is_none(enum.get_name(3))
     assert_in(1, enum)
     assert_in(enum.A, enum)
     assert_not_in('A', enum)
Example #6
0
 def test_enum_with_distinct_key_and_value_should_contain_only_defined_values(
         self):
     enum = Enum(('A', 'c'), ('B', 'd'))
     assert_equal(enum.A, 'c')
     assert_equal(enum.B, 'd')
     assert_equal(list(enum), ['c', 'd'])
     assert_equal(enum.all, ('c', 'd'))
     assert_equal(enum.get_name('c'), 'A')
     with assert_raises(AttributeError):
         enum.C  # pylint: disable=W0104
     assert_is_none(enum.get_name('f'))
     assert_in('c', enum)
     assert_in(enum.A, enum)
     assert_not_in('A', enum)
Example #7
0
 def test_enum_should_contain_only_defined_values(self):
     enum = Enum(
         'A', 'B'
     )
     assert_equal(enum.A, 'A')
     assert_equal(enum.B, 'B')
     assert_equal(list(enum), ['A', 'B'])
     assert_equal(enum.all, ('A', 'B'))
     assert_equal(enum.get_name('A'), 'A')
     with assert_raises(AttributeError):
         enum.C  # pylint: disable=W0104
     assert_is_none(enum.get_name('C'))
     assert_in('A', enum)
     assert_in(enum.A, enum)
     assert_not_in('C', enum)
Example #8
0
 def test_auto_gemerated_num_enum_should_contain_only_defined_values(self):
     enum = NumEnum(
         'A', 'B'
     )
     assert_equal(enum.A, 1)
     assert_equal(enum.B, 2)
     assert_equal(list(enum), [1, 2])
     assert_equal(enum.all, (1, 2))
     assert_equal(enum.get_name(1), 'A')
     with assert_raises(AttributeError):
         enum.C  # pylint: disable=W0104
     assert_is_none(enum.get_name(3))
     assert_in(1, enum)
     assert_in(enum.A, enum)
     assert_not_in('A', enum)
Example #9
0
 def test_enum_with_distinct_key_and_value_should_contain_only_defined_values(self):
     enum = Enum(
         ('A', 'c'), ('B', 'd')
     )
     assert_equal(enum.A, 'c')
     assert_equal(enum.B, 'd')
     assert_equal(list(enum), ['c', 'd'])
     assert_equal(enum.all, ('c', 'd'))
     assert_equal(enum.get_name('c'), 'A')
     with assert_raises(AttributeError):
         enum.C  # pylint: disable=W0104
     assert_is_none(enum.get_name('f'))
     assert_in('c', enum)
     assert_in(enum.A, enum)
     assert_not_in('A', enum)
Example #10
0
    def test_verification_token_should_store_extra_data(self, user):
        EXTRA_DATA_1 = {'a': 123}
        EXTRA_DATA_2 = {'b': 456}

        token = VerificationToken.objects.deactivate_and_create(user, extra_data=EXTRA_DATA_1)
        token.refresh_from_db()
        assert_equal(token.get_extra_data(), EXTRA_DATA_1)

        token.set_extra_data(EXTRA_DATA_2)
        token.save()
        token.refresh_from_db()
        assert_equal(token.get_extra_data(), EXTRA_DATA_2)

        token.extra_data = None
        token.save()
        assert_is_none(token.get_extra_data())
Example #11
0
    def test_sequence_choices_num_enum(self):
        # Only the first state is valid when field is null because it is the initial state
        assert_is_none(self.inst.state_graph)
        assert_raises(PersistenceException, change_and_save, self.inst, state_graph=TestFieldsModel.GRAPH.SECOND)
        assert_raises(PersistenceException, change_and_save, self.inst, state_graph=TestFieldsModel.GRAPH.THIRD)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.FIRST)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.FIRST)

        # Than one can switch only to second state
        assert_raises(PersistenceException, change_and_save, self.inst, state_graph=TestFieldsModel.GRAPH.THIRD)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.SECOND)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.SECOND)

        # We cannot go back to first but we can go to the third state
        assert_raises(PersistenceException, change_and_save, self.inst, state_graph=TestFieldsModel.GRAPH.FIRST)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.THIRD)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.THIRD)
Example #12
0
    def test_output_logged_request_should_be_related_with_object_selected_in_decorator(
            self):
        user1 = self.create_user('test1', '*****@*****.**')
        user2 = self.create_user('test2', '*****@*****.**')

        responses.add(responses.GET, 'http://test.cz', body='test')
        assert_equal(log_context_manager.get_output_request_related_objects(),
                     [])
        with atomic_log(output_requests_slug='test1',
                        output_requests_related_objects=[user1]):
            assert_equal(
                set(log_context_manager.get_output_request_related_objects()),
                {user1})

            requests.get('http://test.cz')
            with atomic_log(output_requests_slug='test2',
                            output_requests_related_objects=[user2]):
                assert_equal(
                    set(log_context_manager.get_output_request_related_objects(
                    )), {user1, user2})
                requests.get('http://test.cz')
            assert_equal(
                set(log_context_manager.get_output_request_related_objects()),
                {user1})
            requests.get('http://test.cz')

        assert_equal(OutputLoggedRequest.objects.count(), 3)
        logged_request3, logged_request2, logged_request1 = OutputLoggedRequest.objects.all(
        )

        assert_equal(logged_request1.related_objects.get().object, user1)
        assert_equal(logged_request1.slug, 'test1')

        assert_equal(
            {
                related_object.object
                for related_object in logged_request2.related_objects.all()
            }, {user1, user2})
        assert_equal(logged_request2.slug, 'test2')

        assert_equal(logged_request3.related_objects.get().object, user1)
        assert_equal(logged_request3.slug, 'test1')
        assert_equal(log_context_manager.get_output_request_related_objects(),
                     [])
        assert_is_none(log_context_manager.get_output_request_slug())
Example #13
0
    def test_get_object_or_none(self):
        obj = ShortcutsModel.objects.create(name='test1', datetime=timezone.now(), number=1)
        ShortcutsModel.objects.create(name='test2', datetime=timezone.now(), number=2)
        ShortcutsModel.objects.create(name='test2', datetime=timezone.now(), number=3)

        assert_equal(get_object_or_none(ShortcutsModel, name='test1'), obj)
        assert_is_none(get_object_or_none(ShortcutsModel, name='test3'))
        assert_is_none(get_object_or_none(ShortcutsModel, number='test3'))
        assert_is_none(get_object_or_none(ShortcutsModel, datetime='test3'))

        assert_raises(FieldError, get_object_or_none, ShortcutsModel, non_field='test2')
        assert_raises(MultipleObjectsReturned, get_object_or_none, ShortcutsModel, name='test2')
Example #14
0
    def test_get_model_from_relation_or_none(self):
        assert_equal(get_model_from_relation_or_none(Issue, 'watched_by'),
                     User)
        assert_equal(get_model_from_relation_or_none(Issue, 'created_by'),
                     User)
        assert_equal(get_model_from_relation_or_none(Issue, 'solver'), User)
        assert_equal(get_model_from_relation_or_none(Issue, 'leader'), User)
        assert_is_none(get_model_from_relation_or_none(Issue, 'name'))
        assert_is_none(get_model_from_relation_or_none(Issue, 'created_at'))
        assert_is_none(get_model_from_relation_or_none(Issue, 'invalid'))

        assert_equal(get_model_from_relation_or_none(User, 'watched_issues'),
                     Issue)
        assert_equal(get_model_from_relation_or_none(User, 'created_issues'),
                     Issue)
        assert_equal(get_model_from_relation_or_none(User, 'solving_issue'),
                     Issue)
        assert_equal(get_model_from_relation_or_none(User, 'leading_issue'),
                     Issue)
 def test_apply_async_on_commit_should_run_task_and_return_none(self):
     assert_is_none(sum_task.apply_async_on_commit(args=(8, 19)))
Example #16
0
 def test_verification_token_expiration_should_be_nullable(self, user):
     token = VerificationToken.objects.deactivate_and_create(user, expiration_in_minutes=None)
     assert_is_none(token.expires_at)
Example #17
0
 def test_rfs_get(self):
     fieldset = rfs(('a', 'b', 'b__c', 'b__g', ('d', ('e__f',))))
     assert_equal(str(fieldset.get('b')), 'b(c,g)')
     assert_is_none(fieldset.get('k'))
     assert_equal(str(fieldset.get('a')), 'a')
Example #18
0
 def test_rfs_get(self):
     fieldset = rfs(('a', 'b', 'b__c', 'b__g', ('d', ('e__f', ))))
     assert_equal(str(fieldset.get('b')), 'b(c,g)')
     assert_is_none(fieldset.get('k'))
     assert_equal(str(fieldset.get('a')), 'a')
Example #19
0
 def test_not_allowed_content_type_body_should_not_be_logged(self):
     with capture_security_logs() as logged_data:
         assert_http_ok(self.get('/home/'))
         assert_is_none(logged_data.input_request[0].response_body)
 def test_delay_on_commit_should_run_task(self):
     assert_is_none(sum_task.delay_on_commit(8, 21))
Example #21
0
 def test_celery_task_should_have_rightly_set_stale_time(self):
     sum_task.apply_async(args=(5, 8))
     celery_task_log = CeleryTaskLog.objects.get()
     assert_equal(celery_task_log.estimated_time_of_first_arrival, now())
     assert_is_none(celery_task_log.expires)
     assert_equal(celery_task_log.stale, now() + timedelta(seconds=30))