Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add __eq__ implementation to class Point #625

Merged
merged 4 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
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()