From fdc4773125fb37bf7140476460a117e575e420ae Mon Sep 17 00:00:00 2001 From: olscholz Date: Mon, 16 Sep 2024 16:40:31 +0200 Subject: [PATCH 1/2] add tryGetColumnByHeaderBy member and static + tests Changes in ArcTavle.fs and ArcTable.Tests.fs adding the functionality discussed in Issue #440 --- src/Core/Table/ArcTable.fs | 10 ++++++++++ tests/Core/ArcTable.Tests.fs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/Core/Table/ArcTable.fs b/src/Core/Table/ArcTable.fs index a852bcf7..dcd1ee3e 100644 --- a/src/Core/Table/ArcTable.fs +++ b/src/Core/Table/ArcTable.fs @@ -385,6 +385,16 @@ type ArcTable(name: string, headers: ResizeArray, values: Syste fun (table:ArcTable) -> table.TryGetColumnByHeader(header) + // tryGetColumnByHeaderBy + member this.TryGetColumnByHeaderBy (headerPredicate:CompositeHeader -> bool) = //better name for header / action + this.Headers + |> Seq.tryFindIndex headerPredicate + |> Option.map (fun i -> this.GetColumn(i)) + + static member tryGetColumnByHeaderBy (headerPredicate:CompositeHeader -> bool) = + fun (table:ArcTable) -> + table.TryGetColumnByHeaderBy(headerPredicate) + member this.GetColumnByHeader (header:CompositeHeader) = match this.TryGetColumnByHeader(header) with | Some c -> c diff --git a/tests/Core/ArcTable.Tests.fs b/tests/Core/ArcTable.Tests.fs index 1c70de8e..50b1b9a0 100644 --- a/tests/Core/ArcTable.Tests.fs +++ b/tests/Core/ArcTable.Tests.fs @@ -1863,6 +1863,40 @@ let private tests_RemoveColumns = Expect.equal table.Values.[(table.ColumnCount-1,table.RowCount-1)] (CompositeCell.createTerm oa_SCIEXInstrumentModel) "table.ColumnCount-1,table.RowCount-1" ) ] + +let private tests_TryGetColumnByHeaderBy = + testList "TryGetColumnByHeaderBy" [ + testCase "on empty column" (fun () -> + let table = create_testTable() + let colOption = table.TryGetColumnByHeaderBy (fun (header:CompositeHeader) -> + match header with + | CompositeHeader.Component oa -> oa = oa_instrumentModel + | _ -> false ) + let col = Expect.wantSome colOption "should have found col but returned None" + Expect.sequenceEqual col.Cells column_component.Cells "cells did not match" + ) + testCase "find ontology with values" (fun () -> + let table = create_testTable() + let column_Chlamy = CompositeColumn.create(CompositeHeader.Characteristic oa_species, createCells_Term 8) + table.AddColumns[|column_Chlamy|] + let colOption = table.TryGetColumnByHeaderBy (fun (header:CompositeHeader) -> + match header with + | CompositeHeader.Characteristic oa -> oa = oa_species + | _ -> false ) + let col = Expect.wantSome colOption "should find column with values" + Expect.sequenceEqual col.Cells column_Chlamy.Cells "cells did match" + ) + testCase "fail to find ontology" (fun () -> + let table = create_testTable() + let colOption = table.TryGetColumnByHeaderBy (fun (header:CompositeHeader) -> + match header with + | CompositeHeader.Parameter oa -> oa = oa_temperature + | _ -> false ) + Expect.isNone colOption "fails to find col therefore returns none" + ) + ] + + let private tests_MoveColumn = testList "MoveColumn" [ testCase "CheckBoundaries" (fun () -> @@ -2541,6 +2575,7 @@ let main = tests_AddColumnFill tests_RemoveColumn tests_RemoveColumns + tests_TryGetColumnByHeaderBy tests_MoveColumn tests_RemoveRow tests_RemoveRows From 1a5c4d64cdd3c591a2c7af73c4bdb7a9261c5ffb Mon Sep 17 00:00:00 2001 From: olscholz Date: Tue, 17 Sep 2024 14:01:00 +0200 Subject: [PATCH 2/2] Update ArcTable.Tests.fs small changes to testCases for tryGetColumnByHeaderBy --- tests/Core/ArcTable.Tests.fs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/Core/ArcTable.Tests.fs b/tests/Core/ArcTable.Tests.fs index 50b1b9a0..c95e6ee8 100644 --- a/tests/Core/ArcTable.Tests.fs +++ b/tests/Core/ArcTable.Tests.fs @@ -1866,27 +1866,31 @@ let private tests_RemoveColumns = let private tests_TryGetColumnByHeaderBy = testList "TryGetColumnByHeaderBy" [ - testCase "on empty column" (fun () -> - let table = create_testTable() - let colOption = table.TryGetColumnByHeaderBy (fun (header:CompositeHeader) -> + testCase "Empty column" (fun () -> + let emptyTable = ArcTable.init("empty table") + let column_species = CompositeColumn.create(CompositeHeader.Characteristic oa_species) + emptyTable.AddColumns[|column_species|] + let colOption = emptyTable.TryGetColumnByHeaderBy (fun (header:CompositeHeader) -> match header with - | CompositeHeader.Component oa -> oa = oa_instrumentModel + | CompositeHeader.Characteristic oa -> oa = oa_species | _ -> false ) let col = Expect.wantSome colOption "should have found col but returned None" - Expect.sequenceEqual col.Cells column_component.Cells "cells did not match" + // Expect.sequenceEqual col.Cells column_component.Cells "cells did not match" + Expect.hasLength col.Cells 0 "cells did not match" ) - testCase "find ontology with values" (fun () -> + testCase "Column with values" (fun () -> let table = create_testTable() - let column_Chlamy = CompositeColumn.create(CompositeHeader.Characteristic oa_species, createCells_Term 8) - table.AddColumns[|column_Chlamy|] + let column_species = CompositeColumn.create(CompositeHeader.Characteristic oa_species, createCells_Term 8) + table.AddColumns[|column_species|] let colOption = table.TryGetColumnByHeaderBy (fun (header:CompositeHeader) -> match header with | CompositeHeader.Characteristic oa -> oa = oa_species | _ -> false ) - let col = Expect.wantSome colOption "should find column with values" - Expect.sequenceEqual col.Cells column_Chlamy.Cells "cells did match" + let col = Expect.wantSome colOption "should have found col but returned None" + // Expect.hasLength col.Cells 8 "cells did not match" + Expect.sequenceEqual col.Cells column_species.Cells "cells did not match" ) - testCase "fail to find ontology" (fun () -> + testCase "Non-existing column" (fun () -> let table = create_testTable() let colOption = table.TryGetColumnByHeaderBy (fun (header:CompositeHeader) -> match header with