Skip to content

Commit

Permalink
add tryGetColumnByHeaderBy member and static + tests
Browse files Browse the repository at this point in the history
Changes in ArcTavle.fs and ArcTable.Tests.fs adding the functionality discussed in Issue nfdi4plants#440
  • Loading branch information
olscholz committed Sep 16, 2024
1 parent 8b4368a commit fdc4773
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Core/Table/ArcTable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,16 @@ type ArcTable(name: string, headers: ResizeArray<CompositeHeader>, 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
Expand Down
35 changes: 35 additions & 0 deletions tests/Core/ArcTable.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 () ->
Expand Down Expand Up @@ -2541,6 +2575,7 @@ let main =
tests_AddColumnFill
tests_RemoveColumn
tests_RemoveColumns
tests_TryGetColumnByHeaderBy
tests_MoveColumn
tests_RemoveRow
tests_RemoveRows
Expand Down

0 comments on commit fdc4773

Please sign in to comment.