public void DataService_AddHeirarchicalTerm_Adds_First_Record_On_Valid_Term()
        {
            //Arrange
            int rowCount = DataUtil.GetRecordCount(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName);
            DataUtil.AddDatabaseObject(virtualScriptFilePath, addHeirarchicalTerm);

            Term term = new Term(4)
                            {
                                Name = "Test Term",
                                ParentTermId = null /*No Parent*/,
                                Weight = 0,
                                Description = "Test Term"
                            };

            DataService ds = new DataService();

            //Act
            int newTermId = ds.AddHeirarchicalTerm(term, Constants.USER_ValidId);

            //Assert
            DatabaseAssert.RecordCountIsEqual(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName,
                                              rowCount + 1);
            DatabaseAssert.RecordLastAddedIdEquals(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName,
                                                   "TermID", newTermId);

            //Assert that new term is correct as first record in Vocabulary
            TestTerm(newTermId, 1, 2);
        }
        public void DataService_AddHeirarchicalTerm_Does_Not_Modify_Terms_From_Other_Vocabularies()
        {
            //Arrange
            int rowCount = DataUtil.GetRecordCount(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName);
            DataUtil.AddDatabaseObject(virtualScriptFilePath, addHeirarchicalTerm);

            Term term = new Term(4)
                            {
                                Name = "Test Term",
                                ParentTermId = null /*No Parent*/,
                                Weight = 0,
                                Description = "Test Term"
                            };

            DataService ds = new DataService();

            //Act
            int newTermId = ds.AddHeirarchicalTerm(term, Constants.USER_ValidId);

            //Assert that electronics terms are untouched
            //Televisions, Id = 1, Left=12, Right=17
            TestTerm(1, 12, 17);
            //Flash, Id = 9, Left=8, Right=9
            TestTerm(9, 8, 9);
            //Electronics, Id = 12, Left=1, Right=18
            TestTerm(12, 1, 18);
        }
        public void DataService_AddHeirarchicalTerm_Inserts_Record_In_Alpha_Order_On_Valid_Term()
        {
            //Arrange
            int rowCount = DataUtil.GetRecordCount(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName);
            DataUtil.AddDatabaseObject(virtualScriptFilePath, addHeirarchicalTerm);

            Term term = new Term(2)
                            {
                                Name = "Plasma",
                                ParentTermId = 1 /*Televisions*/,
                                Weight = 0,
                                Description = "Plasma"
                            };

            DataService ds = new DataService();

            //Act
            int newTermId = ds.AddHeirarchicalTerm(term, Constants.USER_ValidId);

            //Assert
            DatabaseAssert.RecordCountIsEqual(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName,
                                              rowCount + 1);
            DatabaseAssert.RecordLastAddedIdEquals(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName,
                                                   "TermID", newTermId);

            //Assert that new term is correct as middle child of televisions
            TestTerm(newTermId, 15, 16);

            //Assert that existing terms are correct
            //Televisions, Id = 1, Left=12, Right=17 - should be Left=12, Right=19
            TestTerm(1, 12, 19);
            //Flash, Id = 9, Left=8, Right=9 - should be Left=8, Right=9
            TestTerm(9, 8, 9);
            //Electronics, Id = 12, Left=1, Right=18 - should be Left=1, Right=20
            TestTerm(12, 1, 20);
        }
        public void DataService_AddHeirarchicalTerm_Throws_On_InValid_VocabularyId()
        {
            //Arrange
            int rowCount = DataUtil.GetRecordCount(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName);
            DataUtil.AddDatabaseObject(virtualScriptFilePath, addHeirarchicalTerm);

            Term term = ContentTestHelper.CreateValidHeirarchicalTerm(Constants.VOCABULARY_InValidVocabularyId,
                                                                      Constants.TERM_ValidParentTermId);

            DataService ds = new DataService();

            //Act/Assert
            Assert.Throws<SqlException>(() => ds.AddHeirarchicalTerm(term, Constants.USER_ValidId));
        }
        public void DataService_AddHeirarchicalTerm_Inserts_Record_As_First_Child_On_Valid_Term()
        {
            //Arrange
            int rowCount = DataUtil.GetRecordCount(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName);
            DataUtil.AddDatabaseObject(virtualScriptFilePath, addHeirarchicalTerm);

            Term term = new Term(2)
                            {
                                Name = "Game Console",
                                ParentTermId = 12 /*Electronics*/,
                                Weight = 0,
                                Description = "Game Consoles like X-Box 360"
                            };

            DataService ds = new DataService();

            //Act
            int newTermId = ds.AddHeirarchicalTerm(term, Constants.USER_ValidId);

            //Assert
            DatabaseAssert.RecordCountIsEqual(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName,
                                              rowCount + 1);
            DatabaseAssert.RecordLastAddedIdEquals(DataTestHelper.ConnectionString, ContentDataTestHelper.TermsTableName,
                                                   "TermID", newTermId);

            //Assert that new term is correct as first child of electronics
            TestTerm(newTermId, 2, 3);

            //Assert that existing terms are correct
            //Televisions, Id = 1, Left=12, Right=17 - should be Left=14, Right=19
            TestTerm(1, 14, 19);
            //Flash, Id = 9, Left=8, Right=9 - should be Left=10, Right=11
            TestTerm(9, 10, 11);
            //Electronics, Id = 12, Left=1, Right=18 - should be Left=1, Right=20
            TestTerm(12, 1, 20);
        }