Files
DP44/enriched-qwen3-coder-next/DataPRO/DASFactoryDb.Tests.md

77 lines
6.8 KiB
Markdown
Raw Normal View History

2026-04-17 14:55:32 -04:00
---
source_files:
- DataPRO/DASFactoryDb.Tests/DbWrapperShould.cs
generated_at: "2026-04-16T03:45:35.889761+00:00"
model: "Qwen/Qwen3-Coder-Next-FP8"
schema_version: 1
sha256: "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 of `DbWrapper`. Multiple calls return the same reference (verified by test `Connection_ShouldBeSameReference`).
- **`public string Username { get; set; }`**
Property to get or set the database username. Used only when `_usingNTLMAuthentication` is `false`.
- **`public string Password { get; set; }`
Property to get or set the database password. Used only when `_usingNTLMAuthentication` is `false`.
- **`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 of `GetLocalDASFactoryConnectionString` ignores it and hardcodes `"DASFactory"`.
- **`public string Server { get; set; }`**
Property to get or set the SQL Server instance name. Required for `GetLocalDASFactoryConnectionString`; `null` or empty triggers an exception.
- **`public static bool Connected { get; set; }`**
Static property indicating whether the system is considered connected. Used by `GetDeviceId` to enforce connection state.
- **`public string GetLocalDASFactoryConnectionString()`**
Instance method that returns a SQL Server connection string based on current `Server`, `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 `Exception` with message `"Empty Server"` if `Server` is `null` or empty.
*(Note: `DBName` is hardcoded to `"DASFactory"` in all cases, despite the `DBName` property being settable.)*
- **`public static void ResetLocalDASFactoryConnection()`**
Instance method (called via `sut.ResetLocalDASFactoryConnection()` in tests) used to reset internal state—specifically to re-enable `Trusted_Connection=TRUE` after a test modifies `_usingNTLMAuthentication`. Its implementation is not visible in this file, but its effect is to revert `_usingNTLMAuthentication` to `true`.
- **`public static string GetDeviceId(string serial)`**
Static method that returns a device ID based on a serial number. Throws `Exception` with message `"Not connected"` if `Connected` is `false`. *(Note: Actual device ID lookup logic is not shown in this test file.)*
### 3. Invariants
- **Singleton Instance**: `DbWrapper.Connection` must return the same reference on every call (test `Connection_ShouldBeSameReference` confirms this).
- **Authentication Flag State**: `_usingNTLMAuthentication` is a *static* field initialized to `true`. It can be toggled (e.g., in tests), but tests expect it to be reset via `ResetLocalDASFactoryConnection()` to maintain test isolation.
- **Server Validation**: `GetLocalDASFactoryConnectionString()` requires `Server` to be non-null and non-empty; otherwise, it throws an exception with message `"Empty Server"`.
- **Hardcoded Database Name**: Despite `DBName` being a settable property, all generated connection strings use `"DASFactory"` as the database name. This is an implicit invariant: `DBName` is *not* used in connection string generation.
- **Connection State Enforcement**: `GetDeviceId()` requires `Connected == 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` (for `IDbCommand`, `IDbConnection`, `ConnectionState`).
- **Internal Dependencies** (inferred):
- `DbWrapper` likely depends on `System.Data.SqlClient` (or equivalent) at runtime for actual database operations (e.g., `GetDASFactoryCommand` is referenced in an ignored test).
- `DbWrapper` is used by other modules (e.g., tests reference `DbWrapper.GetDeviceId`, `DbWrapper.Connected`, `DbWrapper.Connection`), implying downstream consumers rely on it for connection management.
### 5. Gotchas
- **`DBName` is 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**: `_usingNTLMAuthentication` is a *static* field, meaning its value persists across instances and tests unless explicitly reset. Tests must call `ResetLocalDASFactoryConnection()` 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.
- **`GetDASFactoryCommand` is ignored**: The test `GetDASFactoryCommand_ShouldBeOpened` is 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 like `ArgumentException` or `InvalidOperationException`), 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* `_usingNTLMAuthentication` or other state) are unknown.