def test_from_database_TimeSeriesFixedResolution_default_resolution_unit_is_minutes( self): database_value = """{ "type": "time_series", "index": { "start": "2019-03-23", "resolution": 30 }, "data": [7.0, 5.0, 8.1] }""" time_series = from_database(database_value) self.assertEqual(len(time_series), 3) self.assertEqual(len(time_series.resolution), 1) self.assertEqual(time_series.resolution[0], relativedelta(minutes=30)) database_value = """{ "type": "time_series", "index": { "start": "2019-03-23", "resolution": [30, 45] }, "data": [7.0, 5.0, 8.1] }""" time_series = from_database(database_value) self.assertEqual(len(time_series), 3) self.assertEqual(len(time_series.resolution), 2) self.assertEqual(time_series.resolution[0], relativedelta(minutes=30)) self.assertEqual(time_series.resolution[1], relativedelta(minutes=45))
def query_object_parameter_values_by_object_class_and_object_name( self, object_class_name, object_name): """ When not all data is required, this function can be used to query all parameter values for a certain object class and object name. Handy for objects with only one value. :param object_class_name: Name of the object class. :param object_name: Name of the object. :return: Dict with object_class_name, object_name, parameter_name, parameter_value and alternative """ subquery = self._db_map.object_parameter_value_sq return [{ 'object_class_name': value_row.object_class_name, 'object_name': value_row.object_name, 'parameter_name': value_row.parameter_name, 'parameter_value': from_database(value_row.value, value_row.type), 'alternative': value_row.alternative_name } for value_row in self._db_map.query(subquery).filter( subquery.c.object_class_name == object_class_name).filter( subquery.c.object_name == object_name).all()]
def test_from_database_TimeSeriesFixedResolution_resolution_list(self): database_value = """{ "type": "time_series", "index": { "start": "2019-01-31", "resolution": ["1 day", "1M"], "ignore_year": false, "repeat": false }, "data": [7.0, 5.0, 8.1, -4.1] }""" time_series = from_database(database_value) self.assertEqual(len(time_series), 4) numpy.testing.assert_equal( time_series.indexes, numpy.array( [ numpy.datetime64("2019-01-31"), numpy.datetime64("2019-02-01"), numpy.datetime64("2019-03-01"), numpy.datetime64("2019-03-02"), ], dtype="datetime64[s]", ), ) numpy.testing.assert_equal(time_series.values, numpy.array([7.0, 5.0, 8.1, -4.1])) self.assertEqual(time_series.start, datetime.fromisoformat("2019-01-31")) self.assertEqual(len(time_series.resolution), 2) self.assertEqual(time_series.resolution, [relativedelta(days=1), relativedelta(months=1)]) self.assertFalse(time_series.ignore_year) self.assertFalse(time_series.repeat)
def test_from_database_TimeSeriesFixedResolution(self): days_of_our_lives = """{ "type": "time_series", "index": { "start": "2019-03-23", "resolution": "1 day", "ignore_year": false, "repeat": false }, "data": [7.0, 5.0, 8.1] }""" time_series = from_database(days_of_our_lives) self.assertEqual(len(time_series), 3) numpy.testing.assert_equal( time_series.indexes, numpy.array( [ numpy.datetime64("2019-03-23"), numpy.datetime64("2019-03-24"), numpy.datetime64("2019-03-25"), ], dtype="datetime64[s]", ), ) self.assertTrue(isinstance(time_series.values, numpy.ndarray)) numpy.testing.assert_equal(time_series.values, numpy.array([7.0, 5.0, 8.1])) self.assertEqual(time_series.start, datetime.fromisoformat("2019-03-23")) self.assertEqual(len(time_series.resolution), 1) self.assertEqual(time_series.resolution[0], relativedelta(days=1)) self.assertFalse(time_series.ignore_year) self.assertFalse(time_series.repeat)
def test_from_database_TimeSeriesFixedResolution_no_index(self): database_value = """{ "type": "time_series", "data": [1, 2, 3, 4, 5, 8] } """ time_series = from_database(database_value) self.assertEqual(len(time_series), 6) numpy.testing.assert_equal( time_series.indexes, numpy.array( [ numpy.datetime64("0001-01-01T00:00:00"), numpy.datetime64("0001-01-01T01:00:00"), numpy.datetime64("0001-01-01T02:00:00"), numpy.datetime64("0001-01-01T03:00:00"), numpy.datetime64("0001-01-01T04:00:00"), numpy.datetime64("0001-01-01T05:00:00"), ], dtype="datetime64[s]", ), ) numpy.testing.assert_equal(time_series.values, numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 8.0])) self.assertEqual(time_series.start, datetime.fromisoformat("0001-01-01T00:00:00")) self.assertEqual(len(time_series.resolution), 1) self.assertEqual(time_series.resolution[0], relativedelta(hours=1)) self.assertTrue(time_series.ignore_year) self.assertTrue(time_series.repeat)
def test_from_database_TimeSeriesVariableResolution_as_two_column_array( self): releases = """{ "type": "time_series", "data": [ ["1977-05-25", 4], ["1980-05-21", 5], ["1983-05-25", 6] ] }""" time_series = from_database(releases) numpy.testing.assert_equal( time_series.indexes, numpy.array( [ numpy.datetime64("1977-05-25"), numpy.datetime64("1980-05-21"), numpy.datetime64("1983-05-25"), ], dtype="datetime64[D]", ), ) self.assertEqual(len(time_series), 3) self.assertTrue(isinstance(time_series.values, numpy.ndarray)) numpy.testing.assert_equal(time_series.values, numpy.array([4, 5, 6]))
def test_from_database_Duration_as_list(self): database_value = '{"type": "duration", "data": ["1 hour", "1h", 60, "2 hours"]}' value = from_database(database_value) expected = [ relativedelta(hours=1), relativedelta(hours=1), relativedelta(minutes=60), relativedelta(hours=2), ] self.assertEqual(value.value, expected)
def test_from_database_TimeSeriesFixedResolution_default_repeat(self): database_value = """{ "type": "time_series", "index": { "ignore_year": true }, "data": [["2019-07-02T10:00:00", 7.0], ["2019-07-02T10:00:01", 4.0]] }""" time_series = from_database(database_value) self.assertTrue(time_series.ignore_year) self.assertFalse(time_series.repeat)
def test_from_database_TimeSeriesFixedResolution_default_ignore_year(self): # Should be false if start is given database_value = """{ "type": "time_series", "index": { "start": "2019-03-23", "resolution": "1 day", "repeat": false }, "data": [7.0, 5.0, 8.1] }""" time_series = from_database(database_value) self.assertFalse(time_series.ignore_year) # Should be true if start is omitted database_value = """{ "type": "time_series", "index": { "resolution": "1 day", "repeat": false }, "data": [7.0, 5.0, 8.1] }""" time_series = from_database(database_value) self.assertTrue(time_series.ignore_year)
def test_from_database_TimeSeriesFixedResolution_default_resolution_is_1hour( self): database_value = """{ "type": "time_series", "index": { "start": "2019-03-23", "ignore_year": false, "repeat": false }, "data": [7.0, 5.0, 8.1] }""" time_series = from_database(database_value) self.assertEqual(len(time_series), 3) self.assertEqual(len(time_series.resolution), 1) self.assertEqual(time_series.resolution[0], relativedelta(hours=1))
def test_from_database_TimePattern(self): database_value = """ { "type": "time_pattern", "data": { "m1-4,m9-12": 300, "m5-8": 221.5 } } """ value = from_database(database_value) self.assertEqual(len(value), 2) numpy.testing.assert_equal(value.indexes, numpy.array(["m1-4,m9-12", "m5-8"])) numpy.testing.assert_equal(value.values, numpy.array([300.0, 221.5]))
def test_from_database_plain_number(self): database_value = "23.0" value = from_database(database_value) self.assertTrue(isinstance(value, float)) self.assertEqual(value, 23.0)
def test_from_database_Duration_default_units(self): database_value = '{"type": "duration", "data": 23}' value = from_database(database_value) self.assertEqual(value.value, relativedelta(minutes=23))
def test_from_database_Duration(self): database_value = '{"type": "duration", "data": "4 seconds"}' value = from_database(database_value) self.assertEqual(value.value, relativedelta(seconds=4))
def test_from_database_DateTime(self): database_value = '{"type": "date_time", "data": "2019-06-01T22:15:00+01:00"}' value = from_database(database_value) self.assertEqual(value.value, datetime.fromisoformat("2019-06-01T22:15:00+01:00"))