Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: version update #284

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 3 additions & 5 deletions sharp/content.Tests/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
*/

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()
public async Task Test1()
{
await using var dataSource = NpgsqlDataSource.Create(_connectString);
var videoRepository = (IVideoRepository)new VideoRepository(dataSource);
Expand Down Expand Up @@ -77,7 +75,7 @@ public async void Test1()
}

[Fact(DisplayName = "Command")]
public async void TestInsert()
public async Task TestInsert()
{
await using var dataSource = NpgsqlDataSource.Create(_connectString);
var videoRepository = (IVideoRepository)new VideoRepository(dataSource);
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
10 changes: 4 additions & 6 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 All @@ -96,7 +94,7 @@ public async Task Likes_ReturnsExpectedVideos()


[Fact]
public async void CreateVideo_CallsServiceWithExpectedVideo()
public async Task CreateVideo_CallsServiceWithExpectedVideo()
{
var request = new VideoRequest
{
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
15 changes: 7 additions & 8 deletions sharp/content.Tests/repository/ProbeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
*
*/
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") !;

[Fact]
public async void Method()
public async Task Method()
{
// Arrange
var peg = new Probe(_executablePath);
Expand All @@ -36,7 +35,7 @@ public async void Method()
}

[Fact(DisplayName = "Empty executable path")]
public async void EmptyExecutablePath()
public async Task EmptyExecutablePath()
{
// Arrange
var peg = new Probe(string.Empty);
Expand All @@ -52,17 +51,17 @@ public async void EmptyExecutablePath()
}

[Fact(DisplayName = "empty url")]
public async void EmptyUrl()
public async Task EmptyUrl()
{
// Arrange
var peg = new Probe(_executablePath);
// Act && Assert
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
await Assert.ThrowsAsync<ArgumentException>(async () =>
await peg.GetVideoDuration(string.Empty));
}

[Fact(DisplayName = "resolution")]
public async void Resolution()
public async Task Resolution()
{
// Arrange
var peg = new Probe(_executablePath);
Expand All @@ -75,7 +74,7 @@ public async void Resolution()
}

[Fact(DisplayName = "not a video")]
public async void NotAVideo()
public async Task NotAVideo()
{
// Arrange
var peg = new Probe(_executablePath);
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
Loading
Loading