public void Creates_A_New_Database_If_No_Database_Has_Been_Previously_Loaded()
        {
            IDatabaseForm form  = MockRepository.GenerateMock <IDatabaseForm>();
            IMainPanel    panel = MockRepository.GenerateMock <IMainPanel>();

            DatabasePresenter presenter = new DatabasePresenter(panel, form);

            AutoResetEvent arEvent = new AutoResetEvent(false);

            presenter.NewDatabaseCreated += (sender, e) => arEvent.Set();

            Assert.That(presenter.Database, Is.Null);

            form.Stub(t => t.SelectedDatabaseType).Return(DatabaseTypes.SQLCE);
            form.Stub(t => t.SelectedDatabase).Return("1Table3Columns.sdf");

            var raiser = form.GetEventRaiser(t => t.RefreshSchema += null);

            raiser.Raise(form, null);

            Assert.That(arEvent.WaitOne(10000, true), "The test timed out waiting for the NewDatabaseCreated event to be triggered");

            Assert.That(presenter.Database, Is.Not.Null);
            Assert.That(presenter.Database.Name, Is.EqualTo("1Table3Columns"));
        }
        public void The_Presenter_Refreshes_The_Database_And_Attempts_To_Show_The_Results()
        {
            IDatabaseForm form  = MockRepository.GenerateMock <IDatabaseForm>();
            IMainPanel    panel = MockRepository.GenerateMock <IMainPanel>();
            IDatabase     db    = MockRepository.GenerateStub <IDatabase>();

            db.Stub(d => d.Tables).Return(new ReadOnlyCollection <ITable>(new List <ITable>()));

            var presenter = new DatabasePresenter(panel, form);

            presenter.AttachToModel(db);

            AutoResetEvent arEvent = new AutoResetEvent(false);

            presenter.SchemaRefreshed += (sender, e) => arEvent.Set();

            form.Stub(t => t.SelectedDatabaseType).Return(DatabaseTypes.SQLCE);
            form.Stub(t => t.SelectedDatabase).Return("1Table3Columns.sdf");

            var raiser = form.GetEventRaiser(t => t.RefreshSchema += null);

            raiser.Raise(form, null);

            Assert.That(arEvent.WaitOne(10000, true), "The test timed out waiting for the SchemaRefreshed event to be triggered");

            panel.AssertWasCalled(p => p.ShowDatabaseRefreshResults(
                                      Arg <DatabaseMergeResult> .Matches(t => t.TableOperations.Count() == 1)));
        }
        public void The_Presenter_Tests_The_Connection_Unsuccessfully()
        {
            IDatabaseForm form  = MockRepository.GenerateMock <IDatabaseForm>();
            IMainPanel    panel = MockRepository.GenerateMock <IMainPanel>();

            form.Expect(f => f.SetDatabaseOperationResults(Arg <DatabaseOperationResults> .Matches(pc => pc.Succeeded == false)));

            new DatabasePresenter(panel, form);

            form.Stub(t => t.SelectedDatabaseType).Return(DatabaseTypes.SQLCE);
            form.Stub(t => t.SelectedDatabase).Return("1Table3Columnsaaa.sdf");

            form.GetEventRaiser(t => t.TestConnection += null).Raise(form, null);

            form.VerifyAllExpectations();
        }
        public void A_SQLCEDatabaseLoader_Is_Returned()
        {
            IDatabaseForm form  = MockRepository.GenerateMock <IDatabaseForm>();
            IMainPanel    panel = MockRepository.GenerateMock <IMainPanel>();

            DatabasePresenter presenter = new DatabasePresenter(panel, form);

            form.Stub(t => t.SelectedDatabaseType).Return(DatabaseTypes.SQLCE);
            form.Stub(t => t.SelectedDatabase).Return("1Table3Columns.sdf");

            IDatabaseLoader loader = presenter.CreateDatabaseLoader();

            Assert.That(loader, Is.Not.Null);
            Assert.That(loader, Is.TypeOf(typeof(SQLCEDatabaseLoader)));

            // Will throw an error if the database connection could not be established.
            loader.TestConnection();

            IDatabase db = loader.LoadDatabase();

            Assert.That(db.Name, Is.EqualTo("1Table3Columns"));
            Assert.That(db.Tables, Has.Count(1));             // Basic check to see if we got the correct database back.
        }
        public void A_SQLServer2005DatabaseLoader_Is_Returned()
        {
            IDatabaseForm form  = MockRepository.GenerateMock <IDatabaseForm>();
            IMainPanel    panel = MockRepository.GenerateMock <IMainPanel>();

            DatabasePresenter presenter = new DatabasePresenter(panel, form);

            form.Stub(t => t.SelectedDatabaseType).Return(DatabaseTypes.SQLServer2005);
            form.Stub(t => t.ConnectionStringHelper).Return(new ConnectionStringHelper
            {
                CurrentDbType         = DatabaseTypes.SQLServer2005,
                DatabaseName          = "TestDatabase",
                Password              = "******",
                ServerName            = ".",
                UseFileName           = false,
                UseIntegratedSecurity = false,
                UserName              = "******"
            });

            IDatabaseLoader loader = presenter.CreateDatabaseLoader();

            Assert.That(loader, Is.Not.Null);
            Assert.That(loader, Is.TypeOf(typeof(SQLServer2005DatabaseLoader)));
        }