public void When_doing_a_crm_linq_query_with_an_equals_operator_record_is_returned() { var fakedContext = new XrmFakedContext(); var guid1 = Guid.NewGuid(); var guid2 = Guid.NewGuid(); fakedContext.Initialize(new List<Entity>() { new Contact() { Id = guid1, FirstName = "Jordi" }, new Contact() { Id = guid2, FirstName = "Other" } }); var service = fakedContext.GetFakedOrganizationService(); using (XrmServiceContext ctx = new XrmServiceContext(service)) { var matches = (from c in ctx.CreateQuery<Contact>() where c.FirstName.Equals("Jordi") select c).ToList(); Assert.True(matches.Count == 1); Assert.True(matches[0].FirstName.Equals("Jordi")); matches = (from c in ctx.CreateQuery<Contact>() where c.FirstName == "Jordi" //Using now equality operator select c).ToList(); Assert.True(matches.Count == 1); Assert.True(matches[0].FirstName.Equals("Jordi")); } }
public void When_doing_a_crm_linq_query_and_proxy_types_and_a_selected_attribute_returned_projected_entity_is_thesubclass() { var fakedContext = new XrmFakedContext(); var guid1 = Guid.NewGuid(); var guid2 = Guid.NewGuid(); fakedContext.Initialize(new List<Entity>() { new Contact() { Id = guid1, FirstName = "Jordi" }, new Contact() { Id = guid2, FirstName = "Other" } }); var service = fakedContext.GetFakedOrganizationService(); using (XrmServiceContext ctx = new XrmServiceContext(service)) { var matches = (from c in ctx.CreateQuery<Contact>() where c.FirstName.Equals("Jordi") select new { FirstName = c.FirstName, CrmRecord = c }).ToList(); Assert.True(matches.Count == 1); Assert.True(matches[0].FirstName.Equals("Jordi")); Assert.IsAssignableFrom(typeof(Contact), matches[0].CrmRecord); Assert.True(matches[0].CrmRecord.GetType() == typeof(Contact)); } }
public void When_an_entity_is_created_with_an_empty_logical_name_an_exception_is_thrown() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var e = new Entity("") { Id = Guid.Empty }; var ex = Assert.Throws<InvalidOperationException>(() => service.Create(e)); Assert.Equal(ex.Message, "The LogicalName property must not be empty"); }
public void When_getting_a_fake_service_reference_it_uses_a_singleton_pattern() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var service2 = context.GetFakedOrganizationService(); Assert.Equal(service, service2); }
public static void When_a_query_by_attribute_is_executed_when_one_attribute_right_result_is_returned() { var context = new XrmFakedContext(); var account = new Account() {Id = Guid.NewGuid(), Name = "Test"}; var account2 = new Account() { Id = Guid.NewGuid(), Name = "Other account!" }; context.Initialize(new List<Entity>() { account, account2 }); var service = context.GetFakedOrganizationService(); QueryByAttribute query = new QueryByAttribute(); query.Attributes.AddRange(new string[] { "name" }); query.ColumnSet = new ColumnSet(new string[] { "name" }); query.EntityName = Account.EntityLogicalName; query.Values.AddRange(new object[] { "Test" }); //Execute using a request to test the OOB (XRM) message contracts RetrieveMultipleRequest request = new RetrieveMultipleRequest(); request.Query = query; Collection<Entity> entityList = ((RetrieveMultipleResponse)service.Execute(request)).EntityCollection.Entities; Assert.True(entityList.Count == 1); Assert.Equal(entityList[0]["name"].ToString(), "Test"); }
public static void When_using_proxy_types_assembly_the_attribute_metadata_is_inferred_from_the_proxy_types_assembly() { var fakedContext = new XrmFakedContext(); fakedContext.ProxyTypesAssembly = Assembly.GetExecutingAssembly(); var contact1 = new Entity("contact") { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; contact1["firstname"] = "First 1"; var contact2 = new Entity("contact") { Id = Guid.NewGuid() }; contact2["fullname"] = "Contact 2"; contact2["firstname"] = "First 2"; fakedContext.Initialize(new List<Entity>() { contact1, contact2 }); var guid = Guid.NewGuid(); //Empty contecxt (no Initialize), but we should be able to query any typed entity without an entity not found exception var service = fakedContext.GetFakedOrganizationService(); using (XrmServiceContext ctx = new XrmServiceContext(service)) { var contact = (from c in ctx.CreateQuery<Contact>() where c.FirstName.Equals("First 1") select c).ToList(); Assert.True(contact.Count == 1); } }
public void When_doing_a_crm_linq_query_and_proxy_types_projection_must_be_applied_after_where_clause() { var fakedContext = new XrmFakedContext(); fakedContext.ProxyTypesAssembly = Assembly.GetExecutingAssembly(); var guid1 = Guid.NewGuid(); var guid2 = Guid.NewGuid(); fakedContext.Initialize(new List<Entity>() { new Contact() { Id = guid1, FirstName = "Jordi", LastName = "Montana" }, new Contact() { Id = guid2, FirstName = "Other" } }); var service = fakedContext.GetFakedOrganizationService(); using (XrmServiceContext ctx = new XrmServiceContext(service)) { var matches = (from c in ctx.CreateQuery<Contact>() where c.LastName == "Montana" //Should be able to filter by a non-selected attribute select new { FirstName = c.FirstName }).ToList(); Assert.True(matches.Count == 1); Assert.True(matches[0].FirstName.Equals("Jordi")); } }
public void When_retrieve_is_invoked_with_an_empty_guid_an_exception_is_thrown() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var ex = Assert.Throws<InvalidOperationException>(() => service.Retrieve("account", Guid.Empty, new ColumnSet())); Assert.Equal(ex.Message, "The id must not be empty."); }
public void When_retrieve_is_invoked_with_a_null_columnset_exception_is_thrown() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var ex = Assert.Throws<InvalidOperationException>(() => service.Retrieve("account", Guid.NewGuid(), null)); Assert.Equal(ex.Message, "The columnset parameter must not be null."); }
public void When_a_null_entity_is_created_an_exception_is_thrown() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var ex = Assert.Throws<InvalidOperationException>(() => service.Create(null)); Assert.Equal(ex.Message, "The entity must not be null"); }
public void When_an_entity_is_updated_with_an_empty_guid_an_exception_is_thrown() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var e = new Entity("account") { Id = Guid.Empty }; var ex = Assert.Throws<InvalidOperationException>(() => service.Update(e)); Assert.Equal(ex.Message, "The Id property must not be empty"); }
public static void When_a_who_am_i_request_is_invoked_the_caller_id_is_returned() { var context = new XrmFakedContext(); context.CallerId = new EntityReference() { Id = Guid.NewGuid(), Name = "Super Faked User" }; var service = context.GetFakedOrganizationService(); WhoAmIRequest req = new WhoAmIRequest(); var response = service.Execute(req) as WhoAmIResponse; Assert.Equal(response.UserId, context.CallerId.Id); }
public void When_adding_an_entity_the_returned_guid_must_not_be_empty_and_the_context_should_have_it() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var e = new Entity("account") { Id = Guid.Empty }; var guid = service.Create(e); Assert.True(guid != Guid.Empty); Assert.True(context.Data.Count == 1); Assert.True(context.Data["account"].Count == 1); }
public void Querying_a_dynamic_entity_not_present_in_the_context_should_return_no_records() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); //Find the contact var contact = (from c in context.CreateQuery("contact") select c).ToList(); Assert.Equal(contact.Count, 0); }
public void When_initializing_the_context_with_an_entity_it_should_have_default_createdon_createdby_modifiedon_and_modifiedby_attributes() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var e = new Entity("account") { Id = Guid.NewGuid() }; context.Initialize(new List<Entity>() { e }); Assert.True(e.Attributes.ContainsKey("createdon")); Assert.True(e.Attributes.ContainsKey("modifiedon")); Assert.True(e.Attributes.ContainsKey("createdby")); Assert.True(e.Attributes.ContainsKey("modifiedby")); }
public void Querying_an_early_bound_entity_not_present_in_the_context_should_return_no_records() { var context = new XrmFakedContext(); context.ProxyTypesAssembly = Assembly.GetExecutingAssembly(); var service = context.GetFakedOrganizationService(); //Find the contact var contact = (from c in context.CreateQuery<Contact>() select c).ToList(); Assert.Equal(contact.Count, 0); }
public void When_Creating_Without_Id_It_should_Be_set_Automatically() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var account = new Account { Name = "TestAcc" }; account.Id = service.Create(account); Assert.NotEqual(Guid.Empty, account.Id); }
public void When_retrieve_is_invoked_with_an_empty_logical_name_an_exception_is_thrown() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var ex = Assert.Throws<InvalidOperationException>(() => service.Retrieve(null,Guid.Empty,new ColumnSet())); Assert.Equal(ex.Message, "The entity logical name must not be null or empty."); ex = Assert.Throws<InvalidOperationException>(() => service.Retrieve("", Guid.Empty, new ColumnSet())); Assert.Equal(ex.Message, "The entity logical name must not be null or empty."); ex = Assert.Throws<InvalidOperationException>(() => service.Retrieve(" ", Guid.Empty, new ColumnSet())); Assert.Equal(ex.Message, "The entity logical name must not be null or empty."); }
public void When_creating_a_record_using_early_bound_entities_primary_key_should_be_populated() { var context = new XrmFakedContext(); var c = new Contact(); IOrganizationService service = context.GetFakedOrganizationService(); var id = service.Create(c); //Retrieve the record created var contact = (from con in context.CreateQuery<Contact>() select con).FirstOrDefault(); Assert.True(contact.Attributes.ContainsKey("contactid")); Assert.Equal(id, contact["contactid"]); }
public void When_creating_a_record_using_dynamic_entities_primary_key_should_be_populated() { var context = new XrmFakedContext(); Entity e = new Entity("new_myentity"); IOrganizationService service = context.GetFakedOrganizationService(); var id = service.Create(e); //Retrieve the record created var record = (from r in context.CreateQuery("new_myentity") select r).FirstOrDefault(); Assert.True(record.Attributes.ContainsKey("new_myentityid")); Assert.Equal(id, record["new_myentityid"]); }
public void When_retrieve_is_invoked_with_non_existing_entity_null_is_returned() { var context = new XrmFakedContext(); //Initialize the context with a single entity var guid = Guid.NewGuid(); var data = new List<Entity>() { new Entity("account") { Id = guid } }.AsQueryable(); context.Initialize(data); var service = context.GetFakedOrganizationService(); Assert.Throws<FaultException<OrganizationServiceFault>>(() => service.Retrieve("account", Guid.NewGuid(), new ColumnSet())); }
public void When_Creating_With_Id_It_should_Be_set() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var accId = Guid.NewGuid(); var account = new Account { Name = "TestAcc", Id = accId }; var createdId = service.Create(account); Assert.Equal(accId, createdId); }
public void When_delete_is_invoked_with_an_existing_entity_that_entity_is_delete_from_the_context() { var context = new XrmFakedContext(); //Initialize the context with a single entity var guid = Guid.NewGuid(); var data = new List<Entity>() { new Entity("account") { Id = guid } }.AsQueryable(); context.Initialize(data); var service = context.GetFakedOrganizationService(); service.Delete("account", guid); Assert.True(context.Data["account"].Count == 0); }
public static void When_using_proxy_types_assembly_the_entity_metadata_is_inferred_from_the_proxy_types_assembly() { var fakedContext = new XrmFakedContext(); fakedContext.ProxyTypesAssembly = Assembly.GetExecutingAssembly(); //Empty contecxt (no Initialize), but we should be able to query any typed entity without an entity not found exception var service = fakedContext.GetFakedOrganizationService(); using (XrmServiceContext ctx = new XrmServiceContext(service)) { var contact = (from c in ctx.CreateQuery<Contact>() where c.FirstName.Equals("Anything!") select c).ToList(); Assert.True(contact.Count == 0); } }
public void When_retrieve_is_invoked_with_an_existing_entity_and_all_columns_all_the_attributes_are_returned() { var context = new XrmFakedContext(); //Initialize the context with a single entity var guid = Guid.NewGuid(); var data = new List<Entity>() { new Entity("account") { Id = guid } }.AsQueryable(); context.Initialize(data); var service = context.GetFakedOrganizationService(); var result = service.Retrieve("account", guid, new ColumnSet(true)); Assert.Equal(result.Id, data.FirstOrDefault().Id); Assert.Equal(result.Attributes.Count, data.FirstOrDefault().Attributes.Count); }
public void When_delete_is_invoked_with_non_existing_entity_an_exception_is_thrown() { var context = new XrmFakedContext(); //Initialize the context with a single entity var guid = Guid.NewGuid(); var nonExistingGuid = Guid.NewGuid(); var data = new List<Entity>() { new Entity("account") { Id = guid } }.AsQueryable(); context.Initialize(data); var service = context.GetFakedOrganizationService(); var ex = Assert.Throws<FaultException<OrganizationServiceFault>>(() => service.Delete("account", nonExistingGuid)); Assert.Equal(ex.Message, string.Format("account with Id {0} Does Not Exist", nonExistingGuid)); }
public void When_updating_an_entity_the_context_should_reflect_changes() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var e = new Entity("account") { Id = Guid.Empty }; e["name"] = "Before update"; var guid = service.Create(e); Assert.Equal(context.Data["account"][guid]["name"], "Before update"); //now update the name e = new Entity("account") { Id = guid }; e["name"] = "After update"; service.Update(e); Assert.Equal(context.Data["account"][guid]["name"], "After update"); }
public void After_querying_the_context_with_an_invalid_entity_name_exception_is_thrown() { var context = new XrmFakedContext(); context.ProxyTypesAssembly = Assembly.GetExecutingAssembly(); var service = context.GetFakedOrganizationService(); var guid = Guid.NewGuid(); var data = new List<Entity>() { new Contact() { Id = guid } }.AsQueryable(); context.Initialize(data); Assert.Throws<Exception>(() => { var query = (from c in context.CreateQuery(" ") select c); }); }
public void When_executing_a_linq_query_with_equals_between_2_strings_result_is_returned() { var fakedContext = new XrmFakedContext(); var guid = Guid.NewGuid(); fakedContext.Initialize(new List<Entity>() { new Contact() { Id = guid, FirstName = "Jordi" } }); var service = fakedContext.GetFakedOrganizationService(); using (XrmServiceContext ctx = new XrmServiceContext(service)) { var contact = (from c in ctx.CreateQuery<Contact>() where c.FirstName == "Jordi" select c).FirstOrDefault(); Assert.True(contact != null); } }
public void When_doing_a_crm_linq_query_a_retrievemultiple_with_a_queryexpression_is_called() { var fakedContext = new XrmFakedContext(); var guid = Guid.NewGuid(); fakedContext.Initialize(new List<Entity>() { new Contact() { Id = guid, FirstName = "Jordi" } }); var service = fakedContext.GetFakedOrganizationService(); using (XrmServiceContext ctx = new XrmServiceContext(service)) { var contact = (from c in ctx.CreateQuery<Contact>() where c.FirstName.Equals("Jordi") select c).FirstOrDefault(); } A.CallTo(() => service.Execute(A<OrganizationRequest>.That.Matches(x => x is RetrieveMultipleRequest && ((RetrieveMultipleRequest)x).Query is QueryExpression))).MustHaveHappened(); }