Skip to content

Commit

Permalink
Fix Client type extensions and add tests:
Browse files Browse the repository at this point in the history
- Fix direct package content hash not using "\n" as line endings
- Add `AsIndexType()` directly on Author/OntologyAnnotation
- Add tests to catch these types of bugs in the future
  • Loading branch information
kMutagene committed Jun 12, 2024
1 parent 9ffb9b0 commit 74d64e6
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/AVPRClient/AVPRClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/RELEASE_NOTES.md"))</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageVersion>0.0.6</PackageVersion>
<PackageVersion>0.0.7</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
52 changes: 36 additions & 16 deletions src/AVPRClient/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ public static AVPRClient.PackageContentHash toPackageContentHash(
return new AVPRClient.PackageContentHash
{
PackageName = indexedPackage.Metadata.Name,
Hash = Convert.ToHexString(md5.ComputeHash(File.ReadAllBytes(indexedPackage.RepoPath))),
Hash = Convert.ToHexString(
md5.ComputeHash(
Encoding.UTF8.GetBytes(
File.ReadAllText(indexedPackage.RepoPath)
.ReplaceLineEndings("\n")
)
)
),
PackageMajorVersion = indexedPackage.Metadata.MajorVersion,
PackageMinorVersion = indexedPackage.Metadata.MinorVersion,
PackagePatchVersion = indexedPackage.Metadata.PatchVersion
Expand All @@ -95,34 +102,47 @@ public static AVPRClient.PackageContentHash toPackageContentHash(
}
}

public static AVPRIndex.Domain.Author AsIndexType(
this Author authors
)
{
return
new AVPRIndex.Domain.Author
{
FullName = authors.FullName,
Email = authors.Email,
Affiliation = authors.Affiliation,
AffiliationLink = authors.AffiliationLink
};
}

public static AVPRIndex.Domain.Author [] AsIndexType (
this ICollection<Author> authors
)
{
return authors
.Select(author =>
new AVPRIndex.Domain.Author
{
FullName = author.FullName,
Email = author.Email,
Affiliation = author.Affiliation,
AffiliationLink = author.AffiliationLink
})
.Select(author => author.AsIndexType())
.ToArray();
}

public static AVPRIndex.Domain.OntologyAnnotation AsIndexType(
this OntologyAnnotation tag
)
{
return new AVPRIndex.Domain.OntologyAnnotation
{
Name = tag.Name,
TermSourceREF = tag.TermSourceREF,
TermAccessionNumber = tag.TermAccessionNumber
};
}

public static AVPRIndex.Domain.OntologyAnnotation[] AsIndexType(
this ICollection<OntologyAnnotation> ontologyAnnotations
)
{
return ontologyAnnotations
.Select(tag =>
new AVPRIndex.Domain.OntologyAnnotation
{
Name = tag.Name,
TermSourceREF = tag.TermSourceREF,
TermAccessionNumber = tag.TermAccessionNumber
})
.Select(tag => tag.AsIndexType())
.ToArray();
}

Expand Down
3 changes: 3 additions & 0 deletions src/AVPRClient/RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.0.7
- Further TypeExtension improvements and fixes

## v0.0.6
- Fix some missing fields in type extensions

Expand Down
15 changes: 13 additions & 2 deletions tests/ClientTests/ClientTests.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -9,7 +9,9 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="Tests.fs" />
<Compile Include="Utils.fs" />
<Compile Include="ReferenceObjects.fs" />
<Compile Include="TypeExtensionsTests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

Expand All @@ -26,4 +28,13 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AVPRClient\AVPRClient.csproj" />
<ProjectReference Include="..\..\src\AVPRIndex\AVPRIndex.fsproj" />
</ItemGroup>

<ItemGroup>
<None Include="fixtures\**" CopyToOutputDirectory="Always" />
</ItemGroup>

</Project>
123 changes: 123 additions & 0 deletions tests/ClientTests/ReferenceObjects.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
module ReferenceObjects

open Utils
open AVPRIndex
open AVPRClient

let date = System.DateTime.ParseExact("2021-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)

let testDate = System.DateTimeOffset.Parse("01/01/2024")

module Author =

let mandatoryFieldsClient = AVPRClient.Author(FullName = "test")

let allFieldsClient =
AVPRClient.Author(
FullName = "test",
Email = "[email protected]",
Affiliation = "testaffiliation",
AffiliationLink = "test.com"
)

let mandatoryFieldsIndex = AVPRIndex.Domain.Author(FullName = "test")

let allFieldsIndex =
AVPRIndex.Domain.Author(
FullName = "test",
Email = "[email protected]",
Affiliation = "testaffiliation",
AffiliationLink = "test.com"
)

module OntologyAnnotation =

let mandatoryFieldsClient = AVPRClient.OntologyAnnotation(Name = "test")

let allFieldsClient = AVPRClient.OntologyAnnotation(
Name = "test",
TermSourceREF = "REF",
TermAccessionNumber = "TAN"
)

let mandatoryFieldsIndex = AVPRIndex.Domain.OntologyAnnotation(Name = "test")

let allFieldsIndex = AVPRIndex.Domain.OntologyAnnotation(
Name = "test",
TermSourceREF = "REF",
TermAccessionNumber = "TAN"
)

module ValidationPackageMetadata =

let mandatoryFields = ValidationPackageMetadata(
Name = "name",
Summary = "summary" ,
Description = "description" ,
MajorVersion = 1,
MinorVersion = 0,
PatchVersion = 0
)

let allFields = ValidationPackageMetadata(
Name = "name",
Summary = "summary" ,
Description = "description" ,
MajorVersion = 1,
MinorVersion = 0,
PatchVersion = 0,
Publish = true,
Authors = [|Author.allFieldsIndex|],
Tags = [|OntologyAnnotation.allFieldsIndex|],
ReleaseNotes = "releasenotes",
CQCHookEndpoint = "hookendpoint"
)

module PackageContentHash =
open System
open System.IO
open System.Text
open System.Security.Cryptography

let expected_hash =
let md5 = MD5.Create()
"fixtures/test_validation_package_all_fields.fsx"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> md5.ComputeHash
|> Convert.ToHexString

let allFields = AVPRClient.PackageContentHash(
PackageName = "name",
PackageMajorVersion = 1,
PackageMinorVersion = 0,
PackagePatchVersion = 0,
Hash = expected_hash
)


module ValidationPackage =

open System.IO

let expected_content =

"fixtures/test_validation_package_all_fields.fsx"
|> File.ReadAllBytes


let allFields = AVPRClient.ValidationPackage(
Name = "name",
Summary = "summary" ,
Description = "description" ,
MajorVersion = 1,
MinorVersion = 0,
PatchVersion = 0,
PackageContent = expected_content,
ReleaseDate = date,
Authors = [|Author.allFieldsClient|],
Tags = [|OntologyAnnotation.allFieldsClient|],
ReleaseNotes = "releasenotes",
CQCHookEndpoint = "hookendpoint"
)
8 changes: 0 additions & 8 deletions tests/ClientTests/Tests.fs

This file was deleted.

81 changes: 81 additions & 0 deletions tests/ClientTests/TypeExtensionsTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
namespace TypeExtensionsTests

open System
open System.IO
open System.Text
open Xunit
open AVPRClient

open System.Security.Cryptography

module ValidationPackageIndex =

let test_validation_package_all_fields = AVPRIndex.Domain.ValidationPackageIndex.create(
repoPath = "fixtures/test_validation_package_all_fields.fsx",
fileName = "test_validation_package_all_fields.fsx",
lastUpdated = ReferenceObjects.date,
contentHash = ReferenceObjects.PackageContentHash.expected_hash,
metadata = ReferenceObjects.ValidationPackageMetadata.allFields
)

[<Fact>]
let ``toValidationpackage with release date`` () =
let actual = test_validation_package_all_fields.toValidationPackage(ReferenceObjects.date)
Assert.Equivalent(actual, ReferenceObjects.ValidationPackage.allFields)

[<Fact>]
let ``toPackageContentHash without direct file hash`` () =
let actual = test_validation_package_all_fields.toPackageContentHash()
Assert.Equivalent(ReferenceObjects.PackageContentHash.allFields, actual)

[<Fact>]
let ``toPackageContentHash with direct file hash`` () =
let actual = test_validation_package_all_fields.toPackageContentHash(HashFileDirectly = true)
Assert.Equivalent(ReferenceObjects.PackageContentHash.allFields, actual)

module Author =

open System.Collections
open System.Collections.Generic

[<Fact>]
let ``AsIndexType with mandatory fields`` () =
let actual = ReferenceObjects.Author.mandatoryFieldsClient.AsIndexType()
Assert.Equivalent(actual, ReferenceObjects.Author.mandatoryFieldsClient)

[<Fact>]
let ``AsIndexType with all fields`` () =
let actual = ReferenceObjects.Author.allFieldsClient.AsIndexType()
Assert.Equivalent(actual, ReferenceObjects.Author.allFieldsClient)

[<Fact>]
let ``AsIndexType with mandatory fields on collection`` () =
let actual = [|ReferenceObjects.Author.mandatoryFieldsClient|].AsIndexType()
Assert.Equivalent(actual, [|ReferenceObjects.Author.mandatoryFieldsClient|])

[<Fact>]
let ``AsIndexType with all fields on collection`` () =
let actual = [|ReferenceObjects.Author.allFieldsClient|].AsIndexType()
Assert.Equivalent(actual, [|ReferenceObjects.Author.allFieldsClient|])

module OntologyAnnotation =

[<Fact>]
let ``AsIndexType with mandatory fields`` () =
let actual = ReferenceObjects.OntologyAnnotation.mandatoryFieldsClient.AsIndexType()
Assert.Equivalent(actual, ReferenceObjects.OntologyAnnotation.mandatoryFieldsClient)

[<Fact>]
let ``AsIndexType with all fields`` () =
let actual = ReferenceObjects.OntologyAnnotation.allFieldsClient.AsIndexType()
Assert.Equivalent(actual, ReferenceObjects.OntologyAnnotation.allFieldsClient)

[<Fact>]
let ``AsIndexType with mandatory fields on collection`` () =
let actual = [|ReferenceObjects.OntologyAnnotation.mandatoryFieldsClient|].AsIndexType()
Assert.Equivalent(actual, [|ReferenceObjects.OntologyAnnotation.mandatoryFieldsClient|])

[<Fact>]
let ``AsIndexType with all fields on collection`` () =
let actual = [|ReferenceObjects.OntologyAnnotation.allFieldsClient|].AsIndexType()
Assert.Equivalent(actual, [|ReferenceObjects.OntologyAnnotation.allFieldsClient|])
2 changes: 2 additions & 0 deletions tests/ClientTests/Utils.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Utils

24 changes: 24 additions & 0 deletions tests/ClientTests/fixtures/test_validation_package_all_fields.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(*
---
Name: name
Summary: summary
Description = description
MajorVersion: 1
MinorVersion: 0
PatchVersion: 0
Publish: true
Authors:
- FullName: test
Email: [email protected]
Affiliation: testaffiliation
AffiliationLink: test.com
Tags:
- Name: test
TermSourceREF: REF
TermAccessionNumber: TAN
ReleaseNotes: releasenotes
CQCHookEndpoint: hookendpoint
---
*)

printfn "yes"

0 comments on commit 74d64e6

Please sign in to comment.