def test_timestamp_sensor(self): """Test timestamp sensor.""" s = Sensor.timestamp("a.timestamp", "A timestamp sensor.", "", None) self.assertEqual(s.stype, "timestamp") s.set(timestamp=12345, status=katcp.Sensor.NOMINAL, value=1001.9) # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), ("12345.000000", "nominal", "1001.900000")) self.assertEqual(s.read_formatted(), ("12345.000000", "nominal", "1001.900000")) # Test with katcp v4 parsing formatting self.assertEqual(s.format_reading(s.read(), major=4), ("12345000", "nominal", "1001900")) self.assertEqual(s.read_formatted(major=4), ("12345000", "nominal", "1001900")) self.assertAlmostEqual(s.parse_value("1002.100"), 1002.1) self.assertRaises(ValueError, s.parse_value, "bicycle") s = Sensor.timestamp("a.timestamp", "A timestamp sensor.", "", default=123) self.assertEqual(s._value, 123) s.set_formatted("12346.1", "nominal", "12246.1") self.assertEqual(s.read(), (12346.1, katcp.Sensor.NOMINAL, 12246.1)) # Test with katcp v4 parsing s.set_formatted("12347100", "nominal", "12247100", major=4) self.assertEqual(s.read(), (12347.1, katcp.Sensor.NOMINAL, 12247.1)) s = Sensor.timestamp("a.timestamp", "A timestamp sensor.", "", initial_status=Sensor.NOMINAL) self.assertEquals(s.status(), Sensor.NOMINAL)
def test_timestamp_sensor(self): """Test timestamp sensor.""" s = Sensor.timestamp("a.timestamp", "A timestamp sensor.", "", None) self.assertEqual(s.stype, 'timestamp') s.set(timestamp=12345, status=Sensor.NOMINAL, value=1001.9) # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), (b"12345.000000", b"nominal", b"1001.900000")) self.assertEqual(s.read_formatted(), (b"12345.000000", b"nominal", b"1001.900000")) # Test with katcp v4 parsing formatting self.assertEqual(s.format_reading(s.read(), major=4), (b"12345000", b"nominal", b"1001900")) self.assertEqual(s.read_formatted(major=4), (b"12345000", b"nominal", b"1001900")) self.assertAlmostEqual(s.parse_value(b"1002.100"), 1002.1) self.assertRaises(ValueError, s.parse_value, b"bicycle") s = Sensor.timestamp("a.timestamp", "A timestamp sensor.", "", default=123) self.assertEqual(s._value, 123) s.set_formatted(b'12346.1', b'nominal', b'12246.1') self.assertEqual(s.read(), (12346.1, Sensor.NOMINAL, 12246.1)) # Test with katcp v4 parsing s.set_formatted(b'12347100', b'nominal', b'12247100', major=4) self.assertEqual(s.read(), (12347.1, Sensor.NOMINAL, 12247.1)) s = Sensor.timestamp("a.timestamp", "A timestamp sensor.", "", initial_status=Sensor.NOMINAL) self.assertEqual(s.status(), Sensor.NOMINAL)
def test_discrete_sensor_from_native_strings(self): """Test discrete sensor initialised from native strings.""" s = Sensor.discrete("a.discrete", "A discrete sensor.", "state", ["on", "off"]) self.assertEqual(s.stype, 'discrete') s.set(timestamp=12345, status=Sensor.ERROR, value="on") # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), (b"12345.000000", b"error", b"on")) self.assertEqual(s.read_formatted(), (b"12345.000000", b"error", b"on")) self.assertEqual(s.parse_value(b"on"), "on") self.assertRaises(ValueError, s.parse_value, b"fish") s = Sensor.discrete("a.discrete", "A discrete sensor.", "state", ["on", "off"], default='on') self.assertEqual(s._value, 'on') s = Sensor.discrete("a.discrete", "A discrete sensor.", "state", ["on", "off"], default='off') self.assertEqual(s._value, 'off') s = Sensor.discrete("a.discrete", "A discrete sensor.", "state", ["on", "off"], initial_status=Sensor.UNREACHABLE) self.assertEqual(s.status(), Sensor.UNREACHABLE)
def test_lru_sensor(self): """Test LRU sensor.""" s = Sensor.lru("an.lru", "An LRU sensor.", "state", None) self.assertEqual(s.stype, 'lru') s.set(timestamp=12345, status=Sensor.FAILURE, value=Sensor.LRU_ERROR) # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), (b"12345.000000", b"failure", b"error")) self.assertEqual(s.read_formatted(), (b"12345.000000", b"failure", b"error")) self.assertEqual(s.parse_value(b"nominal"), Sensor.LRU_NOMINAL) self.assertRaises(ValueError, s.parse_value, b"fish") s = Sensor.lru("an.lru", "An LRU sensor.", "state", default=Sensor.LRU_ERROR) self.assertEqual(s._value, Sensor.LRU_ERROR) s = Sensor.lru("an.lru", "An LRU sensor.", "state", default=Sensor.LRU_NOMINAL) self.assertEqual(s._value, Sensor.LRU_NOMINAL) s = Sensor.lru("an.lru", "An LRU sensor.", "state", initial_status=Sensor.FAILURE) self.assertEqual(s.status(), Sensor.FAILURE)
def test_string_sensor(self): """Test string sensor.""" s = Sensor.string("a.string", "A string sensor.", "filename", None) self.assertEqual(s.stype, 'string') s.set(timestamp=12345, status=katcp.Sensor.NOMINAL, value="zwoop") self.assertEqual(s.read_formatted(), ("12345.000000", "nominal", "zwoop")) self.assertEquals(s.parse_value("bar foo"), "bar foo") s = Sensor.string( "a.string", "A string sensor.", "filename", default='baz') self.assertEqual(s._value, 'baz')
def test_address_sensor(self): """Test address sensor.""" s = Sensor.address("a.address", "An address sensor.", "", None) self.assertEqual(s.stype, 'address') s.set(timestamp=12345, status=Sensor.NOMINAL, value=("127.0.0.1", 80)) self.assertEqual(s.read_formatted(), ("12345.000000", "nominal", "127.0.0.1:80")) self.assertEqual(s.parse_value("[::1]:80"), ("::1", 80)) self.assertRaises(ValueError, s.parse_value, "[::1]:foo") s = Sensor.address("a.address", "An address sensor.", "", default=('192.168.101.1', 81)) self.assertEqual(s._value, ('192.168.101.1', 81))
def test_string_sensor(self): """Test string sensor.""" s = Sensor.string("a.string", "A string sensor.", "filename", None) self.assertEqual(s.stype, "string") s.set(timestamp=12345, status=katcp.Sensor.NOMINAL, value="zwoop") # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), ("12345.000000", "nominal", "zwoop")) self.assertEqual(s.read_formatted(), ("12345.000000", "nominal", "zwoop")) self.assertEquals(s.parse_value("bar foo"), "bar foo") s = Sensor.string("a.string", "A string sensor.", "filename", default="baz") self.assertEqual(s._value, "baz") s = Sensor.string("a.string", "A string sensor.", "filename", initial_status=Sensor.WARN) self.assertEquals(s.status(), Sensor.WARN)
def test_boolean_sensor(self): """Test boolean sensor.""" s = Sensor.boolean("a.boolean", "A boolean.", "on/off", None) self.assertEqual(s.stype, 'boolean') s.set(timestamp=12345, status=katcp.Sensor.UNKNOWN, value=True) self.assertEqual(s.read_formatted(), ("12345.000000", "unknown", "1")) self.assertEquals(s.parse_value("1"), True) self.assertEquals(s.parse_value("0"), False) self.assertRaises(ValueError, s.parse_value, "asd") s = Sensor.boolean("a.boolean", "A boolean.", "on/off", default=True) self.assertEqual(s._value, True) s = Sensor.boolean("a.boolean", "A boolean.", "on/off", default=False) self.assertEqual(s._value, False)
def test_address_sensor(self): """Test address sensor.""" s = Sensor.address("a.address", "An address sensor.", "", None) self.assertEqual(s.stype, "address") s.set(timestamp=12345, status=Sensor.NOMINAL, value=("127.0.0.1", 80)) # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), ("12345.000000", "nominal", "127.0.0.1:80")) self.assertEqual(s.read_formatted(), ("12345.000000", "nominal", "127.0.0.1:80")) self.assertEqual(s.parse_value("[::1]:80"), ("::1", 80)) self.assertRaises(ValueError, s.parse_value, "[::1]:foo") s = Sensor.address("a.address", "An address sensor.", "", default=("192.168.101.1", 81)) self.assertEqual(s._value, ("192.168.101.1", 81)) s = Sensor.address("a.address", "An address sensor.", "", initial_status=Sensor.NOMINAL) self.assertEquals(s.status(), Sensor.NOMINAL)
def test_lru_sensor(self): """Test LRU sensor.""" s = Sensor.lru("an.lru", "An LRU sensor.", "state", None) self.assertEqual(s.stype, 'lru') s.set(timestamp=12345, status=Sensor.FAILURE, value=Sensor.LRU_ERROR) self.assertEqual(s.read_formatted(), ("12345.000000", "failure", "error")) self.assertEquals(s.parse_value("nominal"), katcp.Sensor.LRU_NOMINAL) self.assertRaises(ValueError, s.parse_value, "fish") s = Sensor.lru( "an.lru", "An LRU sensor.", "state", default=Sensor.LRU_ERROR) self.assertEqual(s._value, Sensor.LRU_ERROR) s = Sensor.lru( "an.lru", "An LRU sensor.", "state", default=Sensor.LRU_NOMINAL) self.assertEqual(s._value, Sensor.LRU_NOMINAL)
def test_discrete_sensor(self): """Test discrete sensor.""" s = Sensor.discrete( "a.discrete", "A discrete sensor.", "state", ["on", "off"]) self.assertEqual(s.stype, 'discrete') s.set(timestamp=12345, status=katcp.Sensor.ERROR, value="on") self.assertEqual(s.read_formatted(), ("12345.000000", "error", "on")) self.assertEquals(s.parse_value("on"), "on") self.assertRaises(ValueError, s.parse_value, "fish") s = Sensor.discrete("a.discrete", "A discrete sensor.", "state", ["on", "off"], default='on') self.assertEqual(s._value, 'on') s = Sensor.discrete("a.discrete", "A discrete sensor.", "state", ["on", "off"], default='off') self.assertEqual(s._value, 'off')
def test_discrete_sensor(self): """Test discrete sensor.""" s = Sensor.discrete("a.discrete", "A discrete sensor.", "state", ["on", "off"]) self.assertEqual(s.stype, "discrete") s.set(timestamp=12345, status=katcp.Sensor.ERROR, value="on") # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), ("12345.000000", "error", "on")) self.assertEqual(s.read_formatted(), ("12345.000000", "error", "on")) self.assertEquals(s.parse_value("on"), "on") self.assertRaises(ValueError, s.parse_value, "fish") s = Sensor.discrete("a.discrete", "A discrete sensor.", "state", ["on", "off"], default="on") self.assertEqual(s._value, "on") s = Sensor.discrete("a.discrete", "A discrete sensor.", "state", ["on", "off"], default="off") self.assertEqual(s._value, "off") s = Sensor.discrete( "a.discrete", "A discrete sensor.", "state", ["on", "off"], initial_status=Sensor.UNREACHABLE ) self.assertEquals(s.status(), Sensor.UNREACHABLE)
def test_int_sensor(self): """Test integer sensor.""" s = Sensor.integer("an.int", "An integer.", "count", [-4, 3]) self.assertEqual(s.stype, 'integer') s.set(timestamp=12345, status=katcp.Sensor.NOMINAL, value=3) self.assertEqual(s.read_formatted(), ("12345.000000", "nominal", "3")) self.assertEquals(s.parse_value("3"), 3) self.assertEquals(s.parse_value("4"), 4) self.assertEquals(s.parse_value("-10"), -10) self.assertRaises(ValueError, s.parse_value, "asd") s = Sensor(Sensor.INTEGER, "an.int", "An integer.", "count", [-20, 20]) self.assertEquals(s.value(), 0) s = Sensor(Sensor.INTEGER, "an.int", "An integer.", "count", [2, 20]) self.assertEquals(s.value(), 2) s = Sensor.integer("an.int", "An integer.", "count", [2, 20], default=5) self.assertEquals(s.value(), 5)
def test_float_sensor(self): """Test float sensor.""" s = Sensor.float("a.float", "A float.", "power", [0.0, 5.0]) self.assertEqual(s.stype, 'float') s.set(timestamp=12345, status=katcp.Sensor.WARN, value=3.0) self.assertEqual(s.read_formatted(), ("12345.000000", "warn", "3")) self.assertEquals(s.parse_value("3"), 3.0) self.assertEquals(s.parse_value("10"), 10.0) self.assertEquals(s.parse_value("-10"), -10.0) self.assertRaises(ValueError, s.parse_value, "asd") s = Sensor(katcp.Sensor.FLOAT, "a.float", "A float.", "", [-20.0, 20.0]) self.assertEquals(s.value(), 0.0) s = Sensor(katcp.Sensor.FLOAT, "a.float", "A float.", "", [2.0, 20.0]) self.assertEquals(s.value(), 2.0) s = Sensor.float("a.float", "A float.", "", [2.0, 20.0], default=5.0) self.assertEquals(s.value(), 5.0)
def test_int_sensor(self): """Test integer sensor.""" s = Sensor.integer("an.int", "An integer.", "count", [-4, 3]) self.assertEqual(s.stype, "integer") s.set(timestamp=12345, status=katcp.Sensor.NOMINAL, value=3) # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), ("12345.000000", "nominal", "3")) self.assertEqual(s.read_formatted(), ("12345.000000", "nominal", "3")) self.assertEquals(s.parse_value("3"), 3) self.assertEquals(s.parse_value("4"), 4) self.assertEquals(s.parse_value("-10"), -10) self.assertRaises(ValueError, s.parse_value, "asd") s = Sensor(Sensor.INTEGER, "an.int", "An integer.", "count", [-20, 20]) self.assertEquals(s.value(), 0) s = Sensor(Sensor.INTEGER, "an.int", "An integer.", "count", [2, 20]) self.assertEquals(s.value(), 2) s = Sensor.integer("an.int", "An integer.", "count", [2, 20], default=5) self.assertEquals(s.value(), 5) self.assertEquals(s.status(), Sensor.UNKNOWN) s = Sensor.integer("an.int", "An integer.", "count", [2, 20], initial_status=Sensor.NOMINAL) self.assertEquals(s.status(), Sensor.NOMINAL)
def test_boolean_sensor(self): """Test boolean sensor.""" s = Sensor.boolean("a.boolean", "A boolean.", "on/off", None) self.assertEqual(s.stype, 'boolean') s.set(timestamp=12345, status=Sensor.UNKNOWN, value=True) # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), (b"12345.000000", b"unknown", b"1")) self.assertEqual(s.read_formatted(), (b"12345.000000", b"unknown", b"1")) self.assertEqual(s.parse_value(b"1"), True) self.assertEqual(s.parse_value(b"0"), False) self.assertRaises(ValueError, s.parse_value, b"asd") s = Sensor.boolean("a.boolean", "A boolean.", "on/off", default=True) self.assertEqual(s._value, True) s = Sensor.boolean("a.boolean", "A boolean.", "on/off", default=False) self.assertEqual(s._value, False) s = Sensor.boolean("a.boolean", "A boolean.", "on/off", initial_status=Sensor.ERROR) self.assertEqual(s.status(), Sensor.ERROR)
def test_float_sensor(self): """Test float sensor.""" s = Sensor.float("a.float", "A float.", "power", [0.0, 5.0]) self.assertEqual(s.stype, "float") s.set(timestamp=12345, status=katcp.Sensor.WARN, value=3.0) # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), ("12345.000000", "warn", "3")) self.assertEqual(s.read_formatted(), ("12345.000000", "warn", "3")) self.assertEquals(s.parse_value("3"), 3.0) self.assertEquals(s.parse_value("10"), 10.0) self.assertEquals(s.parse_value("-10"), -10.0) self.assertRaises(ValueError, s.parse_value, "asd") s = Sensor(katcp.Sensor.FLOAT, "a.float", "A float.", "", [-20.0, 20.0]) self.assertEquals(s.value(), 0.0) s = Sensor(katcp.Sensor.FLOAT, "a.float", "A float.", "", [2.0, 20.0]) self.assertEquals(s.value(), 2.0) s = Sensor.float("a.float", "A float.", "", [2.0, 20.0], default=5.0) self.assertEquals(s.value(), 5.0) self.assertEquals(s.status(), Sensor.UNKNOWN) s = Sensor.float("a.float", "A float.", "", [2.0, 20.0], initial_status=Sensor.WARN) self.assertEquals(s.status(), Sensor.WARN)
def test_int_sensor_from_byte_strings(self): """Test integer sensor initialised from byte strings.""" s = Sensor.integer(b"an.int", b"An integer.", b"count", [-4, 3]) # sensor attributes must be native strings self.assertEqual(s.name, "an.int") self.assertEqual(s.units, "count") self.assertEqual(s.description, "An integer.") self.assertEqual(s.stype, "integer") s.set(timestamp=12345, status=Sensor.NOMINAL, value=3) self.assertEqual(s.value(), 3) # after formatting, byte strings are required self.assertEqual(s.format_reading(s.read()), (b"12345.000000", b"nominal", b"3")) self.assertEqual(s.read_formatted(), (b"12345.000000", b"nominal", b"3"))
def test_discrete_sensor_from_byte_strings(self): """Test discrete sensor initialised from byte strings.""" s = Sensor.discrete(b"a.discrete", b"A discrete sensor.", b"state", [b"on", b"off"]) # sensor attributes must be native strings self.assertEqual(s.name, "a.discrete") self.assertEqual(s.units, "state") self.assertEqual(s.description, "A discrete sensor.") self.assertEqual(s.stype, "discrete") s.set(timestamp=12345, status=Sensor.ERROR, value="on") self.assertEqual(s.value(), "on") # after formatting, byte strings are required self.assertEqual(s.format_reading(s.read()), (b"12345.000000", b"error", b"on")) self.assertEqual(s.read_formatted(), (b"12345.000000", b"error", b"on"))
def collectd_sensor_update(self, sensor_name, value_time, status, value): """Update the sensor value, register the sensor if it do not exists.""" if sensor_name not in self.sensor_db: description = '' units = '' params = None sensor_obj = Sensor(type(value), sensor_name, description, units, params) self.sensor_db[sensor_name] = sensor_obj self.add_sensor(sensor_obj) self._send_interface_change = True self.sensor_db[sensor_name].set(value_time, status, value) if self._send_interface_change: # first update the sensor value then send out the inform # to tell connected clients that a sensor was added. msg = katcp.Message.inform('interface-changed', 'sensor', sensor_name, 'added') self._send_interface_change = False self.mass_inform(msg)
def test_set_and_get_value(self): """Test getting and setting a sensor value.""" s = Sensor.integer("an.int", "An integer.", "count", [-4, 3]) s.set(timestamp=12345, status=Sensor.NOMINAL, value=3) self.assertEqual(s.value(), 3) s.set_value(2) self.assertEqual(s.value(), 2) s.set_value(3, timestamp=12345) self.assertEqual(s.read(), (12345, Sensor.NOMINAL, 3)) s.set_value(5, timestamp=12345) self.assertEqual(s.read(), (12345, Sensor.NOMINAL, 5)) s.set_formatted(b'12346.1', b'nominal', b'-2') self.assertEqual(s.read(), (12346.1, Sensor.NOMINAL, -2)) # Test setting with katcp v4 parsing s.set_formatted(b'12347100', b'warn', b'-3', major=4) self.assertEqual(s.read(), (12347.1, Sensor.WARN, -3))
def test_set_and_get_value(self): """Test getting and setting a sensor value.""" s = Sensor.integer("an.int", "An integer.", "count", [-4, 3]) s.set(timestamp=12345, status=katcp.Sensor.NOMINAL, value=3) self.assertEqual(s.value(), 3) s.set_value(2) self.assertEqual(s.value(), 2) s.set_value(3, timestamp=12345) self.assertEqual(s.read(), (12345, katcp.Sensor.NOMINAL, 3)) s.set_value(5, timestamp=12345) self.assertEqual(s.read(), (12345, katcp.Sensor.NOMINAL, 5)) s.set_formatted("12346.1", "nominal", "-2") self.assertEqual(s.read(), (12346.1, katcp.Sensor.NOMINAL, -2)) # Test setting with katcp v4 parsing s.set_formatted("12347100", "warn", "-3", major=4) self.assertEqual(s.read(), (12347.1, katcp.Sensor.WARN, -3))
def test_default_descriptions(self): s = Sensor(Sensor.INTEGER, 'a sens', params=[0, 10]) self.assertEqual(s.description, "Integer sensor 'a sens' with no unit") s = Sensor(Sensor.FLOAT, 'fsens', None, 'microseconds', params=[0, 10]) self.assertEqual(s.description, "Float sensor 'fsens' in unit microseconds")
def test_units_none(self): """Test units initialised to None results in empty string.""" s = Sensor.integer("a sens", None, None, None) self.assertEqual(s.units, "")
def test_int_sensor_from_native_strings(self): """Test integer sensor initialised from native strings.""" s = Sensor.integer("an.int", "An integer.", "count", [-4, 3]) # sensor attributes must be native strings self.assertEqual(s.name, "an.int") self.assertEqual(s.units, "count") self.assertEqual(s.description, "An integer.") self.assertEqual(s.stype, "integer") s.set(timestamp=12345, status=Sensor.NOMINAL, value=3) # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), (b"12345.000000", b"nominal", b"3")) self.assertEqual(s.read_formatted(), (b"12345.000000", b"nominal", b"3")) self.assertEqual(s.parse_value(b"3"), 3) self.assertEqual(s.parse_value(b"4"), 4) self.assertEqual(s.parse_value(b"-10"), -10) self.assertRaises(ValueError, s.parse_value, b"asd") self.assertRaises(KatcpTypeError, s.parse_value, u"asd") self.assertRaises(KatcpTypeError, s.parse_value, u"3") s = Sensor(Sensor.INTEGER, "an.int", "An integer.", "count", [-20, 20]) self.assertEqual(s.value(), 0) s = Sensor(Sensor.INTEGER, "an.int", "An integer.", "count", [2, 20]) self.assertEqual(s.value(), 2) s = Sensor.integer("an.int", "An integer.", "count", [2, 20], default=5) self.assertEqual(s.value(), 5) self.assertEqual(s.status(), Sensor.UNKNOWN) s = Sensor.integer("an.int", "An integer.", "count", [2, 20], initial_status=Sensor.NOMINAL) self.assertEqual(s.status(), Sensor.NOMINAL)
def test_float_sensor(self): """Test float sensor.""" s = Sensor.float("a.float", "A float.", "power", [0.0, 5.0]) self.assertEqual(s.stype, 'float') s.set(timestamp=12345, status=Sensor.WARN, value=3.0) # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), (b"12345.000000", b"warn", b"3.0")) self.assertEqual(s.read_formatted(), (b"12345.000000", b"warn", b"3.0")) self.assertEqual(s.parse_value(b"3.0"), 3.0) self.assertEqual(s.parse_value(b"10"), 10.0) self.assertEqual(s.parse_value(b"-10"), -10.0) self.assertRaises(ValueError, s.parse_value, b"asd") s = Sensor(Sensor.FLOAT, "a.float", "A float.", "", [-20.0, 20.0]) self.assertEqual(s.value(), 0.0) s = Sensor(Sensor.FLOAT, "a.float", "A float.", "", [2.0, 20.0]) self.assertEqual(s.value(), 2.0) s = Sensor.float("a.float", "A float.", "", [2.0, 20.0], default=5.0) self.assertEqual(s.value(), 5.0) self.assertEqual(s.status(), Sensor.UNKNOWN) s = Sensor.float("a.float", "A float.", "", [2.0, 20.0], initial_status=Sensor.WARN) self.assertEqual(s.status(), Sensor.WARN)
def test_string_sensor(self): """Test string sensor.""" s = Sensor.string("a.string", "A string sensor.", "filename", None) self.assertEqual(s.stype, 'string') s.set(timestamp=12345, status=Sensor.NOMINAL, value="zwoop") self.assertEqual(s._value, "zwoop") # test both read_formatted and format_reading self.assertEqual(s.format_reading(s.read()), (b"12345.000000", b"nominal", b"zwoop")) self.assertEqual(s.read_formatted(), (b"12345.000000", b"nominal", b"zwoop")) # test parsing byte string returns native string self.assertEqual(s.parse_value(b"bar foo"), "bar foo") # test default value s = Sensor.string("a.string", "A string sensor.", "filename", default='baz') self.assertEqual(s._value, 'baz') # test initial status s = Sensor.string("a.string", "A string sensor.", "filename", initial_status=Sensor.WARN) self.assertEqual(s.status(), Sensor.WARN) # Test using non-string types from set_value are "stringified" s = Sensor.string("a.string", "Value from list.") s.set_value([1, 2, 3]) self.assertEqual(s.value(), [1, 2, 3]) _, _, formatted_value = s.read_formatted() self.assertEqual(formatted_value, b'[1, 2, 3]') # But this is "one-way", if set from formatted byte string, the # "original" type is not inferred s.set_formatted(b'1.23', b'nominal', b'[4, 5, 6]') self.assertEqual(s.value(), '[4, 5, 6]') # Test Python version-specific limitations if future.utils.PY2: test_cases = [ # name, input, raw, from raw ('non-ascii', b"\x00\xff", b"\x00\xff", b"\x00\xff"), ('utf-8', u"räm", b"r\xc3\xa4m", b"r\xc3\xa4m"), ] else: test_cases = [ # name, input, raw, from raw ('non-ascii', b"\x00\xff", b"\x00\xff", UnicodeDecodeError), ('utf-8', u"räm", b"r\xc3\xa4m", u"räm"), ('invalid', u"\ud83d", UnicodeEncodeError, None), ] for name, input_, raw, from_raw in test_cases: # set from input, then try to read back raw s.set_value(input_) if type(raw) is type and issubclass(raw, Exception): with self.assertRaises(raw): s.read_formatted() continue # cannot test setting from raw, so move on else: _, _, formatted_value = s.read_formatted() self.assertEqual(raw, formatted_value) # set raw, and try to read back value s.set_value(None) if type(from_raw) is type and issubclass(from_raw, Exception): with self.assertRaises(from_raw): s.set_formatted(b'1.23', b'nominal', raw) else: s.set_formatted(b'1.23', b'nominal', raw) self.assertEqual(s.value(), from_raw)