diff --git a/CHANGELOG.md b/CHANGELOG.md index 23f7dc23..2f2cb4df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## 1.40.0 [unreleased] +### Features +1. [#625](https://github.com/influxdata/influxdb-client-python/pull/625): Make class `Point` equatable + ### Bug Fixes 1. [#562](https://github.com/influxdata/influxdb-client-python/pull/562): Use `ThreadPoolScheduler` for `WriteApi`'s batch subject instead of `TimeoutScheduler` to prevent creating unnecessary threads repeatedly diff --git a/influxdb_client/client/write/point.py b/influxdb_client/client/write/point.py index 60ce7c40..31d44d5c 100644 --- a/influxdb_client/client/write/point.py +++ b/influxdb_client/client/write/point.py @@ -251,6 +251,18 @@ def __str__(self): """Create string representation of this Point.""" return self.to_line_protocol() + def __eq__(self, other): + """Return true iff other is equal to self.""" + if not isinstance(other, Point): + return False + # assume points are equal iff their instance fields are equal + return (self._tags == other._tags and + self._fields == other._fields and + self._name == other._name and + self._time == other._time and + self._write_precision == other._write_precision and + self._field_types == other._field_types) + def _append_tags(tags): _return = [] diff --git a/tests/test_point.py b/tests/test_point.py index 992ac354..e799ae9c 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -557,6 +557,110 @@ def test_name_start_with_hash(self): self.assertEqual('#hash_start,location=europe level=2.2', point.to_line_protocol()) self.assertEqual(1, len(warnings)) + def test_equality_from_dict(self): + point_dict = { + "measurement": "h2o_feet", + "tags": {"location": "coyote_creek"}, + "fields": { + "water_level": 1.0, + "some_counter": 108913123234 + }, + "field_types": {"some_counter": "float"}, + "time": 1 + } + point_a = Point.from_dict(point_dict) + point_b = Point.from_dict(point_dict) + self.assertEqual(point_a, point_b) + + def test_equality(self): + # https://github.com/influxdata/influxdb-client-python/issues/623#issue-2048573579 + point_a = ( + Point("asd") + .tag("foo", "bar") + .field("value", 123.45) + .time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + + point_b = ( + Point("asd") + .tag("foo", "bar") + .field("value", 123.45) + .time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + self.assertEqual(point_a, point_b) + + def test_not_equal_if_tags_differ(self): + point_a = ( + Point("asd") + .tag("foo", "bar") + .field("value", 123.45) + .time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + + point_b = ( + Point("asd") + .tag("foo", "baz") # not "bar" + .field("value", 123.45) + .time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + self.assertNotEqual(point_a, point_b) + + def test_not_equal_if_fields_differ(self): + point_a = ( + Point("asd") + .tag("foo", "bar") + .field("value", 123.45) + .time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + + point_b = ( + Point("asd") + .tag("foo", "bar") + .field("value", 678.90) # not 123.45 + .time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + self.assertNotEqual(point_a, point_b) + + def test_not_equal_if_measurements_differ(self): + point_a = ( + Point("asd") + .tag("foo", "bar") + .field("value", 123.45) + .time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + + point_b = ( + Point("fgh") # not "asd" + .tag("foo", "bar") + .field("value", 123.45) + .time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + self.assertNotEqual(point_a, point_b) + + def test_not_equal_if_times_differ(self): + point_a = ( + Point("asd") + .tag("foo", "bar") + .field("value", 123.45) + .time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + + point_b = ( + Point("asd") + .tag("foo", "bar") + .field("value", 123.45) + .time(datetime(2024, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + self.assertNotEqual(point_a, point_b) + def test_not_equal_if_other_is_no_point(self): + point_a = ( + Point("asd") + .tag("foo", "bar") + .field("value", 123.45) + .time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc)) + ) + not_a_point = "not a point but a string" + self.assertNotEqual(point_a, not_a_point) if __name__ == '__main__': unittest.main()