From f9a89742b9e10e7cbc59855f5148de425b213cf7 Mon Sep 17 00:00:00 2001 From: konstantin Date: Thu, 4 Jan 2024 09:15:41 +0000 Subject: [PATCH 1/3] feat: Add `__eq__` implementation to class Point --- influxdb_client/client/write/point.py | 12 +++ tests/test_point.py | 104 ++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) 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() From 63435b70f97c4aacaf49738c4cc3124db50bc7ca Mon Sep 17 00:00:00 2001 From: konstantin Date: Thu, 4 Jan 2024 09:19:18 +0000 Subject: [PATCH 2/3] docs: Add Changelog Entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0e4ebc1..8b90c30b 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 + ## 1.39.0 [2023-12-05] ### Features From d32a35a534898b657259cc2ce66e26666c084040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Thu, 4 Jan 2024 10:26:14 +0100 Subject: [PATCH 3/3] docs: Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b0f4d12..2f2cb4df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## 1.40.0 [unreleased] ### Features -1. [#625](https://github.com/influxdata/influxdb-client-python/pull/625) Make class `Point` equatable +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