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

Dynamic store configuration improvements #1695

Merged
merged 8 commits into from
Jun 26, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
additional negative tests
  • Loading branch information
MaceWindu committed Apr 21, 2019
commit 93bf0d9b53180d7bc83d28571b99fafa14452da3
106 changes: 106 additions & 0 deletions Tests/Linq/Mapping/DynamicStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,74 @@ public void SetProperty(string name, object value)
}
}

[Table("DynamicColumnsTestTable")]
[DynamicColumnAccessor(
GetterMethod = nameof(GetProperty), SetterMethod = nameof(SetProperty),
GetterExpressionMethod = nameof(GetPropertyExpression), SetterExpressionMethod = nameof(SetPropertyExpression))]
class MultipleGetterSetterMethods
{
[Column]
public int Id { get; set; }

[DynamicColumnsStore]
public Dictionary<string, object> Values { get; set; } = new Dictionary<string, object>();

public object GetProperty(string name, object defaultValue)
{
if (!Values.TryGetValue(name, out object value))
value = defaultValue;

return value;
}

public void SetProperty(string name, object value)
{
Values[name] = value;
}
public static Expression<Func<InstanceGetterSetterExpressionMethods, string, object, object>> GetPropertyExpression()
{
return (instance, name, defaultValue) => instance.GetProperty(name, defaultValue);
}

public static Expression<Action<InstanceGetterSetterMethods, string, object>> SetPropertyExpression()
{
return (instance, name, value) => instance.SetProperty(name, value);
}
}

[Table("DynamicColumnsTestTable")]
[DynamicColumnAccessor]
class NoGetterSetterMethods
{
[Column]
public int Id { get; set; }

[DynamicColumnsStore]
public Dictionary<string, object> Values { get; set; } = new Dictionary<string, object>();

public object GetProperty(string name, object defaultValue)
{
if (!Values.TryGetValue(name, out object value))
value = defaultValue;

return value;
}

public void SetProperty(string name, object value)
{
Values[name] = value;
}
public static Expression<Func<InstanceGetterSetterExpressionMethods, string, object, object>> GetPropertyExpression()
{
return (instance, name, defaultValue) => instance.GetProperty(name, defaultValue);
}

public static Expression<Action<InstanceGetterSetterMethods, string, object>> SetPropertyExpression()
{
return (instance, name, value) => instance.SetProperty(name, value);
}
}

[Test]
public void TestDynamicColumnStoreFromMetadataReader([IncludeDataSources(true, TestProvName.AllSQLite)] string context)
{
Expand Down Expand Up @@ -732,5 +800,43 @@ public void TestDynamicColumnStoreAccessorsDefaultValue([IncludeDataSources(true
Assert.AreEqual("accessor_def", data[0].Values["Name"]);
}
}

[Test]
public void TestDynamicColumnStoreMultipleGetterSetters([IncludeDataSources(true, TestProvName.AllSQLite)] string context)
{
var ms = new MappingSchema();
ms.GetFluentMappingBuilder()
.Entity<MultipleGetterSetterMethods>()
.Property(x => Sql.Property<string>(x, "Name"));

using (var db = GetDataContext(context, ms))
using (db.CreateLocalTable<DynamicColumnsTestFullTable>())
{
var obj = new MultipleGetterSetterMethods { Id = 5 };
obj.Values.Add("Name", "test_name");

Assert.Throws<LinqToDBException>(() => db.Insert(obj));
Assert.Throws<LinqToDBException>(() => db.GetTable<MultipleGetterSetterMethods>().ToList());
}
}

[Test]
public void TestDynamicColumnStoreNoGetterSetters([IncludeDataSources(true, TestProvName.AllSQLite)] string context)
{
var ms = new MappingSchema();
ms.GetFluentMappingBuilder()
.Entity<NoGetterSetterMethods>()
.Property(x => Sql.Property<string>(x, "Name"));

using (var db = GetDataContext(context, ms))
using (db.CreateLocalTable<DynamicColumnsTestFullTable>())
{
var obj = new NoGetterSetterMethods { Id = 5 };
obj.Values.Add("Name", "test_name");

Assert.Throws<LinqToDBException>(() => db.Insert(obj));
Assert.Throws<LinqToDBException>(() => db.GetTable<NoGetterSetterMethods>().ToList());
}
}
}
}