Skip to content

Commit

Permalink
Added support for the import from json capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
dei79 committed Sep 4, 2022
1 parent 528ac0c commit 691d6f0
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using CoreHelpers.WindowsAzure.Storage.Table.Abstractions;

Expand All @@ -20,6 +21,8 @@ public interface IStorageContext : IDisposable

void AddAttributeMapper(Type type);

void AddAttributeMapper(Type type, String optionalTablenameOverride);

void AddEntityMapper(Type entityType, String partitionKeyFormat, String rowKeyFormat, String tableName);

IStorageContext CreateChildContext();
Expand Down Expand Up @@ -70,5 +73,7 @@ Task<IEnumerable<T>> QueryAsync<T>(string partitionKey, IEnumerable<QueryFilter>
Task<List<string>> QueryTableList();

Task ExportToJsonAsync(string tableName, TextWriter writer, Action<ImportExportOperation> onOperation);

Task ImportFromJsonAsync(string tableName, StreamReader reader, Action<ImportExportOperation> onOperation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ namespace CoreHelpers.WindowsAzure.Storage.Table.Tests
{
[Startup(typeof(Startup))]
[Collection("Sequential")]
public class ITS016BackupRestore
{
private readonly ITestEnvironment env;
public class ITS016ExportToJson
{
private readonly IStorageContext _rootContext;

public ITS016BackupRestore(ITestEnvironment env)
public ITS016ExportToJson(IStorageContext context)
{
this.env = env;
_rootContext = context;

}

[Fact]
public async Task VerifyExportToJson()
{
// Export Table
using (var storageContext = new StorageContext(env.ConnectionString))
using (var storageContext = _rootContext.CreateChildContext())
{
// set the tablename context
storageContext.SetTableContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 Newtonsoft.Json.Linq;
using Xunit.DependencyInjection;

namespace CoreHelpers.WindowsAzure.Storage.Table.Tests
{
[Startup(typeof(Startup))]
[Collection("Sequential")]
public class ITS017ImportFromJson
{
private readonly IStorageContext _rootContext;

public ITS017ImportFromJson(IStorageContext context)
{
_rootContext = context;
}

[Fact]
public async Task VerifyImportFromJson()
{
await Task.CompletedTask;

// Import Table
using (var storageContext = _rootContext.CreateChildContext())
{
// set the tablename context
storageContext.SetTableContext();

// ensure we have a model registered in the correct table
var tableName1 = $"BU".Replace("-", "");
storageContext.AddAttributeMapper(typeof(DemoModel2), tableName1);

// define the import data
var staticExportData = "[{\"RowKey\":\"2\",\"PartitionKey\":\"1\",\"Properties\":[{\"PropertyName\":\"P\",\"PropertyType\":0,\"PropertyValue\":\"1\"},{\"PropertyName\":\"R\",\"PropertyType\":0,\"PropertyValue\":\"2\"}]}]";
var staticExportDataStream = new MemoryStream(Encoding.UTF8.GetBytes(staticExportData ?? ""));

// check if we have an empty tabel before import
Assert.Empty(await storageContext.EnableAutoCreateTable().Query<DemoModel2>().Now());

// open the data stream
using (var streamReader = new StreamReader(staticExportDataStream))
{
// read the data
await storageContext.ImportFromJsonAsync(tableName1, streamReader, (ImportExportOperation) => { });
}

// check if we have the dara correctly imported
Assert.Single(await storageContext.Query<DemoModel2>().Now());

// get the data
var data = await storageContext.Query<DemoModel2>().Now();
Assert.Equal("1", data.First().P);
Assert.Equal("2", data.First().R);

// drop table
await storageContext.DropTableAsync<DemoModel2>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
<Folder Include="Attributes\" />
<Folder Include="Extensions\" />
<Folder Include="Delegates\" />
<Folder Include="Services\" />
<Folder Include="Internal\" />
<Folder Include="Serialization\" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
namespace CoreHelpers.WindowsAzure.Storage.Table.Extensions
{
public enum ExportEdmType
{
String,
Binary,
Boolean,
DateTime,
Double,
Guid,
Int32,
Int64
}

public static class TypeExtensions
{
public static ExportEdmType GetEdmPropertyType(this Type type)
{
if (type == typeof(string))
return ExportEdmType.String;
else if (type == typeof(byte[]))
return ExportEdmType.Binary;
else if (type == typeof(Boolean) || type == typeof(bool))
return ExportEdmType.Boolean;
else if (type == typeof(DateTime) || type == typeof(DateTimeOffset))
return ExportEdmType.DateTime;
else if (type == typeof(Double))
return ExportEdmType.Double;
else if (type == typeof(Guid))
return ExportEdmType.Guid;
else if (type == typeof(Int32) || type == typeof(int))
return ExportEdmType.Int32;
else if (type == typeof(Int64))
return ExportEdmType.Int64;
else
throw new NotImplementedException($"Datatype {type.ToString()} not supporter");
}
}
}




131 changes: 0 additions & 131 deletions CoreHelpers.WindowsAzure.Storage.Table/Services/DataImportService.cs

This file was deleted.

Loading

0 comments on commit 691d6f0

Please sign in to comment.