#!/usr/bin/env python3
# thoth-adviser
# Copyright(C) 2019, 2020 Fridolin Pokorny
#
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Tests for version clash."""

import flexmock
import pytest

from thoth.adviser.exceptions import CannotProduceStack
from thoth.adviser.boots import VersionClashBoot
from thoth.python import Project

from ..base import AdviserTestCase


class TestVersionClashBoot(AdviserTestCase):
    """Test version clash boot."""

    _CASE_PIPFILE_CLASHED = """
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
click = "==7.0"

[dev-packages]
pytest = "==5.3.1"
click = "==7.1"
"""

    _CASE_PIPFILE_NOT_CLASHED = """
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
click = "<=7.0"
tensorflow = "*"

[dev-packages]
click = "==6.9"
"""

    def test_clash(self) -> None:
        """Test raising an exception if a version clash occurs."""
        context = flexmock(project=Project.from_strings(self._CASE_PIPFILE_CLASHED))
        with pytest.raises(CannotProduceStack):
            boot = VersionClashBoot()

            with VersionClashBoot.assigned_context(context):
                boot.run()

    def test_no_clash(self) -> None:
        """Test raising no exception raised if clash occurs."""
        context = flexmock(project=Project.from_strings(self._CASE_PIPFILE_NOT_CLASHED))
        boot = VersionClashBoot()

        with VersionClashBoot.assigned_context(context):
            boot.run()
