1+ using System . Data ;
12using CosmoSQLClient . Core ;
23using CosmoSQLClient . MsSql ;
34using Xunit ;
@@ -10,25 +11,27 @@ public class InvoiceHeaderProcedureTests
1011 private static bool ShouldSkip => string . IsNullOrEmpty ( EnvConn ) ;
1112 private static string ConnectionString => EnvConn ?? "" ;
1213
14+ private const string TestProcName = "CosmoTest_InvoiceHeader" ;
15+
1316 private async Task EnsureProcedureExists ( MsSqlConnection conn )
1417 {
15- // Create a dummy InvoiceHeader procedure if it doesn't exist so the test can run in CI
16- await conn . ExecuteAsync ( @"
17- IF OBJECT_ID('InvoiceHeader ', 'P') IS NOT NULL DROP PROCEDURE InvoiceHeader ;
18+ // Create a dummy procedure if it doesn't exist so the test can run in CI
19+ await conn . ExecuteAsync ( $ @ "
20+ IF OBJECT_ID('{ TestProcName } ', 'P') IS NOT NULL DROP PROCEDURE { TestProcName } ;
1821 " ) ;
19-
20- await conn . ExecuteAsync ( @"
21- CREATE PROCEDURE InvoiceHeader
22+
23+ await conn . ExecuteAsync ( $ @ "
24+ CREATE PROCEDURE { TestProcName }
2225 @TransactionID NVARCHAR(50),
2326 @FinancialYear INT,
2427 @AdminUser INT,
2528 @Language NVARCHAR(50)
2629 AS
2730 BEGIN
28- SELECT
29- @TransactionID as TransactionID,
30- @FinancialYear as FinancialYear,
31- @AdminUser as AdminUser,
31+ SELECT
32+ @TransactionID as TransactionID,
33+ @FinancialYear as FinancialYear,
34+ @AdminUser as AdminUser,
3235 @Language as Language,
3336 'Test' as DummyData;
3437 END
@@ -52,11 +55,14 @@ public async Task ExecuteInvoiceHeaderProcedure_ShouldSucceed()
5255 SqlParameter . Named ( "Language" , SqlValue . From ( "English" ) )
5356 } ;
5457
55- var result = await conn . ExecuteProcAsync ( "InvoiceHeader" , parameters ) ;
58+ var result = await conn . ExecuteProcAsync ( TestProcName , parameters ) ;
5659
5760 Assert . NotNull ( result ) ;
5861 Assert . Single ( result . Rows ) ;
5962 Assert . Equal ( "1-C-96/25" , result . Rows [ 0 ] [ "TransactionID" ] . AsString ( ) ) ;
63+
64+ // Cleanup
65+ await conn . ExecuteAsync ( $ "DROP PROCEDURE { TestProcName } ") ;
6066 }
6167
6268 [ Fact ]
@@ -68,7 +74,7 @@ public async Task ExecuteInvoiceHeaderAsRawSql_ShouldSucceed()
6874 await conn . OpenAsync ( ) ;
6975 await EnsureProcedureExists ( conn ) ;
7076
71- const string sql = "exec InvoiceHeader @TransactionID=@tid, @FinancialYear=@fy, @AdminUser=@au, @Language=@lang" ;
77+ string sql = $ "exec { TestProcName } @TransactionID=@tid, @FinancialYear=@fy, @AdminUser=@au, @Language=@lang";
7278
7379 var parameters = new [ ]
7480 {
@@ -82,6 +88,10 @@ public async Task ExecuteInvoiceHeaderAsRawSql_ShouldSucceed()
8288
8389 Assert . NotNull ( rows ) ;
8490 Assert . Single ( rows ) ;
91+ Assert . Equal ( "1-C-96/25" , rows [ 0 ] [ "TransactionID" ] . AsString ( ) ) ;
92+
93+ // Cleanup
94+ await conn . ExecuteAsync ( $ "DROP PROCEDURE { TestProcName } ") ;
8595 }
8696
8797 [ Fact ]
@@ -93,21 +103,23 @@ public async Task DataTableLoad_WithMsSqlDataReader_ShouldHaveData()
93103 await conn . OpenAsync ( ) ;
94104 await EnsureProcedureExists ( conn ) ;
95105
96- using var cmd = conn . CreateCommand ( ) ;
97- cmd . CommandText = "InvoiceHeader" ;
98- cmd . CommandType = System . Data . CommandType . StoredProcedure ;
99- cmd . Parameters . Add ( new MsSqlParameter ( "@TransactionID" , "1-C-96/25" ) ) ;
100- cmd . Parameters . Add ( new MsSqlParameter ( "@FinancialYear" , 2025 ) ) ;
101- cmd . Parameters . Add ( new MsSqlParameter ( "@AdminUser" , 1 ) ) ;
102- cmd . Parameters . Add ( new MsSqlParameter ( "@Language" , "English" ) ) ;
106+ using var cmd = new MsSqlCommand ( $ "exec { TestProcName } @tid, @fy, @au, @lang", conn ) ;
107+ cmd . Parameters . AddWithValue ( "tid" , "1-C-96/25" ) ;
108+ cmd . Parameters . AddWithValue ( "fy" , 2025 ) ;
109+ cmd . Parameters . AddWithValue ( "au" , 1 ) ;
110+ cmd . Parameters . AddWithValue ( "lang" , "English" ) ;
103111
104- var table = new System . Data . DataTable ( "Header" ) ;
112+ var table = new DataTable ( "Header" ) ;
105113 using var reader = await cmd . ExecuteReaderAsync ( ) ;
106114
107115 table . Load ( reader ) ;
108116
109117 Assert . True ( table . Columns . Count > 0 , "DataTable should have columns" ) ;
110118 Assert . True ( table . Rows . Count > 0 , "DataTable should have rows" ) ;
111119 Assert . Equal ( "1-C-96/25" , table . Rows [ 0 ] [ "TransactionID" ] ) ;
120+ Assert . Equal ( "Test" , table . Rows [ 0 ] [ "DummyData" ] ) ;
121+
122+ // Cleanup
123+ await conn . ExecuteAsync ( $ "DROP PROCEDURE { TestProcName } ") ;
112124 }
113125}
0 commit comments