Example #1
0
    def test_delete_image_compile_error_cache_deleted(self):
        instance = factories.LatexImageErrorFactory()

        instance.delete()
        self.assertIsNone(
            self.test_cache.get(
                get_field_cache_key(instance.tex_key, "compile_error")))
Example #2
0
    def test_get_cached_result_with_compile_error_not_in_cache(self):
        tex_key = "what_ever_error_key"
        instance = factories.LatexImageErrorFactory(tex_key=tex_key,
                                                    creator=self.test_user)

        self.test_cache.clear()

        filter_fields_str = "image"

        with mock.patch(
                "rest_framework.generics.RetrieveUpdateDestroyAPIView.get"
        ) as mock_api_get:
            resp = self.client.get(self.get_detail_url(
                tex_key=tex_key, fields=filter_fields_str),
                                   format='json')
            mock_api_get.assert_not_called()

        self.assertEqual(resp.status_code, 400)
        self.assertEqual(LatexImage.objects.all().count(), 2)
        response_dict = json.loads(resp.content.decode())
        self.assertNotIn(filter_fields_str, response_dict)
        self.assertIn("compile_error", response_dict)
        self.assertEqual(response_dict["compile_error"],
                         instance.compile_error)

        cache_key = self.get_field_cache_key("compile_error", tex_key=tex_key)

        # Compile error get cached.
        self.assertEqual(self.test_cache.get(cache_key),
                         instance.compile_error)
Example #3
0
 def get_data_url_side_effect(file_path):
     result = get_data_url(file_path)
     factories.LatexImageErrorFactory(tex_key=tex_key,
                                      creator=creator,
                                      creation_time=creation_time,
                                      compile_error=compile_error)
     return result
Example #4
0
    def test_create_already_exist_just_before_saving_success(self):
        instance = factories.LatexImageErrorFactory(
            tex_key="key_already_exists")
        tex_key = instance.tex_key
        creator = instance.creator
        creation_time = instance.creation_time
        compile_error = instance.compile_error
        instance.delete()

        def get_data_url_side_effect(file_path):
            result = get_data_url(file_path)
            factories.LatexImageErrorFactory(tex_key=tex_key,
                                             creator=creator,
                                             creation_time=creation_time,
                                             compile_error=compile_error)
            return result

        with mock.patch("latex.converter.get_data_url") as mock_get_data_url:
            mock_get_data_url.side_effect = (get_data_url_side_effect)

            resp = self.client.post(self.get_list_url(),
                                    data=self.get_post_data(
                                        tex_key=tex_key,
                                        creator=creator.pk,
                                        compile_error=compile_error,
                                    ),
                                    format='json')
            self.assertEqual(resp.status_code, 500)
            resp_dict = json.loads(resp.content.decode())
            self.assertEqual(resp_dict.get("tex_key", None),
                             ['LaTeXImage with this Tex Key already exists.'])
            self.assertEqual(LatexImage.objects.all().count(), 1)
Example #5
0
    def test_cache_improperly_configured_works(self):
        with improperly_configured_cache_patch():
            instance1 = factories.LatexImageFactory()
            instance2 = factories.LatexImageErrorFactory()
            self.assertEqual(LatexImage.objects.all().count(), 2)

            instance1.delete()
            instance2.delete()
            self.assertEqual(LatexImage.objects.all().count(), 0)
Example #6
0
    def test_image_create_compile_error_cached(self):
        instance = factories.LatexImageErrorFactory()

        self.assertIsNone(
            self.test_cache.get(
                get_field_cache_key(instance.tex_key, "image")))

        self.assertIsNotNone(
            self.test_cache.get(
                get_field_cache_key(instance.tex_key, "compile_error"),
                instance.compile_error))
Example #7
0
    def test_post_errored_no_convert_if_exist_with_tex_key(self):
        tex_key = "what_ever_key_error"
        factories.LatexImageErrorFactory(tex_key=tex_key)
        self.assertEqual(LatexImage.objects.all().count(), 1)

        with mock.patch("latex.converter.Tex2ImgBase.get_converted_data_url"
                        ) as mock_convert:
            mock_convert.return_value = get_fake_data_url("foob=")
            resp = self.post_latex_form_view(data=self.get_post_data(
                tex_key=tex_key))

            self.assertEqual(resp.status_code, 400)
            self.assertEqual(LatexImage.objects.all().count(), 1)
            self.assertEqual(mock_convert.call_count, 0)
Example #8
0
    def test_get_result_with_obj_exist_compile_error_no_cache(self):
        tex_key = "what_ever_error_key"
        filter_fields_str = "image"

        with improperly_configured_cache_patch():
            instance = factories.LatexImageErrorFactory(tex_key=tex_key,
                                                        creator=self.test_user)

            resp = self.client.get(self.get_detail_url(
                tex_key=instance.tex_key, fields=filter_fields_str),
                                   format='json')

        with improperly_configured_cache_patch():
            self.assertEqual(resp.status_code, 400)
            self.assertEqual(LatexImage.objects.all().count(), 2)
Example #9
0
    def test_no_create_duplicate_errored(self):
        tex_key = "what_ever_key"
        factories.LatexImageErrorFactory(tex_key=tex_key,
                                         creator=self.test_user)

        with mock.patch("latex.converter.Tex2ImgBase.get_converted_data_url"
                        ) as mock_convert:
            mock_convert.return_value = get_fake_data_url("foob=")
            resp = self.client.post(self.get_list_url(),
                                    data=self.get_post_data(tex_key=tex_key),
                                    format='json')
            mock_convert.assert_not_called()

        self.assertEqual(resp.status_code, 400)
        self.assertEqual(LatexImage.objects.all().count(), 1)