-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 40 additions & 12 deletions
52
tests/Equinox.Cosmos.Integration/VerbatimUtf8JsonConverterTests.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,61 @@ | ||
module Equinox.Cosmos.Integration.VerbatimUtf8JsonConverterTests | ||
|
||
open Equinox.Cosmos | ||
open FsCheck.Xunit | ||
open Newtonsoft.Json | ||
open Swensen.Unquote | ||
open System | ||
open Xunit | ||
|
||
let inline serialize (x:'t) = | ||
let serializer = new JsonSerializer() | ||
use sw = new System.IO.StringWriter() | ||
use w = new JsonTextWriter(sw) | ||
serializer.Serialize(w,x) | ||
sw.ToString() | ||
|
||
type Embedded = { embed : string } | ||
type Union = | ||
| A of Embedded | ||
| B of Embedded | ||
interface TypeShape.UnionContract.IUnionContract | ||
|
||
let mkUnionEncoder () = Equinox.UnionCodec.JsonUtf8.Create<Union>(JsonSerializerSettings()) | ||
|
||
[<Fact>] | ||
let ``VerbatimUtf8JsonConverter serializes properly`` () = | ||
let unionEncoder = Equinox.UnionCodec.JsonUtf8.Create<_>(JsonSerializerSettings()) | ||
let encoded = unionEncoder.Encode(A { embed = "\"" }) | ||
let ``VerbatimUtf8JsonConverter encodes correctly`` () = | ||
let encoded = mkUnionEncoder().Encode(A { embed = "\"" }) | ||
let e : Store.Event = | ||
{ p = "streamName"; id = string 0; i = 0L | ||
c = DateTimeOffset.MinValue | ||
t = encoded.caseName | ||
d = encoded.payload | ||
m = null } | ||
let res = serialize e | ||
test <@ res.Contains """"d":{"embed":"\""}""" @> | ||
let res = JsonConvert.SerializeObject(e) | ||
test <@ res.Contains """"d":{"embed":"\""}""" @> | ||
|
||
type Base64ZipUtf8JsonConverterTests() = | ||
let unionEncoder = mkUnionEncoder () | ||
|
||
[<Fact>] | ||
let ``serializes, achieving compression`` () = | ||
let encoded = unionEncoder.Encode(A { embed = String('x',5000) }) | ||
let e : Store.IndexProjection = | ||
{ t = encoded.caseName | ||
d = encoded.payload | ||
m = null } | ||
let res = JsonConvert.SerializeObject e | ||
test <@ res.Contains("\"d\":\"") && res.Length < 100 @> | ||
|
||
[<Property>] | ||
let roundtrips value = | ||
let hasNulls = | ||
match value with | ||
| A x | B x when obj.ReferenceEquals(null, x) -> true | ||
| A { embed = x } | B { embed = x } -> obj.ReferenceEquals(null, x) | ||
if hasNulls then () else | ||
|
||
let encoded = unionEncoder.Encode value | ||
let e : Store.IndexProjection = | ||
{ t = encoded.caseName | ||
d = encoded.payload | ||
m = null } | ||
let ser = JsonConvert.SerializeObject(e) | ||
test <@ ser.Contains("\"d\":\"") @> | ||
let des = JsonConvert.DeserializeObject<Store.IndexProjection>(ser) | ||
let d : Equinox.UnionCodec.EncodedUnion<_> = { caseName = des.t; payload=des.d } | ||
let decoded = unionEncoder.Decode d | ||
test <@ value = decoded @> |