Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URI 2 TAN on creation #62

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/ControlledVocabulary/CvTerm.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace ControlledVocabulary


open FSharpAux


/// Represents a term from a controlled vocabulary (Cv)
/// in the form of: id|accession ; name|value ; refUri
// ?Maybe [<Struct>]
@@ -9,13 +13,47 @@ type CvTerm = {
Name: string
RefUri: string
} with

/// <summary>
/// Checks if the given accession is an URI.
/// </summary>
/// <param name="accession">The input accession.</param>
static member checkForUri accession =
let rx = System.Text.RegularExpressions.Regex("^https?:\/\/[a-zA-Z0-9\/.]+\/[a-zA-Z]+_[0-9]+\/?$")
rx.Match(accession).Success

/// <summary>
/// Takes an URI and returns the respective TAN.
/// </summary>
/// <param name="uri">The input URI.</param>
static member uriToTan (uri : string) : string =
let posLastSlash = String.findIndexBack '/' uri
uri[posLastSlash + 1 ..]
|> String.replace "_" ":"

/// <summary>
/// Creates a CvTerm from a given accession, name and reference.
/// </summary>
/// <param name="accession">The accession of the term.</param>
/// <param name="name">The name of the term.</param>
/// <param name="ref">The term source reference of the term.</param>
static member create(
accession: string,
name: string,
ref : string
) =
{Accession = accession; Name = name; RefUri = ref}

let tanAccession =
if CvTerm.checkForUri accession then
CvTerm.uriToTan accession
else accession

{Accession = tanAccession; Name = name; RefUri = ref}

/// <summary>
/// Creates a CvTerm from a given name. Accession and reference are empty.
/// </summary>
/// <param name="name">The name of the term.</param>
static member create(
name: string
) =
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@

<ItemGroup>
<Compile Include="ReferenceObjects.fs" />
<Compile Include="CvTermTests.fs" />
<Compile Include="CvParamTests.fs" />
<Compile Include="CvBaseTests.fs" />
<Compile Include="ParamTests.fs" />
55 changes: 55 additions & 0 deletions tests/ControlledVocabulary.Tests/CvTermTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace CvTermTests


open ControlledVocabulary
open ReferenceObjects

open Xunit


module CheckForUri =

[<Fact>]
let ``correct check, actual URL 1`` () =
let check = CvTerm.checkForUri testAccession3
Assert.True check

[<Fact>]
let ``correct check, actual URL 2`` () =
let check = CvTerm.checkForUri "https://purl.org/TO_00000003"
Assert.True check

[<Fact>]
let ``correct check, no URL`` () =
let check = CvTerm.checkForUri "purl/123_abc"
Assert.False check


module UriToTan =

[<Fact>]
let ``correct TAN returned`` () =
let expected = "TO:00000003"
let actual = CvTerm.uriToTan testAccession3
Assert.Equal(expected, actual)


module Create =

[<Fact>]
let ``correct CvTerm, primary create function overload`` () =
let expected = testTerm1
let actual = CvTerm.create(testAccession1, testName1, testRef1)
Assert.Equal(expected, actual)

[<Fact>]
let ``correct CvTerm, secondary create function overload (only name given)`` () =
let expected = testTerm4
let actual = CvTerm.create(testName2)
Assert.Equal(expected, actual)

[<Fact>]
let ``correct CvTerm, create function with URL as accession`` () =
let expected = testTerm3
let actual = CvTerm.create(testAccession3, testName2, testRef2)
Assert.Equal(expected, actual)
28 changes: 26 additions & 2 deletions tests/ControlledVocabulary.Tests/ReferenceObjects.fs
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
module ReferenceObjects


open ControlledVocabulary


let testAccession1 = "TO:00000001"
let testName1 = "Test"
let testRef1 = "TO"

let testTerm1 = CvTerm.create(accession = testAccession1, name = testName1, ref = testRef1)
let testTerm1 = {
Accession = testAccession1
Name = testName1
RefUri = testRef1
}

let testAccession2 = "TO:00000002"
let testName2 = "5"
let testRef2 = "TO"

let testTerm2 = CvTerm.create(accession = testAccession2, name = testName2, ref = testRef2)
let testTerm2 = {
Accession = testAccession2
Name = testName2
RefUri = testRef2
}

let testAccession3 = "http://purl.org/TO_00000003"

let testTerm3 = {
Accession = "TO:00000003"
Name = testName2
RefUri = testRef2
}

let testTerm4 = {
Accession = ""
Name = testName2
RefUri = ""
}

let ``CvParam with ParamValue.Value`` = CvParam(testTerm1, ParamValue.Value 5)
let ``CvParam with ParamValue.CvValue`` = CvParam(testTerm1, ParamValue.CvValue testTerm2)