Skip to content

Commit

Permalink
chore: version update
Browse files Browse the repository at this point in the history
  • Loading branch information
sixwaaaay committed Nov 16, 2024
1 parent 5775f1f commit 9f71e48
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 268 deletions.
43 changes: 12 additions & 31 deletions sharp/content.Tests/DomainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@
*/

using content.domainservice;

using JetBrains.Annotations;
using Moq;
using content.repository;

namespace content.Tests;
[TestSubject(typeof(DomainService))]

public class DomainTest
{

private (Mock<IVideoRepository>, Mock<IUserRepository> userRepo, Mock<IVoteRepository> voteRepo, Mock<SearchClient> searchClient) Setup() =>
(new Mock<IVideoRepository>(), new Mock<IUserRepository>(), new Mock<IVoteRepository>(), new Mock<SearchClient>(null!));

[Fact]
public async Task FindById_ReturnsVideoDto_WhenVideoExists()
{
// Arrange
var mockVideoRepo = new Mock<IVideoRepository>();
var mockUserRepo = new Mock<IUserRepository>();
var mockVoteRepo = new Mock<IVoteRepository>();
var mockSearchClient = new Mock<SearchClient>(null);
var (mockVideoRepo, mockUserRepo, mockVoteRepo, mockSearchClient) = Setup();
var video = new Video { Id = 1, UserId = 1 };
var user = new User { Id = "1" };
mockVideoRepo.Setup(repo => repo.FindById(1)).ReturnsAsync(video);
Expand All @@ -52,10 +51,7 @@ public async Task FindById_ReturnsVideoDto_WhenVideoExists()
public async Task FindAllByIds_ReturnsVideoDtos_WhenVideosExist()
{
// Arrange
var mockVideoRepo = new Mock<IVideoRepository>();
var mockUserRepo = new Mock<IUserRepository>();
var mockVoteRepo = new Mock<IVoteRepository>();
var mockSearchClient = new Mock<SearchClient>(null);
var (mockVideoRepo, mockUserRepo, mockVoteRepo, mockSearchClient) = Setup();
var videos = new List<Video> { new() { Id = 1, UserId = 1 }, new() { Id = 2, UserId = 2 } };
var users = new List<User> { new() { Id = "1" }, new() { Id = "2" } };
mockVideoRepo.Setup(repo => repo.FindAllByIds(It.IsAny<IReadOnlyList<long>>())).ReturnsAsync(videos);
Expand All @@ -75,10 +71,7 @@ public async Task FindAllByIds_ReturnsVideoDtos_WhenVideosExist()
public async Task Save_ReturnsSavedVideoDto_WhenVideoIsSaved()
{
// Arrange
var mockVideoRepo = new Mock<IVideoRepository>();
var mockUserRepo = new Mock<IUserRepository>();
var mockVoteRepo = new Mock<IVoteRepository>();
var mockSearchClient = new Mock<SearchClient>(null);
var (mockVideoRepo, mockUserRepo, mockVoteRepo, mockSearchClient) = Setup();
var video = new Video { Id = 1, UserId = 1 };
var user = new User { Id = "1" };
mockVideoRepo.Setup(repo => repo.Save(It.IsAny<Video>())).ReturnsAsync(video);
Expand All @@ -93,10 +86,7 @@ public async Task Save_ReturnsSavedVideoDto_WhenVideoIsSaved()
public async Task FindByUserId_ReturnsExpectedVideos()
{
// Arrange
var mockVideoRepo = new Mock<IVideoRepository>();
var mockUserRepo = new Mock<IUserRepository>();
var mockVoteRepo = new Mock<IVoteRepository>();
var mockSearchClient = new Mock<SearchClient>(null);
var (mockVideoRepo, mockUserRepo, mockVoteRepo, mockSearchClient) = Setup();
var videos = new List<Video> { new() { Id = 1, UserId = 1 }, new() { Id = 2, UserId = 1 } };
var user = new User { Id = "1" };
var voteVideoIds = new List<long> { 1, 2 };
Expand All @@ -117,10 +107,7 @@ public async Task FindByUserId_ReturnsExpectedVideos()
public async Task FindRecent_ReturnsExpectedVideos()
{
// Arrange
var mockVideoRepo = new Mock<IVideoRepository>();
var mockUserRepo = new Mock<IUserRepository>();
var mockVoteRepo = new Mock<IVoteRepository>();
var mockSearchClient = new Mock<SearchClient>(null);
var (mockVideoRepo, mockUserRepo, mockVoteRepo, mockSearchClient) = Setup();
var videos = new List<Video> { new() { Id = 1, UserId = 1 }, new() { Id = 2, UserId = 2 } };
var users = new List<User> { new() { Id = "1" }, new() { Id = "2" } };
var voteVideoIds = new List<long> { 1 };
Expand All @@ -141,10 +128,7 @@ public async Task FindRecent_ReturnsExpectedVideos()
public async Task DailyPopularVideos_ReturnsExpectedVideos()
{
// Arrange
var mockVideoRepo = new Mock<IVideoRepository>();
var mockUserRepo = new Mock<IUserRepository>();
var mockVoteRepo = new Mock<IVoteRepository>();
var mockSearchClient = new Mock<SearchClient>(null);
var (mockVideoRepo, mockUserRepo, mockVoteRepo, mockSearchClient) = Setup();
var videos = new List<Video> { new() { Id = 1, UserId = 1 }, new() { Id = 2, UserId = 2 } };
var users = new List<User> { new() { Id = "1" }, new() { Id = "2" } };
var voteVideoIds = new List<long> { 1 };
Expand All @@ -167,10 +151,7 @@ public async Task DailyPopularVideos_ReturnsExpectedVideos()
public async Task VotedVideos_ReturnsExpectedVideos()
{
// Arrange
var mockVideoRepo = new Mock<IVideoRepository>();
var mockUserRepo = new Mock<IUserRepository>();
var mockVoteRepo = new Mock<IVoteRepository>();
var mockSearchClient = new Mock<SearchClient>(null);
var (mockVideoRepo, mockUserRepo, mockVoteRepo, mockSearchClient) = Setup();
var videoIds = new long[] { 1, 2 };
var videos = new List<Video> { new() { Id = 1, UserId = 1 }, new() { Id = 2, UserId = 2 } };
var users = new List<User> { new() { Id = "1" }, new() { Id = "2" } };
Expand Down
4 changes: 1 addition & 3 deletions sharp/content.Tests/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@
*/

using content.repository;
using JetBrains.Annotations;
using Npgsql;
using Xunit.Abstractions;

namespace content.Tests;

public class UnitTest(ITestOutputHelper testOutputHelper)
{
private readonly string _connectString = Environment.GetEnvironmentVariable("CONNECTION_STRING") !;
private readonly string _connectString = Environment.GetEnvironmentVariable("CONNECTION_STRING")!;

[TestSubject(typeof(VideoRepository))]
[Fact(DisplayName = "Video Repository")]
public async void Test1()

Check warning on line 26 in sharp/content.Tests/UnitTest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Support for 'async void' unit tests is being removed from xUnit.net v3. To simplify upgrading, convert the test to 'async Task' instead. (https://xunit.net/xunit.analyzers/rules/xUnit1048)

Check warning on line 26 in sharp/content.Tests/UnitTest.cs

View workflow job for this annotation

GitHub Actions / build

Support for 'async void' unit tests is being removed from xUnit.net v3. To simplify upgrading, convert the test to 'async Task' instead. (https://xunit.net/xunit.analyzers/rules/xUnit1048)
{
Expand Down
7 changes: 3 additions & 4 deletions sharp/content.Tests/content.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
8 changes: 3 additions & 5 deletions sharp/content.Tests/endpoints/EndpointsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
using content.endpoints;
using content.repository;
using FluentValidation;
using JetBrains.Annotations;
using Moq;
using System.Security.Claims;

namespace content.Tests.endpoints;
[TestSubject(typeof(Endpoints))]
public class EndpointsTests
{
private readonly Mock<IDomainService> _mockService = new();
Expand Down Expand Up @@ -61,7 +59,7 @@ public async Task Videos_ReturnsExpectedVideos()

Assert.Equal(expectedVideos, result);
}

[Fact]
public async Task DailyPopularVideos_ReturnsExpectedVideos()
{
Expand All @@ -76,7 +74,7 @@ public async Task DailyPopularVideos_ReturnsExpectedVideos()

Assert.Equal(expectedVideos, result);
}


[Fact]
public async Task Likes_ReturnsExpectedVideos()
Expand Down Expand Up @@ -149,7 +147,7 @@ public async Task Save_ReturnsCorrectDto_WhenMessageIsSaved()
Type = 1
};
var expectedMessage = new MessageDto
{ Id = 1, ReceiverId = request.ReceiverId, Content = request.Content, Type = request.Type };
{ Id = 1, ReceiverId = request.ReceiverId, Content = request.Content, Type = request.Type };
_mockMessageDomain.Setup(s => s.Save(It.IsAny<Message>())).ReturnsAsync(expectedMessage);

// Act
Expand Down
5 changes: 2 additions & 3 deletions sharp/content.Tests/repository/ProbeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
*
*/
using content.repository;
using JetBrains.Annotations;
using Xunit.Abstractions;

namespace content.Tests.repository;

[TestSubject(typeof(Probe))]

public class ProbeTest(ITestOutputHelper testOutputHelper)
{
private readonly string _executablePath = Environment.GetEnvironmentVariable("FFPROBE_PATH") !;
Expand Down Expand Up @@ -57,7 +56,7 @@ public async void EmptyUrl()
// Arrange
var peg = new Probe(_executablePath);
// Act && Assert
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
await Assert.ThrowsAsync<ArgumentException>(async () =>
await peg.GetVideoDuration(string.Empty));
}

Expand Down
82 changes: 53 additions & 29 deletions sharp/content.Tests/repository/UserRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,72 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/

using System.Net;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
using content.repository;
using Grpc.Core;
using Grpc.Net.Client;
using JetBrains.Annotations;
using Moq;
using Moq.Protected;

namespace content.Tests.repository;

[TestSubject(typeof(UserRepository))]
public class UserRepositoryTest
{

private readonly ChannelBase _channel = GrpcChannel.ForAddress(Environment.GetEnvironmentVariable("USER_STRING") !);

[Fact(DisplayName = "FindUserById")]
public async void Test1()
{
var client = new UserRepository(_channel);
var result = await client.FindById(1);

Assert.NotNull(result);
Assert.Equal("1", result.Id);
}

[Fact(DisplayName = "FindUserByIds")]
public async void Test2()
public async Task Test1()
{
// Arange
var id = 1L;
var user = new User { Id = id.ToString(), Name = "test", AvatarUrl = "https://example.com/avatar.png", BgUrl = "https://example.com/bg.png", Bio = "test user", };
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = JsonContent.Create(user, UserJsonContext.Default.User), });
var httpClient = new HttpClient(mockHttpMessageHandler.Object) { BaseAddress = new Uri("http://localhost:5151") };
var client = new UserRepository(httpClient);
// Act
var result = await client.FindById(id);

var client = new UserRepository(_channel);
var result = await client.FindAllByIds(new []{1L, 2L});
// Assert
Assert.NotNull(result);
Assert.Equal(2, result.Count);
Assert.Equal(id.ToString(), result.Id);
Assert.Equal("test", result.Name);
Assert.Equal("https://example.com/avatar.png", result.AvatarUrl);
Assert.Equal("https://example.com/bg.png", result.BgUrl);
Assert.Equal("test user", result.Bio);
}
[Fact(DisplayName = "invalid token, error")]
public async void Test3()

[Fact(DisplayName = "FindUserByIds")]
public async Task Test2()
{
var client = new UserRepository(_channel)
// Arange
var ids = new List<long> { 1L, 2L };
var users = new List<User>
{
Token = "token"
new() {Id = "1",Name = "test1",AvatarUrl = "https://example.com/avatar1.png",BgUrl = "https://example.com/bg1.png",Bio = "test user1"},
new(){Id = "2",Name = "test2",AvatarUrl = "https://example.com/avatar2.png",BgUrl = "https://example.com/bg2.png",Bio = "test user2"}
};
await Assert.ThrowsAsync<RpcException>(async () => await client.FindAllByIds(new []{493764627922944111, 11}));
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = JsonContent.Create(users, UserJsonContext.Default.IReadOnlyListUser) });
var httpClient = new HttpClient(mockHttpMessageHandler.Object) { BaseAddress = new Uri("http://localhost:5151") };
var client = new UserRepository(httpClient);
// Act
var result = await client.FindAllByIds(ids);
// Assert
Assert.NotNull(result);
Assert.Equal(2, result.Count);
Assert.Equal("1", result[0].Id);
Assert.Equal("https://example.com/avatar1.png", result[0].AvatarUrl);
Assert.Equal("test user1", result[0].Bio);
Assert.Equal("2", result[1].Id);
Assert.Equal("https://example.com/bg2.png", result[1].BgUrl);
}
}
}

[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower)]
[JsonSerializable(typeof(IReadOnlyList<User>))]
partial class UserJsonContext : JsonSerializerContext;
2 changes: 1 addition & 1 deletion sharp/content/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN adduser -u 1000 --gecos "" --disabled-password appuser && chown -R appuser /
USER appuser
EXPOSE 5000

FROM mcr.microsoft.com/dotnet/sdk:9.0.100-preview.7-alpine3.20 AS build
FROM mcr.microsoft.com/dotnet/sdk:9.0.100-alpine3.20 AS build
RUN apk update && apk upgrade && apk add --no-cache clang build-base zlib-dev grpc-plugins
WORKDIR /src
ARG RUNTIME_ID=linux-musl-x64
Expand Down
14 changes: 6 additions & 8 deletions sharp/content/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
.AddSource(serviceName).AddNpgsql()
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName: serviceName))
.AddHttpClientInstrumentation()
.AddGrpcClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(o =>
{
Expand All @@ -66,7 +65,7 @@
}).WithMetrics(mtb =>
{
mtb
.AddMeter("Microsoft.AspNetCore.Hosting", "Microsoft.AspNetCore.Server.Kestrel", "Npgsql")
.AddMeter("Npgsql")

Check warning on line 68 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L68

Added line #L68 was not covered by tests
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddPrometheusExporter();
Expand All @@ -78,16 +77,15 @@
builder.Services.AddAuthorization().AddProbe();
builder.Services.AddProblemDetails().AddResponseCompression();

builder.Services.AddNpgsqlDataSource(builder.Configuration.GetConnectionString("Default") ??
throw new InvalidOperationException("Connection string is null"));
builder.Services.AddNpgsqlDataSource(builder.Configuration.GetConnectionString("Default").EnsureNotNull("Connection string is null"));

Check warning on line 80 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L80

Added line #L80 was not covered by tests

builder.Services.AddVideoRepository().AddNotificationRepository();

builder.Services.AddGrpcUser().AddUserRepository();
builder.Services.AddUserRepository();

Check warning on line 84 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L84

Added line #L84 was not covered by tests

builder.Services.AddVoteRepository(builder.Configuration.GetConnectionString("Vote") ?? throw new InvalidOperationException("Vote connection string is null"));
builder.Services.AddVoteRepository();

Check warning on line 86 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L86

Added line #L86 was not covered by tests

builder.Services.AddSearchClient(builder.Configuration.GetConnectionString("Search") ?? throw new InvalidOperationException("Search connection string is null"),
builder.Configuration["Token"] ?? throw new InvalidOperationException("Token is null"));
builder.Services.AddSearchClient();

Check warning on line 88 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L88

Added line #L88 was not covered by tests

builder.Services.AddDomainService().AddMessageDomain();

Expand Down
Loading

0 comments on commit 9f71e48

Please sign in to comment.