Skip to content

Commit

Permalink
add ROCrateObject decoder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Oct 18, 2024
1 parent 9f9b9c7 commit d9fbb33
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/Json/ARCtrl.Json.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<Compile Include="Process\Protocol.Tests.fs" />
<Compile Include="Process\Process.Tests.fs" />
<Compile Include="Template.Tests.fs" />
<Compile Include="ROCrateObject.Tests.fs" />
<Compile Include="JsonSchema.Tests.fs" />
<Compile Include="Main.fs" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions tests/Json/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let all = testSequenced <| testList "Json" [
Tests.Process.ProcessInput.main
Tests.Process.Protocol.main
Tests.Process.Process.main
Tests.ROCrateObject.main
Tests.SchemaValidation.main
]

Expand Down
82 changes: 82 additions & 0 deletions tests/Json/ROCrateObject.Tests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module Tests.ROCrateObject

open TestingUtils
open TestObjects.Json.ROCrate
open ARCtrl
open ARCtrl.ROCrate
open ARCtrl.Json
open DynamicObj

let private test_read = testList "Read" [
ftestCase "onlyIDAndType" <| fun _ ->
let json = ROCrateObject.fromROCrateJsonString(GenericObjects.onlyIDAndType)
Expect.equal json.Id "MyIdentifier" "id was not parsed correctly"
Expect.equal json.SchemaType "MyType" "type was not parsed correctly"
ftestCase "onlyID" <| fun _ ->
let f = fun _ -> ROCrateObject.fromROCrateJsonString(GenericObjects.onlyID) |> ignore
Expect.throws f "Should fail if Type is missing"
ftestCase "onlyType" <| fun _ ->
let f = fun _ -> ROCrateObject.fromROCrateJsonString(GenericObjects.onlyType) |> ignore
Expect.throws f "Should fail if ID is missing"
ftestCase "withStringFields" <| fun _ ->
let json = ROCrateObject.fromROCrateJsonString(GenericObjects.withStringFields)
Expect.equal json.Id "MyIdentifier" "id was not parsed correctly"
Expect.equal json.SchemaType "MyType" "type was not parsed correctly"
let name = Expect.wantSome (DynObj.tryGetTypedPropertyValue<string> "name" json) "field name was not parsed"
Expect.equal name "MyName" "field name was not parsed correctly"
let description = Expect.wantSome (DynObj.tryGetTypedPropertyValue<string> "description" json) "field description was not parsed"
Expect.equal description "MyDescription" "field description was not parsed correctly"
ftestCase "withIntFields" <| fun _ ->
let json = ROCrateObject.fromROCrateJsonString(GenericObjects.withIntFields)
Expect.equal json.Id "MyIdentifier" "id was not parsed correctly"
Expect.equal json.SchemaType "MyType" "type was not parsed correctly"
let number = Expect.wantSome (DynObj.tryGetTypedPropertyValue<int> "number" json) "field number was not parsed"
Expect.equal number 42 "field number was not parsed correctly"
let anotherNumber = Expect.wantSome (DynObj.tryGetTypedPropertyValue<int> "anotherNumber" json) "field anotherNumber was not parsed"
Expect.equal anotherNumber 1337 "field anotherNumber was not parsed correctly"
ftestCase "withStringArray" <| fun _ ->
let json = ROCrateObject.fromROCrateJsonString(GenericObjects.withStringArray)
Expect.equal json.Id "MyIdentifier" "id was not parsed correctly"
Expect.equal json.SchemaType "MyType" "type was not parsed correctly"
let names = Expect.wantSome (DynObj.tryGetTypedPropertyValue<ResizeArray<obj>> "names" json) "field names was not parsed"
Expect.equal names.Count 2 "ResizeArray length is wrong"
Expect.equal names.[0] "MyName" "First name was not parsed correctly"
Expect.equal names.[1] "MySecondName" "Second name was not parsed correctly"
ftestCase "withNestedObject" <| fun _ ->
let json = ROCrateObject.fromROCrateJsonString(GenericObjects.withNestedObject)
Expect.equal json.Id "OuterIdentifier" "id was not parsed correctly"
Expect.equal json.SchemaType "MyType" "type was not parsed correctly"
let nested = Expect.wantSome (DynObj.tryGetTypedPropertyValue<ROCrateObject> "nested" json) "field nested was not parsed"
Expect.equal nested.Id "MyIdentifier" "nested id was not parsed correctly"
Expect.equal nested.SchemaType "MyType" "nested type was not parsed correctly"
ftestCase "withObjectArray" <| fun _ ->
let json = ROCrateObject.fromROCrateJsonString(GenericObjects.withObjectArray)
Expect.equal json.Id "OuterIdentifier" "id was not parsed correctly"
Expect.equal json.SchemaType "MyType" "type was not parsed correctly"
let nested = Expect.wantSome (DynObj.tryGetTypedPropertyValue<ResizeArray<obj>> "nested" json) "field nested was not parsed"
Expect.equal nested.Count 2 "ResizeArray length is wrong"
let o1 = nested.[0] :?> ROCrateObject
Expect.equal o1.Id "MyIdentifier" "First nested id was not parsed correctly"
Expect.equal o1.SchemaType "MyType" "First nested type was not parsed correctly"
let o2 = nested.[1] :?> ROCrateObject
Expect.equal o2.Id "MyIdentifier" "Second nested id was not parsed correctly"
Expect.equal o2.SchemaType "MyType" "Second nested type was not parsed correctly"
ftestCase "withMixedArray" <| fun _ ->
let json = ROCrateObject.fromROCrateJsonString(GenericObjects.withMixedArray)
Expect.equal json.Id "OuterIdentifier" "id was not parsed correctly"
Expect.equal json.SchemaType "MyType" "type was not parsed correctly"
let nested = Expect.wantSome (DynObj.tryGetTypedPropertyValue<ResizeArray<obj>> "nested" json) "field nested was not parsed"
Expect.equal nested.Count 3 "ResizeArray length is wrong"
let o1 = nested.[0] :?> ROCrateObject
Expect.equal o1.Id "MyIdentifier" "First nested id of object was not parsed correctly"
Expect.equal o1.SchemaType "MyType" "First nested type of object was not parsed correctly"
let o2 = nested.[1] :?> string
Expect.equal o2 "Value2" "Second nested string was not parsed correctly"
let o3 = nested.[2] :?> int
Expect.equal o3 42 "Third nested int was not parsed correctly"

]

let main = testList "ROCrateObject" [
test_read
]
64 changes: 63 additions & 1 deletion tests/TestingUtils/TestObjects.Json/ROCrate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,66 @@ let publication = """{
"authorList": "sdo:author",
"comments": "sdo:disambiguatingDescription"
}
}"""
}"""

module GenericObjects =

let onlyIDAndType =
"""{
"@id": "MyIdentifier",
"@type": "MyType"
}"""

let onlyID =
"""{
"@id": "OnlyIdentifier"
}"""

let onlyType =
"""{
"@type": "MyType"
}"""

let withStringFields =
"""{
"@id": "MyIdentifier",
"@type": "MyType",
"name": "MyName",
"description": "MyDescription"
}"""

let withIntFields =
"""{
"@id": "MyIdentifier",
"@type": "MyType",
"number": 42,
"anotherNumber": 1337
}"""

let withStringArray =
"""{
"@id": "MyIdentifier",
"@type": "MyType",
"names": ["MyName", "MySecondName"]
}"""

let withNestedObject =
sprintf """{
"@id": "OuterIdentifier",
"@type": "MyType",
"nested": %s
}""" onlyIDAndType

let withObjectArray =
sprintf """{
"@id": "OuterIdentifier",
"@type": "MyType",
"nested": [%s, %s]
}""" onlyIDAndType onlyIDAndType

let withMixedArray =
sprintf """{
"@id": "OuterIdentifier",
"@type": "MyType",
"nested": [%s, "Value2", 42]
}""" onlyIDAndType

0 comments on commit d9fbb33

Please sign in to comment.