public void LocalCrmTests_AdvancedCrud()
        {
            // Verify that linked items exist upon create
            var service = GetService();
            var contact = new Contact {Id = Guid.NewGuid()};
            var opp = new Opportunity {ParentContactId = contact.ToEntityReference()};

            try
            {
                service.Create(opp);
                Assert.Fail("Opportunity Creation should have failed since the Contact Doesn't exist");
            }
            catch (System.ServiceModel.FaultException<OrganizationServiceFault> ex)
            {
                Assert.IsTrue(ex.Message.Contains(String.Format("With Id = {0} Does Not Exist", contact.Id)), "Exception type is different than expected");
            }
            catch (AssertFailedException)
            {
                throw;
            }
            catch (Exception)
            {
                Assert.Fail("Exception type is different than expected");
            }

            service.Create(contact);
            AssertCrm.Exists(service, contact);
            opp.Id = service.Create(opp);
            AssertCrm.Exists(service, opp);
        }
        public void LocalCrmTests_RetrieveOuterJoinedColumn()
        {
            var service = GetService();
            var contact = new Contact {FirstName = "Joe"};
            contact.Id = service.Create(contact);

            // Create 2 opportunities
            service.Create(new Opportunity {CustomerId = contact.ToEntityReference()});
            service.Create(new Opportunity());

            var qe = QueryExpressionFactory.Create<Opportunity>();
            qe.AddLink<Contact>(Opportunity.Fields.ParentContactId, Contact.Fields.ContactId, JoinOperator.LeftOuter, c => new {c.FirstName});

            var entities = service.GetEntities(qe);
            Assert.AreEqual(2, entities.Count, "Two opportunities should have been returned!");
            Assert.AreEqual(contact.FirstName, entities.First(o => o.ParentContactId != null).GetAliasedEntity<Contact>().FirstName, "First Name wasn't returned!");
            Assert.IsNull(entities.First(o => o.ParentContactId == null).GetAliasedEntity<Contact>().FirstName, "Second Opportunity some how has a contact!");
        }
        public void LocalCrmTests_RetrieveFilterOnOuterJoinedColumn()
        {
            var service = GetService();
            var contact1 = new Contact {FirstName = "Joe"};
            contact1.Id = service.Create(contact1);
            var contact2 = new Contact {FirstName = "Jim"};
            contact2.Id = service.Create(contact2);
            var contact3 = new Contact {FirstName = "Jake"};
            contact3.Id = service.Create(contact3);

            service.Create(new Opportunity {CustomerId = contact1.ToEntityReference()});
            service.Create(new Opportunity {CustomerId = contact2.ToEntityReference()});
            service.Create(new Opportunity {CustomerId = contact3.ToEntityReference()});
            service.Create(new Opportunity());

            var qe = QueryExpressionFactory.Create<Opportunity>();
            qe.AddLink<Contact>(Opportunity.Fields.ParentContactId, Contact.Fields.ContactId, JoinOperator.LeftOuter, c => new {c.FirstName}).EntityAlias = "MyAlias";
            qe.Criteria.AddCondition("MyAlias", Contact.Fields.FirstName, ConditionOperator.Equal, "Joe");
            var entities = service.GetEntities(qe);

            Assert.AreEqual(1, entities.Count, "Only Joe opportunities should have been returned!");
            Assert.AreEqual(contact1.FirstName, entities[0].GetAliasedEntity<Contact>().FirstName);

            qe.Criteria.AddCondition("MyAlias", Contact.Fields.FirstName, ConditionOperator.Equal, "Jim");
            qe.Criteria.FilterOperator = LogicalOperator.Or;
            entities = service.GetEntities(qe);

            Assert.AreEqual(2, entities.Count, "Joe and Jim opportunities should have been returned!");
        }
        public void LocalCrmTests_BasicCrud()
        {
            var c1 = new Contact {Id = Guid.NewGuid(), FirstName = "Joe", LastName = "Plumber"};
            var c2 = new Contact {Id = Guid.NewGuid(), FirstName = "Bill", LastName = "Carpenter"};
            var opp = new Opportunity {Id = Guid.NewGuid(), CustomerId = c1.ToEntityReference() };

            var service = GetService();
            service.Create(c1);
            service.Create(c2);
            service.Create(opp);

            Assert.IsNotNull(service.GetFirstOrDefault<Opportunity>(Opportunity.Fields.ParentContactId, c1.Id), "Failed Simple Lookup by Attribute Entity Reference");
            Assert.AreEqual(1, service.GetEntitiesById<Contact>(c1.Id).Count, "Failed Simple Where In Lookup by Id");

            var qe = QueryExpressionFactory.Create<Opportunity>();
            qe.AddLink<Contact>(Opportunity.Fields.ParentContactId, "contactid", c => new {c.FirstName});

            var otherC = service.GetFirstOrDefault(qe);

            Assert.IsNotNull(otherC, "Failed Simple Lookup with Linked Entity on Entity Reference");
            Assert.AreEqual(c1.FirstName, otherC.GetAliasedEntity<Contact>().FirstName, "Failed Simple Lookup retrieving Linked Entity columns");
        }