Skip to content

Commit

Permalink
Added context to every unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
dei79 committed Aug 28, 2022
1 parent 77edd42 commit fe10919
Show file tree
Hide file tree
Showing 21 changed files with 296 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,23 @@ Task<IQueryable<T>> QueryAsync<T>(string partitionKey, IEnumerable<QueryFilter>

Task DeleteAsync<T>(IEnumerable<T> models, bool allowMultiPartionRemoval = false) where T : new();

void SetTableNamePrefix(string tableNamePrefix);

void OverrideTableName<T>(string table) where T : new();

Task MergeOrInsertAsync<T>(IEnumerable<T> models) where T : new();

Task MergeOrInsertAsync<T>(T model) where T : new();

Task CreateTableAsync<T>(bool ignoreErrorIfExists = true);

void CreateTable<T>(bool ignoreErrorIfExists = true);

Task <bool> ExistsTableAsync<T>();

Task DropTableAsync<T>(bool ignoreErrorIfNotExists = true);

void DropTable<T>(bool ignoreErrorIfNotExists = true);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
<None Remove="Contracts\" />
<None Remove="Xunit.DependencyInjection" />
<None Remove="TestEnvironments\" />
<None Remove="Extensions\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\" />
<Folder Include="Contracts\" />
<Folder Include="TestEnvironments\" />
<Folder Include="Extensions\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CoreHelpers.WindowsAzure.Storage.Table\CoreHelpers.WindowsAzure.Storage.Table.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
namespace CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions
{
public static class StorageContextExtensions
{
public static string BuildTableContext()
{
return $"T{Guid.NewGuid().ToString().Replace("-", "").Substring(0, 8)}";
}

public static string SetTableContext(this StorageContext context)
{
var contextValue = BuildTableContext();
context.SetTableNamePrefix(contextValue);
return contextValue;
}
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using CoreHelpers.WindowsAzure.Storage.Table.Delegates;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models;
using Xunit.DependencyInjection;

Expand All @@ -22,8 +23,11 @@ public async Task VerifyStaticEntityMapperOperations()
{
using (var scp = new StorageContext(env.ConnectionString))
{
// unification uuid
var runId = Guid.NewGuid().ToString();
// set the tablename context
scp.SetTableContext();

// unification uuid
var runId = Guid.NewGuid().ToString();

// create a new user
var user = new UserModel() { FirstName = "Egon", LastName = "Mueller", Contact = "[email protected]" };
Expand All @@ -50,11 +54,9 @@ public async Task VerifyStaticEntityMapperOperations()
Assert.Equal("Mueller", result.First().LastName);
Assert.Equal($"[email protected].{runId}", result.First().Contact);

// Clean up
await scp.DeleteAsync<UserModel>(result);
result = await scp.QueryAsync<UserModel>();
Assert.NotNull(result);
Assert.Equal(0, result.Count());
// Clean up
await scp.DropTableAsync<UserModel>();
Assert.False(await scp.ExistsTableAsync<UserModel>());
}
}

Expand All @@ -63,8 +65,11 @@ public async Task VerifyVirtualKeysPOCOModel()
{
using (var scp = new StorageContext(env.ConnectionString))
{
// create model
var vpmodel = new VirtualPartitionKeyDemoModelPOCO() { Value1 = "abc", Value2 = "def", Value3 = "ghi" };
// set the tablename context
scp.SetTableContext();

// create model
var vpmodel = new VirtualPartitionKeyDemoModelPOCO() { Value1 = "abc", Value2 = "def", Value3 = "ghi" };

// configure the entity mapper
scp.AddEntityMapper(typeof(VirtualPartitionKeyDemoModelPOCO), new DynamicTableEntityMapper() { TableName = "VirtualPartitionKeyDemoModelPOCO", PartitionKeyFormat = "{{Value1}}-{{Value2}}", RowKeyFormat = "{{Value2}}-{{Value3}}" });
Expand All @@ -88,10 +93,8 @@ public async Task VerifyVirtualKeysPOCOModel()
Assert.Equal("ghi", result.First().Value3);

// Clean up
await scp.DeleteAsync<VirtualPartitionKeyDemoModelPOCO>(result);
result = await scp.QueryAsync<VirtualPartitionKeyDemoModelPOCO>();
Assert.NotNull(result);
Assert.Equal(0, result.Count());
await scp.DropTableAsync<VirtualPartitionKeyDemoModelPOCO>();
Assert.False(await scp.ExistsTableAsync<VirtualPartitionKeyDemoModelPOCO>());
}
}

Expand All @@ -100,8 +103,11 @@ public async Task VerifyStatsDelegate()
{
using (var scp = new StorageContext(env.ConnectionString))
{
// set the delegate
var stats = new StorageContextStatsDelegate();
// set the tablename context
scp.SetTableContext();

// set the delegate
var stats = new StorageContextStatsDelegate();
scp.SetDelegate(stats);

// unification uuid
Expand Down Expand Up @@ -133,7 +139,11 @@ public async Task VerifyStatsDelegate()
// verify stats
Assert.Equal(1, stats.QueryOperations);
Assert.Equal(2, stats.StoreOperations.Values.Sum());
}

// Drop the table
await scp.DropTableAsync<UserModel>();
Assert.False(await scp.ExistsTableAsync<UserModel>());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models;
using Xunit.DependencyInjection;

Expand All @@ -20,7 +21,10 @@ public ITS002StoreWithAttributeMapper(ITestEnvironment env)
public async Task VerifyAttributeMapper()
{
using (var storageContext = new StorageContext(env.ConnectionString))
{
{
// set the tablename context
storageContext.SetTableContext();

// create a new user
var user = new UserModel2() { FirstName = "Egon", LastName = "Mueller", Contact = "[email protected]" };

Expand All @@ -44,7 +48,9 @@ public async Task VerifyAttributeMapper()
await storageContext.DeleteAsync<UserModel2>(result);
result = await storageContext.QueryAsync<UserModel2>();
Assert.NotNull(result);
Assert.Equal(0, result.Count());
Assert.Equal(0, result.Count());

await storageContext.DropTableAsync<UserModel2>();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using CoreHelpers.WindowsAzure.Storage.Table.Tests;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models;
using Xunit.DependencyInjection;

Expand All @@ -22,9 +23,12 @@ public ITS003StoreWithAttributeMapperManualRegistration(ITestEnvironment env)
public async Task VerifyManualRegistration()
{
using (var storageContext = new StorageContext(env.ConnectionString))
{
// create a new user
var user = new UserModel2() { FirstName = "Egon", LastName = "Mueller", Contact = "[email protected]" };
{
// set the tablename context
storageContext.SetTableContext();

// create a new user
var user = new UserModel2() { FirstName = "Egon", LastName = "Mueller", Contact = "[email protected]" };
var vpmodel = new VirtualPartKeyDemoModel() { Value1 = "abc", Value2 = "def", Value3 = "ghi" };

// ensure we are using the attributes
Expand Down Expand Up @@ -64,6 +68,9 @@ public async Task VerifyManualRegistration()
resultVP = await storageContext.QueryAsync<VirtualPartKeyDemoModel>();
Assert.NotNull(resultVP);
Assert.Equal(0, resultVP.Count());

await storageContext.DropTableAsync<UserModel2>();
await storageContext.DropTableAsync<VirtualPartKeyDemoModel>();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using CoreHelpers.WindowsAzure.Storage.Table.Tests;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models;
using Xunit.DependencyInjection;

Expand All @@ -22,9 +23,12 @@ public ITS004GetVirtualArray(ITestEnvironment env)
public async Task VerifyVirtualArray()
{
using (var storageContext = new StorageContext(env.ConnectionString))
{
// create a virtual array model
var model = new VArrayModel() { UUID = "112233" };
{
// set the tablename context
storageContext.SetTableContext();

// create a virtual array model
var model = new VArrayModel() { UUID = "112233" };
model.DataElements.Add(2);
model.DataElements.Add(3);
model.DataElements.Add(4);
Expand All @@ -51,7 +55,9 @@ public async Task VerifyVirtualArray()
await storageContext.DeleteAsync<VArrayModel>(result);
result = await storageContext.QueryAsync<VArrayModel>();
Assert.NotNull(result);
Assert.Equal(0, result.Count());
Assert.Equal(0, result.Count());

await storageContext.DropTableAsync<VArrayModel>();
}
}
}
Expand Down
52 changes: 38 additions & 14 deletions CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS005StoreAsJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using CoreHelpers.WindowsAzure.Storage.Table.Tests;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions;
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models;
using Xunit.DependencyInjection;

Expand All @@ -21,10 +22,15 @@ public ITS005StoreAsJson(ITestEnvironment env)
[Fact]
public async Task VerifyJsonPayloads()
{
using (var storageContext = new StorageContext(env.ConnectionString))
var ctx = StorageContextExtensions.BuildTableContext();

using (var storageContext = new StorageContext(env.ConnectionString))
{
// create the model
var model = new JObjectModel() { UUID = "112233" };
// set the tablename context
storageContext.SetTableNamePrefix(ctx);

// create the model
var model = new JObjectModel() { UUID = "112233" };
model.Data.Add("HEllo", "world");
model.Data2.Value = "Hello 23";

Expand All @@ -48,8 +54,11 @@ public async Task VerifyJsonPayloads()

using (var storageContext = new StorageContext(env.ConnectionString))
{
// ensure we are using the attributes
storageContext.AddAttributeMapper(typeof(JObjectModelVerify));
// set the tablename context
storageContext.SetTableNamePrefix(ctx);

// ensure we are using the attributes
storageContext.AddAttributeMapper(typeof(JObjectModelVerify));

var result = await storageContext.QueryAsync<JObjectModelVerify>();
Assert.Equal(1, result.Count());
Expand All @@ -61,25 +70,33 @@ public async Task VerifyJsonPayloads()

using (var storageContext = new StorageContext(env.ConnectionString))
{
// ensure we are using the attributes
storageContext.AddAttributeMapper(typeof(JObjectModel));
// set the tablename context
storageContext.SetTableNamePrefix(ctx);

// ensure we are using the attributes
storageContext.AddAttributeMapper(typeof(JObjectModel));

// Clean up
var result = await storageContext.QueryAsync<JObjectModel>();
await storageContext.DeleteAsync<JObjectModel>(result);
result = await storageContext.QueryAsync<JObjectModel>();
Assert.NotNull(result);
Assert.Equal(0, result.Count());
}

await storageContext.DropTableAsync<JObjectModel>();
}
}

[Fact]
public async Task VerifyDictionary()
{
using (var storageContext = new StorageContext(env.ConnectionString))
{
// create the model
var model = new DictionaryModel() { Id = Guid.NewGuid().ToString() };
// set the tablename context
storageContext.SetTableContext();

// create the model
var model = new DictionaryModel() { Id = Guid.NewGuid().ToString() };

// ensure we are using the attributes
storageContext.AddAttributeMapper(typeof(DictionaryModel));
Expand All @@ -95,16 +112,21 @@ public async Task VerifyDictionary()
// cleanup
var cleanUpItems = await storageContext.QueryAsync<DictionaryModel>();
await storageContext.DeleteAsync<DictionaryModel>(cleanUpItems, true);
}

await storageContext.DropTableAsync<DictionaryModel>();
}
}

[Fact]
public async Task DeleteMultiPartitionEntries()
{
using (var storageContext = new StorageContext(env.ConnectionString))
{
// ensure we are using the attributes
storageContext.AddAttributeMapper(typeof(DictionaryModel));
// set the tablename context
storageContext.SetTableContext();

// ensure we are using the attributes
storageContext.AddAttributeMapper(typeof(DictionaryModel));

// inser the model
await storageContext.EnableAutoCreateTable().MergeOrInsertAsync<DictionaryModel>(new DictionaryModel() { Id = Guid.NewGuid().ToString() });
Expand All @@ -117,7 +139,9 @@ public async Task DeleteMultiPartitionEntries()

var zeroItems = await storageContext.QueryAsync<DictionaryModel>();
Assert.Equal(0, zeroItems.Count());
}

await storageContext.DropTableAsync<DictionaryModel>();
}
}
}
}
Loading

0 comments on commit fe10919

Please sign in to comment.