From fe10919b48eb4b6a1ae765bfe95b0392d0d77f7d Mon Sep 17 00:00:00 2001 From: Dirk Eisenberg Date: Mon, 29 Aug 2022 01:56:42 +0200 Subject: [PATCH] Added context to every unittest --- .../IStorageContext.cs | 13 ++++ ...rs.WindowsAzure.Storage.Table.Tests.csproj | 2 + .../Extensions/StorageContextExtensions.cs | 19 ++++++ .../ITS001StoreWithStaticEntityMapper.cs | 42 +++++++----- .../ITS002StoreWithAttributeMapper.cs | 10 ++- ...reWithAttributeMapperManualRegistration.cs | 13 +++- .../ITS004GetVirtualArray.cs | 14 ++-- .../ITS005StoreAsJson.cs | 52 +++++++++++---- .../ITS006AutoCreateTable.cs | 9 +++ .../ITS007CreateModelsPaged.cs | 25 +++++-- .../ITS008CheckMaxItems.cs | 12 +++- .../ITS009ReadInterfaceValues.cs | 14 ++-- .../ITS012PartialUpdateMergeModel.cs | 52 +++++++++++---- .../ITS013DynamicallyCreateList.cs | 11 ++++ .../ITS015DynamicTableNameChange.cs | 9 ++- .../ITS016BackupRestore.cs | 6 +- .../ITS018DateTime.cs | 5 ++ .../ITS019QueryFilter.cs | 5 ++ .../Services/DataExportService.cs | 2 +- .../Services/DataImportService.cs | 2 +- .../StorageContext.cs | 65 +++++++++++++++---- 21 files changed, 296 insertions(+), 86 deletions(-) create mode 100644 CoreHelpers.WindowsAzure.Storage.Table.Tests/Extensions/StorageContextExtensions.cs diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Abstractions/IStorageContext.cs b/CoreHelpers.WindowsAzure.Storage.Table.Abstractions/IStorageContext.cs index a333cc3..5512a46 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Abstractions/IStorageContext.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Abstractions/IStorageContext.cs @@ -28,10 +28,23 @@ Task> QueryAsync(string partitionKey, IEnumerable Task DeleteAsync(IEnumerable models, bool allowMultiPartionRemoval = false) where T : new(); + void SetTableNamePrefix(string tableNamePrefix); + void OverrideTableName(string table) where T : new(); Task MergeOrInsertAsync(IEnumerable models) where T : new(); Task MergeOrInsertAsync(T model) where T : new(); + + Task CreateTableAsync(bool ignoreErrorIfExists = true); + + void CreateTable(bool ignoreErrorIfExists = true); + + Task ExistsTableAsync(); + + Task DropTableAsync(bool ignoreErrorIfNotExists = true); + + void DropTable(bool ignoreErrorIfNotExists = true); + } } \ No newline at end of file diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/CoreHelpers.WindowsAzure.Storage.Table.Tests.csproj b/CoreHelpers.WindowsAzure.Storage.Table.Tests/CoreHelpers.WindowsAzure.Storage.Table.Tests.csproj index 428e8bd..548a801 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/CoreHelpers.WindowsAzure.Storage.Table.Tests.csproj +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/CoreHelpers.WindowsAzure.Storage.Table.Tests.csproj @@ -31,11 +31,13 @@ + + diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/Extensions/StorageContextExtensions.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/Extensions/StorageContextExtensions.cs new file mode 100644 index 0000000..3e6af1c --- /dev/null +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/Extensions/StorageContextExtensions.cs @@ -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; + } + } +} + diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS001StoreWithStaticEntityMapper.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS001StoreWithStaticEntityMapper.cs index e39d969..4731eaa 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS001StoreWithStaticEntityMapper.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS001StoreWithStaticEntityMapper.cs @@ -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; @@ -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 = "em@acme.org" }; @@ -50,11 +54,9 @@ public async Task VerifyStaticEntityMapperOperations() Assert.Equal("Mueller", result.First().LastName); Assert.Equal($"em@acme.org.{runId}", result.First().Contact); - // Clean up - await scp.DeleteAsync(result); - result = await scp.QueryAsync(); - Assert.NotNull(result); - Assert.Equal(0, result.Count()); + // Clean up + await scp.DropTableAsync(); + Assert.False(await scp.ExistsTableAsync()); } } @@ -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}}" }); @@ -88,10 +93,8 @@ public async Task VerifyVirtualKeysPOCOModel() Assert.Equal("ghi", result.First().Value3); // Clean up - await scp.DeleteAsync(result); - result = await scp.QueryAsync(); - Assert.NotNull(result); - Assert.Equal(0, result.Count()); + await scp.DropTableAsync(); + Assert.False(await scp.ExistsTableAsync()); } } @@ -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 @@ -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(); + Assert.False(await scp.ExistsTableAsync()); + } } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS002StoreWithAttributeMapper.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS002StoreWithAttributeMapper.cs index da97f07..153aa99 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS002StoreWithAttributeMapper.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS002StoreWithAttributeMapper.cs @@ -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; @@ -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 = "em@acme.org" }; @@ -44,7 +48,9 @@ public async Task VerifyAttributeMapper() await storageContext.DeleteAsync(result); result = await storageContext.QueryAsync(); Assert.NotNull(result); - Assert.Equal(0, result.Count()); + Assert.Equal(0, result.Count()); + + await storageContext.DropTableAsync(); } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS003StoreWithAttributeMapperManualRegistration.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS003StoreWithAttributeMapperManualRegistration.cs index 331c01a..74171ca 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS003StoreWithAttributeMapperManualRegistration.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS003StoreWithAttributeMapperManualRegistration.cs @@ -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; @@ -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 = "em@acme.org" }; + { + // set the tablename context + storageContext.SetTableContext(); + + // create a new user + var user = new UserModel2() { FirstName = "Egon", LastName = "Mueller", Contact = "em@acme.org" }; var vpmodel = new VirtualPartKeyDemoModel() { Value1 = "abc", Value2 = "def", Value3 = "ghi" }; // ensure we are using the attributes @@ -64,6 +68,9 @@ public async Task VerifyManualRegistration() resultVP = await storageContext.QueryAsync(); Assert.NotNull(resultVP); Assert.Equal(0, resultVP.Count()); + + await storageContext.DropTableAsync(); + await storageContext.DropTableAsync(); } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS004GetVirtualArray.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS004GetVirtualArray.cs index dcf084b..8d3fbdc 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS004GetVirtualArray.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS004GetVirtualArray.cs @@ -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; @@ -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); @@ -51,7 +55,9 @@ public async Task VerifyVirtualArray() await storageContext.DeleteAsync(result); result = await storageContext.QueryAsync(); Assert.NotNull(result); - Assert.Equal(0, result.Count()); + Assert.Equal(0, result.Count()); + + await storageContext.DropTableAsync(); } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS005StoreAsJson.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS005StoreAsJson.cs index cc22fe7..e2cfd25 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS005StoreAsJson.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS005StoreAsJson.cs @@ -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; @@ -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"; @@ -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(); Assert.Equal(1, result.Count()); @@ -61,8 +70,11 @@ 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(); @@ -70,7 +82,9 @@ public async Task VerifyJsonPayloads() result = await storageContext.QueryAsync(); Assert.NotNull(result); Assert.Equal(0, result.Count()); - } + + await storageContext.DropTableAsync(); + } } [Fact] @@ -78,8 +92,11 @@ 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)); @@ -95,7 +112,9 @@ public async Task VerifyDictionary() // cleanup var cleanUpItems = await storageContext.QueryAsync(); await storageContext.DeleteAsync(cleanUpItems, true); - } + + await storageContext.DropTableAsync(); + } } [Fact] @@ -103,8 +122,11 @@ 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(new DictionaryModel() { Id = Guid.NewGuid().ToString() }); @@ -117,7 +139,9 @@ public async Task DeleteMultiPartitionEntries() var zeroItems = await storageContext.QueryAsync(); Assert.Equal(0, zeroItems.Count()); - } + + await storageContext.DropTableAsync(); + } } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS006AutoCreateTable.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS006AutoCreateTable.cs index de5ea36..fbfb00e 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS006AutoCreateTable.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS006AutoCreateTable.cs @@ -3,6 +3,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 Microsoft.WindowsAzure.Storage; using Xunit.DependencyInjection; @@ -26,6 +27,9 @@ public async Task VerifyAutoCreateDuringWrite() using (var storageContext = new StorageContext(env.ConnectionString)) { + // set the tablename context + storageContext.SetTableContext(); + // create a new user var user = new UserModel() { FirstName = "Egon", LastName = "Mueller", Contact = "em@acme.org" }; @@ -59,6 +63,9 @@ public async Task VerifyAutoCreateDuringRead() { using (var storageContext = new StorageContext(env.ConnectionString)) { + // set the tablename context + storageContext.SetTableContext(); + // create a new user var user = new UserModel() { FirstName = "Egon", LastName = "Mueller", Contact = "em@acme.org" }; @@ -74,6 +81,8 @@ public async Task VerifyAutoCreateDuringRead() // query all by creating a new table var result = await storageContext.EnableAutoCreateTable().QueryAsync(); Assert.Equal(0, result.Count()); + + await storageContext.DropTableAsync(); } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS007CreateModelsPaged.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS007CreateModelsPaged.cs index 4112eb8..2255213 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS007CreateModelsPaged.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS007CreateModelsPaged.cs @@ -5,6 +5,7 @@ using CoreHelpers.WindowsAzure.Storage.Table.Tests; using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts; using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models; +using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions; namespace CoreHelpers.WindowsAzure.Storage.Table.Tests { @@ -26,8 +27,11 @@ public async Task VerifyPagedWriting() Console.WriteLine(this.GetType().FullName); using (var storageContext = new StorageContext(env.ConnectionString)) - { - // ensure we are using the attributes + { + // set the tablename context + storageContext.SetTableContext(); + + // ensure we are using the attributes storageContext.AddAttributeMapper(typeof(UserModel2), "DemoUserModel2"); // create tables @@ -64,7 +68,9 @@ public async Task VerifyPagedWriting() // Clean up await storageContext.DeleteAsync(result); result = await storageContext.QueryAsync(); - Assert.Equal(0, result.Count()); + Assert.Equal(0, result.Count()); + + await storageContext.DropTableAsync(); } } @@ -73,8 +79,11 @@ public async Task VerifyPageReading() { using (var storageContext = new StorageContext(env.ConnectionString)) { - // ensure we are using the attributes - storageContext.AddAttributeMapper(typeof(HugeDemoEntry)); + // set the tablename context + storageContext.SetTableContext(); + + // ensure we are using the attributes + storageContext.AddAttributeMapper(typeof(HugeDemoEntry)); // delete all var result = await storageContext.EnableAutoCreateTable().QueryAsync(); @@ -107,8 +116,10 @@ public async Task VerifyPageReading() // remove all entries await storageContext.DeleteAsync(items); result = await storageContext.QueryAsync(); - Assert.Equal(0, result.Count()); - } + Assert.Equal(0, result.Count()); + + await storageContext.DropTableAsync(); + } } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS008CheckMaxItems.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS008CheckMaxItems.cs index 46903e3..f487ec3 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS008CheckMaxItems.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS008CheckMaxItems.cs @@ -6,6 +6,7 @@ using CoreHelpers.WindowsAzure.Storage.Table.Tests; using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts; using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models; +using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions; namespace CoreHelpers.WindowsAzure.Storage.Table.Tests { @@ -27,8 +28,11 @@ public async Task VerifyMaxItems() Console.WriteLine(this.GetType().FullName); using (var storageContext = new StorageContext(env.ConnectionString)) - { - storageContext.AddAttributeMapper(typeof(UserModel2), "DemoUserModel2"); + { + // set the tablename context + storageContext.SetTableContext(); + + storageContext.AddAttributeMapper(typeof(UserModel2), "DemoUserModel2"); storageContext.CreateTable(true); var data = new List() { @@ -55,7 +59,9 @@ public async Task VerifyMaxItems() // Clean up await storageContext.DeleteAsync(allitems); var result = await storageContext.QueryAsync(); - Assert.Equal(0, result.Count()); + Assert.Equal(0, result.Count()); + + await storageContext.DropTableAsync(); } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS009ReadInterfaceValues.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS009ReadInterfaceValues.cs index c0e0d5a..61122c4 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS009ReadInterfaceValues.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS009ReadInterfaceValues.cs @@ -6,6 +6,7 @@ using Xunit.DependencyInjection; using CoreHelpers.WindowsAzure.Storage.Table.Tests; using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models; +using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions; namespace CoreHelpers.WindowsAzure.Storage.Table.Tests { @@ -24,9 +25,12 @@ public ITS009ReadInterfaceValues(ITestEnvironment env) public async Task VerifyReadingInterfaceValues() { using (var storageContext = new StorageContext(env.ConnectionString)) - { - // create a new user - var user02 = new UserModel3() { FirstName = "Egon", LastName = "Mueller", Contact = "em@acme.org" }; + { + // set the tablename context + storageContext.SetTableContext(); + + // create a new user + var user02 = new UserModel3() { FirstName = "Egon", LastName = "Mueller", Contact = "em@acme.org" }; user02.Codes.Add(new Code() { CodeType = "x1", CodeValue = "x2" }); user02.Codes.Add(new Code() { CodeType = "x3", CodeValue = "x4" }); @@ -44,7 +48,9 @@ public async Task VerifyReadingInterfaceValues() await storageContext.DeleteAsync(result); result = await storageContext.QueryAsync(); Assert.Equal(0, result.Count()); - } + + await storageContext.DropTableAsync(); + } } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS012PartialUpdateMergeModel.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS012PartialUpdateMergeModel.cs index b7e2074..c237cb3 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS012PartialUpdateMergeModel.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS012PartialUpdateMergeModel.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Threading.Tasks; using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts; +using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions; using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models; using Xunit.DependencyInjection; @@ -22,47 +23,70 @@ public ITS012PartialUpdateMergeModel(ITestEnvironment env) public async Task VerifyPartialModelUpdate() { using (var storageContext = new StorageContext(env.ConnectionString)) - { + { + // set the tablename context + storageContext.SetTableContext(); + // create a new user - var model = new DemoEntryWithOptionalValues() { Identifier = "X" }; - + var model = new DemoEntryWithOptionalValues() { Identifier = "X" }; + // ensure we are using the attributes storageContext.AddAttributeMapper(); - // build the table name - var tableName = $"DemoEntryWithOptinalValues{Guid.NewGuid().ToString().Replace("-", "")}"; - storageContext.OverrideTableName(tableName); - // ensure the table exists await storageContext.CreateTableAsync(); - + // inser the model await storageContext.MergeOrInsertAsync(model); - + // query all var result = (await storageContext.QueryAsync()).FirstOrDefault(); Assert.NotNull(result); Assert.Equal("X", result.Identifier); Assert.Null(result.Name); Assert.False(result.Costs.HasValue); - + // update the model result.Costs = 5.4; await storageContext.MergeOrInsertAsync(result); - + // query all result = (await storageContext.QueryAsync()).FirstOrDefault(); Assert.NotNull(result); Assert.Equal("X", result.Identifier); Assert.Null(result.Name); Assert.True(result.Costs.HasValue); - Assert.Equal(5.4, result.Costs.Value); - + Assert.Equal(5.4, result.Costs.Value); + // Clean up await storageContext.DeleteAsync(result); var elements = await storageContext.QueryAsync(); Assert.Equal(0, elements.Count()); - } + + await storageContext.DropTableAsync(); + } + } + + [Fact] + public async Task CheckOverrideTableNAme() + { + using (var storageContext = new StorageContext(env.ConnectionString)) + { + var ctx = StorageContextExtensions.BuildTableContext(); + + // ensure we are using the attributes + storageContext.AddAttributeMapper(); + + // override the name + storageContext.OverrideTableName($"{ctx}DemoEntryWithOptionalValues"); + + // ensure the table exists + await storageContext.CreateTableAsync(); + Assert.True(await storageContext.ExistsTableAsync()); + + await storageContext.DropTableAsync(); + Assert.False(await storageContext.ExistsTableAsync()); + } } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS013DynamicallyCreateList.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS013DynamicallyCreateList.cs index b33d22f..c2fd8bf 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS013DynamicallyCreateList.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS013DynamicallyCreateList.cs @@ -5,6 +5,7 @@ using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts; using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models; using Xunit.DependencyInjection; +using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions; namespace CoreHelpers.WindowsAzure.Storage.Table.Tests { @@ -24,6 +25,9 @@ public async Task VerifyDynamicLists() { using (var storageContext = new StorageContext(env.ConnectionString)) { + // set the tablename context + storageContext.SetTableContext(); + // create model with data in list var model = new DemoMeterModel() { ExtendedCosts = new List() }; model.ExtendedCosts.Add(5.5); @@ -52,6 +56,8 @@ public async Task VerifyDynamicLists() await storageContext.DeleteAsync(result); var elements = await storageContext.QueryAsync(); Assert.Equal(0, elements.Count()); + + await storageContext.DropTableAsync(); } } @@ -60,6 +66,9 @@ public async Task VerifyNullListWriting() { using (var storageContext = new StorageContext(env.ConnectionString)) { + // set the tablename context + storageContext.SetTableContext(); + // create model with data in list var model = new NullListModel(); @@ -85,6 +94,8 @@ public async Task VerifyNullListWriting() await storageContext.DeleteAsync(result); var elements = await storageContext.QueryAsync(); Assert.Equal(0, elements.Count()); + + await storageContext.DropTableAsync(); } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS015DynamicTableNameChange.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS015DynamicTableNameChange.cs index 2a56bba..840b5bd 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS015DynamicTableNameChange.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS015DynamicTableNameChange.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using CoreHelpers.WindowsAzure.Storage.Table.Abstractions; 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; @@ -25,10 +27,13 @@ public async Task VerifyDynamicNames() using (var storageContextParent = new StorageContext(env.ConnectionString)) { + // set the tablename context + storageContextParent.SetTableContext(); + using (var storageContext = new StorageContext(storageContextParent)) { - var tableName1 = $"MT{Guid.NewGuid().ToString()}".Replace("-", ""); - var tableName2 = $"MT{Guid.NewGuid().ToString()}".Replace("-", ""); + var tableName1 = $"MT1"; + var tableName2 = $"MT2"; // create model with data in list var model = new DemoModel2() { P = "1", R = "2" }; diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS016BackupRestore.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS016BackupRestore.cs index 1f6284d..70be598 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS016BackupRestore.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS016BackupRestore.cs @@ -1,5 +1,6 @@ using System.Text; using CoreHelpers.WindowsAzure.Storage.Table.Tests.Contracts; +using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions; using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models; using Microsoft.WindowsAzure.Storage; using Newtonsoft.Json; @@ -24,8 +25,11 @@ public async Task VerifyExportToJson() // Export Table using (var storageContext = new StorageContext(env.ConnectionString)) { + // set the tablename context + storageContext.SetTableContext(); + // ensure we have a model registered in the correct table - var tableName1 = $"BU{Guid.NewGuid().ToString()}".Replace("-", ""); + var tableName1 = $"BU".Replace("-", ""); storageContext.AddAttributeMapper(typeof(DemoModel2), tableName1); // create model with data in list diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS018DateTime.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS018DateTime.cs index 13e4b95..f39f939 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS018DateTime.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS018DateTime.cs @@ -3,6 +3,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; @@ -25,6 +26,9 @@ public async Task VerifyDateTimeHandling() // Import from Blob using (var storageContext = new StorageContext(env.ConnectionString)) { + // set the tablename context + storageContext.SetTableContext(); + // create the model var model = new DatetimeModel() { ActivatedAt = DateTime.Now.ToUniversalTime() }; @@ -44,6 +48,7 @@ public async Task VerifyDateTimeHandling() // Clean up await storageContext.DeleteAsync(result); + await storageContext.DropTableAsync(); } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS019QueryFilter.cs b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS019QueryFilter.cs index d1e82c2..070c172 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS019QueryFilter.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS019QueryFilter.cs @@ -1,6 +1,7 @@ using CoreHelpers.WindowsAzure.Storage.Table.Abstractions; 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; @@ -23,6 +24,9 @@ public async Task VerifyQueryfilter() // Import from Blob using (var storageContext = new StorageContext(env.ConnectionString)) { + // set the tablename context + storageContext.SetTableContext(); + // create the model var models = new List() { @@ -75,6 +79,7 @@ public async Task VerifyQueryfilter() // Clean up var all = await storageContext.QueryAsync(); await storageContext.DeleteAsync(all); + await storageContext.DropTableAsync(); } } } diff --git a/CoreHelpers.WindowsAzure.Storage.Table/Services/DataExportService.cs b/CoreHelpers.WindowsAzure.Storage.Table/Services/DataExportService.cs index 11448bc..3b5d119 100755 --- a/CoreHelpers.WindowsAzure.Storage.Table/Services/DataExportService.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table/Services/DataExportService.cs @@ -20,7 +20,7 @@ public async Task ExportToJson(string tableName, TextWriter writer, Action { try { - var table = storageContext.GetTableReference(tableName); + var table = storageContext.RequestTableReference(tableName); var existsTable = await table.ExistsAsync(); if (!existsTable) diff --git a/CoreHelpers.WindowsAzure.Storage.Table/Services/DataImportService.cs b/CoreHelpers.WindowsAzure.Storage.Table/Services/DataImportService.cs index f39df96..9fa3ecf 100755 --- a/CoreHelpers.WindowsAzure.Storage.Table/Services/DataImportService.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table/Services/DataImportService.cs @@ -22,7 +22,7 @@ public DataImportService(StorageContext storageContext) public async Task ImportFromJsonStreamAsync(string tableName, StreamReader streamReader, Action progress = null) { // ensure table exists - var targetTable = storageContext.GetTableReference(tableName); + var targetTable = storageContext.RequestTableReference(tableName); if (!await targetTable.ExistsAsync()) await CreateAzureTableAsync(targetTable); diff --git a/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs b/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs index 9882423..2af3477 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs @@ -11,6 +11,7 @@ using CoreHelpers.WindowsAzure.Storage.Table.Extensions; using CoreHelpers.WindowsAzure.Storage.Table.Services; using System.Runtime.ExceptionServices; +using System.Text.RegularExpressions; namespace CoreHelpers.WindowsAzure.Storage.Table { @@ -34,7 +35,8 @@ public class StorageContext : IStorageContext private Dictionary _entityMapperRegistry { get; set; } = new Dictionary(); private bool _autoCreateTable { get; set; } = false; private IStorageContextDelegate _delegate { get; set; } - + private string _tableNamePrefix; + public StorageContext(string storageAccountName, string storageAccountKey, string storageEndpointSuffix = null) { var connectionString = String.Format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2}", "https", storageAccountName, storageAccountKey); @@ -59,7 +61,10 @@ public StorageContext(StorageContext parentContext) // we are using the delegate this.SetDelegate(parentContext._delegate); - } + + // take the tablename prefix + _tableNamePrefix = parentContext._tableNamePrefix; + } public void Dispose() { @@ -170,6 +175,11 @@ public IEnumerable GetRegisteredMappers() return _entityMapperRegistry.Keys; } + public void SetTableNamePrefix(string tableNamePrefix) + { + _tableNamePrefix = tableNamePrefix; + } + public void OverrideTableName(string table) where T : new() { OverrideTableName(typeof(T), table); } @@ -229,6 +239,12 @@ public async Task DropTableAsync(Type entityType, bool ignoreErrorIfNotExists = await table.DeleteAsync(); } + public async Task ExistsTableAsync() + { + CloudTable table = GetTableReference(GetTableName(typeof(T))); + return await table.ExistsAsync(); + } + public async Task DropTableAsync(bool ignoreErrorIfNotExists = true) { await DropTableAsync(typeof(T), ignoreErrorIfNotExists); @@ -301,10 +317,19 @@ private string GetTableName(Type entityType) var entityMapper = _entityMapperRegistry[entityType]; // get the table name - return entityMapper.TableName; - } - - public async Task StoreAsync(nStoreOperation storaeOperationType, IEnumerable models) where T : new() + return GetTableName(entityMapper.TableName); + } + + private string GetTableName(string tableName) + { + // get the table name + if (String.IsNullOrEmpty(_tableNamePrefix)) + return tableName; + else + return Regex.Replace($"{_tableNamePrefix}{tableName}", "[^A-Za-z0-9]", ""); + } + + public async Task StoreAsync(nStoreOperation storaeOperationType, IEnumerable models) where T : new() { try { @@ -559,7 +584,7 @@ private string GetTableName(Type entityType) return result.Items; } - internal CloudTable GetTableReference(string tableName) { + private CloudTable GetTableReference(string tableName) { // create the table client var storageTableClient = _storageAccount.CreateCloudTableClient(); @@ -570,9 +595,15 @@ internal CloudTable GetTableReference(string tableName) { // Retrieve a reference to the table. return tableClient.GetTableReference(tableName); } - - - public StorageContextQueryCursor QueryPaged(string partitionKey, string rowKey, IEnumerable queryFilters = null, int maxItems = 0) where T : new() + + internal CloudTable RequestTableReference(string tableName) + { + var tableNamePatched = GetTableName(tableName); + return GetTableReference(tableNamePatched); + } + + + public StorageContextQueryCursor QueryPaged(string partitionKey, string rowKey, IEnumerable queryFilters = null, int maxItems = 0) where T : new() { return new StorageContextQueryCursor(this, partitionKey, rowKey, queryFilters, maxItems); } @@ -596,16 +627,22 @@ public async Task> QueryTableList() { public async Task ExportToJsonAsync(string tableName, TextWriter writer) { - var logsTable = GetTableReference(DataExportService.TableName); - await logsTable.CreateIfNotExistsAsync(); + var logsTable = GetTableReference(GetTableName(DataExportService.TableName)); + + if (!await logsTable.ExistsAsync()) + await logsTable.CreateIfNotExistsAsync(); + var exporter = new DataExportService(this); await exporter.ExportToJson(tableName, writer); } public async Task ImportFromJsonAsync(string tableName, StreamReader reader) { - var logsTable = GetTableReference(DataImportService.TableName); - await logsTable.CreateIfNotExistsAsync(); + var logsTable = GetTableReference(GetTableName(DataImportService.TableName)); + + if (!await logsTable.ExistsAsync()) + await logsTable.CreateIfNotExistsAsync(); + var importer = new DataImportService(this); await importer.ImportFromJsonStreamAsync(tableName, reader); }