private void ErrorOnInsert <TEntity, TEntityList>()
            where TEntity : EntityBase, INamedEntity, new()
            where TEntityList : EntityListBase <TEntity, NamedBindingItem <TEntity> >, new()
        {
            const string name = "Performance";
            var          list = new TEntityList {
                Session = Session
            };

            list.Populate(); // Creates an empty BindingList
            var bindingList = list.BindingList;
            var item1       = bindingList.AddNew();

            list.OnRowEnter(0);
            item1.Name = name;
            list.OnRowValidated(0);
            var item2 = bindingList.AddNew();

            item2.Name = name;
            var exception = Assert.Catch <DatabaseUpdateErrorException>(
                () => list.OnRowValidated(1),
                "Duplicate name should have thrown DatabaseUpdateErrorException.");

            Assert.AreEqual("Another EventType with key 'Performance' already exists.",
                            exception.Message, "Message");
            Assert.AreEqual(StatementType.Insert, exception.ChangeAction, "ChangeAction");
            Assert.AreEqual(1, exception.RowIndex, "RowIndex");
            Assert.AreEqual(0, exception.ColumnIndex, "ColumnIndex");
            Assert.IsInstanceOf(typeof(DuplicateNameException), exception.InnerException,
                                "InnerException");
            Assert.AreSame(exception, list.LastDatabaseUpdateErrorException,
                           "LastDatabaseUpdateErrorException");
        }
        private void Edit <TEntity, TEntityList>()
            where TEntity : EntityBase, INamedEntity, new()
            where TEntityList : EntityListBase <TEntity, NamedBindingItem <TEntity> >, new()
        {
            const string name1 = "Performance";
            const string name2 = "Interview";
            const string name3 = "Rehearsal";
            var          list  = new TEntityList {
                Session = Session
            };

            list.Populate(); // Creates an empty BindingList
            var bindingList = list.BindingList;
            var item1       = bindingList.AddNew();

            list.OnRowEnter(0);
            item1.Name = name1;
            list.OnRowValidated(0);
            Assert.AreEqual(1, list.Count, "Entity count after 1st add");
            var entity1 = (INamedEntity)list[0];

            Assert.AreEqual(name1, entity1.Name, "1st entity Name after 1st add");
            var item2 = bindingList.AddNew();

            item2.Name = name2;
            list.OnRowValidated(1);
            Assert.AreEqual(2, list.Count, "Entity count after 2nd add");
            var entity2 = (INamedEntity)list[1];

            Assert.AreEqual(name2, entity2.Name, "2nd entity Name after 2nd add");
            // Refresh the grid from the saved entities on the database
            list.Populate();
            bindingList = list.BindingList;
            Assert.AreEqual(2, bindingList.Count, "editor.Count after Populate");
            // After being refreshed by Populate, the table should now be sorted into Name order.
            Assert.AreEqual(name2, bindingList[0].Name, "1st item Name after populate");
            Assert.AreEqual(name1, bindingList[1].Name, "2nd item Name after populate");
            list.OnRowValidated(0); // Should have no effect
            Assert.AreEqual(2, bindingList.Count, "editor.Count going to existing row");
            // Rename the first item
            bindingList[0].Name = name3;
            entity1             = list[0];
            Assert.AreEqual(name3, entity1.Name, "1st entity Name after update");
            list.DeleteEntity(0); // And delete it
            list.Populate();      // And refresh the grid from the database again.
            bindingList = list.BindingList;
            Assert.AreEqual(1, list.Count, "Entity count after delete and repopulate");
            entity1 = list[0];
            Assert.AreEqual(name1, entity1.Name, "1st entity Name after delete and repopulate");
            Assert.AreEqual(1, bindingList.Count, "editor.Count after delete and repopulate");
            Assert.AreEqual(name1, bindingList[0].Name,
                            "1st item Name after delete and repopulate");
        }
        private void ErrorOnDelete <TEntityList>()
            where TEntityList : IEntityList, new()
        {
            var list = new TEntityList {
                Session = Session
            };

            list.Populate();
            list.OnRowEnter(1);
            var exception = Assert.Catch <DatabaseUpdateErrorException>(
                () => list.DeleteEntity(1),
                "DeleteEntity should have thrown DatabaseUpdateErrorException.");

            Assert.AreEqual(StatementType.Delete, exception.ChangeAction, "ChangeAction");
            Assert.IsTrue(
                exception.Message.Contains("cannot be deleted because it is referenced by"),
                "Message");
            Assert.AreEqual(1, exception.RowIndex, "RowIndex");
            Assert.IsInstanceOf(typeof(ConstraintException), exception.InnerException,
                                "InnerException");
            Assert.AreSame(exception, list.LastDatabaseUpdateErrorException,
                           "LastDatabaseUpdateErrorException");
        }