Skip to content

Commit 4d9e4a5

Browse files
test(hbar): improve unit test coverage (#1483)
Signed-off-by: CODEAbhinav-art <abhinav15102003@gmail.com>
1 parent d32f14a commit 4d9e4a5

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
66

77
## [Unreleased]
88

9+
### Tests
10+
- Improve unit test coverage for `Hbar`, including edge cases, validation, comparisons, and hashing. (#1483)
11+
912
### Added
13+
1014
- Advanced-check bot unassigns users from issues if they do not meet the requirements and provides an explanatory message. (#1477)
1115
- Auto-assignment bot for beginner-labeled issues with `/assign` command support and helpful reminders. (#1368)
1216
- Added comprehensive docstring to `FeeAssessmentMethod` enum explaining inclusive vs exclusive fee assessment methods with usage examples. (#1391)

tests/unit/hbar_test.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,74 @@ def test_factory_methods():
200200
assert Hbar.from_gigabars(1).to_hbars() == 1_000_000_000
201201
assert Hbar.from_gigabars(0).to_hbars() == 0
202202
assert Hbar.from_gigabars(-1).to_hbars() == -1_000_000_000
203-
assert Hbar.from_gigabars(1).to_tinybars() == 100_000_000_000_000_000
203+
assert Hbar.from_gigabars(1).to_tinybars() == 100_000_000_000_000_000
204+
205+
206+
# NEW TESTS: Coverage improvements for issue #1447
207+
# ---------------------------------------------------------------------------
208+
209+
def test_from_tinybars_rejects_non_int():
210+
"""from_tinybars() should reject non-integer input."""
211+
with pytest.raises(TypeError, match="tinybars must be an int"):
212+
Hbar.from_tinybars(1.5)
213+
214+
with pytest.raises(TypeError, match="tinybars must be an int"):
215+
Hbar.from_tinybars("1000")
216+
217+
with pytest.raises(TypeError, match="tinybars must be an int"):
218+
Hbar.from_tinybars(Decimal("1000"))
219+
220+
221+
222+
@pytest.mark.parametrize("other", [1, 1.0, "1", None])
223+
def test_comparison_with_non_hbar_raises_type_error(other):
224+
"""Ordering comparisons with non-Hbar types should raise TypeError."""
225+
h = Hbar(1)
226+
227+
with pytest.raises(TypeError):
228+
_ = h < other
229+
230+
with pytest.raises(TypeError):
231+
_ = h > other
232+
233+
with pytest.raises(TypeError):
234+
_ = h <= other
235+
236+
with pytest.raises(TypeError):
237+
_ = h >= other
238+
239+
240+
241+
def test_str_formatting_and_negatives():
242+
"""String representation should use fixed 8 decimal places."""
243+
assert str(Hbar(1)) == "1.00000000 ℏ"
244+
assert str(Hbar(-1)) == "-1.00000000 ℏ"
245+
246+
247+
def test_repr_contains_class_name_and_value():
248+
"""repr() should include class name and value."""
249+
h = Hbar(2)
250+
r = repr(h)
251+
252+
assert r.startswith("Hbar(")
253+
assert "2" in r
254+
255+
256+
257+
def test_hash_consistency_for_equal_values():
258+
"""Equal Hbar values must have identical hashes."""
259+
h1 = Hbar(1)
260+
h2 = Hbar(1)
261+
262+
assert h1 == h2
263+
assert hash(h1) == hash(h2)
264+
265+
values = {h1, h2}
266+
assert len(values) == 1
267+
268+
d = {h1: "value1"}
269+
d[h2] = "value2"
270+
assert len(d) == 1
271+
assert d[h1] == "value2"
272+
273+

0 commit comments

Comments
 (0)