Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Commit

Permalink
Merge branch 'BetimBeja-UpdateNullValues'
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Feb 7, 2019
2 parents 01ec397 + 122cf9a commit 71f0ef0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
32 changes: 20 additions & 12 deletions FakeXrmEasy.Shared/Extensions/EntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,9 @@ public static void ProjectAttributes(Entity e, Entity projected, LinkEntity le,

public static Entity ProjectAttributes(this Entity e, QueryExpression qe, XrmFakedContext context)
{
if (qe.ColumnSet == null) return e;

if (qe.ColumnSet.AllColumns)
if (qe.ColumnSet == null || qe.ColumnSet.AllColumns)
{
return e; //return all the original attributes
return RemoveNullAttributes(e); //return all the original attributes
}
else
{
Expand Down Expand Up @@ -170,15 +168,28 @@ public static Entity ProjectAttributes(this Entity e, QueryExpression qe, XrmFak
//Plus attributes from joins
foreach (var le in qe.LinkEntities)
{
ProjectAttributes(e, projected, le, context);
ProjectAttributes(RemoveNullAttributes(e), projected, le, context);
}
//foreach (var attKey in e.Attributes.Keys)
//{
// if(e[attKey] is AliasedValue && !projected.Attributes.ContainsKey(attKey))
// projected[attKey] = e[attKey];
//}
return projected;
return RemoveNullAttributes(projected);
}
}

public static Entity RemoveNullAttributes(Entity entity)
{
IList<string> nullAttributes = entity.Attributes
.Where(attribute => attribute.Value == null ||
(attribute.Value is AliasedValue && (attribute.Value as AliasedValue).Value == null))
.Select(attribute => attribute.Key).ToList();
foreach (var nullAttribute in nullAttributes)
{
entity.Attributes.Remove(nullAttribute);
}
return entity;
}

public static object CloneAttribute(object attributeValue)
Expand Down Expand Up @@ -206,7 +217,7 @@ public static object CloneAttribute(object attributeValue)
clone.KeyAttributes.AddRange(original.KeyAttributes.Select(kvp => new KeyValuePair<string, object>(CloneAttribute(kvp.Key) as string, kvp.Value)).ToArray());
}
#endif
return clone;
return clone;
}
else if (type == typeof(BooleanManagedProperty))
{
Expand Down Expand Up @@ -332,10 +343,7 @@ public static Entity Clone(this Entity e, Type t)

foreach (var attKey in e.Attributes.Keys)
{
if (e[attKey] != null)
{
cloned[attKey] = CloneAttribute(e[attKey]);
}
cloned[attKey] = e[attKey] != null ? CloneAttribute(e[attKey]) : null;
}

#if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 && !FAKE_XRM_EASY_2015
Expand Down Expand Up @@ -529,6 +537,6 @@ public static EntityReference ToEntityReferenceWithKeyAttributes(this Entity e)
return result;
}


}
}
11 changes: 7 additions & 4 deletions FakeXrmEasy.Shared/XrmFakedContext.Crud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,14 @@ protected void UpdateEntity(Entity e)
foreach (var sAttributeName in e.Attributes.Keys.ToList())
{
var attribute = e[sAttributeName];
if (attribute is DateTime)
if (attribute == null)
{
cachedEntity.Attributes.Remove(sAttributeName);
}
else if (attribute is DateTime)
{
cachedEntity[sAttributeName] = ConvertToUtc((DateTime)e[sAttributeName]);
}

else
{
if (attribute is EntityReference && ValidateReferences)
Expand Down Expand Up @@ -244,7 +247,7 @@ protected EntityReference ResolveEntityReference(EntityReference er)
protected EntityReference ResolveEntityReferenceByAlternateKeys(EntityReference er)
{
var resolvedId = GetRecordUniqueId(er);

return new EntityReference()
{
LogicalName = er.LogicalName,
Expand Down Expand Up @@ -361,7 +364,7 @@ protected void AddEntityDefaultAttributes(Entity e)
Data["systemuser"].Add(CallerId.Id, new Entity("systemuser") { Id = CallerId.Id });
}
}

}

var isManyToManyRelationshipEntity = e.LogicalName != null && this.Relationships.ContainsKey(e.LogicalName);
Expand Down
27 changes: 27 additions & 0 deletions FakeXrmEasy.Tests.Shared/FakeContextTests/FakeContextTestUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ public void When_an_entity_is_updated_with_an_empty_logical_name_an_exception_is
Assert.Equal("The entity logical name must not be null or empty.", ex.Message);
}

[Fact]
public void When_an_entity_is_updated_with_a_null_attribute_the_attribute_is_removed()
{
var context = new XrmFakedContext();
var entity = new Account { Id = Guid.NewGuid() };
entity.DoNotEMail = true;
context.Initialize(entity);

var update = new Account() { Id = entity.Id };
update.DoNotEMail = null;

var service = context.GetOrganizationService();
service.Update(update);

var updatedEntityAllAttributes = service.Retrieve(Account.EntityLogicalName, update.Id, new ColumnSet(true));
var updatedEntityAllAttributesEarlyBound = updatedEntityAllAttributes.ToEntity<Account>();

var updatedEntitySingleAttribute = service.Retrieve(Account.EntityLogicalName, update.Id, new ColumnSet(new string[] { "donotemail" }));
var updatedEntitySingleAttributeEarlyBound = updatedEntityAllAttributes.ToEntity<Account>();

Assert.Null(updatedEntityAllAttributesEarlyBound.DoNotEMail);
Assert.False(updatedEntityAllAttributes.Attributes.ContainsKey("donotemail"));

Assert.Null(updatedEntitySingleAttributeEarlyBound.DoNotEMail);
Assert.False(updatedEntitySingleAttribute.Attributes.ContainsKey("donotemail"));
}

[Fact]
public void When_updating_an_entity_the_context_should_reflect_changes()
{
Expand Down

0 comments on commit 71f0ef0

Please sign in to comment.