Skip to content

Commit

Permalink
fix and add rough tests for updateFileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Aug 17, 2023
1 parent 21f56b2 commit fd5b08e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/ARCtrl/ARCtrl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,24 @@ module ARCAux =
[<AttachMembers>]
type ARC(?isa : ISA.ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSystem) =

let mutable cwl = cwl
let mutable fs =
let mutable _isa = isa
let mutable _cwl = cwl
let mutable _fs =
fs
|> Option.defaultValue (FileSystem.create(FileSystemTree.Folder ("",[||])))
|> ARCAux.updateFSByISA isa
|> ARCAux.updateFSByCWL cwl

member val ISA : ArcInvestigation option = isa with get, set
member this.ISA
with get() = _isa
and set(newISA : ArcInvestigation option) =
_isa <- newISA

member this.CWL
with get() = cwl

member this.FileSystem
with get() = fs
with get() = _fs

//static member updateISA (isa : ISA.Investigation) (arc : ARC) : ARC =
// raise (System.NotImplementedException())
Expand Down Expand Up @@ -149,7 +153,7 @@ type ARC(?isa : ISA.ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSyste

// to-do: function that returns read contracts based on a list of paths.
member this.GetReadContracts () =
fs.Tree.ToFilePaths() |> Array.choose Contract.ARC.tryISAReadContractFromPath
_fs.Tree.ToFilePaths() |> Array.choose Contract.ARC.tryISAReadContractFromPath

/// <summary>
/// This function creates the ARC-model from fullfilled READ contracts. The necessary READ contracts can be created with `ARC.getReadContracts`.
Expand Down Expand Up @@ -189,12 +193,11 @@ type ARC(?isa : ISA.ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSyste
)
this.ISA <- Some investigation


member this.UpdateFileSystem() =
let newFS =
ARCAux.updateFSByISA isa fs
|> ARCAux.updateFSByCWL cwl
fs <- newFS
ARCAux.updateFSByISA _isa _fs
|> ARCAux.updateFSByCWL _cwl
_fs <- newFS


/// <summary>
Expand Down Expand Up @@ -224,13 +227,18 @@ type ARC(?isa : ISA.ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSyste
printfn "ARC contains no ISA part."

// Iterates over filesystem and creates a write contract for every file. If possible, include DTO.
fs.Tree.ToFilePaths(true)
_fs.Tree.ToFilePaths(true)
|> Array.map (fun fp ->
match Dictionary.tryGet fp workbooks with
| Some (dto,wb) -> Contract.createCreate(fp,dto,DTO.Spreadsheet wb)
| None -> Contract.createCreate(fp, DTOType.PlainText)

)

member this.Copy() =
let isaCopy = _isa |> Option.map (fun i -> i.Copy())
let fsCopy = _fs.Copy()
new ARC(?isa = isaCopy, ?cwl = _cwl, fs = fsCopy)



Expand Down
5 changes: 5 additions & 0 deletions src/FileSystem/FileSystem.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ type FileSystem =
let tree = this.Tree.Union(other.Tree)
let history = Array.append this.History other.History
FileSystem.create(tree, history)

member this.Copy() =
let fstCopy = this.Tree.Copy()
let historyCopy = this.History |> Array.map id
FileSystem.create(fstCopy,historyCopy)
29 changes: 29 additions & 0 deletions src/FileSystem/FileSystemTree.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ type FileSystemTree =

static member createRootFolder(children:FileSystemTree array) =
Folder(FileSystemTree.ROOT_NAME,children)

/// Non-recursive lookup of child with the given name
member this.TryGetChildByName(name : string) =
match this with
| Folder (_,children) ->
children
|> Array.tryFind (fun c -> c.Name = name)
| File _ -> None

static member tryGetChildByName (name : string) =
fun (fst : FileSystemTree) -> fst.TryGetChildByName name

/// Non-recursive lookup of child with the given name
member this.ContainsChildWithName(name : string) =
match this with
| Folder (_,children) ->
children
|> Array.exists (fun c -> c.Name = name)
| File _ -> false

static member containsChildWithName (name : string) =
fun (fst : FileSystemTree) -> fst.ContainsChildWithName name

member this.AddFile (path: string) : FileSystemTree =
let existingPaths = this.ToFilePaths()
Expand Down Expand Up @@ -122,3 +144,10 @@ type FileSystemTree =
|> Array.append (otherFST.ToFilePaths())
|> Array.distinct
|> FileSystemTree.fromFilePaths

member this.Copy() =
match this with
| Folder(name,children) ->
Folder (name, children |> Array.map (fun c -> c.Copy()))
| File(name) ->
File name
46 changes: 45 additions & 1 deletion tests/ARCtrl/ARCtrl.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let private test_isaFromContracts = testList "read_contracts" [
let aContract = TestObjects.ISAContracts.SimpleISA.assayReadContract
let arc = ARC()
arc.SetISAFromContracts([|iContract; sContract; aContract|])
Expect.isSome arc.ISA "isa should be fille out"
Expect.isSome arc.ISA "isa should be filled out"
let inv = arc.ISA.Value
Expect.equal inv.Identifier TestObjects.Investigation.investigationIdentifier "investigation identifier should have been read from investigation contract"

Expand Down Expand Up @@ -128,8 +128,52 @@ let private test_writeContracts = testList "write_contracts" [
)
]

let private test_updateFileSystem = testList "update_Filesystem" [
testCase "empty noChanges" (fun () ->
let arc = ARC()
let oldFS = arc.FileSystem.Copy()
arc.UpdateFileSystem()
let newFS = arc.FileSystem
Expect.equal oldFS.Tree newFS.Tree "Tree should be equal"
)
testCase "empty addInvestigationWithStudy" (fun () ->
let arc = ARC()
let oldFS = arc.FileSystem.Copy()
let study = ArcStudy("MyStudy")
let inv = ArcInvestigation("MyInvestigation")
inv.AddStudy(study)
arc.ISA <- Some (inv)
arc.UpdateFileSystem()
let newFS = arc.FileSystem
Expect.notEqual oldFS.Tree newFS.Tree "Tree should be unequal"
)
testCase "simple noChanges" (fun () ->
let study = ArcStudy("MyStudy")
let inv = ArcInvestigation("MyInvestigation")
inv.AddStudy(study)
let arc = ARC(isa = inv)
let oldFS = arc.FileSystem.Copy()
arc.UpdateFileSystem()
let newFS = arc.FileSystem
Expect.equal oldFS.Tree newFS.Tree "Tree should be equal"
)
testCase "simple addAssayToStudy" (fun () ->
let study = ArcStudy("MyStudy")
let inv = ArcInvestigation("MyInvestigation")
inv.AddStudy(study)
let arc = ARC(isa = inv)
let oldFS = arc.FileSystem.Copy()
let assay = ArcAssay("MyAssay")
study.AddAssay(assay)
arc.UpdateFileSystem()
let newFS = arc.FileSystem
Expect.notEqual oldFS.Tree newFS.Tree "Tree should be unequal"
)
]

let main = testList "main" [
test_model
test_updateFileSystem
test_isaFromContracts
test_writeContracts
]

0 comments on commit fd5b08e

Please sign in to comment.