Skip to content

Commit 024c1b1

Browse files
hf-kkleinbednar
andauthored
feat: Add __eq__ implementation to class Point (#625)
* feat: Add `__eq__` implementation to class Point * docs: Add Changelog Entry * docs: Update CHANGELOG.md --------- Co-authored-by: Jakub Bednář <[email protected]>
1 parent 8286f45 commit 024c1b1

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 1.40.0 [unreleased]
22

3+
### Features
4+
1. [#625](https://github.com/influxdata/influxdb-client-python/pull/625): Make class `Point` equatable
5+
36
### Bug Fixes
47
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
58

influxdb_client/client/write/point.py

+12
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ def __str__(self):
251251
"""Create string representation of this Point."""
252252
return self.to_line_protocol()
253253

254+
def __eq__(self, other):
255+
"""Return true iff other is equal to self."""
256+
if not isinstance(other, Point):
257+
return False
258+
# assume points are equal iff their instance fields are equal
259+
return (self._tags == other._tags and
260+
self._fields == other._fields and
261+
self._name == other._name and
262+
self._time == other._time and
263+
self._write_precision == other._write_precision and
264+
self._field_types == other._field_types)
265+
254266

255267
def _append_tags(tags):
256268
_return = []

tests/test_point.py

+104
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,110 @@ def test_name_start_with_hash(self):
557557
self.assertEqual('#hash_start,location=europe level=2.2', point.to_line_protocol())
558558
self.assertEqual(1, len(warnings))
559559

560+
def test_equality_from_dict(self):
561+
point_dict = {
562+
"measurement": "h2o_feet",
563+
"tags": {"location": "coyote_creek"},
564+
"fields": {
565+
"water_level": 1.0,
566+
"some_counter": 108913123234
567+
},
568+
"field_types": {"some_counter": "float"},
569+
"time": 1
570+
}
571+
point_a = Point.from_dict(point_dict)
572+
point_b = Point.from_dict(point_dict)
573+
self.assertEqual(point_a, point_b)
574+
575+
def test_equality(self):
576+
# https://github.com/influxdata/influxdb-client-python/issues/623#issue-2048573579
577+
point_a = (
578+
Point("asd")
579+
.tag("foo", "bar")
580+
.field("value", 123.45)
581+
.time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
582+
)
583+
584+
point_b = (
585+
Point("asd")
586+
.tag("foo", "bar")
587+
.field("value", 123.45)
588+
.time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
589+
)
590+
self.assertEqual(point_a, point_b)
591+
592+
def test_not_equal_if_tags_differ(self):
593+
point_a = (
594+
Point("asd")
595+
.tag("foo", "bar")
596+
.field("value", 123.45)
597+
.time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
598+
)
599+
600+
point_b = (
601+
Point("asd")
602+
.tag("foo", "baz") # not "bar"
603+
.field("value", 123.45)
604+
.time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
605+
)
606+
self.assertNotEqual(point_a, point_b)
607+
608+
def test_not_equal_if_fields_differ(self):
609+
point_a = (
610+
Point("asd")
611+
.tag("foo", "bar")
612+
.field("value", 123.45)
613+
.time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
614+
)
615+
616+
point_b = (
617+
Point("asd")
618+
.tag("foo", "bar")
619+
.field("value", 678.90) # not 123.45
620+
.time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
621+
)
622+
self.assertNotEqual(point_a, point_b)
623+
624+
def test_not_equal_if_measurements_differ(self):
625+
point_a = (
626+
Point("asd")
627+
.tag("foo", "bar")
628+
.field("value", 123.45)
629+
.time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
630+
)
631+
632+
point_b = (
633+
Point("fgh") # not "asd"
634+
.tag("foo", "bar")
635+
.field("value", 123.45)
636+
.time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
637+
)
638+
self.assertNotEqual(point_a, point_b)
639+
640+
def test_not_equal_if_times_differ(self):
641+
point_a = (
642+
Point("asd")
643+
.tag("foo", "bar")
644+
.field("value", 123.45)
645+
.time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
646+
)
647+
648+
point_b = (
649+
Point("asd")
650+
.tag("foo", "bar")
651+
.field("value", 123.45)
652+
.time(datetime(2024, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
653+
)
654+
self.assertNotEqual(point_a, point_b)
655+
def test_not_equal_if_other_is_no_point(self):
656+
point_a = (
657+
Point("asd")
658+
.tag("foo", "bar")
659+
.field("value", 123.45)
660+
.time(datetime(2023, 12, 19, 13, 27, 42, 215000, tzinfo=timezone.utc))
661+
)
662+
not_a_point = "not a point but a string"
663+
self.assertNotEqual(point_a, not_a_point)
560664

561665
if __name__ == '__main__':
562666
unittest.main()

0 commit comments

Comments
 (0)