Skip to content

Commit

Permalink
Added client test
Browse files Browse the repository at this point in the history
  • Loading branch information
sdiemer committed Jul 30, 2021
1 parent ae37db3 commit 0c18abb
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 13 deletions.
11 changes: 11 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -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/
38 changes: 38 additions & 0 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var/
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
*.log

# Unit test / coverage reports
htmlcov/
Expand All @@ -47,14 +48,14 @@ coverage.xml
*.mo
*.pot

# Django stuff:
*.log
# Confs
conf*.json

# Sphinx documentation
docs/_build/

# PyBuilder
target/

#Ipython Notebook
# Ipython Notebook
.ipynb_checkpoints
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ PYFILES = mm_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
Expand Down
9 changes: 4 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ classifiers =
Development Status :: 5 - Production/Stable
Environment :: Console
License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Topic :: Multimedia :: Video
Topic :: Software Development :: Libraries :: Python Modules

Expand All @@ -32,15 +33,13 @@ install_requires =
packages =
mm_client
mm_client.lib
setup_requires=
setuptools
setup_requires =
setuptools >= 30.3.0
wheel

[options.extras_require]
dev =
black
flake8
twine

[bdist_wheel]
universal = 1
64 changes: 64 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Miris Manager client test file.
'''
from unittest.mock import patch
import json
import logging
import os
import sys
import unittest
import urllib3

CONFIG = {
'SERVER_URL': 'https://mmctest'
}


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/':
return MockResponse({'version': '8.0.0'}, 200)

return MockResponse(None, 404)


class MMClientTest(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 mm_client.client import MirisManagerClient
mmc = MirisManagerClient(local_conf=CONFIG)
response = mmc.api_request('PING')
self.assertTrue(isinstance(response, dict))
self.assertEqual(response['version'], '8.0.0')

self.assertEqual(len(mock_get.call_args_list), 1)


if __name__ == '__main__':
unittest.main()

0 comments on commit 0c18abb

Please sign in to comment.