def test_write_dict_to_pi_file(): """Test to write a dict of TimeseriesStub-like objects to a PI XML file.""" dict_series =\ {"precipitation": SparseTimeseriesStub(datetime(2011, 10, 25), [10.0, 20.0, 30.0]), "seepage": SparseTimeseriesStub(datetime(2011, 10, 25), [40.0, 50.0, 60.0]), } testdata = pkg_resources.resource_filename("timeseries", "testdata/") filename = "open-water-incoming-flows.xml" filepath = os.path.join(testdata, filename) if filename in os.listdir(testdata): os.remove(filepath) write_to_pi_file(location_id="SAP", filename=filepath, timeseries=dict_series) obj = TimeSeries.as_dict(filepath) if filename in os.listdir(testdata): os.remove(filepath) for parameter_id, series in dict_series.iteritems(): stored_events = [(e[0], e[1][0]) for e in obj[("SAP", parameter_id)].get_events()] assert stored_events == list(series.events())
def test_a(self): """Test events returns the right events.""" timeseries = SparseTimeseriesStub(datetime(2011, 4, 8), \ [10.0, 20.0, 30.0]) expected_events = [(datetime(2011, 4, 8), 10.0), (datetime(2011, 4, 9), 20.0), (datetime(2011, 4, 10), 30.0)] self.assertEqual(expected_events, list(timeseries.events()))
def test_write_to_pi_file(): """Test the way to write a TimeseriesStub-like object to a PI XML file.""" series = \ SparseTimeseriesStub(datetime(2011, 10, 25), [10.0, 20.0, 30.0]) testdata = pkg_resources.resource_filename("timeseries", "testdata/") filename = "sluice-error.xml" filepath = os.path.join(testdata, filename) if filename in os.listdir(testdata): os.remove(filepath) write_to_pi_file(location_id="SAP", parameter_id="sluice-error", filename=filepath, timeseries=series) obj = TimeSeries.as_dict(filepath) if filename in os.listdir(testdata): os.remove(filepath) stored_events = [(e[0], e[1][0]) for e in obj[("SAP", "sluice-error")].get_events()] assert stored_events == list(series.events())
def test_b(self): """Test add_value adds the right events.""" timeseries = SparseTimeseriesStub() timeseries.add_value(datetime(2011, 4, 8), 10.0) timeseries.add_value(datetime(2011, 4, 9), 20.0) timeseries.add_value(datetime(2011, 4, 10), 30.0) expected_events = [(datetime(2011, 4, 8), 10.0), (datetime(2011, 4, 9), 20.0), (datetime(2011, 4, 10), 30.0)] self.assertEqual(expected_events, list(timeseries.events()))
def test_h(self): """Test events returns a subset of the events. The subset starts after the first event and does not include the later events. """ timeseries = SparseTimeseriesStub() timeseries.add_value(datetime(2011, 4, 8), 10.0) timeseries.add_value(datetime(2011, 4, 9), 20.0) timeseries.add_value(datetime(2011, 4, 10), 30.0) timeseries.add_value(datetime(2011, 4, 11), 40.0) start_date, end_date = datetime(2011, 4, 9), datetime(2011, 4, 11) events = list(timeseries.events(start_date, end_date)) self.assertEqual(2, len(events)) self.assertEqual((datetime(2011, 4, 9), 20.0), events[0]) self.assertEqual((datetime(2011, 4, 10), 30.0), events[1])
def test_e(self): """Test events returns a subset of the events. The subset includes all events. """ timeseries = SparseTimeseriesStub() timeseries.add_value(datetime(2011, 4, 8), 10.0) timeseries.add_value(datetime(2011, 4, 9), 20.0) timeseries.add_value(datetime(2011, 4, 10), 30.0) timeseries.add_value(datetime(2011, 4, 11), 40.0) start_date, end_date = datetime(2011, 4, 8), datetime(2011, 4, 12) events = list(timeseries.events(start_date, end_date)) self.assertEqual(4, len(events)) self.assertEqual((datetime(2011, 4, 8), 10.0), events[0]) self.assertEqual((datetime(2011, 4, 9), 20.0), events[1]) self.assertEqual((datetime(2011, 4, 10), 30.0), events[2]) self.assertEqual((datetime(2011, 4, 11), 40.0), events[3])
def test_sorted_event_keys(): """Test the way to attach a method to a TimeseriesStub-like object.""" timeseries = \ SparseTimeseriesStub(datetime(2011, 10, 25), [10.0, 20.0, 30.0]) timeseries.sorted_event_items = lambda: list(timeseries.events()) assert timeseries.sorted_event_items() == list(timeseries.events())
def test_c(self): """Test add_value can only add events on consecutive days.""" timeseries = SparseTimeseriesStub() timeseries.add_value(datetime(2011, 4, 8), 10.0) self.assertRaises(AssertionError, timeseries.add_value, \ datetime(2011, 4, 10), 30.0)
def test_d(self): """Test add_value can only add events on consecutive days.""" timeseries = SparseTimeseriesStub(datetime(2011, 4, 8), \ [10.0, 20.0, 30.0]) self.assertRaises(AssertionError, timeseries.add_value, \ datetime(2011, 4, 12), 30.0)