From bf50e243822abe43c00d58d1b46cfec75b115c13 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Wed, 3 Jan 2024 13:58:00 +0800 Subject: [PATCH] Backend.Tests: 2nd acceptance test for bug 242 --- src/GWallet.Backend.Tests/Deserialization.fs | 78 ++++++++++++++++++++ src/GWallet.Backend.Tests/MarshallingData.fs | 11 ++- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/src/GWallet.Backend.Tests/Deserialization.fs b/src/GWallet.Backend.Tests/Deserialization.fs index 1d7a1f535..664e2274d 100644 --- a/src/GWallet.Backend.Tests/Deserialization.fs +++ b/src/GWallet.Backend.Tests/Deserialization.fs @@ -256,3 +256,81 @@ type Deserialization() = let serialized = Marshalling.Serialize c let deserialized = Marshalling.Deserialize serialized Assert.That(c, Is.EqualTo deserialized) + + [] + member __.``can deserialize from lower version``() = + let currentVersion = MarshallingData.Version |> Version + if currentVersion.Revision > 0 then + let revisionMinusOne = + Version( + sprintf "%i.%i.%i.%i" + currentVersion.Major + currentVersion.Minor + currentVersion.Build + (currentVersion.Revision - 1) + ).ToString() + let signedTxWithNewVersion = + MarshallingData.ReplaceVersion MarshallingData.SignedBtcTransactionExampleInJson revisionMinusOne + let deserializedBtcTransaction: SignedTransaction = + Marshalling.Deserialize signedTxWithNewVersion + Assert.That(deserializedBtcTransaction, Is.Not.Null) + Assert.That(deserializedBtcTransaction.TransactionInfo.Proposal.Amount.ValueToSend, + Is.EqualTo 10.01m) + () + + if currentVersion.Build <= 0 then + Assert.Fail "Version's build (the 'c' in a.b.c.d) should be higher than zero" + + let buildMinusOne = + Version( + sprintf "%i.%i.%i.%i" + currentVersion.Major + currentVersion.Minor + (currentVersion.Build - 1) + currentVersion.Revision + ).ToString() + let signedTxWithNewVersion = + MarshallingData.ReplaceVersion MarshallingData.SignedBtcTransactionExampleInJson buildMinusOne + let deserializedBtcTransaction: SignedTransaction = + Marshalling.Deserialize signedTxWithNewVersion + Assert.That(deserializedBtcTransaction, Is.Not.Null) + Assert.That(deserializedBtcTransaction.TransactionInfo.Proposal.Amount.ValueToSend, + Is.EqualTo 10.01m) + + if currentVersion.Minor <= 0 then + Assert.Fail "Version's minor (the 'b' in a.b.c.d) should be higher than zero" + let minorMinusOne = + Version( + sprintf "%i.%i.%i.%i" + currentVersion.Major + (currentVersion.Minor - 1) + currentVersion.Build + currentVersion.Revision + ).ToString() + let signedTxWithNewVersion = + MarshallingData.ReplaceVersion MarshallingData.SignedBtcTransactionExampleInJson minorMinusOne + let deserializedBtcTransaction: SignedTransaction = + Marshalling.Deserialize signedTxWithNewVersion + Assert.That(deserializedBtcTransaction, Is.Not.Null) + Assert.That(deserializedBtcTransaction.TransactionInfo.Proposal.Amount.ValueToSend, + Is.EqualTo 10.01m) + + if currentVersion.Major <= 0 then + Assert.Fail "Version's major (the 'a' in a.b.c.d) should be higher than zero" + let majorMinusOne = + Version( + sprintf "%i.%i.%i.%i" + (currentVersion.Major - 1) + currentVersion.Minor + currentVersion.Build + currentVersion.Revision + ).ToString() + let signedTxWithNewVersion = + MarshallingData.ReplaceVersion MarshallingData.SignedBtcTransactionExampleInJson majorMinusOne + let deserializedBtcTransaction: SignedTransaction = + Marshalling.Deserialize signedTxWithNewVersion + Assert.That(deserializedBtcTransaction, Is.Not.Null) + Assert.That(deserializedBtcTransaction.TransactionInfo.Proposal.Amount.ValueToSend, + Is.EqualTo 10.01m) + () + diff --git a/src/GWallet.Backend.Tests/MarshallingData.fs b/src/GWallet.Backend.Tests/MarshallingData.fs index 90e1e6a37..fa9f7eb01 100644 --- a/src/GWallet.Backend.Tests/MarshallingData.fs +++ b/src/GWallet.Backend.Tests/MarshallingData.fs @@ -14,7 +14,7 @@ open GWallet.Backend.Ether module MarshallingData = let private executingAssembly = Assembly.GetExecutingAssembly() - let private version = VersionHelper.CURRENT_VERSION + let internal Version = VersionHelper.CURRENT_VERSION let private binPath = executingAssembly.Location |> FileInfo let private prjPath = Path.Combine(binPath.Directory.FullName, "..") |> DirectoryInfo @@ -24,7 +24,10 @@ module MarshallingData = .Replace("\t", String.Empty) let private InjectCurrentVersion (jsonContent: string): string = - jsonContent.Replace("{version}", version) + jsonContent.Replace("{version}", Version) + + let internal ReplaceVersion (jsonContent: string) (newVersion: string) = + jsonContent.Replace(Version, newVersion) let private InjectCurrentDir (jsonContent: string): string = jsonContent.Replace("{prjDirAbsolutePath}", prjPath.FullName.Replace("\\", "/")) @@ -167,7 +170,7 @@ module MarshallingData = "Addresses": {}, "Balances": {} } -}""" version (EmptyCachingDataExample.GetType().FullName) +}""" Version (EmptyCachingDataExample.GetType().FullName) let private balances = Map.empty.Add(Currency.BTC.ToString(), 0m) .Add(Currency.ETC.ToString(), 123456789.12345678m) @@ -199,7 +202,7 @@ module MarshallingData = "ETC": 123456789.12345678 } } -}""" version (typedefof.FullName) +}""" Version (typedefof.FullName) let private someUnsignedBtcTransactionProposal = {