Skip to content

Commit

Permalink
feat: add templates for unit test; chore: update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryanthelol committed Nov 20, 2023
1 parent d85e227 commit b78d3ad
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pychassiscli"
version = "0.3.0"
version = "0.3.1"
description = "A lightweight Python distributed microservice command line tool"
authors = [
{ name = "BryantHe", email = "bryantsisu@qq.com" },
Expand All @@ -14,10 +14,11 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules",
]
dependencies = [
"python-on-whales==0.67.0",
"python-on-whales>=0.67.0",
"rich>=13.7.0",
"click>=8.1.7",
"mako>=1.3.0"
"mako>=1.3.0",
"pytest>=7.4.3",
]
requires-python = ">=3.8"
readme = "README.md"
Expand Down
15 changes: 13 additions & 2 deletions src/pychassiscli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

from pychassiscli.utils import get_directory, status, copy_files, check_docker

from utils import (start_metric_servers, stop_metric_servers, start_prometheus, start_statsd_exporter,
from pychassiscli.utils import (start_metric_servers, stop_metric_servers, start_prometheus, start_statsd_exporter,
start_statsd_agent, start_grafana, start_metric_network, stop_metric_network, stop_grafana,
stop_prometheus, stop_statsd_exporter, stop_statsd_agent, refresh_metric_servers)

GENERATE_TYPE_CHOICES = ['apiflask', 'nameko', 'metrics']
GENERATE_TYPE_CHOICES = ['apiflask', 'nameko', 'metrics', 'unit_test']
SERVICE_CHOICES = ['metrics', 'metrics_network', 'prometheus', 'grafana', 'statsd_exporter', 'statsd_agent']
REFRESH_SERVICE_CHOICES = ['metrics']

Expand Down Expand Up @@ -69,6 +69,17 @@ def gen(directory, _type, nameko_module, class_name_str):
if not class_name_str:
# TODO input 让用户在命令行输入
pass
if _type == 'unit_test':
if not os.access(directory, os.F_OK) or not os.listdir(directory):
rich_print('Directory {} dose not exist or is empty'.format(directory))
return

tests_dir = os.path.join(get_directory('tests'), 'unit')
if not os.access(tests_dir, os.F_OK):
rich_print('No such test type {}'.format('unit'))
return

copy_files(tests_dir, directory)
else:
template_dir = os.path.join(get_directory('templates'), _type)
if not os.access(template_dir, os.F_OK):
Expand Down
Empty file.
Empty file.
29 changes: 29 additions & 0 deletions src/pychassiscli/tests/unit/test_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Service unit testing best practice.
"""

import pytest
from nameko.testing.services import worker_factory


@pytest.mark.parametrize(
'value, expected',
[
('John Doe', 'Hello, John Doe!'),
('', 'Hello, !'),
('Bryant', 'Hello, Bryant!'),
],
)
def test_example_service(value, expected):
"""
Test example service.
"""
# create worker with mock dependencies
service = worker_factory(ServiceName) # TODO replace ServiceName with the name of the service and import it

# add side effects to the mock rpc dependency on the "remote" service
service.remote.hello.side_effect = lambda name: "Hello, {}!".format(name)

# test remote_hello business logic
assert service.remote_hello(value) == expected
service.remote.hello.assert_called_once_with(value)

0 comments on commit b78d3ad

Please sign in to comment.