def test_get_timeseries(self): try: # can't use decorator because this method gets called import nds2 except ImportError as e: pytest.skip(str(e)) product, parser = self.test_init_cli() args = parser.parse_args(self.TEST_ARGS + ['--out', TEMP_PLOT_FILE]) product.post_arg(args) random.seed(0) xts = TimeSeries(random.rand(10240), t0=1000000000, sample_rate=1024, name='X1:TEST-CHANNEL') yts = TimeSeries(random.rand(10240), t0=1000000000, sample_rate=1024, name='Y1:TEST-CHANNEL') nds_connection = mocks.nds2_connection(buffers=[ mocks.nds2_buffer_from_timeseries(xts), mocks.nds2_buffer_from_timeseries(yts), ]) with mock.patch('nds2.connection') as mock_connection, \ mock.patch('nds2.buffer'): mock_connection.return_value = nds_connection product.getTimeSeries(args) assert len(product.timeseries) == (len(product.chan_list) * len(product.start_list)) return product, args
def test_makePlot(self): product, parser = self.test_init_cli() args = parser.parse_args(self.TEST_ARGS + ['--out', TEMP_PLOT_FILE]) product.post_arg(args) args.interactive = True random.seed(0) xts = TimeSeries(random.rand(10240), t0=1000000000, sample_rate=1024, name='X1:TEST-CHANNEL') yts = TimeSeries(random.rand(10240), t0=1000000000, sample_rate=1024, name='Y1:TEST-CHANNEL') nds_connection = mocks.nds2_connection(buffers=[ mocks.nds2_buffer_from_timeseries(xts), mocks.nds2_buffer_from_timeseries(yts), ]) with mock.patch('nds2.connection') as mock_connection, \ mock.patch('nds2.buffer'): mock_connection.return_value = nds_connection try: product.makePlot(args, parser) finally: if os.path.isfile(args.out): os.remove(args.out) assert product.is_interactive is True
def test_connect(self): """Test :func:`gwpy.io.connect` """ import nds2 nds_connection = mocks.nds2_connection(host='nds.test.gwpy') with mock.patch('nds2.connection') as mock_connection: mock_connection.return_value = nds_connection conn = io_nds2.connect('nds.test.gwpy') assert conn.get_host() == 'nds.test.gwpy' assert conn.get_port() == 31200 nds_connection = mocks.nds2_connection(host='nds2.test.gwpy', port=8088) with mock.patch('nds2.connection') as mock_connection: mock_connection.return_value = nds_connection conn = io_nds2.connect('nds2.test.gwpy') assert conn.get_host() == 'nds2.test.gwpy' assert conn.get_port() == 8088
def test_fetch(self): ts = self.create(name='X1:TEST', t0=1000000000, unit='m') nds_buffer = mocks.nds2_buffer_from_timeseries(ts) nds_connection = mocks.nds2_connection(buffers=[nds_buffer]) with mock.patch('nds2.connection') as mock_connection, \ mock.patch('nds2.buffer', nds_buffer): mock_connection.return_value = nds_connection # use verbose=True to hit more lines ts2 = self.TEST_CLASS.fetch('X1:TEST', *ts.span, verbose=True) # check open connection works ts2 = self.TEST_CLASS.fetch('X1:TEST', *ts.span, verbose=True, connection=nds_connection) utils.assert_quantity_sub_equal(ts, ts2, exclude=['channel'])
def test_query_nds2_availability(self): # mock NDS2 query ndsb = mocks.nds2_buffer(self.NAMES[0], [], 0, 64, 'm') availability = [ mocks.nds2_availability(self.NAMES[0], [(0, 10), (20, 30)]), ] conn = mocks.nds2_connection(buffers=[ndsb]) conn.get_availability.return_value = availability with mock.patch('nds2.connection') as ndsc: ndsc.return_value = conn avail = self.TEST_CLASS.query_nds2_availability( [self.NAMES[0]], 0, 30, host='test') assert isinstance(avail, SegmentListDict) utils.assert_segmentlist_equal(avail[self.NAMES[0]], [(0, 10), (20, 30)])
def test_query_nds2(self): # mock NDS2 query buffers = [] for name, fs in zip(self.NAMES[:-1], self.SAMPLE_RATES[:-1]): buffers.append(mocks.nds2_buffer(name, [], 0, fs, 'm')) conn = mocks.nds2_connection(buffers=buffers) with mock.patch('nds2.connection') as ndsc: ndsc.return_value = conn # test query_nds2 c = self.TEST_CLASS.query_nds2(self.NAMES[:-1], host='test') assert len(c) == len(self.NAMES) - 1 assert c[0].name == self.NAMES[0] assert c[0].sample_rate == self.SAMPLE_RATES[0] * units.Hz # check errors assert len( self.TEST_CLASS.query_nds2([self.NAMES[-1]], host='test')) == 0
def test_query_nds2_availability(self): # mock NDS2 query ndsb = mocks.nds2_buffer(self.NAMES[0], [], 0, 64, 'm') availability = [ mocks.nds2_availability(self.NAMES[0], [(0, 10), (20, 30)]), ] conn = mocks.nds2_connection(buffers=[ndsb]) conn.get_availability.return_value = availability with mock.patch('nds2.connection') as ndsc: ndsc.return_value = conn avail = self.TEST_CLASS.query_nds2_availability([self.NAMES[0]], 0, 30, host='test') assert isinstance(avail, SegmentListDict) utils.assert_segmentlist_equal(avail[self.NAMES[0]], [(0, 10), (20, 30)])
def test_query_nds2(self, name): # mock NDS2 query ndsb = mocks.nds2_buffer('X1:TEST-CHANNEL', [], 0, 64, 'm') if ndsb.name == name: buffers = [ndsb] else: buffers = [] conn = mocks.nds2_connection(buffers=buffers) with mock.patch('nds2.connection') as ndsc, \ mock.patch('nds2.buffer', ndsb): ndsc.return_value = conn # test query_nds2 if buffers: c = self.TEST_CLASS.query_nds2(name, host='test') assert c.name == name assert c.sample_rate == 64 * units.Hz assert c.unit == units.m assert c.dtype == numpy.dtype('float32') assert c.type == 'raw' else: with pytest.raises(ValueError): c = self.TEST_CLASS.query_nds2(name, host='test')