From 89bceb3555c70578b2725376978d9d7aba815380 Mon Sep 17 00:00:00 2001 From: Nico Alt <nicoalt@posteo.org> Date: Thu, 6 Jun 2019 22:58:40 +0200 Subject: [PATCH] Add unit tests with pytest --- .gitignore | 1 + .gitlab-ci.yml | 15 ++++++++++++- requirements-dev.txt | 2 ++ tests/briar/api/models/test_contacts.py | 30 +++++++++++++++++++++++++ tools/run-tests.sh | 1 + tools/tests/test-pytest.sh | 3 +++ 6 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/briar/api/models/test_contacts.py create mode 100755 tools/tests/test-pytest.sh diff --git a/.gitignore b/.gitignore index 49269ac..412a383 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ builddir .flatpak-builder +__pycache__ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 41c9760..a69bd5f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,20 @@ -test:lints: +test:pycodestyle: image: debian:stretch script: - apt update && apt install --no-install-recommends -y gir1.2-gtk-3.0 python3-gi python3-pip python3-setuptools - pip3 install -r requirements-dev.txt - tools/tests/test-pycodestyle.sh + +test:pylint: + image: debian:stretch + script: + - apt update && apt install --no-install-recommends -y gir1.2-gtk-3.0 python3-gi python3-pip python3-setuptools + - pip3 install -r requirements-dev.txt - tools/tests/test-pylint.sh + +test:pytest: + image: debian:stretch + script: + - apt update && apt install --no-install-recommends -y gir1.2-gtk-3.0 python3-gi python3-pip python3-setuptools + - pip3 install -r requirements-dev.txt + - tools/tests/test-pytest.sh diff --git a/requirements-dev.txt b/requirements-dev.txt index 1180199..6d98279 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,5 @@ -r requirements.txt pycodestyle>=2.5.0 pylint>=2.3.1 +pytest>=4.6.2 +requests_mock>=1.6.0 diff --git a/tests/briar/api/models/test_contacts.py b/tests/briar/api/models/test_contacts.py new file mode 100644 index 0000000..92e50f6 --- /dev/null +++ b/tests/briar/api/models/test_contacts.py @@ -0,0 +1,30 @@ +# Copyright (c) 2019 Nico Alt +# SPDX-License-Identifier: AGPL-3.0-only +# License-Filename: LICENSE.md + +import json +import requests_mock +from unittest import mock, TestCase + +from briar.api.models.contacts import Contacts + + +class TestContacts(TestCase): + + @requests_mock.Mocker() + def test_get(self, requests_mock): + api = mock.Mock() + contacts = Contacts(api) + response_mock = {} + response_mock["author"] = {} + response_mock["author"]["formatVersion"] = 1 + response_mock["author"]["id"] = "y1wkIzAimAbYoCGgWxkWlr6vnq1F8t1QRA/UMPgI0E0=" + response_mock["author"]["name"] = "Test" + response_mock["author"]["publicKey"] = "BDu6h1S02bF4W6rgoZfZ6BMjTj/9S9hNN7EQoV05qUo=" + response_mock["contactId"] = 1 + response_mock["alias"] = "A local nickname" + response_mock["handshakePublicKey"] = "XnYRd7a7E4CTqgAvh4hCxh/YZ0EPscxknB9ZcEOpSzY=" + response_mock["verified"] = True + + requests_mock.get("http://localhost:7000/v1/contacts", text=json.dumps(response_mock)) + self.assertEqual(contacts.get(), response_mock) diff --git a/tools/run-tests.sh b/tools/run-tests.sh index 38df236..1d09c55 100755 --- a/tools/run-tests.sh +++ b/tools/run-tests.sh @@ -2,3 +2,4 @@ tools/tests/test-pycodestyle.sh tools/tests/test-pylint.sh +tools/tests/test-pytest.sh diff --git a/tools/tests/test-pytest.sh b/tools/tests/test-pytest.sh new file mode 100755 index 0000000..274c7b8 --- /dev/null +++ b/tools/tests/test-pytest.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +PYTHONPATH=src pytest tests/ -- GitLab