diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..8e4ef5f --- /dev/null +++ b/.flake8 @@ -0,0 +1,11 @@ +# Run flake8 (pycodestyle + pyflakes) check. +# https://pycodestyle.readthedocs.io/en/latest/intro.html#error-codes +# Ignored errors: +# - E501: line too long +# - E265: block comment should start with '# ' (makes it easier to enable/disable code) +# - W503: line break before binary operator (deprecated rule) +# - W505: doc line too long + +[flake8] +ignore = E501,E265,W503,W505 +exclude = .git/,.virtualenv/,__pycache__/,build/,dist/ diff --git a/.github/workflows/python-ci.yml b/.github/workflows/python-ci.yml new file mode 100644 index 0000000..1f7dec7 --- /dev/null +++ b/.github/workflows/python-ci.yml @@ -0,0 +1,38 @@ +# This workflow will install Python dependencies, run tests and lint with a variety of Python versions +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Python CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: [3.7, 3.9] + + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python3 -m pip install --upgrade pip + python3 -m pip install flake8 + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + python3 setup.py install + - name: Lint code with Python ${{ matrix.python-version }} + run: | + make lint + - name: Test code with Python ${{ matrix.python-version }} + run: | + make test diff --git a/.gitignore b/.gitignore index bdaa830..a17e5c0 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,7 @@ var/ # Installer logs pip-log.txt pip-delete-this-directory.txt +*.log # Unit test / coverage reports htmlcov/ @@ -47,8 +48,8 @@ coverage.xml *.mo *.pot -# Django stuff: -*.log +# Confs +conf*.json # Sphinx documentation docs/_build/ @@ -56,8 +57,5 @@ docs/_build/ # PyBuilder target/ -#Ipython Notebook +# Ipython Notebook .ipynb_checkpoints - -# MediaServer client -config.json diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 55f3ca7..0000000 --- a/.travis.yml +++ /dev/null @@ -1,12 +0,0 @@ -language: python -services: - - docker -matrix: - include: - - env: - - DOCKER_IMAGE=python:3.7 - - env: - - DOCKER_IMAGE=python:3.9 -before_install: - - docker pull $DOCKER_IMAGE -script: docker run -v "$(pwd):/usr/src/app" -w "/usr/src/app" $DOCKER_IMAGE python setup.py test diff --git a/Makefile b/Makefile index f39cd53..5ef361d 100644 --- a/Makefile +++ b/Makefile @@ -2,20 +2,17 @@ PYFILES = ms_client/ examples/ all: -format: - black ${PYFILES} - lint: flake8 ${PYFILES} -develop: - pip install -e .[dev] +test: + python3 -m unittest discover tests/ -v build: clean python setup.py sdist bdist_wheel install: build - pip install -I dist/mediaserver_api_client-*.whl + pip install -I dist/*.whl publish_dry: build twine check dist/*.{whl,tar.gz} diff --git a/setup.cfg b/setup.cfg index bf344d5..a675431 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,6 +12,7 @@ keywords = api client mediaserver + nudgis ubicast license = LGPLv3 license_file = LICENSE @@ -38,9 +39,7 @@ setup_requires = [options.extras_require] dev = - black flake8 - twine [bdist_wheel] universal = 1 diff --git a/tests/test_client.py b/tests/test_client.py new file mode 100644 index 0000000..c48530c --- /dev/null +++ b/tests/test_client.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +''' +MediaServer client test file. +''' +from unittest.mock import patch +import json +import logging +import os +import sys +import unittest +import urllib3 + +CONFIG = { + 'SERVER_URL': 'https://msctest', + 'USE_SESSION': False, +} + + +def mocked_requests_get(*args, **kwargs): + class MockResponse: + def __init__(self, json_data, status_code): + self.text = json.dumps(json_data) + self.json_data = json_data + self.status_code = status_code + + def json(self): + return self.json_data + + if kwargs['url'] == CONFIG['SERVER_URL'] + '/api/v2/': + return MockResponse({'mediaserver': '10.0.0', 'success': True}, 200) + + return MockResponse(None, 404) + + +class MSClientTest(unittest.TestCase): + maxDiff = None + + def setUp(self): + print('\n\033[96m----- %s.%s -----\033[0m' % (self.__class__.__name__, self._testMethodName)) + # Setup logging + logging.basicConfig( + level=logging.DEBUG, + format='%(asctime)s %(name)s %(levelname)s %(message)s', + stream=sys.stdout + ) + urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + # Setup sys path + sys.path.pop(0) # Remove current dir + src_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + sys.path.insert(0, src_dir) + + @patch('requests.get', side_effect=mocked_requests_get) + def test_client(self, mock_get): + from ms_client.client import MediaServerClient + msc = MediaServerClient(local_conf=CONFIG) + response = msc.api('/') + self.assertTrue(isinstance(response, dict)) + self.assertEqual(response['mediaserver'], '10.0.0') + + self.assertEqual(len(mock_get.call_args_list), 1) + + +if __name__ == '__main__': + unittest.main()