-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCreateOrEditTest.cs
113 lines (104 loc) · 4.11 KB
/
CreateOrEditTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Collections.Generic;
using System.IO;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using ContosoAds.Web.Commands;
using ContosoAds.Web.Model;
using Dapr.Client;
using FakeItEasy;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using static ContosoAds.Web.UnitTests.TestSupport;
namespace ContosoAds.Web.UnitTests;
public class CreateOrEditTest
{
[Theory]
[InlineData("A car", "Nice ride", "+1 425 555 1234", 5000, Category.Cars)]
[InlineData("A freebie", "Nice junk", "+1 425 555 1234", 0, Category.FreeStuff)]
[InlineData("A house", "Nice real estate", "+1 425 555 1234", 1000000, Category.RealEstate)]
public async Task It_Creates_Ad(
string title,
string description,
string phone,
int price,
Category category)
{
// Arrange
const string dbName = nameof(CreateOrEditTest);
var logger = A.Fake<ILogger<CreateOrEditAd>>();
var daprClient = A.Fake<DaprClient>();
await using var initialDbContext = await CreateTestDbContext(dbName);
var ad = new Ad
{
Title = title,
Description = description,
Phone = phone,
Price = price,
Category = category
};
// Act
var command = new CreateOrEditAd(initialDbContext, daprClient, logger);
await command.ExecuteAsync(ad, null);
// Assert
await using var testDbContext = await CreateTestDbContext(dbName);
var createdAd = await testDbContext.Ads.FindAsync([ad.Id], TestContext.Current.CancellationToken);
Assert.Equal(title, createdAd?.Title);
Assert.Equal(description, createdAd?.Description);
Assert.Equal(phone, createdAd?.Phone);
Assert.Equal(price, createdAd?.Price);
Assert.Equal(category, createdAd?.Category);
}
[Theory]
[InlineData("A car", "Nice ride", "+1 425 555 1234", 5000, Category.Cars, "car.jpg")]
[InlineData("A freebie", "Nice junk", "+1 425 555 1234", 0, Category.FreeStuff, "freebie.jpg")]
[InlineData("A house", "Nice real estate", "+1 425 555 1234", 1000000, Category.RealEstate, "house.jpg")]
public async Task It_Sets_Image_For_Ad(
string title,
string description,
string phone,
int price,
Category category,
string fileName)
{
// Arrange
const string dbName = nameof(CreateOrEditTest);
var logger = A.Fake<ILogger<CreateOrEditAd>>();
var daprClient = A.Fake<DaprClient>();
A.CallTo(() => daprClient.InvokeBindingAsync<byte[], JsonNode>(
A<string>._,
A<string>._,
A<byte[]>._,
A<IReadOnlyDictionary<string, string>>._,
A<CancellationToken>._))
.Returns(Task.FromResult(CreateBlobResponse(fileName)));
await using var initialDbContext = await CreateTestDbContext(dbName);
var formFile = A.Fake<IFormFile>();
A.CallTo(() => formFile.FileName).Returns(fileName);
A.CallTo(() => formFile.Length).Returns(1);
A.CallTo(() => formFile.ContentType).Returns("image/jpeg");
A.CallTo(() => formFile.OpenReadStream()).Returns(new MemoryStream());
var ad = new Ad
{
Title = title,
Description = description,
Phone = phone,
Price = price,
Category = category
};
// Act
var command = new CreateOrEditAd(initialDbContext, daprClient, logger);
await command.ExecuteAsync(ad, formFile);
// Assert
await using var testDbContext = await CreateTestDbContext(dbName);
var createdAd = await testDbContext.Ads.FindAsync([ad.Id], TestContext.Current.CancellationToken);
Assert.NotNull(createdAd?.ImageUri);
A.CallTo(() => daprClient.InvokeBindingAsync<byte[], JsonNode>(
A<string>._,
A<string>._,
A<byte[]>._,
A<IReadOnlyDictionary<string, string>>._,
A<CancellationToken>._))
.MustHaveHappenedOnceExactly();
}
}