Skip to content

Commit

Permalink
Introduce example of app testing with patches
Browse files Browse the repository at this point in the history
  • Loading branch information
ponyisi authored and BenGalewsky committed Nov 16, 2024
1 parent 4822f5a commit 17ae241
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ test = [
"pytest>=7.2.0",
"pytest-mock>=3.10.0",
"pytest-cov>=4.0.0",
"pytest-console-scripts>=1.4.1",
"flake8>=5.0.4",
"mypy>=0.981",
"pytest-asyncio>=0.21.0",
Expand Down
2 changes: 2 additions & 0 deletions servicex/app/cli_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
url_cli_option = typer.Option(None, "-u", "--url", help="URL of ServiceX server")
backend_cli_option = typer.Option(None, "-b", "--backend",
help="Name of backend server from .servicex file")
config_file_option = typer.Option(None, "-c", "--config",
help="ServiceX client configuration file")
12 changes: 7 additions & 5 deletions servicex/app/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import rich
import typer

from servicex.app.cli_options import url_cli_option, backend_cli_option
from servicex.app.cli_options import url_cli_option, backend_cli_option, config_file_option
from servicex.servicex_client import ServiceXClient
from typing import Optional

Expand All @@ -39,11 +39,12 @@
@codegen_app.command(no_args_is_help=False)
def flush(
url: Optional[str] = url_cli_option,
backend: Optional[str] = backend_cli_option):
backend: Optional[str] = backend_cli_option,
config_path: Optional[str] = config_file_option):
"""
Flush the available code generators from the cache
"""
sx = ServiceXClient(url=url, backend=backend)
sx = ServiceXClient(url=url, backend=backend, config_path=config_path)
cache = sx.query_cache
cache.delete_codegen_by_backend(backend)
rich.print("Deleted cached code generators.")
Expand All @@ -52,9 +53,10 @@ def flush(
@codegen_app.command(no_args_is_help=False)
def list(
url: Optional[str] = url_cli_option,
backend: Optional[str] = backend_cli_option):
backend: Optional[str] = backend_cli_option,
config_path: Optional[str] = config_file_option):
"""
List the available code generators
"""
sx = ServiceXClient(url=url, backend=backend)
sx = ServiceXClient(url=url, backend=backend, config_path=config_path)
rich.print_json(data=sx.get_code_generators())
60 changes: 60 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) 2024, IRIS-HEP
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from unittest.mock import patch


def test_app_version(script_runner):
import servicex._version
result = script_runner.run(['servicex', '--version'])
assert result.returncode == 0
assert result.stdout == f'ServiceX {servicex._version.__version__}\n'


def test_codegen_list(script_runner):
with patch('servicex.servicex_adapter.ServiceXAdapter.get_code_generators', return_value={
"uproot": "http://uproot-codegen",
"xaod": "http://xaod-codegen"
}):
result = script_runner.run(['servicex', 'codegen', 'list', '-c',
'tests/example_config.yaml'])
assert result.returncode == 0
assert result.stdout == '''{
"uproot": "http://uproot-codegen",
"xaod": "http://xaod-codegen"
}
'''


def test_codegen_flush(script_runner):
with patch('servicex.query_cache.QueryCache.delete_codegen_by_backend') as p:
result = script_runner.run(['servicex', 'codegen', 'flush',
'-c', 'tests/example_config.yaml',
'-b', 'localhost'])
assert result.returncode == 0
p.assert_called_once_with('localhost')

0 comments on commit 17ae241

Please sign in to comment.