Skip to content

Commit

Permalink
Fix #1131 Add the method to check equality in Block Kit model classes (
Browse files Browse the repository at this point in the history
…#1137)

Co-authored-by: Kazuhiro Sera <seratch@gmail.com>
  • Loading branch information
horn553 and seratch authored Nov 24, 2021
1 parent 6c46f32 commit 07d1dbf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions slack_sdk/models/basic_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ def __repr__(self):
else:
return self.__str__()

def __eq__(self, other: Any) -> bool:
if not isinstance(other, JsonObject):
return False
return self.to_dict() == other.to_dict()


class JsonValidator:
def __init__(self, message: str):
Expand Down
5 changes: 5 additions & 0 deletions tests/slack_sdk/models/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def test_parse(self):
block.to_dict(),
)

def test_eq(self):
self.assertEqual(Block(), Block())
self.assertEqual(Block(type="test"), Block(type="test"))
self.assertNotEqual(Block(type="test"), Block(type="another test"))


# ----------------------------------------------
# Section
Expand Down
10 changes: 10 additions & 0 deletions tests/slack_sdk/models/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from slack_sdk.errors import SlackObjectFormationError
from slack_sdk.models.blocks import (
BlockElement,
ButtonElement,
DatePickerElement,
TimePickerElement,
Expand Down Expand Up @@ -29,6 +30,15 @@
from . import STRING_3001_CHARS, STRING_301_CHARS


class BlockElementTests(unittest.TestCase):
def test_eq(self):
self.assertEqual(BlockElement(), BlockElement())
self.assertEqual(BlockElement(type="test"), BlockElement(type="test"))
self.assertNotEqual(
BlockElement(type="test"), BlockElement(type="another test")
)


# -------------------------------------------------
# Interactive Elements
# -------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions tests/slack_sdk/models/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ def test_get_non_null_attributes_nested_2(self):
)
self.assertDictEqual(expected, nested.get_non_null_attributes())

def test_eq(self):
obj1 = SimpleJsonObject()
self.assertEqual(self.good_test_object, obj1)

obj2 = SimpleJsonObject()
obj2.test = "another"
self.assertNotEqual(self.good_test_object, obj2)


class JsonValidatorTests(unittest.TestCase):
def setUp(self) -> None:
Expand Down
12 changes: 12 additions & 0 deletions tests/slack_sdk/models/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,15 @@ def test_load_home_tab_view_005(self):
def test_load_home_tab_view_006(self):
with open("tests/slack_sdk_fixture/view_home_006.json") as file:
self.verify_loaded_view_object(file)

def test_eq(self):
input = {
"type": "modal",
"blocks": [DividerBlock()],
}
another_input = {
"type": "modal",
"blocks": [DividerBlock(), DividerBlock()],
}
self.assertEqual(View(**input), View(**input))
self.assertNotEqual(View(**input), View(**another_input))

0 comments on commit 07d1dbf

Please sign in to comment.