Skip to content

Commit

Permalink
fix: restore tests and fix them (#254)
Browse files Browse the repository at this point in the history
* fix: restore tests

* feat: add nocontent tests

* fix: add tests for fsproj

* fix: method names

* fix: map endpoints

* fix: formatting

* fix: some PetStore tests

* fix: petstore

* fix: more tests

* fix: user agent parsing

* fix: one platform for testing should be enough
  • Loading branch information
sergey-tihon authored Jan 6, 2025
1 parent 2f09c7a commit a4d902a
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}

steps:
Expand Down
2 changes: 1 addition & 1 deletion build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Target.create "BuildTests" (fun _ -> dotnet "build" "SwaggerProvider.TestsAndDoc
// Run the unit tests using test runner

let runTests assembly =
dotnet "test" $"{assembly} -c Release --no-build"
dotnet $"{assembly}" ""

Target.create "RunUnitTests" (fun _ -> runTests "tests/SwaggerProvider.Tests/bin/Release/net9.0/SwaggerProvider.Tests.dll")

Expand Down
8 changes: 4 additions & 4 deletions src/Common/AssemblyInfo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ open System.Reflection
[<assembly: AssemblyTitleAttribute("SwaggerProvider")>]
[<assembly: AssemblyProductAttribute("SwaggerProvider")>]
[<assembly: AssemblyDescriptionAttribute("F# Type Provider for Swagger & Open API")>]
[<assembly: AssemblyVersionAttribute("2.1.3")>]
[<assembly: AssemblyFileVersionAttribute("2.1.3")>]
[<assembly: AssemblyVersionAttribute("2.2.0")>]
[<assembly: AssemblyFileVersionAttribute("2.2.0")>]
do ()

module internal AssemblyVersionInformation =
let [<Literal>] AssemblyTitle = "SwaggerProvider"
let [<Literal>] AssemblyProduct = "SwaggerProvider"
let [<Literal>] AssemblyDescription = "F# Type Provider for Swagger & Open API"
let [<Literal>] AssemblyVersion = "2.1.3"
let [<Literal>] AssemblyFileVersion = "2.1.3"
let [<Literal>] AssemblyVersion = "2.2.0"
let [<Literal>] AssemblyFileVersion = "2.2.0"
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net9.0</TargetFrameworks>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
Expand Down Expand Up @@ -28,6 +29,7 @@
<Compile Include="v3\Swashbuckle.UpdateControllers.Tests.fs" />
<Compile Include="v3\Swashbuckle.ResourceControllers.Tests.fs" />
<Compile Include="v3\Swashbuckle.FileController.Tests.fs" />
<Compile Include="v3\Swashbuckle.NoContentControllers.Tests.fs" />
<Compile Include="v3\Swashbuckle.SpecialCasesControllers.Tests.fs" />
<Compile Include="..\SwaggerProvider.Tests\APIs.guru.fs">
<Link>APIs.guru.fs</Link>
Expand Down
15 changes: 9 additions & 6 deletions tests/SwaggerProvider.ProviderTests/v2/Swagger.GitHub.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ open System.Net.Http
[<Literal>]
let Schema = __SOURCE_DIRECTORY__ + "/../Schemas/v2/github.json"

[<Literal>]
let UserAgent =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"


[<Literal>]
let Host = "https://api.github.com"

Expand All @@ -18,9 +23,8 @@ let github() =
let client = GitHub.Client()
client.HttpClient.BaseAddress <- Uri Host

"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"
|> Headers.ProductInfoHeaderValue.Parse
|> client.HttpClient.DefaultRequestHeaders.UserAgent.Add
client.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", UserAgent)
|> ignore

client

Expand All @@ -32,9 +36,8 @@ let taskGitHub() =
let client = TaskGitHub.Client()
client.HttpClient.BaseAddress <- Uri Host

"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"
|> Headers.ProductInfoHeaderValue.Parse
|> client.HttpClient.DefaultRequestHeaders.UserAgent.Add
client.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", UserAgent)
|> ignore

client

Expand Down
20 changes: 9 additions & 11 deletions tests/SwaggerProvider.ProviderTests/v2/Swagger.PetStore.Tests.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Swagger.PetStore.Tests
module Swagger.PetStore.Tests

open SwaggerProvider
open Swagger
Expand All @@ -25,7 +25,7 @@ let ``Test provided Host property``() =
let store = PetStore.Client()

store.HttpClient.BaseAddress.ToString()
|> shouldEqual "https://petstore.swagger.io/v2/"
|> shouldEqual "https://petstore.swagger.io/"

store.HttpClient.BaseAddress <- Uri "http://petstore.swagger.io/v3/"

Expand All @@ -45,24 +45,22 @@ let ``Instantiate provided objects``() =
let ``throw custom exceptions from async``() =
task {
try
let! _ = store.GetPetById(-142L)
let! _ = store.GetPetById(242L)
failwith "Call should fail"
with :? System.AggregateException as aex ->
match aex.InnerException with
| :? OpenApiException as ex -> ex.Description |> shouldEqual "Pet not found"
| :? System.Net.Http.HttpRequestException as ex -> ex.Message |> shouldContainText "Not Found"
| _ -> raise aex
}

[<Fact>]
let ``throw custom exceptions from task``() =
task {
try
let! _ = storeTask.GetPetById(-142L)
let! _ = storeTask.GetPetById(342L)
failwith "Call should fail"
with :? System.AggregateException as aex ->
match aex.InnerException with
| :? OpenApiException as ex -> ex.Description |> shouldEqual "Pet not found"
| _ -> raise aex
with :? System.Net.Http.HttpRequestException as ex ->
ex.Message |> shouldContainText "Not Found"
}

[<Fact>]
Expand All @@ -76,7 +74,7 @@ let ``call provided methods``() =
()

let tag = PetStore.Tag(None, "foobar")
tag.ToString() |> shouldEqual "foobar"
tag.Name |> shouldEqual "foobar"
let pet = PetStore.Pet("foo", [||], Some id)
pet.ToString() |> shouldContainText(id.ToString())

Expand All @@ -102,7 +100,7 @@ let ``call provided methods``() =
[<Fact>]
let ``create types with Nullable properties``() =
let tag = PetStoreNullable.Tag(Nullable<_>(), "foobar")
tag.ToString() |> shouldEqual "foobar"
tag.Name |> shouldEqual "foobar"
let tag2 = PetStoreNullable.Tag(Name = "foobar")
tag2.ToString() |> shouldContainText "foobar"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Swashbuckle.v2.ResourceControllersTests
module Swashbuckle.v2.ResourceControllersTests

open Xunit
open FsUnitTyped
Expand All @@ -7,18 +7,18 @@ open Swashbuckle.v2.ReturnControllersTests
[<Fact>]
let ``ResourceStringString Add and get from resource dictionary``() =
task {
do! api.PutApiResourceStringString("lang", "F#")
do! api.GetApiResourceStringString("lang") |> asyncEqual "F#"
do! api.PutApiResourceStringString("language", "Fsharp")
do! api.GetApiResourceStringString("language") |> asyncEqual "Fsharp"
}

[<Fact>]
let ``ResourceStringString Update value in the resource dictionary``() =
task {
do! api.PutApiResourceStringString("name", "Sergey")
do! api.GetApiResourceStringString("name") |> asyncEqual "Sergey"
do! api.PutApiResourceStringString("name2", "Sergey")
do! api.GetApiResourceStringString("name2") |> asyncEqual "Sergey"

do! api.PostApiResourceStringString("name", "Siarhei")
do! api.GetApiResourceStringString("name") |> asyncEqual "Siarhei"
do! api.PostApiResourceStringString("name2", "Siarhei")
do! api.GetApiResourceStringString("name2") |> asyncEqual "Siarhei"
}

let ``ResourceStringString Delete from the dictionary``() =
Expand Down
12 changes: 5 additions & 7 deletions tests/SwaggerProvider.ProviderTests/v3/Swagger.PetStore.Tests.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Swagger.v3.PetStore.Tests
module Swagger.v3.PetStore.Tests

open SwaggerProvider
open Swagger
Expand Down Expand Up @@ -59,10 +59,8 @@ let ``throw custom exceptions from task``() =
try
let! _ = storeTask.GetPetById(-142L)
failwith "Call should fail"
with :? System.AggregateException as aex ->
match aex.InnerException with
| :? OpenApiException as ex -> ex.Description |> shouldEqual "Pet not found"
| _ -> raise aex
with :? OpenApiException as ex ->
ex.Description |> shouldEqual "Pet not found"
}

[<Fact>]
Expand All @@ -76,7 +74,7 @@ let ``call provided methods``() =
()

let tag = PetStore.Tag(None, "foobar")
tag.ToString() |> shouldEqual "foobar"
tag.Name |> shouldEqual "foobar"
let pet = PetStore.Pet("foo", [||], Some id)
pet.ToString() |> shouldContainText(id.ToString())

Expand All @@ -102,7 +100,7 @@ let ``call provided methods``() =
[<Fact>]
let ``create types with Nullable properties``() =
let tag = PetStoreNullable.Tag(Nullable<_>(), "foobar")
tag.ToString() |> shouldEqual "foobar"
tag.Name |> shouldEqual "foobar"
let tag2 = PetStoreNullable.Tag(Name = "foobar")
tag2.ToString() |> shouldContainText "foobar"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Swashbuckle.v3.NoContentControllersTests

open Xunit
open Swashbuckle.v3.ReturnControllersTests

[<Fact>]
let ``Test 204 with GET``() =
task { do! api.GetApiNoContent() }

[<Fact>]
let ``Test 204 with POST``() =
task { do! api.PostApiNoContent() }

[<Fact>]
let ``Test 204 with PUT``() =
task { do! api.PutApiNoContent() }

[<Fact>]
let ``Test 204 with DELETE``() =
task { do! api.DeleteApiNoContent() }
2 changes: 1 addition & 1 deletion tests/SwaggerProvider.Tests/Schema.Parser.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ let unsupportedSchemaPaths =
|> List.filter(fun s -> s.IndexOf("unsupported") > 0)
|> List.map(fun s -> [| box s |])

[<Theory; MemberData(nameof(unsupportedSchemaPaths))>]
[<Theory(Skip = "no samples"); MemberData(nameof(unsupportedSchemaPaths))>]
let ``Fail to parse`` file =
let file = Path.Combine(rootFolder, file)
shouldFail(fun () -> parserTestBody file |> Async.AwaitTask |> Async.RunSynchronously)
Expand Down
1 change: 1 addition & 0 deletions tests/SwaggerProvider.Tests/SwaggerProvider.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net9.0</TargetFrameworks>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Swashbuckle.WebApi.Server.Controllers

open Microsoft.AspNetCore.Mvc

[<Route("api/[controller]")>]
[<ApiController>]
type NoContentController() =
inherit ControllerBase()

[<HttpGet>]
[<Produces("application/json")>]
[<ProducesResponseType(204)>]
member x.Get() =
x.NoContent()

[<HttpPost>]
[<Produces("application/json")>]
[<ProducesResponseType(204)>]
member x.Post() =
x.NoContent()

[<HttpPut>]
[<Produces("application/json")>]
[<ProducesResponseType(204)>]
member x.Put() =
x.NoContent()

[<HttpDelete>]
[<Produces("application/json")>]
[<ProducesResponseType(204)>]
member x.Delete() =
x.NoContent()
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
namespace Swashbuckle.WebApi.Server.Controllers

open System
open Microsoft.AspNetCore.Mvc
open Swagger.Internal

[<Route("api/[controller]")>]
[<ApiController>]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
namespace Swashbuckle.WebApi.Server.Controllers

open System
open System.Collections.Generic
open System.Linq
open System.Threading.Tasks
open Microsoft.AspNetCore.Mvc


(*
//[<Route("api/[controller]")>]
Expand Down
6 changes: 0 additions & 6 deletions tests/Swashbuckle.WebApi.Server/Program.fs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
namespace Swashbuckle.WebApi.Server

open System
open System.Collections.Generic
open System.IO
open System.Linq
open System.Threading.Tasks
open Microsoft.AspNetCore
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.Logging

module Program =
let exitCode = 0
Expand Down
6 changes: 4 additions & 2 deletions tests/Swashbuckle.WebApi.Server/Startup.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace Swashbuckle.WebApi.Server

open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Mvc
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.DependencyInjection
open Microsoft.OpenApi.Models
Expand Down Expand Up @@ -33,7 +32,7 @@ type Startup private () =
|> ignore

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
member this.Configure(app: IApplicationBuilder, env: IWebHostEnvironment) =
member this.Configure(app: IApplicationBuilder, _: IWebHostEnvironment) =
app.UseDeveloperExceptionPage() |> ignore
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
//app.UseHsts() |> ignore
Expand All @@ -59,4 +58,7 @@ type Startup private () =
//app.UseHttpsRedirection() |> ignore
app.UseRouting() |> ignore

app.UseEndpoints(fun endpoints -> endpoints.MapControllers() |> ignore)
|> ignore

member val Configuration: IConfiguration = null with get, set
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Compile Include="Controllers\ResourceControllers.fs" />
<Compile Include="Controllers\ValuesController.fs" />
<Compile Include="Controllers\FileController.fs" />
<Compile Include="Controllers\NoContentControllers.fs" />
<Compile Include="Controllers\SpecialCasesControllers.fs" />
<Compile Include="Startup.fs" />
<Compile Include="Program.fs" />
Expand Down

0 comments on commit a4d902a

Please sign in to comment.