Skip to content

Commit

Permalink
feat: Add __eq__ implementation to class Point
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-kklein committed Jan 4, 2024
1 parent eb5afd1 commit f9a8974
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
12 changes: 12 additions & 0 deletions influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
104 changes: 104 additions & 0 deletions tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit f9a8974

Please sign in to comment.