Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
89 changes: 83 additions & 6 deletions infra/modules/cosmos.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,88 @@ param tags object = {}

/*********************************************************************************/

var compositeIndices = [
[
{ path: '/BestPictureWinner', order: 'ascending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/BestPictureWinner', order: 'descending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Duration', order: 'ascending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Duration', order: 'descending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Rating', order: 'ascending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Rating', order: 'descending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/ReleaseDate', order: 'ascending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/ReleaseDate', order: 'descending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Title', order: 'ascending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Title', order: 'descending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/UpdatedAt', order: 'ascending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/UpdatedAt', order: 'descending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Year', order: 'ascending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Year', order: 'descending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Year', order: 'ascending' }
{ path: '/Title', order: 'ascending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Year', order: 'descending' }
{ path: '/Title', order: 'ascending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Year', order: 'ascending' }
{ path: '/Title', order: 'descending' }
{ path: '/id', order: 'ascending' }
]
[
{ path: '/Year', order: 'descending' }
{ path: '/Title', order: 'descending' }
{ path: '/id', order: 'ascending' }
]
]

/*********************************************************************************/


resource cosmos_account 'Microsoft.DocumentDB/databaseAccounts@2024-02-15-preview' = {
name: serverName
location: location
Expand Down Expand Up @@ -70,12 +152,7 @@ resource cosmos_container 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/co
excludedPaths: [
{ path: '/_etag/?' }
]
compositeIndexes: [
[
{ path: '/UpdatedAt', order: 'ascending' }
{ path: '/Id', order: 'ascending' }
]
]
compositeIndexes: compositeIndices
}
defaultTtl: 86400
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Datasync.TestCommon;
using CommunityToolkit.Datasync.TestCommon.Databases;
using CommunityToolkit.Datasync.TestCommon.Models;
using LiteDB;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ public abstract class LiveControllerTests<TEntity> : BaseTest where TEntity : cl
protected virtual bool CanRunLiveTests() => true;

/// <summary>
/// Some tests require the ability to run math queries. This method returns true if the
/// service can run those tests. Notably, Cosmos can't run those tests.
/// The driver name - used for skipping tests when the driver doesn't support a feature.
/// </summary>
protected virtual bool CanRunMathQueryTests() => true;
protected virtual string DriverName { get; } = "Default";

/// <summary>
/// The actual test class must provide an implementation that retrieves the entity through
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace CommunityToolkit.Datasync.Server.Test.Helpers;
/// </summary>
public class DatabaseFixture
{

public bool AzureSqlIsInitialized { get; set; } = false;
public bool CosmosIsInitialized { get; set; } = false;
public bool PgIsInitialized { get; set; } = false;
}

[CollectionDefinition("LiveTestsCollection", DisableParallelization = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ public class AzureSQL_Controller_Tests : LiveControllerTests<AzureSqlEntityMovie
private readonly Random random = new();
private readonly string connectionString;
private readonly List<AzureSqlEntityMovie> movies;
private readonly Lazy<AzureSqlDbContext> _context;

public AzureSQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
{
this._fixture = fixture;
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_AZSQL_CONNECTIONSTRING");
if (!string.IsNullOrEmpty(this.connectionString))
{
this._context = new Lazy<AzureSqlDbContext>(() => AzureSqlDbContext.CreateContext(this.connectionString, output));
output.WriteLine($"AzureSqlIsInitialized = {this._fixture.AzureSqlIsInitialized}");
Context = AzureSqlDbContext.CreateContext(this.connectionString, output, clearEntities: !this._fixture.AzureSqlIsInitialized);
this.movies = [.. Context.Movies.AsNoTracking()];
this._fixture.AzureSqlIsInitialized = true;
}
}

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

protected override string DriverName { get; } = "AzureSQL";

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 Cosmos_Controller_Tests : LiveControllerTests<CosmosEntityMovie>
{
#region Setup
private readonly DatabaseFixture _fixture;
private readonly Random random = new();
private readonly string connectionString;
private readonly List<CosmosEntityMovie> movies;

public Cosmos_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : base()
{
this._fixture = fixture;
this.connectionString = Environment.GetEnvironmentVariable("DATASYNC_COSMOS_CONNECTIONSTRING");
if (!string.IsNullOrEmpty(this.connectionString))
{
// Note: we don't clear entities on every run to speed up the test runs. This can only be done because
// the tests are read-only (associated with the query and get capabilities). If the test being run writes
// to the database then change clearEntities to true.
output.WriteLine($"CosmosIsInitialized = {this._fixture.CosmosIsInitialized}");
Context = CosmosDbContext.CreateContext(this.connectionString, output, clearEntities: !this._fixture.CosmosIsInitialized);
this.movies = [.. Context.Movies.AsNoTracking()];
this._fixture.CosmosIsInitialized = true;
}
}

private CosmosDbContext Context { get; set; }

protected override string DriverName { get; } = "Cosmos";

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

protected override Task<CosmosEntityMovie> 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<CosmosEntityMovie>> GetPopulatedRepositoryAsync()
=> Task.FromResult<IRepository<CosmosEntityMovie>>(new EntityTableRepository<CosmosEntityMovie>(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
@@ -0,0 +1,36 @@
// 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.InMemory;
using CommunityToolkit.Datasync.Server.Test.Helpers;
using CommunityToolkit.Datasync.TestCommon.Databases;

namespace CommunityToolkit.Datasync.Server.Test.Live;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class InMemory_Controller_Tests : LiveControllerTests<InMemoryMovie>
{
#region Setup
private readonly Random random = new();
private InMemoryRepository<InMemoryMovie> repository;

protected override string DriverName { get; } = "InMemory";

protected override Task<InMemoryMovie> GetEntityAsync(string id)
=> Task.FromResult(this.repository.GetEntity(id));

protected override Task<int> GetEntityCountAsync()
=> Task.FromResult(this.repository.GetEntities().Count);

protected override Task<IRepository<InMemoryMovie>> GetPopulatedRepositoryAsync()
{
this.repository = new InMemoryRepository<InMemoryMovie>(TestCommon.TestData.Movies.OfType<InMemoryMovie>());
return Task.FromResult<IRepository<InMemoryMovie>>(this.repository);
}

protected override Task<string> GetRandomEntityIdAsync(bool exists)
=> Task.FromResult(exists ? this.repository.GetEntities()[this.random.Next(this.repository.GetEntities().Count)].Id : Guid.NewGuid().ToString());
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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.Abstractions.Http;
using CommunityToolkit.Datasync.Server.InMemory;
using CommunityToolkit.Datasync.Server.LiteDb;
using CommunityToolkit.Datasync.Server.Test.Helpers;
using CommunityToolkit.Datasync.TestCommon.Databases;
using LiteDB;

namespace CommunityToolkit.Datasync.Server.Test.Live;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class LiteDb_Controller_Tests : LiveControllerTests<LiteDbMovie>, IDisposable
{
#region Setup
private readonly Random random = new();
private string dbFilename;
private LiteDatabase database;
private ILiteCollection<LiteDbMovie> collection;
private LiteDbRepository<LiteDbMovie> repository;
private readonly List<LiteDbMovie> movies = [];

protected override Task<LiteDbMovie> GetEntityAsync(string id)
=> Task.FromResult(this.collection.FindById(id));

protected override Task<int> GetEntityCountAsync()
=> Task.FromResult(this.collection.Count());

protected override Task<IRepository<LiteDbMovie>> GetPopulatedRepositoryAsync()
{
this.dbFilename = Path.GetTempFileName();
this.database = new LiteDatabase($"Filename={this.dbFilename};Connection=direct;InitialSize=0");
this.collection = this.database.GetCollection<LiteDbMovie>("litedbmovies");

foreach (LiteDbMovie movie in TestCommon.TestData.Movies.OfType<LiteDbMovie>())
{
movie.UpdatedAt = DateTimeOffset.Now;
movie.Version = Guid.NewGuid().ToByteArray();
this.collection.Insert(movie);
this.movies.Add(movie);
}

this.repository = new LiteDbRepository<LiteDbMovie>(this.database);
return Task.FromResult<IRepository<LiteDbMovie>>(this.repository);
}

protected override Task<string> GetRandomEntityIdAsync(bool exists)
=> Task.FromResult(exists ? this.movies[random.Next(this.movies.Count)].Id : Guid.NewGuid().ToString());

Check warning on line 51 in tests/CommunityToolkit.Datasync.Server.Test/Live/LiteDb_Controller_Tests.cs

View workflow job for this annotation

GitHub Actions / build

Add 'this' or 'Me' qualification (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0009)

Check warning on line 51 in tests/CommunityToolkit.Datasync.Server.Test/Live/LiteDb_Controller_Tests.cs

View workflow job for this annotation

GitHub Actions / build

Add 'this' or 'Me' qualification (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0009)

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this.database?.Dispose();
if (!string.IsNullOrEmpty(this.dbFilename))
{
File.Delete(this.dbFilename);
}
}
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ public class PgSQL_Controller_Tests : LiveControllerTests<PgEntityMovie>
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));
output.WriteLine($"PgIsInitialized = {this._fixture.PgIsInitialized}");
Context = PgDbContext.CreateContext(this.connectionString, output, clearEntities: !this._fixture.PgIsInitialized);
this.movies = Context.Movies.AsNoTracking().ToList();
this._fixture.PgIsInitialized = true;
}
}

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

protected override string DriverName { get; } = "PgSQL";

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
<ProjectReference Include="..\..\src\CommunityToolkit.Datasync.Server.Abstractions\CommunityToolkit.Datasync.Server.Abstractions.csproj" />
<ProjectReference Include="..\..\src\CommunityToolkit.Datasync.Server.EntityFrameworkCore\CommunityToolkit.Datasync.Server.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\src\CommunityToolkit.Datasync.Server.InMemory\CommunityToolkit.Datasync.Server.InMemory.csproj" />
<ProjectReference Include="..\..\src\CommunityToolkit.Datasync.Server.LiteDb\CommunityToolkit.Datasync.Server.LiteDb.csproj" />
</ItemGroup>
</Project>
Loading
Loading