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

add some commonly used predicates for IParams #102

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
87 changes: 68 additions & 19 deletions src/ARCExpect/ValidationFunctions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,57 @@
open ARCTokenization.StructuralOntology

/// <summary>
/// Top level API for performing validation.
/// A collection of commonly used predicates for validation
/// </summary>
module Predicates =

type Param =

static member HasAccession (param : #IParam) = param.Accession <> ""

static member HasName (param : #IParam) = param.Name <> ""

static member HasReferenceOntology (param : #IParam) = param.RefUri <> ""

static member HasTerm (param : #IParam) =
Param.HasAccession param
&& Param.HasName param
&& Param.HasReferenceOntology param

static member HasSimpleValue (param : #IParam) =
match param.Value with
| ParamValue.Value _ -> true | _ -> false

static member HasCvValue (param : #IParam) =
match param.Value with
| ParamValue.CvValue _ -> true | _ -> false

static member HasUnitizedValue (param : #IParam) =
match param.Value with
| ParamValue.WithCvUnitAccession _ -> true | _ -> false

static member ParamValueIsEqualTo (expectedParamValue : ParamValue) (param : #IParam) = param.Value = expectedParamValue

static member SimpleValueIsEqualTo (expectedValue : System.IConvertible) (param : #IParam) =
match param.Value with
| ParamValue.Value v -> v = expectedValue
| _ -> false

static member CvValueIsEqualTo (expectedTerm:CvTerm) (param : #IParam) =
match param.Value with
| ParamValue.CvValue term -> term = expectedTerm
| _ -> false

static member UnitizedValueIsEqualTo (expectedValue : System.IConvertible) (expectedUnit:CvUnit) (param : #IParam) =
match param.Value with
| ParamValue.WithCvUnitAccession (value,unit) -> value = expectedValue && unit = expectedUnit
| _ -> false


/// <summary>
/// Top level API for formulating expectations as validation cases
/// </summary>
[<RequireQualifiedAccess>]
module Validate =

/// <summary>
Expand All @@ -26,7 +75,7 @@
|> ErrorMessage.ofIParam "is empty."
|> Expecto.Tests.failtestNoStackf "%s"

/// <summary>

Check warning on line 78 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-linux

This XML comment is invalid: unknown parameter 'expectedValue'

Check warning on line 78 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-linux

This XML comment is incomplete: no documentation for parameter 'targetValue'

Check warning on line 78 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-windows

This XML comment is invalid: unknown parameter 'expectedValue'

Check warning on line 78 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-windows

This XML comment is incomplete: no documentation for parameter 'targetValue'
/// Validates if the value of the given Param is equal to the expected value.
/// </summary>
/// <param name="expectedValue">The expected value to validate against</param>
Expand Down Expand Up @@ -70,7 +119,7 @@
|> ErrorMessage.ofIParam "is invalid."
|> Expecto.Tests.failtestNoStackf "%s"

/// <summary>

Check warning on line 122 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-linux

This XML comment is invalid: unknown parameter 'pattern'

Check warning on line 122 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-linux

This XML comment is incomplete: no documentation for parameter 'regex'

Check warning on line 122 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-windows

This XML comment is invalid: unknown parameter 'pattern'

Check warning on line 122 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-windows

This XML comment is incomplete: no documentation for parameter 'regex'
/// Validates if the value of the given Param matches a regex.
/// </summary>
/// <param name="pattern">The regex that the value should match</param>
Expand Down Expand Up @@ -118,12 +167,27 @@
/// </summary>
type ParamCollection =

/// <summary>
/// Generic method to validate wether a collection of IParams satisfies any kind of predicate.
/// </summary>
/// <summary>
/// Validates whether a given predicate function returns true for the sequence of IParams.
/// </summary>
/// <param name="predicate">Function that projects the whole sequence.</param>
/// <param name="paramCollection">The param collection to validate.</param>
static member SatisfiesPredicate (predicate :#seq<#IParam> -> bool) (paramCollection : #seq<#IParam>) =
match predicate paramCollection with
| true -> ()
| false ->
ErrorMessage.ofValue $"The does not satisfy the predicate." "paramCollection"
|> Expecto.Tests.failtestNoStackf "%s"

/// <summary>
/// Validates if at least one Param with the expected value in the given collection exists.
/// </summary>
/// <param name="expectedValue">the value expected to occur in at least 1 Param in the given collection</param>
/// <param name="paramCollection">The param collection to validate</param>
static member ContainsParamWithValue (expectedValue : #System.IConvertible) (paramCollection : #seq<#IParam>) =
static member ContainsItemWithValue (expectedValue : #System.IConvertible) (paramCollection : #seq<#IParam>) =
match Seq.exists (fun (param : #IParam)-> (param.Value |> ParamValue.getValue) = (expectedValue :> System.IConvertible)) paramCollection with
| true -> ()
| false ->
Expand All @@ -136,7 +200,7 @@
/// </summary>
/// <param name="expectedTerm">the term expected to occur in at least 1 Param in the given collection</param>
/// <param name="paramCollection">The param collection to validate</param>
static member ContainsParamWithTerm (expectedTerm : CvTerm) (paramCollection: #seq<#IParam>) =
static member ContainsItemWithTerm (expectedTerm : CvTerm) (paramCollection: #seq<#IParam>) =
match Seq.exists (fun e -> Param.getTerm e = expectedTerm) paramCollection with
| true -> ()
| false ->
Expand All @@ -149,7 +213,7 @@
/// </summary>
/// <param name="expectedParam">the param expected to occur at least once in the given collection</param>
/// <param name="paramCollection">The param collection to validate</param>
static member ContainsParam (expectedParam : #IParam) (paramCollection : #seq<#IParam>) =
static member ContainsItem (expectedParam : #IParam) (paramCollection : #seq<#IParam>) =

let tmp =
Param.getParamValue expectedParam
Expand All @@ -163,21 +227,6 @@
|> ErrorMessage.ofIParam $"does not exist"
|> Expecto.Tests.failtestNoStackf "%s"

/// <summary>
/// Generic method to validate wether a collection of IParams satisfies any kind of predicate.
/// </summary>
/// <summary>
/// Validates whether a given predicate function returns true for the sequence of IParams.
/// </summary>
/// <param name="predicate">Function that projects the whole sequence.</param>
/// <param name="paramCollection">The param collection to validate.</param>
static member SatisfiesPredicate (predicate :#seq<#IParam> -> bool) (paramCollection : #seq<#IParam>) =
match predicate paramCollection with
| true -> ()
| false ->
ErrorMessage.ofValue $"The does not satisfy the predicate." "paramCollection"
|> Expecto.Tests.failtestNoStackf "%s"


/// <summary>
/// Validates wether the given Param's value is an email that matches a pre-defined regex pattern ("^[^@\s]+@[^@\s]+\.[^@\s]+$")
Expand Down
Loading