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

Commit

Permalink
Merge branch 'bacon1986-issue78'
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Jul 29, 2016
2 parents e3d8467 + 0fac3e3 commit b927db0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 22 deletions.
14 changes: 5 additions & 9 deletions FakeXrmEasy.Shared/FakeMessageExecutors/CreateRequestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ public bool CanExecute(OrganizationRequest request)
public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
{
var createRequest = (CreateRequest)request;

var service = ctx.GetFakedOrganizationService();

var id = service.Create(createRequest.Target);
var guid = service.Create(createRequest.Target);

var response = new CreateResponse()
return new CreateResponse()
{
Results = new ParameterCollection
{
{ "id", id }
}
ResponseName = "Create",
Results = new ParameterCollection { { "id", guid } }
};
return response;

}
}
}
50 changes: 38 additions & 12 deletions FakeXrmEasy.Shared/XrmFakedContext.Crud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected static void FakeRetrieve(XrmFakedContext context, IOrganizationService
entityName));
}
}

//Entity logical name exists, so , check if the requested entity exists
if (context.Data.ContainsKey(entityName) && context.Data[entityName] != null
&& context.Data[entityName].ContainsKey(id))
Expand All @@ -73,7 +73,7 @@ protected static void FakeRetrieve(XrmFakedContext context, IOrganizationService
{
//Return the subset of columns requested only
var foundEntity = context.Data[entityName][id];
Entity projected = foundEntity.ProjectAttributes(columnSet,context);
Entity projected = foundEntity.ProjectAttributes(columnSet, context);
return projected;
}
}
Expand Down Expand Up @@ -234,10 +234,10 @@ protected static void FakeEnableProxyTypes(XrmFakedContext context, Organization
#region Other protected methods
protected void EnsureEntityNameExistsInMetadata(string sEntityName)
{
if(Relationships.Values.Any(value => new[]{value.Entity1LogicalName, value.Entity2LogicalName, value.IntersectEntity}.Contains(sEntityName, StringComparer.InvariantCultureIgnoreCase)))
{
return;
}
if (Relationships.Values.Any(value => new[] { value.Entity1LogicalName, value.Entity2LogicalName, value.IntersectEntity }.Contains(sEntityName, StringComparer.InvariantCultureIgnoreCase)))
{
return;
}
//Entity metadata is checked differently when we are using a ProxyTypesAssembly => we can infer that from the generated types assembly
if (ProxyTypesAssembly != null)
{
Expand Down Expand Up @@ -324,7 +324,7 @@ protected internal void AddEntity(Entity e)
Data[e.LogicalName].Add(e.Id, e);
}


//Update metadata for that entity
if (!AttributeMetadata.ContainsKey(e.LogicalName))
AttributeMetadata.Add(e.LogicalName, new Dictionary<string, string>());
Expand All @@ -343,9 +343,9 @@ protected internal void AddEntity(Entity e)
AttributeMetadata[e.LogicalName].Add(p.Name, p.Name);
}
}
else
else
throw new Exception(string.Format("Couldnt find reflected type for {0}", e.LogicalName));

}
else
{
Expand All @@ -357,16 +357,42 @@ protected internal void AddEntity(Entity e)
AttributeMetadata[e.LogicalName].Add(attKey, attKey);
}
}

if (e.RelatedEntities.Count > 0)
{
foreach (var relationshipSet in e.RelatedEntities)
{
Relationship relationship = relationshipSet.Key;
foreach (var relatedEntity in relationshipSet.Value.Entities)
{
AddEntity(relatedEntity);
}

if (FakeMessageExecutors.ContainsKey(typeof(AssociateRequest)))
{
var entityReferenceCollection = new EntityReferenceCollection(relationshipSet.Value.Entities.Select(en => en.ToEntityReference()).ToList());
var request = new AssociateRequest()
{
Target = new EntityReference() { Id = e.Id, LogicalName = e.LogicalName },
Relationship = relationship,
RelatedEntities = entityReferenceCollection
};
FakeMessageExecutors[typeof(AssociateRequest)].Execute(request, this);
}
else
throw PullRequestException.NotImplementedOrganizationRequest(typeof(AssociateRequest));
}
}
}

protected internal bool AttributeExistsInMetadata(string sEntityName, string sAttributeName)
{
//Early bound types
if(ProxyTypesAssembly != null)
if (ProxyTypesAssembly != null)
{
//Check if attribute exists in the early bound type
var earlyBoundType = FindReflectedType(sEntityName);
if(earlyBoundType != null)
if (earlyBoundType != null)
{
//Get that type properties
var attributeFound = earlyBoundType
Expand All @@ -383,7 +409,7 @@ protected internal bool AttributeExistsInMetadata(string sEntityName, string sAt
//Dynamic entities => just return true
return true;
}
#endregion
#endregion

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System.Text;
using Xunit;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;

namespace FakeXrmEasy.Tests.FakeContextTests.OrgServiceContextTests
{
Expand All @@ -16,7 +19,7 @@ public void When_calling_context_add_and_save_changes_entity_is_added_to_the_fak
var context = new XrmFakedContext();
var service = context.GetFakedOrganizationService();

using(var ctx = new XrmServiceContext(service))
using (var ctx = new XrmServiceContext(service))
{
ctx.AddObject(new Account() { Name = "Test account" });
ctx.SaveChanges();
Expand All @@ -28,5 +31,71 @@ public void When_calling_context_add_and_save_changes_entity_is_added_to_the_fak
Assert.Equal("Test account", account.Name);
}
}

[Fact]
public void When_calling_context_add_and_save_changes_returns_correct_result()
{
var context = new XrmFakedContext();
var service = context.GetFakedOrganizationService();

using (var ctx = new XrmServiceContext(service))
{
ctx.AddObject(new Account() { Name = "Test account" });
var result = ctx.SaveChanges();

Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.Equal("Create", result.First().Response.ResponseName);
Assert.IsType<CreateResponse>(result.First().Response);
Assert.NotEqual(Guid.Empty, ((CreateResponse)result.First().Response).id);
}
}

[Fact]
public void When_calling_context_add_addrelated_and_save_changes_entities_are_added_to_the_faked_context()
{
var context = new XrmFakedContext();

var relationship = new XrmFakedRelationship()
{
IntersectEntity = "accountleads",
Entity1Attribute = "accountid",
Entity2Attribute = "leadid",
Entity1LogicalName = "account",
Entity2LogicalName = "lead"
};
context.AddRelationship("accountleads", relationship);

var service = context.GetFakedOrganizationService();

using (var ctx = new XrmServiceContext(service))
{
var account = new Account() { Name = "Test account" };
ctx.AddObject(account);

var contact = new Lead() { FirstName = "Jane", LastName = "Doe" };
ctx.AddRelatedObject(account, new Relationship("accountleads"), contact);
var result = ctx.SaveChanges();

var resultaccount = ctx.CreateQuery<Account>()
.ToList()
.FirstOrDefault();

Assert.NotNull(resultaccount);
Assert.Equal("Test account", resultaccount.Name);

var reaultlead = ctx.CreateQuery<Lead>()
.ToList()
.FirstOrDefault();

Assert.NotNull(reaultlead);
Assert.Equal("Jane", reaultlead.FirstName);
Assert.Equal("Doe", reaultlead.LastName);

var relationshipRecords = ctx.CreateQuery("accountleads")
.ToList();
Assert.NotEmpty(relationshipRecords);
}
}
}
}

0 comments on commit b927db0

Please sign in to comment.