public void Should_Generate_Insert_Command()
        {
            // Arrange
            using (var sandbox = ConnectionTestsSandbox.Create())
            {
                using (var dbConnection = sandbox.Container.Resolve <CrmDbConnection>())
                {
                    var selectCommand = new CrmDbCommand(dbConnection, "SELECT contactid, firstname, lastname FROM contact");

                    using (var adapter = new CrmDataAdapter(selectCommand))
                    {
                        adapter.SelectCommand = selectCommand;

                        using (var sut = new CrmCommandBuilder(adapter))
                        {
                            // verify that the crmcommand builder generates appropriate insert / udapte and delete commands.
                            // Act
                            var insertCommand = sut.GetInsertCommand();

                            // Assert
                            Assert.That(insertCommand, Is.Not.Null);
                            Assert.That(insertCommand.CommandText, Is.Not.Null);
                            Assert.That(insertCommand.CommandText, Is.Not.EqualTo(""));

                            Console.WriteLine(insertCommand.CommandText);
                        }
                    }
                }
            }
        }
        public void Should_Not_Support_Generation_Of_Update_Command_Using_Conflict_Option_Compare_All_Searchable_Values()
        {
            // Arrange
            using (var sandbox = ConnectionTestsSandbox.Create())
            {
                using (var dbConnection = sandbox.Container.Resolve <CrmDbConnection>())
                {
                    var selectCommand = new CrmDbCommand(dbConnection, "SELECT contactid, firstname, lastname FROM contact");

                    using (var adapter = new CrmDataAdapter(selectCommand))
                    {
                        adapter.SelectCommand = selectCommand;

                        using (var sut = new CrmCommandBuilder(adapter))
                        {
                            sut.ConflictOption = ConflictOption.CompareAllSearchableValues;
                            // verify that the crmcommand builder generates appropriate insert / udapte and delete commands.
                            // Act
                            var updateCommand = sut.GetUpdateCommand();

                            // Assert
                            Assert.That(updateCommand, Is.Not.Null);
                            Assert.That(updateCommand.CommandText, Is.Not.Null);
                            Assert.That(updateCommand.CommandText, Is.Not.EqualTo(""));

                            Console.WriteLine(updateCommand.CommandText);
                        }
                    }
                }
            }
        }
 public void Should_Be_Able_To_Create_A_New_Connection()
 {
     using (ConnectionTestsSandbox.Create())
     {
         var dbConnection = ResolveTestSubjectInstance();
     }
 }
        public void Should_Throw_On_Attempt_To_Open_A_Non_Closed_Connection()
        {
            // Arrange
            using (var sandbox = ConnectionTestsSandbox.Create())
            {
                var dbConnection = ResolveTestSubjectInstance();

                // Act
                dbConnection.Open();
                dbConnection.Open();
            }
        }
Beispiel #5
0
        public void Should_Be_Able_To_Fill_DataSet_Schema_Only()
        {
            // Arrange
            using (var sandbox = ConnectionTestsSandbox.Create())
            {
                var dbConnection  = sandbox.Container.Resolve <CrmDbConnection>();
                var selectCommand = new CrmDbCommand(dbConnection);
                selectCommand.CommandText = "SELECT * FROM contact";

                int resultCount = 100;

                var            entityDataGenerator = new EntityDataGenerator();
                IList <Entity> fakeContactsData    = entityDataGenerator.GenerateFakeEntities("contact", resultCount);

                // This is the fake reponse that the org service will return when its requested to get the data.
                var response = new RetrieveMultipleResponse
                {
                    Results = new ParameterCollection
                    {
                        { "EntityCollection", new EntityCollection(fakeContactsData)
                          {
                              EntityName = "contact"
                          } }
                    }
                };

                // Setup fake org service to return fake response.
                sandbox.FakeOrgService.Stub(f => f.Execute(Arg <OrganizationRequest> .Matches(new OrganizationRequestMessageConstraint <RetrieveMultipleRequest>())))
                .WhenCalled(x =>
                {
                    var request = ((RetrieveMultipleRequest)x.Arguments[0]);
                }).Return(response);

                // Act
                var ds      = new DataSet();
                var subject = ResolveTestSubjectInstance();
                subject.SelectCommand = selectCommand;
                subject.FillSchema(ds, SchemaType.Source);

                // var result = subject.Fill(ds);

                // Assert
                Assert.NotNull(ds);
                Assert.NotNull(ds.Tables);
                Assert.That(ds.Tables.Count, NUnit.Framework.Is.EqualTo(1));

                var table = ds.Tables[0];
                Assert.That(table.Rows.Count, NUnit.Framework.Is.EqualTo(0));
                Assert.That(table.Columns.Count, NUnit.Framework.Is.GreaterThan(0));
            }
        }
        public void Should_Obtain_Organisation_Service_Instance_On_Open()
        {
            // Arrange
            using (var sandbox = ConnectionTestsSandbox.Create())
            {
                var dbConnection = ResolveTestSubjectInstance();

                // Act
                dbConnection.Open();

                // Assert
                sandbox.FakeServiceProvider.AssertWasCalled(o => o.GetOrganisationService(), options => options.Repeat.Once());
            }
        }
 public void Should_Be_Able_To_Open_And_Close_Connection()
 {
     // Arrange
     using (ConnectionTestsSandbox.Create())
     {
         var dbConnection = ResolveTestSubjectInstance();
         // Act
         dbConnection.Open();
         // Assert
         Assert.That(dbConnection.State == ConnectionState.Open);
         dbConnection.Close();
         Assert.That(dbConnection.State == ConnectionState.Closed);
     }
 }
        public void Should_Be_Able_To_Spawn_A_Command()
        {
            // Arrange
            using (var sandbox = ConnectionTestsSandbox.Create())
            {
                var dbConnection = ResolveTestSubjectInstance();

                // Act
                var command = dbConnection.CreateCommand();

                // Assert
                Assert.That(command != null);
            }
        }
        public void Should_Dispose_Organisation_Service_Instance_On_Close()
        {
            // Arrange
            using (var sandbox = ConnectionTestsSandbox.Create())
            {
                var dbConnection = ResolveTestSubjectInstance();

                // Act
                dbConnection.Open();
                dbConnection.Close();

                // Assert
                var orgs = sandbox.FakeOrgService as IDisposable;
                orgs.AssertWasCalled(o => o.Dispose(), options => options.Repeat.Once());
            }
        }
        public void Should_Close_Connection_On_Dispose()
        {
            // Arrange
            using (var sandbox = ConnectionTestsSandbox.Create())
            {
                var dbConnection = ResolveTestSubjectInstance();

                // Act
                using (dbConnection)
                {
                    dbConnection.Open();
                }

                // Assert
                Assert.That(dbConnection.State == ConnectionState.Closed);
            }
        }
Beispiel #11
0
        public void Should_Be_Able_To_Update_DataSet_Containing_An_Insert_An_Update_And_A_Delete_Using_Command_Builder()
        {
            // Arrange
            using (var sandbox = ConnectionTestsSandbox.Create())
            {
                var dbConnection  = sandbox.Container.Resolve <CrmDbConnection>();
                var selectCommand = new CrmDbCommand(dbConnection);
                selectCommand.CommandText = "SELECT contactid, firstname, lastname FROM contact";

                // Create a dataset, fill with schema.
                var ds      = new DataSet();
                var subject = ResolveTestSubjectInstance();
                subject.SelectCommand = selectCommand;

                // Use command builder to generate automatically the update / delete / insert commands.
                using (var commandBuilder = new CrmCommandBuilder(subject))
                {
                    var result = subject.FillSchema(ds, SchemaType.Source);


                    // Fill the dataset with 100 contact entities from the data source.
                    int            resultCount         = 100;
                    var            entityDataGenerator = new EntityDataGenerator();
                    IList <Entity> fakeContactsData    = entityDataGenerator.GenerateFakeEntities("contact", resultCount);

                    // This is the fake reponse that the org service will return when its requested to get the data.
                    var retrieveResponse = new RetrieveMultipleResponse
                    {
                        Results = new ParameterCollection
                        {
                            { "EntityCollection", new EntityCollection(fakeContactsData)
                              {
                                  EntityName = "contact"
                              } }
                        }
                    };

                    // Setup fake org service to return fake response.
                    sandbox.FakeOrgService.Stub(f => f.Execute(Arg <OrganizationRequest> .Matches(new OrganizationRequestMessageConstraint <RetrieveMultipleRequest>())))
                    .WhenCalled(x =>
                    {
                        var request = ((RetrieveMultipleRequest)x.Arguments[0]);
                    }).Return(retrieveResponse);

                    subject.Fill(ds);

                    // Now add a new contact, update an existing contact, and delete an existing contact.
                    var newContact       = entityDataGenerator.GenerateFakeEntities("contact", 1)[0];
                    var contactDataTable = ds.Tables[0];

                    var firstNameCol = contactDataTable.Columns["firstname"];
                    var lastnameCol  = contactDataTable.Columns["lastname"];
                    var contactidcol = contactDataTable.Columns["contactid"];

                    var newRow = contactDataTable.NewRow();

                    newRow.SetField(firstNameCol, newContact["firstname"]);
                    newRow.SetField(lastnameCol, newContact["lastname"]);
                    newRow.SetField(contactidcol, newContact.Id);

                    contactDataTable.Rows.Add(newRow);

                    // update existing contact.
                    var modifiedRow = contactDataTable.Rows[50];

                    var updatedFirstName = "Jessie";
                    var updatedLastName  = "James";
                    modifiedRow.SetField(firstNameCol, updatedFirstName);
                    modifiedRow.SetField(lastnameCol, updatedLastName);

                    // Delete existing contact
                    var deleteRow       = contactDataTable.Rows[99];
                    var deleteContactId = (Guid)deleteRow[contactidcol];
                    deleteRow.Delete();

                    // When we call update on the dataset we need to verify that the org service is sent
                    // an appropriate Create / Updated and Delete Request.
                    var createResponse = new CreateResponse
                    {
                        Results = new ParameterCollection
                        {
                            { "id", newContact.Id }
                        }
                    };

                    // Setup fake org service create response.
                    CreateRequest capturedCreateRequest = null;

                    sandbox.FakeOrgService.Stub(f => f.Execute(Arg <OrganizationRequest> .Matches(new OrganizationRequestMessageConstraint <CreateRequest>())))
                    .WhenCalled(x =>
                    {
                        var request           = ((CreateRequest)x.Arguments[0]);
                        capturedCreateRequest = request;
                    }).Return(createResponse);


                    // Setup fake org service update response.
                    var updateResponse = new UpdateResponse
                    {
                        Results = new ParameterCollection()
                        {
                        }
                    };

                    UpdateRequest capturedUpdateRequest = null;

                    sandbox.FakeOrgService.Stub(f => f.Execute(Arg <OrganizationRequest> .Matches(new OrganizationRequestMessageConstraint <UpdateRequest>())))
                    .WhenCalled(x =>
                    {
                        var request           = ((UpdateRequest)x.Arguments[0]);
                        capturedUpdateRequest = request;
                    }).Return(updateResponse);

                    // Setup fake org service delete response.
                    var deleteResponse = new DeleteResponse
                    {
                        Results = new ParameterCollection()
                        {
                        }
                    };


                    DeleteRequest capturedDeleteRequest = null;
                    sandbox.FakeOrgService.Stub(f => f.Execute(Arg <OrganizationRequest> .Matches(new OrganizationRequestMessageConstraint <DeleteRequest>())))
                    .WhenCalled(x =>
                    {
                        var request           = ((DeleteRequest)x.Arguments[0]);
                        capturedDeleteRequest = request;
                    }).Return(deleteResponse);


                    var updateCommand = commandBuilder.GetUpdateCommand();
                    Console.WriteLine(updateCommand.CommandText);

                    // ACT
                    subject.Update(ds);

                    // ASSERT
                    // A create request for the new row data should have been captured.
                    // An update request for the modified row data should have been captured.
                    // A delete request for the deleted row should have been captured.
                    Assert.NotNull(capturedCreateRequest);
                    Assert.NotNull(capturedUpdateRequest);
                    Assert.NotNull(capturedDeleteRequest);

                    var forCreate = capturedCreateRequest.Target;
                    Assert.AreEqual(forCreate.Id, newContact.Id);
                    Assert.AreEqual(forCreate["firstname"], newContact["firstname"]);
                    Assert.AreEqual(forCreate["lastname"], newContact["lastname"]);

                    var forUpdate = capturedUpdateRequest.Target;
                    Assert.AreEqual(forUpdate.Id, modifiedRow[contactidcol]);
                    Assert.AreEqual(forUpdate["firstname"], updatedFirstName);
                    Assert.AreEqual(forUpdate["lastname"], updatedLastName);

                    var forDelete = capturedDeleteRequest.Target;
                    Assert.AreEqual(forDelete.Id, deleteContactId);
                }
            }
        }