6.8 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T03:45:35.889761+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 3a41cad42be76aa0 |
DASFactoryDb.Tests
Documentation: DbWrapper Module
1. Purpose
The DbWrapper class serves as a centralized, singleton-style abstraction over database connection configuration and connection string generation for the DASFactory database (specifically the DASFactory database). It encapsulates connection parameters (Server, DBName, Username, Password) and exposes a method (GetLocalDASFactoryConnectionString) to construct SQL Server connection strings using either Windows Authentication (Trusted Connection) or SQL Authentication, depending on the _usingNTLMAuthentication flag. It also provides a static Connected state flag and a static GetDeviceId method that enforces connection state before proceeding. Its primary role is to decouple connection string construction logic from data access layers and enforce consistent authentication behavior.
2. Public Interface
-
public static DbWrapper Connection { get; }
Static property returning the singleton instance ofDbWrapper. Multiple calls return the same reference (verified by testConnection_ShouldBeSameReference). -
public string Username { get; set; }
Property to get or set the database username. Used only when_usingNTLMAuthenticationisfalse. -
**
public string Password { get; set; }
Property to get or set the database password. Used only when_usingNTLMAuthenticationisfalse. -
public string DBName { get; set; }
Property to get or set the database name. Currently, tests hardcode"DASFactory"in generated connection strings, but this property is settable and used in tests (e.g.,DBName = "DataPro"), implying it should influence the connection string—though the current implementation ofGetLocalDASFactoryConnectionStringignores it and hardcodes"DASFactory". -
public string Server { get; set; }
Property to get or set the SQL Server instance name. Required forGetLocalDASFactoryConnectionString;nullor empty triggers an exception. -
public static bool Connected { get; set; }
Static property indicating whether the system is considered connected. Used byGetDeviceIdto enforce connection state. -
public string GetLocalDASFactoryConnectionString()
Instance method that returns a SQL Server connection string based on currentServer,DBName,Username,Password, and_usingNTLMAuthentication.- If
_usingNTLMAuthentication == true:
"Server={Server};Database=DASFactory;Trusted_Connection=TRUE;" - If
_usingNTLMAuthentication == false:
"Server={Server};Database=DASFactory;User Id={Username};Password={Password};" - Throws
Exceptionwith message"Empty Server"ifServerisnullor empty.
(Note:DBNameis hardcoded to"DASFactory"in all cases, despite theDBNameproperty being settable.)
- If
-
public static void ResetLocalDASFactoryConnection()
Instance method (called viasut.ResetLocalDASFactoryConnection()in tests) used to reset internal state—specifically to re-enableTrusted_Connection=TRUEafter a test modifies_usingNTLMAuthentication. Its implementation is not visible in this file, but its effect is to revert_usingNTLMAuthenticationtotrue. -
public static string GetDeviceId(string serial)
Static method that returns a device ID based on a serial number. ThrowsExceptionwith message"Not connected"ifConnectedisfalse. (Note: Actual device ID lookup logic is not shown in this test file.)
3. Invariants
- Singleton Instance:
DbWrapper.Connectionmust return the same reference on every call (testConnection_ShouldBeSameReferenceconfirms this). - Authentication Flag State:
_usingNTLMAuthenticationis a static field initialized totrue. It can be toggled (e.g., in tests), but tests expect it to be reset viaResetLocalDASFactoryConnection()to maintain test isolation. - Server Validation:
GetLocalDASFactoryConnectionString()requiresServerto be non-null and non-empty; otherwise, it throws an exception with message"Empty Server". - Hardcoded Database Name: Despite
DBNamebeing a settable property, all generated connection strings use"DASFactory"as the database name. This is an implicit invariant:DBNameis not used in connection string generation. - Connection State Enforcement:
GetDeviceId()requiresConnected == true; otherwise, it throws"Not connected".
4. Dependencies
- Test Dependencies (from source file):
NSubstitute(for mocking in other tests, though not used in this specific file).NUnit(for[TestFixture],[Test],Assert).System,System.Data(forIDbCommand,IDbConnection,ConnectionState).
- Internal Dependencies (inferred):
DbWrapperlikely depends onSystem.Data.SqlClient(or equivalent) at runtime for actual database operations (e.g.,GetDASFactoryCommandis referenced in an ignored test).DbWrapperis used by other modules (e.g., tests referenceDbWrapper.GetDeviceId,DbWrapper.Connected,DbWrapper.Connection), implying downstream consumers rely on it for connection management.
5. Gotchas
DBNameis ignored in connection string generation: Despite being a public settable property,GetLocalDASFactoryConnectionString()hardcodes"DASFactory"as the database name. This is likely a bug or technical debt.- Static mutable state:
_usingNTLMAuthenticationis a static field, meaning its value persists across instances and tests unless explicitly reset. Tests must callResetLocalDASFactoryConnection()to avoid cross-test pollution (as noted in the test comment: "Important to reset the connection otherwise the class needs to be redesigned to not be singleton"). - Singleton design with mutable state: The singleton pattern combined with mutable instance properties (
Server,Username, etc.) and static mutable state (_usingNTLMAuthentication,Connected) makes the class difficult to test in isolation and prone to side effects. GetDASFactoryCommandis ignored: The testGetDASFactoryCommand_ShouldBeOpenedis marked[Ignore("Not testing DAL")], indicating the actual data access logic is not unit-tested here. This method is not fully implemented or verified in this module.- Exception types are generic: All exceptions thrown are of type
Exception(not custom or more specific types likeArgumentExceptionorInvalidOperationException), which is not ideal for robust error handling. ResetLocalDASFactoryConnection()is not documented in source: Its behavior is inferred solely from test usage. Its implementation details (e.g., whether it resets only_usingNTLMAuthenticationor other state) are unknown.