From 0a01111395bb43a0a132310b1074711355ce87ac Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Sat, 12 Dec 2020 11:36:53 +0100 Subject: [PATCH] pytest: check millisatoshi by float raises This only adds a test that currently makes sure its not possible to init a Millisatoshi by a floating number. Discussion: As @gallizoltan points out, initialization with a float should be possible: https://github.com/ElementsProject/lightning/pull/4273#discussion_r540369093 > Millisatoshi(5) / 2 currently works, and removes the half msat. > So, I think Millisatoshi(5 / 2) should be the same. --- contrib/pyln-client/pyln/client/lightning.py | 4 ++++ contrib/pyln-client/tests/test_units.py | 23 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/contrib/pyln-client/pyln/client/lightning.py b/contrib/pyln-client/pyln/client/lightning.py index a4e7b7df3..22f559120 100644 --- a/contrib/pyln-client/pyln/client/lightning.py +++ b/contrib/pyln-client/pyln/client/lightning.py @@ -68,6 +68,10 @@ class Millisatoshi: elif int(v) == v: self.millisatoshis = int(v) + + elif isinstance(v, float): + raise TypeError("Millisatoshi by float is currently not supported") + else: raise TypeError( "Millisatoshi must be string with msat/sat/btc suffix or int" diff --git a/contrib/pyln-client/tests/test_units.py b/contrib/pyln-client/tests/test_units.py index 837b6e539..35571862c 100644 --- a/contrib/pyln-client/tests/test_units.py +++ b/contrib/pyln-client/tests/test_units.py @@ -312,3 +312,26 @@ def test_div(): assert amount == 14 amount = Millisatoshi(42) // Millisatoshi(4) assert amount == 10 + + +def test_init(): + # Note: Ongoing Discussion, hence the `with pytest.raises`. + # https://github.com/ElementsProject/lightning/pull/4273#discussion_r540369093 + # + # Initialization with a float should be possible: + # Millisatoshi(5) / 2 currently works, and removes the half msat. + # So Millisatoshi(5 / 2) should be the same. + amount = Millisatoshi(5) / 2 + assert amount == Millisatoshi(2) + with pytest.raises(TypeError, match="Millisatoshi by float is currently not supported"): + assert amount == Millisatoshi(5 / 2) + + ratio = Millisatoshi(8) / Millisatoshi(5) + assert isinstance(ratio, float) + with pytest.raises(TypeError, match="Millisatoshi by float is currently not supported"): + assert Millisatoshi(ratio) == Millisatoshi(8 / 5) + + # Check that init by a round float is allowed. + # Required by some existing tests: tests/test_wallet.py::test_txprepare + amount = Millisatoshi(42.0) + assert amount == 42