Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace CommunityToolkit.Datasync.Server.Test.Live;
namespace CommunityToolkit.Datasync.Server.Test.Helpers;

/// <summary>
/// The base set of tests for the controller tests going against a live server.
Expand Down Expand Up @@ -82,6 +81,21 @@ protected virtual async Task CreateControllerAsync(HttpMethod method = null, str
this.tableController.ControllerContext.HttpContext = CreateHttpContext(method ?? HttpMethod.Get, uri);
}

private async Task<List<TEntity>> GetListOfEntitiesAsync(IEnumerable<string> ids)
{
List<TEntity> entities = [];
foreach (string id in ids)
{
TEntity entity = await GetEntityAsync(id);
if (entity != null)
{
entities.Add(entity);
}
}

return entities;
}

/// <summary>
/// This is the base test for the individual query tests.
/// </summary>
Expand All @@ -104,7 +118,8 @@ private async Task MovieQueryTest(string pathAndQuery, int itemCount, string nex
List<TEntity> items = result.Items.Cast<TEntity>().ToList();
items.Should().HaveCount(itemCount);
result.Count.Should().Be(totalCount);
items.Select(m => m.Id).Take(firstItems.Length).Should().BeEquivalentTo(firstItems);
List<string> actualItems = items.Select(m => m.Id).Take(firstItems.Length).ToList();
actualItems.Should().BeEquivalentTo(firstItems);

if (nextLinkQuery is not null)
{
Expand All @@ -113,7 +128,7 @@ private async Task MovieQueryTest(string pathAndQuery, int itemCount, string nex
}
else
{
result.NextLink.Should().BeNull();
result.NextLink.Should().BeNull();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
using Microsoft.EntityFrameworkCore;
using Xunit.Abstractions;

namespace CommunityToolkit.Datasync.Server.Test.Live.AzureSQL;
namespace CommunityToolkit.Datasync.Server.Test.Live;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class AzureSql_Controller_Query_Tests : LiveControllerTests<AzureSqlEntityMovie>
public class AzureSQL_Controller_Tests : LiveControllerTests<AzureSqlEntityMovie>
{
#region Setup
private readonly DatabaseFixture _fixture;
Expand All @@ -21,7 +21,7 @@ public class AzureSql_Controller_Query_Tests : LiveControllerTests<AzureSqlEntit
private readonly List<AzureSqlEntityMovie> movies;
private readonly Lazy<AzureSqlDbContext> _context;

public AzureSql_Controller_Query_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
public AzureSQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
{
this._fixture = fixture;
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_AZSQL_CONNECTIONSTRING");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Datasync.Server.EntityFrameworkCore;
using CommunityToolkit.Datasync.Server.Test.Helpers;
using CommunityToolkit.Datasync.TestCommon.Databases;
using Microsoft.EntityFrameworkCore;
using Xunit.Abstractions;

namespace CommunityToolkit.Datasync.Server.Test.Live;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class PgSQL_Controller_Tests : LiveControllerTests<PgEntityMovie>
{
#region Setup
private readonly DatabaseFixture _fixture;
private readonly Random random = new();
private readonly string connectionString;
private readonly List<PgEntityMovie> movies;
private readonly Lazy<PgDbContext> _context;

public PgSQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
{
this._fixture = fixture;
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_PGSQL_CONNECTIONSTRING");
if (!string.IsNullOrEmpty(this.connectionString))
{
this._context = new Lazy<PgDbContext>(() => PgDbContext.CreateContext(this.connectionString, output));
this.movies = Context.Movies.AsNoTracking().ToList();
}
}

private PgDbContext Context { get => this._context.Value; }

protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);

protected override Task<PgEntityMovie> GetEntityAsync(string id)
=> Task.FromResult(Context.Movies.AsNoTracking().SingleOrDefault(m => m.Id == id));

protected override Task<int> GetEntityCountAsync()
=> Task.FromResult(Context.Movies.Count());

protected override Task<IRepository<PgEntityMovie>> GetPopulatedRepositoryAsync()
=> Task.FromResult<IRepository<PgEntityMovie>>(new EntityTableRepository<PgEntityMovie>(Context));

protected override Task<string> GetRandomEntityIdAsync(bool exists)
=> Task.FromResult(exists ? this.movies[this.random.Next(this.movies.Count)].Id : Guid.NewGuid().ToString());
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ FOR EACH ROW EXECUTE PROCEDURE

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PgEntityMovie>()
.Property(m => m.UpdatedAt).HasDefaultValueSql("NOW() AT TIME ZONE 'UTC'");
modelBuilder.Entity<PgEntityMovie>().Property(m => m.UpdatedAt)
.HasDefaultValueSql("NOW() AT TIME ZONE 'UTC'");

// Ensures stable ordering across all database types.
modelBuilder.Entity<PgEntityMovie>().Property(m => m.Title)
.UseCollation("POSIX");

base.OnModelCreating(modelBuilder);
}
}
Loading