Skip to content

Commit

Permalink
Don't include ignored props in generated SQL.
Browse files Browse the repository at this point in the history
Fixes #3.
  • Loading branch information
jtompkins committed Mar 17, 2015
1 parent dc10e59 commit 0d5ec60
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
29 changes: 23 additions & 6 deletions Haberdasher.Tests/PropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,32 +102,49 @@ private class TestClass
[Fact]
public void MarksPropertiesAsNotSelectable() {
var totalProperty = _testClassType.GetProperty<TestClass>(t => t.Total);
var zipProperty = _testClassType.GetProperty<TestClass>(t => t.Zip);

Assert.Equal(false, totalProperty.IsSelectable);
Assert.Equal(false, zipProperty.IsSelectable);
}

[Fact]
public void MarksPropertiesAsNotInsertable() {
var addressProperty = _testClassType.GetProperty<TestClass>(t => t.Address);
var stateProperty = _testClassType.GetProperty<TestClass>(t => t.State);
var zipProperty = _testClassType.GetProperty<TestClass>(t => t.Zip);

Assert.Equal(false, addressProperty.IsInsertable);
Assert.Equal(false, stateProperty.IsInsertable);
Assert.Equal(false, zipProperty.IsInsertable);
}

[Fact]
public void MarksPropertiesAsNotUpdatable() {
var cityProperty = _testClassType.GetProperty<TestClass>(t => t.City);
var stateProperty = _testClassType.GetProperty<TestClass>(t => t.State);
var zipProperty = _testClassType.GetProperty<TestClass>(t => t.Zip);

Assert.Equal(false, cityProperty.IsUpdatable);
Assert.Equal(false, stateProperty.IsUpdatable);
Assert.Equal(false, zipProperty.IsUpdatable);
}

[Fact]
public void RemovesIgnoredPropertiesFromCache() {
var totalProperty = _testClassType.GetProperty<TestClass>(t => t.Total);
var cityProperty = _testClassType.GetProperty<TestClass>(t => t.City);
var stateProperty = _testClassType.GetProperty<TestClass>(t => t.State);

// ignore: select
Assert.False(_testClassType.SelectFields.Contains(totalProperty));

// ignore: update
Assert.False(_testClassType.UpdateFields.Contains(cityProperty));

// ignore: writes
Assert.False(_testClassType.InsertFields.Contains(stateProperty));
Assert.False(_testClassType.UpdateFields.Contains(stateProperty));
}

[Fact]
public void ForgetsPropertiesThatAreIgnoreAll()
{
Assert.Throws<InvalidOperationException>(() => _testClassType.GetProperty<TestClass>(t => t.Zip));
}
}
}
53 changes: 53 additions & 0 deletions Haberdasher/EntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,58 @@ public class EntityType<TEntity> where TEntity : class, new()
return cachedProperty;
}

private void RemoveFromSelect(EntityProperty property) {
if (SelectFields.Contains(property))
SelectFields.Remove(property);
}

private void RemoveFromInsert(EntityProperty property) {
if (InsertFields.Contains(property))
InsertFields.Remove(property);
}

private void RemoveFromUpdate(EntityProperty property) {
if (UpdateFields.Contains(property))
UpdateFields.Remove(property);
}

private void RemoveFromFieldCache(EntityProperty property, IgnoreTypeEnum? type)
{
if (!type.HasValue)
return;

switch (type)
{
case IgnoreTypeEnum.Select:
RemoveFromSelect(property);

break;

case IgnoreTypeEnum.Insert:
RemoveFromInsert(property);

break;

case IgnoreTypeEnum.Update:
RemoveFromUpdate(property);

break;

case IgnoreTypeEnum.Writes:
RemoveFromInsert(property);
RemoveFromUpdate(property);

break;

case IgnoreTypeEnum.All:
RemoveFromSelect(property);
RemoveFromInsert(property);
RemoveFromUpdate(property);

break;
}
}

#endregion

#region Fluent Interface Methods
Expand Down Expand Up @@ -121,6 +173,7 @@ public class EntityType<TEntity> where TEntity : class, new()
var cachedProperty = GetProperty(property);

cachedProperty.SetIgnore(type);
RemoveFromFieldCache(cachedProperty, type);

return this;
}
Expand Down

0 comments on commit 0d5ec60

Please sign in to comment.