Commit e6cadf7d authored by Tanner Prestegard's avatar Tanner Prestegard

Add unit tests for update_grbevent method and CLI command

parent 6e174797
......@@ -23,7 +23,7 @@ COMMANDS_TEST_DATA = [
"tags", "voevents"]),
("remove", ["event", "label", "tag"]),
("search", ["events", "superevents"]),
("update", ["event", "signoff", "superevent"]),
("update", ["event", "signoff", "superevent", "grbevent"]),
]
......
......@@ -109,3 +109,50 @@ def test_update_superevent_subcommand(CLI, t_start, t_0, t_end, pref_ev):
assert cli_args == (s_id,)
assert cli_kwargs == {'t_start': t_start, 't_0': t_0, 't_end': t_end,
'preferred_event': pref_ev}
GRBEVENT_TEST_DATA = [
(None, None, None, None, None, None),
(1, 2, 3, 4, 5, 'GRB000101A'),
(1, None, 3, None, None, 'GRB000101123'),
]
@pytest.mark.parametrize( # noqa: E302
"ra,dec,error_radius,t90,redshift,designation",
GRBEVENT_TEST_DATA
)
def test_update_grbevent_subcommand(
CLI, ra, dec, error_radius, t90, redshift, designation
):
"""Test update grbevent subcommand"""
gid = 'E123456'
# Generate command
cmd = "update grbevent {gid}".format(gid=gid)
if ra is not None:
cmd += " --ra={ra}".format(ra=ra)
if dec is not None:
cmd += " --dec={dec}".format(dec=dec)
if error_radius is not None:
cmd += " --error-radius={er}".format(er=error_radius)
if t90 is not None:
cmd += " --t90={t90}".format(t90=t90)
if redshift is not None:
cmd += " --redshift={rs}".format(rs=redshift)
if designation is not None:
cmd += " --designation={d}".format(d=designation)
func = 'ligo.gracedb.rest.GraceDb.update_grbevent'
with mock.patch(func) as mock_cli_func:
CLI(shlex.split(cmd))
# Check call count
assert mock_cli_func.call_count == 1
# Get args used in function call
cli_args, cli_kwargs = mock_cli_func.call_args
# Check args used in function call
assert cli_args == (gid,)
assert cli_kwargs == {'ra': ra, 'dec': dec, 'error_radius': error_radius,
't90': t90, 'redshift': redshift,
'designation': designation}
import datetime
try:
from unittest import mock
except ImportError:
import mock
from cryptography import x509
from cryptography.x509.oid import NameOID
......@@ -7,6 +11,16 @@ from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa
import pytest
from ligo.gracedb.rest import GraceDb
@pytest.fixture
def safe_client():
"""A client class which has its request() method mocked away"""
with mock.patch.object(GraceDb, 'request'):
client = GraceDb()
return client
@pytest.fixture
def x509_key():
......
import pytest
try:
from unittest import mock
except ImportError: # py < 3
import mock
def test_update_grbevent_no_args_provided(safe_client):
err_str = ('Provide at least one of ra, dec, error_radius, t90, '
'redshift, or designation')
with pytest.raises(ValueError, match=err_str):
safe_client.update_grbevent('E123456')
GRBEVENT_DATA = [
(1, None, None, None, None, None),
(None, 2, 3, None, 5, 'test'),
(1, 2, 3, 4, 5, 'test'),
]
@pytest.mark.parametrize( # noqa: E302
"ra,dec,error_radius,t90,redshift,designation",
GRBEVENT_DATA
)
def test_update_grbevent_with_args(
safe_client, ra, dec, error_radius, t90, redshift, designation
):
with mock.patch.object(safe_client, 'patch') as mock_patch, \
mock.patch('ligo.gracedb.rest.GraceDb.templates', # noqa: E127
new_callable=mock.PropertyMock):
safe_client.update_grbevent(
'E123456', ra=ra, dec=dec, error_radius=error_radius, t90=t90,
redshift=redshift, designation=designation
)
# Check calls to patch() method
call_args, call_kwargs = mock_patch.call_args
assert len(call_kwargs) == 1
assert 'body' in call_kwargs
# Construct expected body content
expected_body = {}
if ra is not None:
expected_body['ra'] = ra
if dec is not None:
expected_body['dec'] = dec
if error_radius is not None:
expected_body['error_radius'] = error_radius
if t90 is not None:
expected_body['t90'] = t90
if redshift is not None:
expected_body['redshift'] = redshift
if designation is not None:
expected_body['designation'] = designation
assert call_kwargs['body'] == expected_body
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment