Skip to content

Commit

Permalink
Add unit tests for cover images and recipe list with details
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpotts committed Jan 31, 2024
1 parent ec5e381 commit 79992df
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
75 changes: 75 additions & 0 deletions tests/WebApi.Tests/Apis/RecipesApiUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,30 @@ public async void GetListAsyncReturnsRecipes()
Assert.NotNull(okResult.Value);

Assert.NotEmpty(okResult.Value.Items);

var firstItem = okResult.Value.Items.First();

Assert.Multiple(
() => Assert.Equal(6462416804118528, firstItem.Id),
() => Assert.Equal("73edf737-df51-4c06-ac6f-3ec6d79f1f12", firstItem.OwnerId),
() => Assert.Equal("Test Recipe 5", firstItem.Name),
() =>
{
Assert.NotNull(firstItem.CoverImage);
Assert.Equal("/api/v1/recipes/6462416804118528/coverImage", firstItem.CoverImage.Url);
Assert.Equal("A photo of test recipe 5", firstItem.CoverImage.AltText);
},
() =>
{
Assert.NotNull(firstItem.Cuisine);
Assert.Equal(1, firstItem.Cuisine.Id);
Assert.Equal("Test", firstItem.Cuisine.Name);
},
() => Assert.Null(firstItem.Description),
() => Assert.Equal(new DateTime(638412047602332665, DateTimeKind.Utc), firstItem.Created),
() => Assert.Null(firstItem.Modified),
() => Assert.Null(firstItem.Ingredients),
() => Assert.Null(firstItem.Instructions));
}

[Fact]
Expand Down Expand Up @@ -151,6 +175,51 @@ public async void GetListAsyncReturnsRecipesForCuisineIds()
() => Assert.All(okResult.Value.Items, x => Assert.Equal(4, x.Cuisine?.Id)));
}

[Fact]
public async void GetListAsyncReturnsRecipesForWithDetails()
{
// Act
var result = await RecipesApi.GetListAsync(_services, null, null, null, true);

// Assert
Assert.IsType<Ok<PagedResult<RecipeWithCuisineDto>>>(result.Result);
var okResult = (Ok<PagedResult<RecipeWithCuisineDto>>)result.Result;
Assert.NotNull(okResult.Value);

Assert.NotEmpty(okResult.Value.Items);

var firstItem = okResult.Value.Items.First();

Assert.Multiple(
() => Assert.Equal(6462416804118528, firstItem.Id),
() => Assert.Equal("73edf737-df51-4c06-ac6f-3ec6d79f1f12", firstItem.OwnerId),
() => Assert.Equal("Test Recipe 5", firstItem.Name),
() =>
{
Assert.NotNull(firstItem.CoverImage);
Assert.Equal("/api/v1/recipes/6462416804118528/coverImage", firstItem.CoverImage.Url);
Assert.Equal("A photo of test recipe 5", firstItem.CoverImage.AltText);
},
() =>
{
Assert.NotNull(firstItem.Cuisine);
Assert.Equal(1, firstItem.Cuisine.Id);
Assert.Equal("Test", firstItem.Cuisine.Name);
},
() => Assert.Equal("This is a test.", firstItem.Description),
() => Assert.Equal(new DateTime(638412047602332665, DateTimeKind.Utc), firstItem.Created),
() => Assert.Null(firstItem.Modified),
() =>
{
Assert.NotNull(firstItem.Ingredients);
Assert.Collection(firstItem.Ingredients,
x => Assert.Equal("1 tsp of test ingredient 1", x),
x => Assert.Equal("1 cup of test ingredient 2", x));
},
() => Assert.Equal("This is a test.", firstItem.Instructions?.Markdown),
() => Assert.Equal("<p>This is a test.</p>\n", firstItem.Instructions?.Html));
}

[Fact]
public async void GetAsyncReturnsRecipeForValidId()
{
Expand All @@ -167,6 +236,12 @@ public async void GetAsyncReturnsRecipeForValidId()
() => Assert.Equal("73edf737-df51-4c06-ac6f-3ec6d79f1f12", okResult.Value.OwnerId),
() => Assert.Equal("Test Recipe 3", okResult.Value.Name),
() =>
{
Assert.NotNull(okResult.Value.CoverImage);
Assert.Equal("/api/v1/recipes/6462258523668480/coverImage", okResult.Value.CoverImage.Url);
Assert.Equal("A photo of test recipe 3", okResult.Value.CoverImage.AltText);
},
() =>
{
Assert.NotNull(okResult.Value.Cuisine);
Assert.Equal(1, okResult.Value.Cuisine.Id);
Expand Down
25 changes: 25 additions & 0 deletions tests/WebApi.Tests/Apis/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public static class TestData
Id = 6461870173061120,
OwnerId = "73edf737-df51-4c06-ac6f-3ec6d79f1f12",
Name = "Test Recipe 1",
CoverImage = new ImageData
{
Url = "6461870173061120.webp",
AltText = "A photo of test recipe 1"
},
CuisineId = 1,
Description = "This is a test.",
Created = new DateTime(638412046299055561, DateTimeKind.Utc),
Expand All @@ -100,6 +105,11 @@ public static class TestData
Id = 6462160192405504,
OwnerId = "d7df5331-1c53-491f-8b71-91989846874f",
Name = "Test Recipe 2",
CoverImage = new ImageData
{
Url = "6462160192405504.webp",
AltText = "A photo of test recipe 2"
},
CuisineId = 1,
Description = "This is a test.",
Created = new DateTime(638412046990521543, DateTimeKind.Utc),
Expand All @@ -119,6 +129,11 @@ public static class TestData
Id = 6462258523668480,
OwnerId = "73edf737-df51-4c06-ac6f-3ec6d79f1f12",
Name = "Test Recipe 3",
CoverImage = new ImageData
{
Url = "6462258523668480.webp",
AltText = "A photo of test recipe 3"
},
CuisineId = 1,
Description = "This is a test.",
Created = new DateTime(638412047224957774, DateTimeKind.Utc),
Expand All @@ -138,6 +153,11 @@ public static class TestData
Id = 6462318867120128,
OwnerId = "d7df5331-1c53-491f-8b71-91989846874f",
Name = "Test Recipe 4",
CoverImage = new ImageData
{
Url = "6462318867120128.webp",
AltText = "A photo of test recipe 4"
},
CuisineId = 4,
Description = "This is a test.",
Created = new DateTime(638412047368832961, DateTimeKind.Utc),
Expand All @@ -157,6 +177,11 @@ public static class TestData
Id = 6462416804118528,
OwnerId = "73edf737-df51-4c06-ac6f-3ec6d79f1f12",
Name = "Test Recipe 5",
CoverImage = new ImageData
{
Url = "6462416804118528.webp",
AltText = "A photo of test recipe 5"
},
CuisineId = 1,
Description = "This is a test.",
Created = new DateTime(638412047602332665, DateTimeKind.Utc),
Expand Down

0 comments on commit 79992df

Please sign in to comment.