<commit_before>"""
Tests for helper function for adding a target to a Vuforia database.
"""

import io
import random

import pytest
from mock_vws import MockVWS, States
from PIL import Image
from requests import codes

from vws import VWS
from vws.exceptions import (
    BadImage,
    Fail,
    ImageTooLarge,
    MetadataTooLarge,
    ProjectInactive,
    TargetNameExist,
)


def make_image_file(
    file_format: str,
    color_space: str,
    width: int,
    height: int,
) -> io.BytesIO:
    """
    Return an image file in the given format and color space.

    The image file is filled with randomly colored pixels.

    Args:
        file_format: See
            http://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html
        color_space: One of "L", "RGB", or "CMYK". "L" means greyscale.
        width: The width, in pixels of the image.
        height: The width, in pixels of the image.

    Returns:
        An image file in the given format and color space.
    """
    image_buffer = io.BytesIO()
    image = Image.new(color_space, (width, height))
    pixels = image.load()
    for i in range(height):
        for j in range(width):
            red = random.randint(0, 255)
            green = random.randint(0, 255)
            blue = random.randint(0, 255)
            if color_space != 'L':
                pixels[j, i] = (red, green, blue)
    image.save(image_buffer, file_format)
    image_buffer.seek(0)
    return image_buffer


class TestSuccess:
    """
    Tests for successfully adding a target.
    """

    def test_add_target(
        self,
        client: VWS,
        high_quality_image: io.BytesIO,
    ) -> None:
        """
        No exception is raised when adding one target.
        """
        name = 'x'
        width = 1
        target_id = client.add_target(
            name=name,
            width=width,
            image=high_quality_image,
        )
        target_record = client.get_target_record(target_id=target_id)
        assert target_record['name'] == name
        assert target_record['width'] == width
        assert target_record['active_flag'] is True

    def test_add_two_targets(
        self,
        client: VWS,
        high_quality_image: io.BytesIO,
    ) -> None:
        """
        No exception is raised when adding two targets with different names.

        This demonstrates that the image seek position is not changed.
        """
        client.add_target(name='x', width=1, image=high_quality_image)
        client.add_target(name='a', width=1, image=high_quality_image)


class TestName:
    """
    Tests for the ``name`` parameter to ``add_target``.
    """

    def test_add_two_targets_same_name(
        self,
        client: VWS,
        high_quality_image: io.BytesIO,
    ) -> None:
        """
        A ``TargetNameExist`` exception is raised after adding two targets with
        the same name.
        """
        client.add_target(name='x', width=1, image=high_quality_image)
        with pytest.raises(TargetNameExist) as exc:
            client.add_target(name='x', width=1, image=high_quality_image)

        assert exc.value.response.status_code == codes.FORBIDDEN



class TestAuthentication:
    """
    Tests for authentication issues.
    """

    def test_authentication_error(
        self,
        high_quality_image: io.BytesIO,
    ) -> None:
        """
        A ``Fail`` exception is raised when there are authentication issues.
        """
        with MockVWS() as mock:
            client = VWS(
                server_access_key='a',
                server_secret_key=mock.server_secret_key,
            )

            with pytest.raises(Fail) as exc:
                client.add_target(
                    name='x',
                    width=1,
                    image=high_quality_image,
                )

        exception = exc.value
        assert exception.response.status_code == codes.BAD_REQUEST


class TestImage:
    """
    Tests for the ``image`` parameter to ``add_target``.
    """

    def test_not_an_image(self, client: VWS) -> None:
        """
        A ``BadImage`` exception is raised when a non-image is given.
        """
        not_an_image = io.BytesIO(b'Not an image')
        with pytest.raises(BadImage):
            client.add_target(name='x', width=1, image=not_an_image)

    def test_image_too_large(self, client: VWS) -> None:
        """
        An ``ImageTooLarge`` exception is raised when the given image is too
        large.
        """
        width = height = 890

        png_too_large = make_image_file(
            file_format='PNG',
            color_space='RGB',
            width=width,
            height=height,
        )

        with pytest.raises(ImageTooLarge):
            client.add_target(name='x', width=1, image=png_too_large)


class TestCustomBaseURL:
    """
    Tests for adding images to databases under custom VWS URLs.
    """

    def test_custom_base_url(self, high_quality_image: io.BytesIO) -> None:
        """
        It is possible to use add a target to a database under a custom VWS
        URL.
        """
        base_vws_url = 'http://example.com'
        with MockVWS(base_vws_url=base_vws_url) as mock:
            client = VWS(
                server_access_key=mock.server_access_key,
                server_secret_key=mock.server_secret_key,
                base_vws_url=base_vws_url,
            )

            client.add_target(
                name='x',<commit_msg>Remove duplicate tests<commit_after>"""
Tests for helper function for adding a target to a Vuforia database.
"""

import io

import pytest
from mock_vws import MockVWS, States
from requests import codes

from vws import VWS
from vws.exceptions import (
    BadImage,
    Fail,
    MetadataTooLarge,
    ProjectInactive,
    TargetNameExist,
)


class TestSuccess:
    """
    Tests for successfully adding a target.
    """

    def test_add_target(
        self,
        client: VWS,
        high_quality_image: io.BytesIO,
    ) -> None:
        """
        No exception is raised when adding one target.
        """
        name = 'x'
        width = 1
        target_id = client.add_target(
            name=name,
            width=width,
            image=high_quality_image,
        )
        target_record = client.get_target_record(target_id=target_id)
        assert target_record['name'] == name
        assert target_record['width'] == width
        assert target_record['active_flag'] is True

    def test_add_two_targets(
        self,
        client: VWS,
        high_quality_image: io.BytesIO,
    ) -> None:
        """
        No exception is raised when adding two targets with different names.

        This demonstrates that the image seek position is not changed.
        """
        client.add_target(name='x', width=1, image=high_quality_image)
        client.add_target(name='a', width=1, image=high_quality_image)


class TestName:
    """
    Tests for the ``name`` parameter to ``add_target``.
    """

    def test_add_two_targets_same_name(
        self,
        client: VWS,
        high_quality_image: io.BytesIO,
    ) -> None:
        """
        A ``TargetNameExist`` exception is raised after adding two targets with
        the same name.
        """
        client.add_target(name='x', width=1, image=high_quality_image)
        with pytest.raises(TargetNameExist) as exc:
            client.add_target(name='x', width=1, image=high_quality_image)

        assert exc.value.response.status_code == codes.FORBIDDEN


class TestAuthentication:
    """
    Tests for authentication issues.
    """

    def test_authentication_error(
        self,
        high_quality_image: io.BytesIO,
    ) -> None:
        """
        A ``Fail`` exception is raised when there are authentication issues.
        """
        with MockVWS() as mock:
            client = VWS(
                server_access_key='a',
                server_secret_key=mock.server_secret_key,
            )

            with pytest.raises(Fail) as exc:
                client.add_target(
                    name='x',
                    width=1,
                    image=high_quality_image,
                )

        exception = exc.value
        assert exception.response.status_code == codes.BAD_REQUEST


class TestImage:
    """
    Tests for the ``image`` parameter to ``add_target``.
    """

    def test_not_an_image(self, client: VWS) -> None:
        """
        A ``BadImage`` exception is raised when a non-image is given.
        """
        not_an_image = io.BytesIO(b'Not an image')
        with pytest.raises(BadImage):
            client.add_target(name='x', width=1, image=not_an_image)


class TestCustomBaseURL:
    """
    Tests for adding images to databases under custom VWS URLs.
    """

    def test_custom_base_url(self, high_quality_image: io.BytesIO) -> None:
        """
        It is possible to use add a target to a database under a custom VWS
        URL.
        """
        base_vws_url = 'http://example.com'
        with MockVWS(base_vws_url=base_vws_url) as mock:
            client = VWS(
                server_access_key=mock.server_access_key,
                server_secret_key=mock.server_secret_key,
                base_vws_url=base_vws_url,
            )

            client.add_target(
                name='x',
{'fraction_non_alphanumeric': 0.09391378757086319, 'fraction_numerical': 0.003529789282276179, 'mean_word_length': 2.044610875936177, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '780568', 'n_tokens_mistral': 2796, 'n_tokens_neox': 2715, 'n_words': 715}