Skip to content

Commit

Permalink
Backend.Tests: 2nd acceptance test for bug 242
Browse files Browse the repository at this point in the history
  • Loading branch information
knocte committed Jan 3, 2024
1 parent 261ec5a commit c62dcef
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
82 changes: 82 additions & 0 deletions src/GWallet.Backend.Tests/Deserialization.fs
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,85 @@ type Deserialization() =
let serialized = Marshalling.Serialize c
let deserialized = Marshalling.Deserialize<Currency> serialized
Assert.That(c, Is.EqualTo deserialized)

[<Test>]
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<UtxoCoin.TransactionMetadata> =
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<UtxoCoin.TransactionMetadata> =
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<UtxoCoin.TransactionMetadata> =
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(
sprintf "Version's major (the 'a' in a.b.c.d) should be equal or higher than zero (now was %i, from %s)"
currentVersion.Major
(currentVersion.ToString())
)
let majorPlusOne =
Version(
sprintf "%i.%i.%i.%i"
(currentVersion.Major + 1)
currentVersion.Minor
currentVersion.Build
currentVersion.Revision
).ToString()
let signedTxWithNewVersion =
MarshallingData.ReplaceVersion MarshallingData.SignedBtcTransactionExampleInJson majorPlusOne
let deserializedBtcTransaction: SignedTransaction<UtxoCoin.TransactionMetadata> =
Marshalling.Deserialize signedTxWithNewVersion
Assert.That(deserializedBtcTransaction, Is.Not.Null)
Assert.That(deserializedBtcTransaction.TransactionInfo.Proposal.Amount.ValueToSend,
Is.EqualTo 10.01m)
()

11 changes: 7 additions & 4 deletions src/GWallet.Backend.Tests/MarshallingData.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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("\\", "/"))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -199,7 +202,7 @@ module MarshallingData =
"ETC": 123456789.12345678
}
}
}""" version (typedefof<DietCache>.FullName)
}""" Version (typedefof<DietCache>.FullName)

let private someUnsignedBtcTransactionProposal =
{
Expand Down
7 changes: 5 additions & 2 deletions src/GWallet.Backend/Marshalling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,11 @@ module Marshalling =
| theType when typeof<Exception>.IsAssignableFrom theType ->
let marshalledException: MarshalledException = DeserializeCustom(json, DefaultSettings)
BinaryMarshalling.DeserializeFromString marshalledException.FullBinaryForm :?> 'T
| _ ->
DeserializeCustom(json, DefaultSettings)
| anotherType ->
let res = DeserializeCustom(json, DefaultSettings)
if Object.ReferenceEquals(res, null) then
failwith <| SPrintF2 "Deserialization to type '%s' failed (returned null): %s" anotherType.FullName json
res

let private SerializeInternal<'T>(value: 'T) (settings: JsonSerializerSettings) (formatting: Formatting): string =
JsonConvert.SerializeObject(MarshallingWrapper<'T>.New value,
Expand Down

0 comments on commit c62dcef

Please sign in to comment.