11 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
2026-04-16T03:51:39.334243+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | f699b3e38a2dc576 |
Database Unit Testing Module Documentation
1. Purpose
This module provides a structured framework for database unit testing within the DataPRO system, using NUnit to validate database interactions via stored procedures and the DbAPI interface. It ensures data integrity and correct behavior of database operations (insert, update, delete, get) for core entities—such as DAS, DAS channels, laboratory details, customer details, calculated channels, and regions of interest—by leveraging database snapshots and transactional rollbacks to maintain test isolation and repeatability. The module supports two distinct testing approaches: low-level stored procedure testing (via ResultSetTester and DatabaseModificationTester) and high-level API testing (via DbAPI methods), enabling comprehensive coverage of database functionality.
2. Public Interface
TestSetups (Base Class for Test Fixtures)
[OneTimeSetUp] TFSetup()
Initializes aSqlConnection, opens it, and instantiatesDatabaseModificationTester, which creates a database snapshot for test isolation.[OneTimeTearDown] TFTearDown()
DisposesDatabaseModificationTester(dropping the snapshot) and closes the connection; does not delete test data.[SetUp] SetUp()
Begins a new transaction viaDatabaseModificationTester.BeginTestTransaction(), sets up aSqlCommandbound to the transaction.[TearDown] TearDown()
Rolls back the current test transaction viaDatabaseModificationTester.EndTestTransaction().- Properties
TestResultPath: Returns path to test results directory (..\..\TestResults\relative to app base directory).UnitTests: Exposes theDatabaseModificationTesterinstance.BSETUPMODE: Alwaysfalse(hardcoded constant).Command: Returns theSqlCommandbound to the current transaction.
ResultSetTester
ResultSetTester(SqlConnection connection, string procedureName)
Constructs aSqlCommandfor the given stored procedure name, withCommandType.StoredProcedure.ResultSetTester(SqlCommand command)
Wraps an existingSqlCommand.SetInputParameter(string parameterName, object parameterValue)
Adds or replaces an input parameter on the command.SetOutputParameter(string parameterName, SqlDbType type, int size)
Adds or replaces an output parameter on the command, setting its direction toOutput.PrintOuputParameterValues()
Returns anIEnumerable<KeyValuePair<string, object>>of all output parameter names and values.CompareToFile(string filename)
Parses the stored procedure result set usingResultSetParser, reads expected results from XML file viaXmlFileAdapter, and returnstrueif equal.OutputToFile(string filename)
Parses the stored procedure result set and writes it to an XML file viaXmlFileAdapter.
DatabaseModificationTester
DatabaseModificationTester(SqlConnection connection, string databaseName, string snapshotName)
Initializes a snapshot-based testing environment; creates a database snapshot on construction.BeginTestTransaction()
Begins a new transaction on the connection; throws if a transaction already exists (rolls back first).EndTestTransaction()
Rolls back and disposes the current transaction; callsCleanUp()internally.WriteDiffsToXml(string filename)
Generates differences between current transaction and the snapshot, writes to XML file.CompareDiffsToXml(string filename)
Compares current transaction differences to expected XML file; returnstrueif equal.AreEqual()
Returnstrueif no differences exist between current transaction and snapshot.AddColumnToIgnore(string schemaName, string objectName, string columnName)
Adds a single column to the ignore list for comparisons.AddColumnsToIgnore(string schema1, string name1, List<string> columnNames)
Adds multiple columns to the ignore list for a given table.AddObjectComparison(string schemaName, string tableName)
Registers a table for comparison (likely default behavior).Dispose()
Drops the database snapshot if active.
DbAPI Tests (Partial Classes)
The DbAPITests partial class and its companions (DbAPITestsRegionsOfInterest, DbAPITestsChannels, DbAPITestsSensorsAnalog, DbAPITestsGroupHardware, DbAPITestsCustomerDetails) contain integration tests for the DbAPI layer. Key methods include:
TestConnect()/TestLogin()
Verify database connectivity and user authentication.RegionsOfInterest()
Tests versioned ROI functionality (sp_TestSetupsUpdateInsert_92,sp_TestSetupROIsInsert,sp_ROIPeriodChannelsInsert, etc.), including graceful handling of pre-Version 92 databases.TestChannelsInsert()/TestChannelsUpdate()/TestChannelsGet()/TestChannelsDelete()
Validate channel CRUD operations viaDbAPI.Channels.*methods.TestSensorsDeleteAll()/TestSensorsAnalogInsertAndDelete()/TestSensorAnalogInsertAndDeleteShouldFail()/TestSensorsAnalogBridgeResistanceGet()
Validate analog sensor operations, including error handling for null user/connection and special test sensors that persist after bulk delete.TestGroupHardwareInsertGetAndDelete()
Validates group-hardware associations (GroupHardwaretable) including multi-DAS/group scenarios.CustomerDetails()
Validates customer details CRUD, includingnull-name delete (delete all) and name-based lookup.
3. Invariants
- Test Isolation: Each test runs inside a transaction that is rolled back after the test (
BeginTestTransaction/EndTestTransaction), ensuring no persistent side effects. - Snapshot Consistency: A database snapshot is created once per test fixture (
[OneTimeSetUp]) and dropped at teardown ([OneTimeTearDown]), providing a known baseline for all tests in the fixture. - No Test Data Cleanup: The teardown explicitly skips deletion of test data; only the transaction is rolled back and snapshot dropped.
- Transaction Lifecycle: A transaction must exist before calling
EndTestTransaction(); otherwise,InvalidOperationExceptionis thrown. - Output Parameter Handling:
ResultSetTester.SetOutputParameter()removes any existing parameter with the same name before adding a new one. - Versioned ROI Support: ROI tests handle both pre-Version 92 (string-based) and Version 92+ (normalized tables) databases;
RegionsOfInterestInsertis expected to fail on Version ≤91.
4. Dependencies
Internal Dependencies
DatabaseUnitTesting.Utilitiesnamespace:DatabaseComparer,DatabaseAdapter,ResultSetParser,XmlFileAdapter(used byDatabaseModificationTesterandResultSetTester).
DatabaseUnitTesting.Utilities.Resultsnamespace:Database(used byResultSetParserandXmlFileAdapter).
DTS.Common.*namespaces:DTS.Common.Interface.Database,DTS.Common.Interface.TestSetups,DTS.Common.Interface.Channels,DTS.Common.Interface.Sensors,DTS.Common.Interface.Groups,DTS.Common.Interface.TestMetaData,DTS.Common.Classes.*,DTS.Common.Enums.*,DTS.Common.Storage(used byDbAPITests*classes).
Properties.Settings.Default:- Used for
ConnectionString,DBName,DBSnapshotinTestSetups.
- Used for
External Dependencies
- NUnit: Testing framework (
[TestFixture],[Test],[SetUp],[TearDown],[OneTimeSetUp],[OneTimeTearDown],Assert.*). - System.Data.SqlClient:
SqlConnection,SqlCommand,SqlTransaction,SqlDbType,ParameterDirection. - System.Data:
DataTable,IDataReader,DbDataAdapter(used viaResultSetParser). - LocalDB / SQL Server: Target database engine; connection via
LocalDb((localdb)\DataPROInstance). - External Scripts:
AttachDBs.bat,dbfolder (used byDbAPI.Connections.ConnectToDb).
Inferred Usage
- All test fixture classes (
DefaultPropertiesTests,LaboratoryDetailsTests,DASTests, etc.) inherit fromTestSetups. ResultSetTesteris used by tests to execute stored procedures and compare result sets.DatabaseModificationTesteris used by tests to assert database state changes (viaCompareDiffsToXml,AreEqual, etc.).DbAPIis used for integration tests against the high-level API layer.
5. Gotchas
BSETUPMODEis Alwaysfalse: The constant_BSETUPMODE = falseis hardcoded and never configurable; tests cannot switch to a setup mode.- Transaction Reuse Risk:
BeginTestTransaction()throws if a transaction already exists; tests must not callBeginTestTransaction()multiple times per test. - Snapshot Cleanup: The snapshot is dropped only in
DatabaseModificationTester.Dispose(), which is called in[OneTimeTearDown]. If tests are run in parallel, snapshot conflicts may occur. - Output Parameter Overwrite:
ResultSetTester.SetOutputParameter()removes existing parameters with the same name before adding a new one—this may mask bugs if parameter names are reused unintentionally. WriteDiffsToXmlGenerates Manual Verification Files: The XML files produced byWriteDiffsToXmlmust be manually reviewed and accepted as expected results; they are not auto-validated.CustomerDetails.Nameis Required: Tests and comments indicateNameis the identifying field for insert/update;nullmay cause errors.DASRequiresSerialNumber: Tests and comments indicateSerialNumberis mandatory for DAS operations.SensorsDeleteAllPreserves Test Sensors:TestSensorsDeleteAllexpects that specific test sensors (SensorConstants.TEST_SPECIFIC_ANALOG_SERIAL,SensorConstants.TEST_SPECIFIC_CLOCK_SERIAL) persist after a bulk delete.- ROI Version Handling:
RegionsOfInterestInsertis expected to fail (non-zerohr) on databases with version ≤91; tests must check version before invoking. DbAPIError Codes: Return codes (hr) are used for success/failure (0= success); tests rely on this convention rather than exceptions.- Path Resolution Complexity:
DbAPITests.Setup()includes complex logic to locateAttachDBs.batanddbfolder; behavior may vary depending on build configuration (e.g., Debug/Release, x86/x64).