Skip to content

Commit

Permalink
Fix json tests ✔️
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Apr 3, 2024
1 parent d893131 commit b84919f
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 66 deletions.
17 changes: 12 additions & 5 deletions src/Core/OntologyAnnotation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,18 @@ type OntologyAnnotation(?name,?tsr,?tan, ?comments) =
override this.GetHashCode() =
[|
HashCodes.boxHashOption this.Name
HashCodes.boxHashOption this.TermSourceREF
if this.TANInfo.IsSome then
HashCodes.hash this.TANInfo
else
HashCodes.boxHashOption this.TermAccessionNumber
match this.TermSourceREF, this.TANInfo with
| None, Some taninfo -> // if we get taninfo we assume tsr to be inferrable by taninfo
HashCodes.hash {|tsr = taninfo.IDSpace; tan = taninfo.IDSpace + ":" + taninfo.LocalID|}
| Some tsr, Some taninfo -> // if we get taninfo + tsr we do NOT override tsr
HashCodes.hash {|tsr = tsr; tan = taninfo.IDSpace + ":" + taninfo.LocalID|}
| Some tsr, None ->
let tan = this.TermAccessionNumber |> Option.defaultValue ""
HashCodes.hash {|tsr = tsr; tan = tan|}
| None, None ->
let tan = this.TermAccessionNumber |> Option.defaultValue ""
let tsr = this.TermAccessionNumber |> Option.defaultValue ""
HashCodes.hash {|tsr = tsr; tan = tan|}
//HashCodes.boxHashSeq this.Comments
|]
|> HashCodes.boxHashArray
Expand Down
9 changes: 7 additions & 2 deletions tests/Core/OntologyAnnotation.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,19 @@ let private tests_equals = testList "Equals" [
let oa1 = OntologyAnnotation("instrument model", "MS", "MS:00000001", ResizeArray [Comment("KeyName", "Value1")])
let oa2 = OntologyAnnotation("instrument model", "MS", "MS:00000001")
Expect.equal oa1 oa2 ""
testCase "TANInfo1" <| fun _ ->
testCase "TANInfo" <| fun _ ->
let oa1 = OntologyAnnotation("instrument model", "MS", "http://purl.obolibrary.org/obo/MS_00000001", ResizeArray [Comment("KeyName", "Value1")])
let oa2 = OntologyAnnotation("instrument model", "MS", "MS:00000001", ResizeArray [Comment("KeyName", "Value1")])
Expect.equal oa1 oa2 ""
testCase "TANInfo, different tsr" <| fun _ ->
testCase "TANInfo, missing tsr" <| fun _ ->
let oa1 = OntologyAnnotation("instrument model", "MS", "http://purl.obolibrary.org/obo/MS_00000001", ResizeArray [Comment("KeyName", "Value1")])
let oa2 = OntologyAnnotation("instrument model", tan= "MS:00000001", comments=ResizeArray [Comment("KeyName", "Value1")])
Expect.equal oa1 oa2 "This should not fail"
testCase "TANInfo, different tsr" <| fun _ ->
let oa1 = OntologyAnnotation("instrument model", "MS", "http://purl.obolibrary.org/obo/MS_00000001", ResizeArray [Comment("KeyName", "Value1")])
let oa2 = OntologyAnnotation("instrument model", "MSS", tan= "MS:00000001", comments=ResizeArray [Comment("KeyName", "Value1")])
Expect.notEqual oa1 oa2 ""
Expect.isFalse (oa1 = oa2) ""
testCase "not equal" <| fun _ ->
let oa1 = OntologyAnnotation("instrument model", "MS", "http://purl.obolibrary.org/obo/MS_00000001", ResizeArray [Comment("KeyName", "Value1")])
let oa2 = OntologyAnnotation(tan= "MS:00000001", comments=ResizeArray [Comment("KeyName", "Value1")])
Expand Down
2 changes: 1 addition & 1 deletion tests/Json/ARCtrl.Json.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<Compile Include="ArcTable.Tests.fs" />
<Compile Include="Person.Tests.fs" />
<Compile Include="Publication.Tests.fs" />
<Compile Include="Assay.Tests..fs" />
<Compile Include="Assay.Tests.fs" />
<Compile Include="Study.Tests.fs" />
<Compile Include="Investigation.Tests.fs" />
<Compile Include="Process\ProcessInput.Tests.fs" />
Expand Down
4 changes: 4 additions & 0 deletions tests/Json/ArcTable.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ let private tests_core =
ArcTable.toJsonString
ArcTable.fromJsonString
None
None

let private tests_coreEmpty =
let json = """{"name":"New Table"}"""
Expand All @@ -43,6 +44,7 @@ let private tests_coreEmpty =
ArcTable.toJsonString
ArcTable.fromJsonString
None
None

let private tests_compressedEmpty =
let json = """{"cellTable":[],"oaTable":[],"stringTable":["New Table"],"table":{"n":0}}"""
Expand All @@ -52,6 +54,7 @@ let private tests_compressedEmpty =
ArcTable.toCompressedJsonString
ArcTable.fromCompressedJsonString
None
None

let private tests_compressedFilled =
let json = """{"cellTable":[{"t":2,"v":[1]},{"t":2,"v":[3]},{"t":4,"v":[0]},{"t":2,"v":[5]},{"t":2,"v":[6]}],"oaTable":[{"a":7}],"stringTable":["New Table","Input 1","FreeText","Input 2","Term","Output 1","Output 2","SCIEX instrument model"],"table":{"n":0,"h":[{"headertype":"Input","values":["Source Name"]},{"headertype":"Component","values":[{"annotationValue":"instrument model","termSource":"MS","termAccession":"MS:424242"}]},{"headertype":"Output","values":["Sample Name"]}],"c":[[0,1],[2,2],[3,4]]}}"""
Expand All @@ -61,6 +64,7 @@ let private tests_compressedFilled =
ArcTable.toCompressedJsonString
ArcTable.fromCompressedJsonString
None
None

let private tests = testList "extended" [
testList "compressed" [
Expand Down
19 changes: 19 additions & 0 deletions tests/Json/Assay.Tests..fs → tests/Json/Assay.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ module Helper =
ResizeArray [|Comment.create("Hello", "World")|]
)

let compare =
fun (a1: ArcAssay) (a2: ArcAssay) ->
Expect.equal a1.Identifier a2.Identifier "Identifier"
Expect.equal a1.MeasurementType a2.MeasurementType "MeasurementType"
Expect.equal a1.TechnologyType a2.TechnologyType "TechnologyType"
Expect.equal a1.TechnologyPlatform a2.TechnologyPlatform "TechnologyPlatform"
Expect.sequenceEqual a1.Tables a2.Tables "Tables"
Expect.sequenceEqual a1.Performers a2.Performers "Performers"
Expect.sequenceEqual a1.Comments a2.Comments "Comments"
|> Some

open Helper

let private test_coreEmpty =
Expand All @@ -26,6 +37,7 @@ let private test_coreEmpty =
ArcAssay.toJsonString
ArcAssay.fromJsonString
None
compare

let private test_compressedEmpty =
createBaseJsonTests
Expand All @@ -34,6 +46,7 @@ let private test_compressedEmpty =
ArcAssay.toCompressedJsonString
ArcAssay.fromCompressedJsonString
None
compare

let private test_isaEmpty =
createBaseJsonTests
Expand All @@ -46,6 +59,7 @@ let private test_isaEmpty =
#else
None
#endif
compare

let private test_roCrateEmpty =
createBaseJsonTests
Expand All @@ -54,6 +68,7 @@ let private test_roCrateEmpty =
(fun () -> ArcAssay.toROCrateJsonString None)
ArcAssay.fromROCrateJsonString
None
compare

let private test_core =
createBaseJsonTests
Expand All @@ -62,6 +77,7 @@ let private test_core =
ArcAssay.toJsonString
ArcAssay.fromJsonString
None
compare

let private test_compressed =
createBaseJsonTests
Expand All @@ -70,6 +86,7 @@ let private test_compressed =
ArcAssay.toCompressedJsonString
ArcAssay.fromCompressedJsonString
None
compare

open TestObjects.Json

Expand All @@ -87,6 +104,7 @@ let private test_isa =
#else
None
#endif
compare

let private test_roCrate = testList "ROCrate" [
// Wait for some issues to be resolved: https://github.com/nfdi4plants/isa-ro-crate-profile/issues
Expand All @@ -101,6 +119,7 @@ let private test_roCrate = testList "ROCrate" [
(fun () -> ArcAssay.toROCrateJsonString None)
ArcAssay.fromROCrateJsonString
None
compare
]

let main = testList "Assay" [
Expand Down
25 changes: 17 additions & 8 deletions tests/Json/BaseJsonTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ open ARCtrl.Json



let createBaseJsonTests (name:string) (createObj: unit -> 'A) (toJson: unit -> 'A -> string) (fromJson: string -> 'A) (schemaValidation : Validation.ValidateFunction option) =
testList $"baseTests - {name}" [
let createBaseJsonTests (name:string) (createObj: unit -> 'A) (toJson: unit -> 'A -> string) (fromJson: string -> 'A) (schemaValidation : Validation.ValidateFunction option) (detailedCompareFunc: option<'A -> 'A -> unit>) =
let name = if name = "" then "baseTests" else $"baseTests - {name}"
testList name [

if schemaValidation.IsSome then
testAsync "WriterSchemaCorrectness" {
if detailedCompareFunc.IsSome then
testCase "detailed" <| fun _ ->
let obj = createObj()
let json = toJson () obj
let! validation = schemaValidation.Value json
let errorList = validation.GetErrors() |> Array.fold (fun acc x -> acc + x + "\n") ""
Expect.isTrue validation.Success $"Object did not match schema: {errorList}"
}
let actual = fromJson json
let expected = createObj()
detailedCompareFunc.Value actual expected

testCase "roundabout" <| fun _ ->
let obj = createObj()
let json = toJson () obj
let actual = fromJson json
let expected = createObj()
Expect.equal actual expected ""

if schemaValidation.IsSome then
testAsync "WriterSchemaCorrectness" {
let obj = createObj()
let json = toJson () obj
let! validation = schemaValidation.Value json
let errorList = validation.GetErrors() |> Array.fold (fun acc x -> acc + x + "\n") ""
Expect.isTrue validation.Success $"Object did not match schema: {errorList}"
}
]
39 changes: 24 additions & 15 deletions tests/Json/Investigation.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,22 @@ module Helper =
inv.AddAssay(assay2, inv.Studies)
inv

let compareFields (expected: ArcInvestigation) (actual: ArcInvestigation) =
Expect.equal actual.Identifier expected.Identifier "Identifier"
Expect.equal actual.Title expected.Title "Title"
Expect.equal actual.Description expected.Description "Description"
Expect.equal actual.SubmissionDate expected.SubmissionDate "SubmissionDate"
Expect.equal actual.PublicReleaseDate expected.PublicReleaseDate "PublicReleaseDate"
Expect.sequenceEqual actual.OntologySourceReferences expected.OntologySourceReferences "OntologySourceReferences"
Expect.sequenceEqual actual.Publications expected.Publications "Publications"
Expect.sequenceEqual actual.Contacts expected.Contacts "Contacts"
Expect.sequenceEqual actual.Assays expected.Assays "Assays"
Expect.sequenceEqual actual.Studies expected.Studies "Studies"
Expect.sequenceEqual actual.RegisteredStudyIdentifiers expected.RegisteredStudyIdentifiers "RegisteredStudyIdentifiers"
Expect.sequenceEqual actual.Comments expected.Comments "Comments"
Expect.sequenceEqual actual.Remarks expected.Remarks "Remarks"
let compareFields =
fun (actual: ArcInvestigation) (expected: ArcInvestigation) ->
Expect.equal actual.Identifier expected.Identifier "Identifier"
Expect.equal actual.Title expected.Title "Title"
Expect.equal actual.Description expected.Description "Description"
Expect.equal actual.SubmissionDate expected.SubmissionDate "SubmissionDate"
Expect.equal actual.PublicReleaseDate expected.PublicReleaseDate "PublicReleaseDate"
Expect.sequenceEqual actual.OntologySourceReferences expected.OntologySourceReferences "OntologySourceReferences"
Expect.sequenceEqual actual.Publications expected.Publications "Publications"
Expect.sequenceEqual actual.Contacts expected.Contacts "Contacts"
Expect.sequenceEqual actual.Assays expected.Assays "Assays"
Expect.sequenceEqual actual.Studies expected.Studies "Studies"
Expect.sequenceEqual actual.RegisteredStudyIdentifiers expected.RegisteredStudyIdentifiers "RegisteredStudyIdentifiers"
Expect.sequenceEqual actual.Comments expected.Comments "Comments"
Expect.sequenceEqual actual.Remarks expected.Remarks "Remarks"
|> Some

open Helper

Expand All @@ -61,6 +63,7 @@ let private test_coreEmpty =
ArcInvestigation.toJsonString
ArcInvestigation.fromJsonString
None
compareFields

let private test_compressedEmpty =
createBaseJsonTests
Expand All @@ -69,6 +72,7 @@ let private test_compressedEmpty =
ArcInvestigation.toCompressedJsonString
ArcInvestigation.fromCompressedJsonString
None
compareFields

let private test_isaEmpty =
createBaseJsonTests
Expand All @@ -81,6 +85,7 @@ let private test_isaEmpty =
#else
None
#endif
compareFields

let private test_roCrateEmpty =
createBaseJsonTests
Expand All @@ -89,7 +94,7 @@ let private test_roCrateEmpty =
ArcInvestigation.toROCrateJsonString
ArcInvestigation.fromROCrateJsonString
None

compareFields

let private test_core =
createBaseJsonTests
Expand All @@ -98,6 +103,7 @@ let private test_core =
ArcInvestigation.toJsonString
ArcInvestigation.fromJsonString
None
compareFields

let private test_compressed =

Expand All @@ -107,6 +113,7 @@ let private test_compressed =
ArcInvestigation.toCompressedJsonString
ArcInvestigation.fromCompressedJsonString
None
compareFields

let private test_isa =

Expand All @@ -120,6 +127,7 @@ let private test_isa =
#else
None
#endif
compareFields

let private test_roCrate =
createBaseJsonTests
Expand All @@ -128,6 +136,7 @@ let private test_roCrate =
ArcInvestigation.toROCrateJsonString
ArcInvestigation.fromROCrateJsonString
None
compareFields

let main = testList "Investigation" [
test_coreEmpty
Expand Down
4 changes: 3 additions & 1 deletion tests/Json/OntologyAnnotation.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ let private tests_core =
OntologyAnnotation.toJsonString
OntologyAnnotation.fromJsonString
None
None

let private tests_isa =
createBaseJsonTests
"isa"
create_oa

OntologyAnnotation.toISAJsonString
OntologyAnnotation.fromISAJsonString
#if !FABLE_COMPILER_PYTHON
(Some Validation.validateOntologyAnnotation)
#else
None
#endif
None

let private tests_roCrate = testList "ROCrate" [
testCase "Write-PropertyValue" <| fun _ ->
Expand All @@ -50,6 +51,7 @@ let private tests_roCrate = testList "ROCrate" [
OntologyAnnotation.toROCrateJsonString
OntologyAnnotation.fromROCrateJsonString
None
None
]

let main = testList "OntologyAnnotation" [
Expand Down
3 changes: 3 additions & 0 deletions tests/Json/Person.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let private tests_isa = testList "ISA" [
Person.toISAJsonString
Person.fromISAJsonString
None
None
testCase "ReaderSuccess" (fun () ->
let readingSuccess =
try
Expand Down Expand Up @@ -106,6 +107,7 @@ let private tests_rocrate = testList "ROCrate" [
Person.toROCrateJsonString
Person.fromROCrateJsonString
None
None
]

let tests = testList "core" [
Expand All @@ -115,6 +117,7 @@ let tests = testList "core" [
Person.toJsonString
Person.fromJsonString
None
None
]

let main = testList "Person" [
Expand Down
Loading

0 comments on commit b84919f

Please sign in to comment.