Skip to content

Commit

Permalink
fix cosmos tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Sokol committed Jan 13, 2023
1 parent 479f27e commit fb9f1d5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
23 changes: 22 additions & 1 deletion ManagedCode.Database.Tests/CosmosTests/CosmosCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ManagedCode.Database.Tests.BaseTests;
using ManagedCode.Database.Tests.Common;
using ManagedCode.Database.Tests.TestContainers;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -12,7 +13,7 @@ namespace ManagedCode.Database.Tests.CosmosTests;
[Collection(nameof(CosmosTestContainer))]
public class CosmosCollectionTests : BaseCollectionTests<string, TestCosmosItem>
{
public CosmosCollectionTests(ITestOutputHelper testOutputHelper, CosmosTestContainer container) : base(container)
public CosmosCollectionTests(CosmosTestContainer container) : base(container)
{
}

Expand All @@ -36,4 +37,24 @@ public override async Task DeleteListOfItems_WhenItemsDontExist()

await baseMethod.Should().ThrowExactlyAsync<DatabaseException>();
}

[Fact]
public override async Task DeleteCollectionAsync()
{
// Arrange
int itemsCount = 5;
List<TestCosmosItem> list = new();

for (var i = 0; i < itemsCount; i++)
{
list.Add(CreateNewItem());
}

await Collection.InsertAsync(list);
// Act
var isDeleted = await Collection.DeleteCollectionAsync();

// Assert
isDeleted.Should().BeTrue();
}
}
11 changes: 6 additions & 5 deletions ManagedCode.Database.Tests/TestContainers/CosmosTestContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace ManagedCode.Database.Tests.TestContainers;

[CollectionDefinition(nameof(CosmosTestContainer))]
public class CosmosTestContainer : ITestContainer<string, TestCosmosItem>,
public class CosmosTestContainer : ITestContainer<string, TestCosmosItem>,
ICollectionFixture<CosmosTestContainer>, IDisposable
{
private readonly TestcontainersContainer _cosmosTestContainer;
Expand Down Expand Up @@ -83,21 +83,22 @@ public async Task InitializeAsync()
var listContainers = await _dockerClient.Containers.ListContainersAsync(new ContainersListParameters());

ContainerListResponse containerListResponse = listContainers.FirstOrDefault(container => container.Names.Contains($"/{containerName}"));
if(containerListResponse != null)

if (containerListResponse != null)
{
publicPort = containerListResponse.Ports.Single(port => port.PrivatePort == privatePort).PublicPort;

containerId = containerListResponse.ID;
}
}


_database = new CosmosDatabase(new CosmosOptions
{
ConnectionString =
$"AccountEndpoint=https://localhost:{publicPort}/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
DatabaseName = "database",
CollectionName = $"testContainers",
CollectionName = $"testContainer",
AllowTableCreation = true,
CosmosClientOptions = new CosmosClientOptions()
{
Expand All @@ -120,7 +121,7 @@ public async Task InitializeAsync()

public async Task DisposeAsync()
{
await _database.DeleteAsync();
// await _database.DeleteAsync();
await _database.DisposeAsync();

/* _testOutputHelper.WriteLine($"Cosmos container State:{_cosmosContainer.State}");
Expand Down

0 comments on commit fb9f1d5

Please sign in to comment.