Skip to content

Commit 8c67c36

Browse files
committed
add json parser and tests for updated data objects
1 parent 08fcb3b commit 8c67c36

9 files changed

Lines changed: 147 additions & 1 deletion

tests/Core/ARCtrl.Core.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Compile Include="Regex.Tests.fs" />
1010
<Compile Include="Person.Tests.fs" />
1111
<Compile Include="OntologyAnnotation.Tests.fs" />
12+
<Compile Include="Data.Tests.fs" />
1213
<Compile Include="CompositeCell.Tests.fs" />
1314
<Compile Include="CompositeHeader.Tests.fs" />
1415
<Compile Include="CompositeColumn.Tests.fs" />

tests/Core/CompositeCell.Tests.fs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,18 @@ let private tests_create =
107107
let expected = CompositeCell.Unitized("42", oa)
108108
Expect.equal newCell expected ""
109109
)
110-
]
110+
testCase "createData" (fun () ->
111+
let d = Data(name = "MyData#row=1", format = "text/csv", selectorFormat = "MySelector")
112+
let newCell : CompositeCell = CompositeCell.createData(d)
113+
let expected = CompositeCell.Data d
114+
Expect.equal newCell expected ""
115+
)
116+
testCase "createDataFromString" (fun () ->
117+
let newCell : CompositeCell = CompositeCell.createDataFromString("MyData#row=1", "text/csv", "MySelector")
118+
let expected = CompositeCell.Data <| Data(name = "MyData#row=1", format = "text/csv", selectorFormat = "MySelector")
119+
Expect.equal newCell expected ""
120+
)
121+
]
111122

112123
let private tests_ToString = testList "ToString" [
113124
testCase "FreeText" <| fun _ ->
@@ -125,6 +136,12 @@ let private tests_ToString = testList "ToString" [
125136
let actual = cc.ToString()
126137
let expected = "20 degree celcius"
127138
Expect.equal actual expected ""
139+
testCase "Data" <| fun _ ->
140+
let d = Data(name = "MyData#row=1", format = "text/csv", selectorFormat = "MySelector")
141+
let cc = CompositeCell.createData d
142+
let actual = cc.ToString()
143+
let expected = "MyData#row=1"
144+
Expect.equal actual expected ""
128145
]
129146

130147
let private tests_GetContent = testList "GetContent" [
@@ -140,6 +157,11 @@ let private tests_GetContent = testList "GetContent" [
140157
let cell = CompositeCell.createUnitized ("12", OntologyAnnotation("My Unit Name"))
141158
let actual = cell.GetContent()
142159
Expect.equal actual [|"12"; "My Unit Name"; ""; ""|] ""
160+
testCase "Data" <| fun _ ->
161+
let d = Data(name = "MyData#row=1", format = "text/csv", selectorFormat = "MySelector")
162+
let cell = CompositeCell.createData d
163+
let actual = cell.GetContent()
164+
Expect.equal actual [|"MyData#row=1"; "text/csv"; "MySelector"|] ""
143165
testCase "Matching" <| fun _ ->
144166
let matchContent (content: string []) =
145167
match content with
@@ -189,6 +211,11 @@ let private tests_GetHashCode = testList "GetHashCode" [
189211
let cc = CompositeCell.createFreeText("TestTerm")
190212
cc.GetHashCode() |> ignore
191213
Expect.isTrue true " "
214+
testCase "Data" <| fun _ ->
215+
let d = Data(name = "MyData#row=1", format = "text/csv", selectorFormat = "MySelector")
216+
let cc = CompositeCell.createData d
217+
cc.GetHashCode() |> ignore
218+
Expect.isTrue true " "
192219
]
193220
]
194221

tests/Core/Data.Tests.fs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Data.Tests
2+
3+
open ARCtrl
4+
5+
open TestingUtils
6+
7+
let private tests_GetHashCode = testList "GetHashCode" [
8+
testCase "equal" <| fun _ ->
9+
let d1 = Data("MyID","MyName",Process.DataFile.RawDataFile,"text/csv","MySelector",ResizeArray [Comment.create("MyKey","MyValue")])
10+
let d2 = Data("MyID","MyName",Process.DataFile.RawDataFile,"text/csv","MySelector",ResizeArray [Comment.create("MyKey","MyValue")])
11+
let hash1 = d1.GetHashCode()
12+
let hash2 = d2.GetHashCode()
13+
Expect.equal d1 d2 "Should be equal"
14+
Expect.equal hash1 hash2 "HashCode should be equal"
15+
testCase "unequal, different name" <| fun _ ->
16+
let d1 = Data("MyID","MyName",Process.DataFile.RawDataFile,"text/csv","MySelector",ResizeArray [Comment.create("MyKey","MyValue")])
17+
let d2 = Data("MyID","MyName2",Process.DataFile.RawDataFile,"text/csv","MySelector",ResizeArray [Comment.create("MyKey","MyValue")])
18+
let hash1 = d1.GetHashCode()
19+
let hash2 = d2.GetHashCode()
20+
Expect.notEqual d1 d2 "Should not be equal"
21+
Expect.notEqual hash1 hash2 "HashCode should not be equal"
22+
]
23+
24+
25+
let main =
26+
testList "Data" [
27+
tests_GetHashCode
28+
]

tests/Core/Main.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let all = testSequenced <| testList "Core" [
88
Regex.Tests.main
99
Person.Tests.main
1010
CompositeHeader.Tests.main
11+
Data.Tests.main
1112
CompositeCell.Tests.main
1213
CompositeColumn.Tests.main
1314
ArcTables.Tests.main

tests/Json/ARCtrl.Json.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<Compile Include="BaseJsonTests.fs" />
1818
<Compile Include="Comment.Tests.fs" />
1919
<Compile Include="OntologyAnnotation.Tests.fs" />
20+
<Compile Include="Data.Tests.fs" />
2021
<Compile Include="CompositeCell.Tests.fs" />
2122
<Compile Include="IOType.Tests.fs" />
2223
<Compile Include="CompositeHeader.Tests.fs" />

tests/Json/ArcTable.Tests.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,24 @@ let private tests = testList "extended" [
9191
]
9292
]
9393

94+
let private tests_dataColumns =
95+
testList "dataColumns" [
96+
testCase "dataColumns" <| fun _ ->
97+
let testTable = ArcTable.init("Test")
98+
testTable.AddColumn (CompositeHeader.Input IOType.Data, [|CompositeCell.Data (Data(name="MyInputDataFile.csv"))|])
99+
testTable.AddColumn (CompositeHeader.Output IOType.Data, [|CompositeCell.Data (Data(name="MyData.csv#row=1",format="text/csv",selectorFormat = "MySelector"))|])
100+
101+
let encoded = testTable.ToJsonString()
102+
let decoded = ArcTable.fromJsonString encoded
103+
Expect.arcTableEqual decoded testTable "decompressed table should be equal to original table"
104+
]
105+
106+
94107
let main = testList "ArcTable" [
95108
tests
96109
tests_core
97110
tests_coreEmpty
98111
tests_compressedEmpty
99112
tests_compressedFilled
113+
tests_dataColumns
100114
]

tests/Json/CompositeCell.Tests.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ let tests_extended = testList "extended" [
1515
let cell_term_empty_jsonString = sprintf """{"%s":"Term","values":[{}]}""" CompositeCell.CellType
1616
let cell_unitized_jsonString = sprintf """{"%s":"Unitized","values":["42",{"annotationValue":"My Name","termSource":"MY","termAccession":"MY:1"}]}""" CompositeCell.CellType
1717
let cell_unitized_empty_jsonString = sprintf """{"%s":"Unitized","values":["",{}]}""" CompositeCell.CellType
18+
let cell_data_empty_jsonString = sprintf """{"%s":"Data","values":[{}]}""" CompositeCell.CellType
19+
let cell_data_jsonString = sprintf """{"%s":"Data","values":[{"@id":"MyID","name":"MyName","dataType":"Raw Data File","format":"text/csv","selectorFormat":"MySelector","comments":[{"key":"MyKey","value":"MyValue"}]}]}""" CompositeCell.CellType
20+
1821
testList "encoder (toJsonString)" [
1922
testCase "FreeText" <| fun _ ->
2023
let actual = CompositeCell.encoder cell_freetext |> Encode.toJsonString 0
@@ -36,6 +39,14 @@ let tests_extended = testList "extended" [
3639
let actual = CompositeCell.encoder cell_unitized_empty |> Encode.toJsonString 0
3740
let expected = cell_unitized_empty_jsonString
3841
Expect.equal actual expected ""
42+
testCase "Data" <| fun _ ->
43+
let actual = CompositeCell.encoder (CompositeCell.Data(Data("MyID","MyName",Process.DataFile.RawDataFile,"text/csv","MySelector",ResizeArray [Comment.create("MyKey","MyValue")])) ) |> Encode.toJsonString 0
44+
let expected = cell_data_jsonString
45+
Expect.equal actual expected ""
46+
testCase "Data empty" <| fun _ ->
47+
let actual = CompositeCell.encoder (CompositeCell.Data(Data())) |> Encode.toJsonString 0
48+
let expected = cell_data_empty_jsonString
49+
Expect.equal actual expected ""
3950
]
4051
testList "decoder (fromJsonString)" [
4152
testCase "FreeText" <| fun _ ->
@@ -58,6 +69,14 @@ let tests_extended = testList "extended" [
5869
let actual = Decode.fromJsonString CompositeCell.decoder cell_unitized_empty_jsonString
5970
let expected = cell_unitized_empty
6071
Expect.equal actual expected ""
72+
testCase "Data" <| fun _ ->
73+
let actual = Decode.fromJsonString CompositeCell.decoder cell_data_jsonString
74+
let expected = CompositeCell.Data(Data("MyID","MyName",Process.DataFile.RawDataFile,"text/csv","MySelector",ResizeArray [Comment.create("MyKey","MyValue")]))
75+
Expect.equal actual expected ""
76+
testCase "Data empty" <| fun _ ->
77+
let actual = Decode.fromJsonString CompositeCell.decoder cell_data_empty_jsonString
78+
let expected = CompositeCell.Data(Data())
79+
Expect.equal actual expected ""
6180
]
6281
]
6382

tests/Json/Data.Tests.fs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module Tests.Data
2+
3+
open TestingUtils
4+
open ARCtrl
5+
open ARCtrl.Json
6+
7+
let basic_tests = testList "BasicJson" [
8+
testCase "AllFields" <| fun _ ->
9+
let d = Data("MyID","MyName",Process.DataFile.RawDataFile,"text/csv","MySelector",ResizeArray [Comment.create("MyKey","MyValue")])
10+
let json = Data.encoder d |> Encode.toJsonString 2
11+
let d2 = Decode.fromJsonString Data.decoder json
12+
Expect.equal d2 d "Different after write and read"
13+
]
14+
15+
let isa_tests = testList "ISAJson" [
16+
testCase "NativeISAFieldsIO" <| fun _ ->
17+
let d = Data("MyID","MyName",Process.DataFile.RawDataFile,comments = ResizeArray [Comment.create("MyKey","MyValue")])
18+
let json = Data.ISAJson.encoder d |> Encode.toJsonString 2
19+
let d2 = Decode.fromJsonString Data.ISAJson.decoder json
20+
Expect.equal d2 d "Different after write and read"
21+
#if !FABLE_COMPILER_PYTHON
22+
testAsync "WriterSchemaCorrectness" {
23+
let d = Data("MyID","MyName",Process.DataFile.RawDataFile, "text/csv", "MySelector", ResizeArray [Comment.create("MyKey","MyValue")])
24+
let json = Data.ISAJson.encoder d |> Encode.toJsonString 2
25+
let! validation = Validation.validateData json
26+
Expect.isTrue validation.Success $"Data did not match schema: {validation.GetErrors()}"
27+
}
28+
#endif
29+
]
30+
31+
let rocrate_tests = testList "RO-CrateJson" [
32+
testCase "AllFields" <| fun _ ->
33+
let d = Data("MyID","MyName",Process.DataFile.RawDataFile,"text/csv","MySelector",ResizeArray [Comment.create("MyKey","MyValue")])
34+
let json = Data.ROCrate.encoder d |> Encode.toJsonString 2
35+
let d2 = Decode.fromJsonString Data.ROCrate.decoder json
36+
Expect.equal d2 d "Different after write and read"
37+
]
38+
39+
let compressed_tests =
40+
testList "CompressedJson" [
41+
testCase "AllFields" <| fun _ ->
42+
let d = Data("MyID","MyName",Process.DataFile.RawDataFile,"text/csv","MySelector",ResizeArray [Comment.create("MyKey","MyValue")])
43+
let stringTable = StringTable.StringTableMap()
44+
let json = Data.compressedEncoder stringTable d |> Encode.toJsonString 2
45+
let d2 = Decode.fromJsonString (Data.compressedDecoder (StringTable.arrayFromMap stringTable)) json
46+
Expect.equal d2 d "Different after write and read"
47+
]
48+
49+
let main = testList "Data" [
50+
basic_tests
51+
isa_tests
52+
rocrate_tests
53+
compressed_tests
54+
]

tests/Json/Main.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ let all = testSequenced <| testList "Json" [
66
Tests.Decoder.main
77
Tests.Comment.main
88
Tests.OntologyAnnotation.main
9+
Tests.Data.main
910
Tests.CompositeCell.main
1011
Tests.IOType.main
1112
Tests.CompositeHeader.main

0 commit comments

Comments
 (0)