From aa218586f4cbc631593172ade4cb685b05188759 Mon Sep 17 00:00:00 2001 From: Jakub Kadlcik Date: Nov 30 2020 11:43:23 +0000 Subject: common: add first tests for copr-common package --- diff --git a/common/python-copr-common.spec b/common/python-copr-common.spec index 808f1d5..5f9c94a 100644 --- a/common/python-copr-common.spec +++ b/common/python-copr-common.spec @@ -7,10 +7,12 @@ %if 0%{?fedora} || 0%{?rhel} > 7 %global with_python3 1 +%global __python %_bindir/python3 %endif %if 0%{?fedora} < 28 || 0%{?rhel} && 0%{?rhel} <= 7 %global with_python2 1 +%global __python %_bindir/python2 %endif Name: python-copr-common @@ -31,11 +33,16 @@ BuildArch: noarch %if %{with python2} BuildRequires: python2-devel BuildRequires: python-setuptools +BuildRequires: python-pytest +BuildRequires: python-mock +BuildRequires: python-requests %endif %if %{with python3} BuildRequires: python3-devel BuildRequires: python3-setuptools +BuildRequires: python3-pytest +BuildRequires: python3-requests %endif %global _description\ @@ -89,6 +96,10 @@ version=%version %py2_install %endif +%check +%{__python} -m pytest -vv tests + + %if %{with python3} %files -n python3-%{srcname} %license LICENSE diff --git a/common/run_tests.sh b/common/run_tests.sh index 6d8b13b..e1ba2fb 100755 --- a/common/run_tests.sh +++ b/common/run_tests.sh @@ -1,3 +1,3 @@ #! /bin/bash -echo "TODO: no tests so far" +python3 -B -m pytest --cov-report term-missing --cov ./copr_common/ $@ diff --git a/common/tests/__init__.py b/common/tests/__init__.py new file mode 100644 index 0000000..921b9b9 --- /dev/null +++ b/common/tests/__init__.py @@ -0,0 +1,8 @@ +import six + +if six.PY3: + from unittest import mock + from unittest.mock import MagicMock +else: + import mock + from mock import MagicMock diff --git a/common/tests/test_request.py b/common/tests/test_request.py new file mode 100644 index 0000000..0192001 --- /dev/null +++ b/common/tests/test_request.py @@ -0,0 +1,32 @@ +import logging +from unittest import TestCase +from requests import RequestException +from copr_common.request import SafeRequest, RequestRetryError +from . import mock + + +class TestStringMethods(TestCase): + + def setUp(self): + self.url = "http://example.com/" + self.data = { + "foo": "bar", + "bar": [1, 3, 5], + } + self.log = logging.getLogger("testlog") + + @mock.patch("copr_common.request.post") + def test_send_request_not_200(self, post_req): + post_req.return_value.status_code = 501 + with self.assertRaises(RequestRetryError): + request = SafeRequest(log=self.log) + request._send_request(self.url, "post", self.data) + self.assertTrue(post_req.called) + + @mock.patch("copr_common.request.post") + def test_send_request_post_error(self, post_req): + post_req.side_effect = RequestException() + with self.assertRaises(RequestRetryError): + request = SafeRequest(log=self.log) + request._send_request(self.url, "post", self.data) + self.assertTrue(post_req.called)