using DTS.Common.Classes.TestSetups; using DTS.Common.Interface.RegionOfInterest; using NUnit.Framework; using System.Linq; using System; using System.Collections.Generic; using DTS.Common.Interface.TestSetups.TestSetupsList; using DTS.Common.Storage; namespace DatabaseUnitTesting { [TestFixture] public partial class DBAPITests { public ITestSetupRecord CreateFakeTestSetupWithROIs(int numberOfROIs) { var record = new TestSetupRecord(); record.Id = -1; record.Name = Guid.NewGuid().ToString().Substring(0, 15); record.LastModified = DateTime.Today; record.LastModifiedBy = "NUNIT"; record.TestEngineerDetails = Guid.NewGuid().ToString().Substring(0, 15); record.CustomerDetails = Guid.NewGuid().ToString().Substring(0, 15); record.LabDetails = Guid.NewGuid().ToString().Substring(0, 15); record.Settings = ""; var regionsOfInterestList = new System.ComponentModel.BindingList(); var start = 0D; var end = 0D; for (int i = 0; i < numberOfROIs; i++) { var channelNameList = new List(); switch (i) { case 0: start = -1D; end = 1D; channelNameList.Add("an1"); channelNameList.Add("an2"); channelNameList.Add("an3"); break; case 1: start = -0.9D; end = 0.9D; channelNameList.Add("an1"); channelNameList.Add("an2"); break; } var regionOfInterest = CreateRegionOfInterest($"_ROI Period {i + 1}", start, end, true, true); regionOfInterest.ChannelNames = channelNameList.ToArray(); regionsOfInterestList.Add(regionOfInterest); } record.RegionsOfInterest = regionsOfInterestList; return record; } public IRegionOfInterest CreateRegionOfInterest(string suffix, double start, double end, bool isEnabled, bool isDefault) { var record = new RegionOfInterest(); record.Suffix = suffix; record.Start = start; record.End = end; record.IsEnabled = isEnabled; record.IsDefault = isDefault; return record; } /// /// Tests included: /// sp_TestSetupsUpdateInsert_92 (and sp_TestSetupsInsert_92) /// sp_TestSetupROIsInsert /// sp_TestSetupROIsGet /// sp_TestSetupsDeleteManyById /// sp_TestSetupsGet_92 /// [Test] public void TestSetupROIsGetOneROI() { if (!_setup) { Setup(); } TestLogin(); var connections = DbAPI.DbAPI.Connections.GetActiveConnections(); //***Testing*** sp_TestSetupsUpdateInsert_92 var testSetupRecordToInsert = CreateFakeTestSetupWithROIs(1); var hr = DbAPI.DbAPI.TestSetups.TestSetupsUpdateInsert(_user, connections[0], 92, ref testSetupRecordToInsert); Assert.IsTrue(0 == hr, "Should be able to insert a test setup"); DbAPI.DbAPI.GetDatabaseVersion(connections[0], out int serverDbVersion); if (serverDbVersion <= 91) { var regionOfInterest = testSetupRecordToInsert.RegionsOfInterest[0]; hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsInsert(_user, connections[0], testSetupRecordToInsert.Id, regionOfInterest, out var testSetupROIId); Assert.IsTrue(0 != hr, "RegionsOfInterestInsert should not work if database Version is 91 or less"); } else if (serverDbVersion >= 92) { //***Testing*** sp_TestSetupROIsInsert var regionOfInterest = CreateRegionOfInterest("", -1, 1, true, true); var testSetupROIId = -1; hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsInsert(_user, connections[0], testSetupRecordToInsert.Id, regionOfInterest, out testSetupROIId); Assert.IsTrue(0 == hr, "Should be able to insert a record in the TestSetupROIs table"); Assert.IsTrue(testSetupROIId > 0, "A TestSetupROIId should have been generated and returned"); //***Testing*** sp_TestSetupROIsGet var testSetupROIRecords = new DTS.Common.Interface.TestSetups.ITestSetupROIRecord[0]; hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsGet(_user, connections[0], testSetupRecordToInsert.Id, out testSetupROIRecords); Assert.IsTrue(0 == hr, "Should be able to get a record from the TestSetupROIs table"); Assert.IsTrue(testSetupROIRecords.Length == 1, "Should get one and only one record from the TestSetupROIs table"); Assert.IsTrue(testSetupROIRecords[0].Suffix == "", "Suffix in record from the TestSetupROIs table should be empty"); Assert.IsTrue(testSetupROIRecords[0].ROIStart == -1, "ROIStart in record from the TestSetupROIs table should be -1"); Assert.IsTrue(testSetupROIRecords[0].ROIEnd == 1, "ROIEnd in record from the TestSetupROIs table should be 1"); Assert.IsTrue(testSetupROIRecords[0].IsEnabled, "IsEnabled in record from the TestSetupROIs table should be 'true'"); Assert.IsTrue(testSetupROIRecords[0].IsDefault, "IsDefault in record from the TestSetupROIs table should be 'true'"); //***Testing*** sp_ROIPeriodChannelsGet var roiPeriodChannelRecords = new DTS.Common.Interface.TestSetups.IROIPeriodChannelRecord[0]; hr = DbAPI.DbAPI.RegionsOfInterest.ROIPeriodChannelsGet(_user, connections[0], DbOperations.CURRENT_DB_VERSION, testSetupROIRecords[0].TestSetupROIId, out roiPeriodChannelRecords); Assert.IsTrue(0 == hr, "Should not fail when getting zero records from the ROIPeriodChannels table based on the TestSetupROIId in the TestSetupROIs table"); Assert.IsTrue(roiPeriodChannelRecords.Length == 0, "Should get zero records from the ROIPeriodChannels table"); } //***Testing*** sp_TestSetupsDeleteManyById //Ensure that deleting a Test Setup will delete related record(s) in the TestSetupROIs and ROIPeriodChannels tables hr = DbAPI.DbAPI.TestSetups.TestSetupsDeleteById(_user, connections[0], new int[] { testSetupRecordToInsert.Id }); //Since hr will be 0 in this case, check by doing a Get //***Testing*** sp_TestSetupsGet_92 //Make sure the Test Setup was deleted hr = DbAPI.DbAPI.TestSetups.TestSetupsGet(_user, connections[0], 92, null, null, double.NaN, double.NaN, false, false, out var records, out var errors); Assert.IsTrue(null == records || records.Length == 0, "TestSetupsGet should not have retrieved records"); Assert.IsFalse(records.Any(r => r.Name == testSetupRecordToInsert.Name), "TestSetupsGet should not have retrieved our record"); if (serverDbVersion <= 91) { var regionOfInterest = testSetupRecordToInsert.RegionsOfInterest[0]; hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsInsert(_user, connections[0], testSetupRecordToInsert.Id, regionOfInterest, out var testSetupROIId); Assert.IsTrue(0 != hr, "RegionsOfInterestInsert should not work if database Version is 91 or less"); } else if (serverDbVersion >= 92) { //Make sure the record in the TestSetupROIs table was deleted hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsGet(_user, connections[0], testSetupRecordToInsert.Id, out var testSetupROIRecords); Assert.IsTrue(0 == hr && testSetupROIRecords.Length == 0, "Record in TestSetupROIs table should have been deleted"); //Make sure the record in the ROIPeriodChannels table was deleted hr = DbAPI.DbAPI.RegionsOfInterest.ROIPeriodChannelsGet(_user, connections[0], DbOperations.CURRENT_DB_VERSION, testSetupRecordToInsert.Id, out var roiPeriodChannelRecords); Assert.IsTrue(0 == hr && testSetupROIRecords.Length == 0, "Record in ROIPeriodChannels table should have been deleted"); //test that it fails when no user provided hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsGet(null, connections[0], testSetupRecordToInsert.Id, out testSetupROIRecords); Assert.IsTrue(0 != hr, "Should not return 0 when user is invalid"); //test that it fails when no connection provided hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsGet(_user, null, testSetupRecordToInsert.Id, out testSetupROIRecords); Assert.IsTrue(0 != hr, "Should not return 0 when connection is invalid"); } } /// /// Tests included: /// sp_TestSetupsUpdateInsert_92 (and sp_TestSetupsInsert_92) /// sp_TestSetupROIsInsert /// sp_TestSetupROIsGet /// sp_TestSetupsDeleteManyById /// sp_TestSetupsGet_92 /// [Test] public void TestSetupROIsGetMultipleROIs() { if (!_setup) { Setup(); } TestLogin(); var connections = DbAPI.DbAPI.Connections.GetActiveConnections(); //***Testing*** sp_TestSetupsUpdateInsert_92 var testSetupRecordToInsert = CreateFakeTestSetupWithROIs(2); var hr = DbAPI.DbAPI.TestSetups.TestSetupsUpdateInsert(_user, connections[0], 92, ref testSetupRecordToInsert); Assert.IsTrue(0 == hr, "Should be able to insert a test setup"); DbAPI.DbAPI.GetDatabaseVersion(connections[0], out int serverDbVersion); if (serverDbVersion <= 91) { var regionOfInterest = testSetupRecordToInsert.RegionsOfInterest[0]; hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsInsert(_user, connections[0], testSetupRecordToInsert.Id, regionOfInterest, out var testSetupROIId); Assert.IsTrue(0 != hr, "RegionsOfInterestInsert should not work if database Version is 91 or less"); } else if (serverDbVersion >= 92) { //***Testing*** sp_TestSetupROIsInsert var regionOfInterest = testSetupRecordToInsert.RegionsOfInterest[0]; var testSetupROIId = -1; hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsInsert(_user, connections[0], testSetupRecordToInsert.Id, regionOfInterest, out testSetupROIId); Assert.IsTrue(0 == hr, "Should be able to insert the first ROI record in the TestSetupROIs table"); Assert.IsTrue(testSetupROIId > 0, "A TestSetupROIId should have been generated and returned (1)"); hr = DbAPI.DbAPI.RegionsOfInterest.ROIPeriodChannelsInsert(_user, connections[0], DbOperations.CURRENT_DB_VERSION, testSetupROIId, regionOfInterest.ChannelNames[0], regionOfInterest.ChannelIds[0]); Assert.IsTrue(0 == hr, "Should be able to insert the first record in the ROIPeriodChannels table"); hr = DbAPI.DbAPI.RegionsOfInterest.ROIPeriodChannelsInsert(_user, connections[0], DbOperations.CURRENT_DB_VERSION, testSetupROIId, regionOfInterest.ChannelNames[1], regionOfInterest.ChannelIds[1]); Assert.IsTrue(0 == hr, "Should be able to insert the second record in the ROIPeriodChannels table"); hr = DbAPI.DbAPI.RegionsOfInterest.ROIPeriodChannelsInsert(_user, connections[0], DbOperations.CURRENT_DB_VERSION, testSetupROIId, regionOfInterest.ChannelNames[2], regionOfInterest.ChannelIds[2]); Assert.IsTrue(0 == hr, "Should be able to insert the third record in the ROIPeriodChannels table"); //***Testing*** sp_TestSetupROIsInsert regionOfInterest = testSetupRecordToInsert.RegionsOfInterest.Last(); testSetupROIId = -1; hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsInsert(_user, connections[0], testSetupRecordToInsert.Id, regionOfInterest, out testSetupROIId); Assert.IsTrue(0 == hr, "Should be able to insert the last ROI record in the TestSetupROIs table"); Assert.IsTrue(testSetupROIId > 0, "A TestSetupROIId should have been generated and returned (2)"); hr = DbAPI.DbAPI.RegionsOfInterest.ROIPeriodChannelsInsert(_user, connections[0], DbOperations.CURRENT_DB_VERSION, testSetupROIId, regionOfInterest.ChannelNames[0], regionOfInterest.ChannelIds[0]); Assert.IsTrue(0 == hr, "Should be able to insert the fourth record in the ROIPeriodChannels table"); hr = DbAPI.DbAPI.RegionsOfInterest.ROIPeriodChannelsInsert(_user, connections[0], DbOperations.CURRENT_DB_VERSION, testSetupROIId, regionOfInterest.ChannelNames[1], regionOfInterest.ChannelIds[1]); Assert.IsTrue(0 == hr, "Should be able to insert the fifth record in the ROIPeriodChannels table"); //***Testing*** sp_TestSetupROIsGet var testSetupROIRecords = new DTS.Common.Interface.TestSetups.ITestSetupROIRecord[0]; hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsGet(_user, connections[0], testSetupRecordToInsert.Id, out testSetupROIRecords); Assert.IsTrue(0 == hr, "Should be able to get a record from the TestSetupROIs table"); Assert.IsTrue(testSetupROIRecords.Length == 2, "Should get two and only two records from the TestSetupROIs table"); Assert.IsTrue(testSetupROIRecords[0].Suffix == "_ROI Period 1", "Suffix in first record from the TestSetupROIs table should be empty"); Assert.IsTrue(testSetupROIRecords[0].ROIStart == -1, "ROIStart in first record from the TestSetupROIs table should be -1"); Assert.IsTrue(testSetupROIRecords[0].ROIEnd == 1, "ROIEnd in first record from the TestSetupROIs table should be 1"); Assert.IsTrue(testSetupROIRecords[0].IsEnabled, "IsEnabled in first record from the TestSetupROIs table should be 'true'"); Assert.IsTrue(testSetupROIRecords[0].IsDefault, "IsDefault in first record from the TestSetupROIs table should be 'true'"); Assert.IsTrue(testSetupROIRecords[1].Suffix == "_ROI Period 2", "Suffix in second record from the TestSetupROIs table should be empty"); Assert.IsTrue(testSetupROIRecords[1].ROIStart == -0.9D, "ROIStart in second record from the TestSetupROIs table should be -1"); Assert.IsTrue(testSetupROIRecords[1].ROIEnd == 0.9D, "ROIEnd in second record from the TestSetupROIs table should be 1"); Assert.IsTrue(testSetupROIRecords[1].IsEnabled, "IsEnabled in second record from the TestSetupROIs table should be 'true'"); Assert.IsTrue(testSetupROIRecords[1].IsDefault, "IsDefault in second record from the TestSetupROIs table should be 'true'"); //***Testing*** sp_ROIPeriodChannelsGet var roiPeriodChannelRecords = new DTS.Common.Interface.TestSetups.IROIPeriodChannelRecord[0]; hr = DbAPI.DbAPI.RegionsOfInterest.ROIPeriodChannelsGet(_user, connections[0], DbOperations.CURRENT_DB_VERSION, testSetupROIRecords[0].TestSetupROIId, out roiPeriodChannelRecords); Assert.IsTrue(0 == hr, "Should be able to get three records from the ROIPeriodChannels table based on the TestSetupROIId in the TestSetupROIs table"); Assert.IsTrue(roiPeriodChannelRecords.Length == 3, "Should get three and only three records from the ROIPeriodChannels table"); Assert.IsTrue(roiPeriodChannelRecords[0].ChannelName == "an1", "ChannelName[0] in record 0 should be 'an1'"); Assert.IsTrue(roiPeriodChannelRecords[1].ChannelName == "an2", "ChannelName[1] in record 0 should be 'an2'"); Assert.IsTrue(roiPeriodChannelRecords[2].ChannelName == "an3", "ChannelName[2] in record 0 should be 'an3'"); hr = DbAPI.DbAPI.RegionsOfInterest.ROIPeriodChannelsGet(_user, connections[0], DbOperations.CURRENT_DB_VERSION, testSetupROIRecords[1].TestSetupROIId, out roiPeriodChannelRecords); Assert.IsTrue(0 == hr, "Should be able to get two records from the ROIPeriodChannels table based on the TestSetupROIId in the TestSetupROIs table"); Assert.IsTrue(roiPeriodChannelRecords.Length == 2, "Should get two and only two records from the ROIPeriodChannels table"); Assert.IsTrue(roiPeriodChannelRecords[0].ChannelName == "an1", "ChannelName[0] in record 1 should be 'an1'"); Assert.IsTrue(roiPeriodChannelRecords[1].ChannelName == "an2", "ChannelName[1] in record 1 should be 'an2'"); } //***Testing*** sp_TestSetupsDeleteManyById //Ensure that deleting a Test Setup will delete related record(s) in the TestSetupROIs table hr = DbAPI.DbAPI.TestSetups.TestSetupsDeleteById(_user, connections[0], new int[] { testSetupRecordToInsert.Id }); //Since hr will be 0 in this case, check by doing a Get //Assert.IsTrue(0 != hr, $"Test Setup should not be able to be deleted before all related records in the TestSetupROIs table. hr={hr}"); //***Testing*** sp_TestSetupsGet_92 //Make sure the Test Setup was deleted hr = DbAPI.DbAPI.TestSetups.TestSetupsGet(_user, connections[0], 92, null, null, double.NaN, double.NaN, false, false, out var records, out var errors); Assert.IsTrue(null == records || records.Length == 0, "TestSetupsGet should not have retrieved records"); Assert.IsFalse(records.Any(r => r.Name == testSetupRecordToInsert.Name), "TestSetupsGet should not have retrieved our record"); if (serverDbVersion >= 92) { //Make sure that both records in the TestSetupROIs table were deleted hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsGet(_user, connections[0], testSetupRecordToInsert.Id, out var testSetupROIRecords); Assert.IsTrue(0 == hr && testSetupROIRecords.Length == 0, "Record in TestSetupROIs table should have been deleted"); //Make sure the record in the ROIPeriodChannels table was deleted hr = DbAPI.DbAPI.RegionsOfInterest.ROIPeriodChannelsGet(_user, connections[0], DbOperations.CURRENT_DB_VERSION, testSetupRecordToInsert.Id, out var roiPeriodChannelRecords); Assert.IsTrue(0 == hr && testSetupROIRecords.Length == 0, "Record in ROIPeriodChannels table should have been deleted"); //test that it fails when no user provided hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsGet(null, connections[0], testSetupRecordToInsert.Id, out testSetupROIRecords); Assert.IsTrue(0 != hr, "Should not return 0 when user is invalid"); //test that it fails when no connection provided hr = DbAPI.DbAPI.RegionsOfInterest.TestSetupROIsGet(_user, null, testSetupRecordToInsert.Id, out testSetupROIRecords); Assert.IsTrue(0 != hr, "Should not return 0 when connection is invalid"); } } } }