test_tool.py 4.1 KB
Newer Older
1 2 3
import json
import os

Min-A Cho's avatar
Min-A Cho committed
4
# import astropy.utils.data
5 6 7 8 9 10 11 12
import pkg_resources
import pytest

from ..tool import main


class MockGraceDb(object):

13 14
    def __init__(self, service):
        assert service == 'https://gracedb.invalid/api/'
Min-A Cho's avatar
Min-A Cho committed
15
        self.service_url = service
16

17
    def _open(self, graceid, filename):
18
        if filename.endswith('.fits.gz'):
Min-A Cho's avatar
Min-A Cho committed
19 20 21 22 23 24
            # url = ('https://dcc.ligo.org/public/0145/T1700453/001/'
            #        'LALInference_v1.fits.gz')
            # filename = astropy.utils.data.download_file(url, cache=True)
            filename = os.path.join('data', graceid, filename)
            return open(pkg_resources.resource_filename(__name__,
                                                        filename), 'rb')
25
        else:
Leo Pound Singer's avatar
Leo Pound Singer committed
26
            filename = os.path.join('data', graceid, filename)
Min-A Cho's avatar
Min-A Cho committed
27 28 29 30 31 32 33 34
            if 'bayestar-gbm' in filename:
                filename += '.gz'
                return open(pkg_resources.resource_filename(
                    __name__, filename), 'rb')

            elif filename.endswith('.fits'):
                return open(pkg_resources.resource_filename(
                    __name__, filename), 'rb')
35

36 37
            else:
                f = open(pkg_resources.resource_filename(__name__, filename))
38

39 40 41 42 43
                def get_json():
                    return json.load(f)

                f.json = get_json
                return f
44

Min-A Cho's avatar
Min-A Cho committed
45 46 47
    def superevent(self, graceid):
        return self._open(graceid, 'superevent.json')

48 49 50 51 52 53 54 55 56
    def event(self, graceid):
        return self._open(graceid, 'event.json')

    def logs(self, graceid):
        return self._open(graceid, 'logs.json')

    def voevents(self, graceid):
        return self._open(graceid, 'voevents.json')

57
    def files(self, graceid, filename=None, raw=True):
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        if filename is None:
            return self._open(graceid, 'files.json')
        else:
            return self._open(graceid, os.path.join('files', filename))


@pytest.fixture
def mock_gracedb(monkeypatch):
    return monkeypatch.setattr('ligo.gracedb.rest.GraceDb', MockGraceDb)


@pytest.fixture
def mock_webbrowser_open(mocker):
    return mocker.patch('webbrowser.open')


74
def test_cbc_compose(mock_gracedb):
Min-A Cho's avatar
Min-A Cho committed
75
    main(['--service', 'https://gracedb.invalid/api/', 'compose', 'S1234'])
76 77


78 79 80 81
def test_burst_compose(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/', 'compose', 'S2468'])


82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
def test_skymap_update(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/',
          'compose_update', 'S5678', ['sky_localization']])


def test_general_update(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/',
          'compose_update', 'S5678',
          ['sky_localization', 'p_astro', 'em_bright']])


def test_classification_update(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/',
          'compose_update', 'S5678', ['p_astro', 'em_bright']])


98 99 100 101
def test_compose_mailto(mock_gracedb, mock_webbrowser_open):
    main(['--service', 'https://gracedb.invalid/api/', 'compose',
          '--mailto', 'S1234'])
    assert mock_webbrowser_open.called_once()
102 103


104 105
def test_raven_with_initial_circular(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/', 'compose_raven',
106 107 108
          'S1234'])


109 110
def test_raven_without_initial_circular(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/', 'compose_raven',
111
          'S5678'])
112 113


114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
def test_medium_latency_cbc_detection(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/',
          'compose_grb_medium_latency', 'E1234'])


def test_medium_latency_cbc_burst_detection(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/',
          'compose_grb_medium_latency', 'E1122'])


def test_medium_latency_cbc_exclusion(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/',
          'compose_grb_medium_latency', 'E1133'])


def test_medium_latency_cbc_burst_exclusion(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/',
          'compose_grb_medium_latency', 'E2244'])


134 135 136
def test_retraction(mock_gracedb):
    main(['--service', 'https://gracedb.invalid/api/', 'compose_retraction',
          'S1234'])