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

Refactor #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
75 changes: 0 additions & 75 deletions CosmosTestHelpers.IntegrationTests/ContainerMockTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,45 @@
using Microsoft.Azure.Cosmos;
using Xunit;

namespace CosmosTestHelpers.IntegrationTests
namespace CosmosTestHelpers.IntegrationTests;

[Collection("Integration Tests")]
public sealed class CosmosCreateContainerWithUniqueKeyIncludingId : IAsyncLifetime, IDisposable
{
[Collection("Integration Tests")]
public sealed class CosmosCreateContainerWithUniqueKeyIncludingId : IAsyncLifetime, IDisposable
{
private TestCosmos _testCosmos;
private TestCosmos _testCosmos;

[Fact]
public async Task CreateUniqueKeyViolationIsEquivalent()
[Fact]
public async Task CreateUniqueKeyViolationIsEquivalent()
{
var uniqueKeyPolicy = new UniqueKeyPolicy
{
var (realException, testException) = await _testCosmos.SetupAsyncProducesExceptions("/partitionKey", new UniqueKeyPolicy { UniqueKeys = { new UniqueKey { Paths = { "/id", "/ItemId", "/Type" } } } });
UniqueKeys = { new UniqueKey { Paths = { "/id", "/ItemId", "/Type" } } }
};

var (realException, testException) = await _testCosmos.SetupAsyncProducesExceptions(
"/partitionKey",
uniqueKeyPolicy
);

realException.Should().NotBeNull();
testException.Should().NotBeNull();
realException.StatusCode.Should().Be(testException.StatusCode);
realException.Should().BeOfType(testException.GetType());
}
realException.Should().NotBeNull();
testException.Should().NotBeNull();
realException.StatusCode.Should().Be(testException.StatusCode);
realException.Should().BeOfType(testException.GetType());
}

public Task InitializeAsync()
{
_testCosmos = new TestCosmos();
return Task.CompletedTask;
}
public Task InitializeAsync()
{
_testCosmos = new TestCosmos();
return Task.CompletedTask;
}

public async Task DisposeAsync()
{
await _testCosmos.CleanupAsync();
}
public async Task DisposeAsync()
{
await _testCosmos.CleanupAsync();
}

public void Dispose()
{
_testCosmos?.Dispose();
}
public void Dispose()
{
_testCosmos?.Dispose();
}
}
213 changes: 104 additions & 109 deletions CosmosTestHelpers.IntegrationTests/CosmosCreateStreamTests.cs
Original file line number Diff line number Diff line change
@@ -1,129 +1,124 @@
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Text;
using CosmosTestHelpers.IntegrationTests.TestModels;
using FluentAssertions;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
using Xunit;

namespace CosmosTestHelpers.IntegrationTests
namespace CosmosTestHelpers.IntegrationTests;

[Collection("Integration Tests")]
public sealed class CosmosCreateStreamTests : IAsyncLifetime, IDisposable
{
[Collection("Integration Tests")]
public sealed class CosmosCreateStreamTests : IAsyncLifetime, IDisposable
{
private TestCosmos _testCosmos;
private TestCosmos _testCosmos;

[Fact]
[SuppressMessage("", "CA2000", Justification = "AsyncDispose....")]
public async Task CreateStreamNonExistingIsEquivalent()
{
var bytes = GetItemBytes(
new TestModel
{
Id = "RECORD2",
Name = "Bob Bobertson",
EnumValue = TestEnum.Value1,
PartitionKey = "test-cst1"
});

await using var ms = new MemoryStream(bytes);
var (real, test) = await _testCosmos.WhenCreatingStream(ms, new PartitionKey("test-cst1"));

real.StatusCode.Should().Be(test.StatusCode);
}

[Fact]
[SuppressMessage("", "CA2000", Justification = "AsyncDispose....")]
public async Task CreateStreamExistingIsEquivalent()
[Fact]
public async Task CreateStreamNonExistingIsEquivalent()
{
var bytes = GetItemBytes(new TestModel
{
await _testCosmos.GivenAnExistingItem(new TestModel
{
Id = "RECORD1",
Name = "Bob Bobertson",
EnumValue = TestEnum.Value2,
PartitionKey = "test-cst2"
});

var bytes = GetItemBytes(
new TestModel
{
Id = "RECORD1",
Name = "Bob Bobertson",
EnumValue = TestEnum.Value1,
PartitionKey = "test-cst2"
});

await using var ms = new MemoryStream(bytes);
var (real, test) = await _testCosmos.WhenCreatingStream(ms, new PartitionKey("test-cst2"));

real.StatusCode.Should().Be(test.StatusCode);
}

[Fact]
[SuppressMessage("", "CA2000", Justification = "AsyncDispose....")]
public async Task CreateStreamWithEmptyIdIsEquivalent()
Id = "RECORD2",
Name = "Bob Bobertson",
EnumValue = TestEnum.Value1,
PartitionKey = "test-cst1"
});

await using var ms = new MemoryStream(bytes);
var (real, test) = await _testCosmos.WhenCreatingStream(ms, new PartitionKey("test-cst1"));

real.StatusCode.Should().Be(test.StatusCode);
}

[Fact]
public async Task CreateStreamExistingIsEquivalent()
{
await _testCosmos.GivenAnExistingItem(new TestModel
{
var bytes = GetItemBytes(
new TestModel
{
Id = string.Empty,
Name = "Bob Bobertson",
EnumValue = TestEnum.Value1,
PartitionKey = "test-cst2"
});

await using var ms = new MemoryStream(bytes);
var (real, test) = await _testCosmos.WhenCreatingStream(ms, new PartitionKey("test-cst2"));

real.StatusCode.Should().Be(test.StatusCode);
}

[Fact]
[SuppressMessage("", "CA2000", Justification = "AsyncDispose....")]
public async Task CreateStreamUniqueKeyViolationIsEquivalent()
Id = "RECORD1",
Name = "Bob Bobertson",
EnumValue = TestEnum.Value2,
PartitionKey = "test-cst2"
});

var bytes = GetItemBytes(new TestModel
{
await _testCosmos.GivenAnExistingItem(new TestModel
{
Id = "RECORD1",
Name = "Bob Bobertson",
EnumValue = TestEnum.Value2,
PartitionKey = "test-cst3"
});

var bytes = GetItemBytes(
new TestModel
{
Id = "RECORD2",
Name = "Bob Bobertson",
EnumValue = TestEnum.Value1,
PartitionKey = "test-cst3"
});

await using var ms = new MemoryStream(bytes);
var (real, test) = await _testCosmos.WhenCreatingStream(ms, new PartitionKey("test-cst3"));

real.StatusCode.Should().Be(test.StatusCode);
}

public Task InitializeAsync()
Id = "RECORD1",
Name = "Bob Bobertson",
EnumValue = TestEnum.Value1,
PartitionKey = "test-cst2"
});

await using var ms = new MemoryStream(bytes);
var (real, test) = await _testCosmos.WhenCreatingStream(ms, new PartitionKey("test-cst2"));

real.StatusCode.Should().Be(test.StatusCode);
}

[Fact]
public async Task CreateStreamWithEmptyIdIsEquivalent()
{
var bytes = GetItemBytes(new TestModel
{
_testCosmos = new TestCosmos();
return _testCosmos.SetupAsync("/partitionKey", new UniqueKeyPolicy { UniqueKeys = { new UniqueKey { Paths = {"/Name"}}}});
}
Id = string.Empty,
Name = "Bob Bobertson",
EnumValue = TestEnum.Value1,
PartitionKey = "test-cst2"
});

public async Task DisposeAsync()
await using var ms = new MemoryStream(bytes);
var (real, test) = await _testCosmos.WhenCreatingStream(ms, new PartitionKey("test-cst2"));

real.StatusCode.Should().Be(test.StatusCode);
}

[Fact]
public async Task CreateStreamUniqueKeyViolationIsEquivalent()
{
await _testCosmos.GivenAnExistingItem(new TestModel
{
await _testCosmos.CleanupAsync();
}
Id = "RECORD1",
Name = "Bob Bobertson",
EnumValue = TestEnum.Value2,
PartitionKey = "test-cst3"
});

public void Dispose()
var bytes = GetItemBytes(new TestModel
{
_testCosmos?.Dispose();
}
Id = "RECORD2",
Name = "Bob Bobertson",
EnumValue = TestEnum.Value1,
PartitionKey = "test-cst3"
});

await using var ms = new MemoryStream(bytes);
var (real, test) = await _testCosmos.WhenCreatingStream(ms, new PartitionKey("test-cst3"));

real.StatusCode.Should().Be(test.StatusCode);
}

private static byte[] GetItemBytes(TestModel model)
public async Task InitializeAsync()
{
var uniqueKeyPolicy = new UniqueKeyPolicy
{
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model));
}
UniqueKeys = { new UniqueKey { Paths = { "/Name" } } }
};

_testCosmos = new TestCosmos();
await _testCosmos.SetupAsync("/partitionKey", uniqueKeyPolicy);
}

public async Task DisposeAsync()
{
await _testCosmos.CleanupAsync();
}

public void Dispose()
{
_testCosmos?.Dispose();
}

private static byte[] GetItemBytes(TestModel model)
{
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model));
}
}
Loading