1- using static ConfigurationManager ;
1+ using APITestingRunner . Database ;
2+ using APITestingRunner . Excetions ;
3+ using FluentAssertions ;
4+ using Microsoft . Extensions . Logging ;
5+ using Moq ;
6+ using static ConfigurationManager ;
27
3- namespace APITestingRunner . Unit . Tests
4- {
8+ namespace APITestingRunner . Unit . Tests {
59 [ TestClass ]
6- public class DataAccessTests
7- {
8- private Config ? _config ;
10+ public class DataAccessTests : TestBase {
11+ private readonly Config _config = new ( ) {
12+ UrlBase = "http://localhost:5152/" ,
13+ CompareUrlBase = string . Empty ,
14+ CompareUrlPath = string . Empty ,
15+ UrlPath = "/Data" ,
16+ UrlParam = new List < Param >
17+ {
18+ new Param ( "urlKey" , "test" ) ,
19+ new Param ( "id" , "sqlId" )
20+ } ,
21+ HeaderParam = new List < Param > {
22+ new Param ( "accept" , "application/json" )
23+ } ,
24+ RequestBody = null ,
25+ DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;" ,
26+ DBQuery = "select id as sqlId from dbo.sampleTable;" ,
27+ DBFields = new List < Param >
28+ {
29+ new Param ( "sqlId" , "sqlId" )
30+ } ,
31+ RequestType = RequestType . GET ,
32+ ResultsStoreOption = StoreResultsOption . All ,
33+ ConfigMode = TesterConfigMode . Run ,
34+ OutputLocation = DirectoryServices . AssemblyDirectory
35+ } ;
936
1037 [ TestInitialize ]
11- public void TestInit ( )
12- {
13- _config = new ( )
14- {
15- UrlBase = "http://localhost:5152/" ,
16- CompareUrlBase = string . Empty ,
17- CompareUrlPath = string . Empty ,
18- UrlPath = "/Data" ,
19- UrlParam = new List < Param >
20- {
21- new Param ( "urlKey" , "test" ) ,
22- new Param ( "id" , "sqlId" )
23- } ,
24- HeaderParam = new List < Param > {
25- new Param ( "accept" , "application/json" )
26- } ,
27- RequestBody = null ,
28- DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;" ,
29- DBQuery = "select id as sqlId from dbo.sampleTable;" ,
30- DBFields = new List < Param >
31- {
32- new Param ( "sqlId" , "sqlId" )
33- } ,
34- RequestType = RequestType . GET ,
35- ResultsStoreOption = StoreResultsOption . All ,
36- ConfigMode = TesterConfigMode . Run ,
37- LogLocation = DirectoryServices . AssemblyDirectory
38- } ;
38+ public void TestInit ( ) {
39+
3940 }
4041
4142 [ TestMethod ]
42- public void DataAccess_Tests_ConstructorShouldPass ( )
43- {
44- _ = new DataAccess ( _config ) ;
43+ public void DataAccess_Tests_ConstructorShouldPass ( ) {
44+ _ = new DataAccess ( _config , new Mock < ILogger > ( ) . Object ) ;
4545 }
4646
47+ //Type Safety takes care of this - shouldn't be needed.
48+ [ TestMethod ]
49+ public void DataAccess_Tests_MissingConfig_ConstructorShouldThrowArgumentNullException ( ) {
50+ _ = Assert . ThrowsException < ArgumentNullException > ( ( ) => new DataAccess ( null , null ) ) ;
51+ }
4752 [ TestMethod ]
48- public void DataAccess_Tests_ConstructorShouldThrowArgumentNullException ( )
49- {
50- _ = Assert . ThrowsException < ArgumentNullException > ( ( ) => new DataAccess ( null ) ) ;
53+ public void DataAccess_Tests_MissingLogger_ConstructorShouldThrowArgumentNullException ( ) {
54+ _ = Assert . ThrowsException < ArgumentNullException > ( ( ) => new DataAccess ( _config , null ) ) ;
5155 }
5256
5357 [ TestMethod ]
54- public async Task FetchDataForRunnerAsync_PassNullForConnectionString_shouldThrowConfigurationErrorsException ( )
55- {
58+ public async Task FetchDataForRunnerAsync_PassNullForConnectionString_shouldThrowConfigurationErrorsException ( ) {
5659 Config testConfig = _config ;
57- try
58- {
60+ try {
5961 testConfig . DBConnectionString = null ;
60- DataAccess da = new ( testConfig ) ;
62+ DataAccess da = new ( testConfig , new Mock < Logger > ( ) . Object ) ;
6163 _ = await da . FetchDataForRunnerAsync ( ) ;
6264 Assert . Fail ( ) ;
63- }
64- catch ( TestRunnerConfigurationErrorsException ex )
65- {
65+ } catch ( TestRunnerConfigurationErrorsException ex ) {
6666 Assert . AreEqual ( "Failed to load connection string" , ex . Message ) ;
67- }
68- catch
69- {
67+ } catch {
7068 Assert . Fail ( ) ;
7169 }
7270 }
71+
72+ [ TestMethod ]
73+ public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_DataSet_withOneFieldFromDbForBinder ( ) {
74+ _config . DBConnectionString = "Data Source=(LocalDB)\\ MSSQLLocalDB;AttachDbFilename=C:\\ code\\ cpoDesign\\ APITestingRunner\\ APITestingRunner.Unit.Tests\\ SampleDb.mdf;Integrated Security=True" ;
75+
76+ var data = new DataAccess ( _config , new Mock < Logger > ( ) . Object ) ;
77+
78+ var records = await data . FetchDataForRunnerAsync ( ) ;
79+
80+ _ = records . Should ( ) . NotBeEmpty ( ) ;
81+ _ = records . Should ( ) . HaveCount ( 3 ) ;
82+
83+ _ = records . Last ( ) . Should ( ) . BeEquivalentTo ( new DataQueryResult {
84+ RowId = 3 ,
85+ Results = new List < KeyValuePair < string , string > > { new KeyValuePair < string , string > ( "sqlId" , "3" ) ,
86+ }
87+ } ) ;
88+
89+ _ = records . Last ( ) . Results . Should ( ) . HaveCount ( 1 ) ;
90+ }
91+
92+ [ TestMethod ]
93+ public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_EmptyDataSet_withOneFieldFromDbForBinder ( ) {
94+ _config . DBConnectionString = "Data Source=(LocalDB)\\ MSSQLLocalDB;AttachDbFilename=C:\\ code\\ cpoDesign\\ APITestingRunner\\ APITestingRunner.Unit.Tests\\ SampleDb.mdf;Integrated Security=True" ;
95+
96+ _config . DBQuery = "select id as sqlId from dbo.sampleTable where id>5;" ;
97+ var data = new DataAccess ( _config , new Mock < Logger > ( ) . Object ) ;
98+
99+ var records = await data . FetchDataForRunnerAsync ( ) ;
100+
101+ _ = records . Should ( ) . BeEmpty ( ) ;
102+ }
103+
104+
105+ [ TestMethod ]
106+ public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_SingleFieldDataSet_withOneFieldFromDbForBinder ( ) {
107+ _config . DBConnectionString = "Data Source=(LocalDB)\\ MSSQLLocalDB;AttachDbFilename=C:\\ code\\ cpoDesign\\ APITestingRunner\\ APITestingRunner.Unit.Tests\\ SampleDb.mdf;Integrated Security=True" ;
108+
109+ _config . DBQuery = "select id as sqlId, name as fieldName from dbo.sampleTable" ;
110+ var data = new DataAccess ( _config , new Mock < Logger > ( ) . Object ) ;
111+
112+ var records = await data . FetchDataForRunnerAsync ( ) ;
113+
114+ _ = records . Should ( ) . NotBeEmpty ( ) ;
115+ _ = records . Should ( ) . HaveCount ( 3 ) ;
116+
117+ _ = records . Last ( ) . Should ( ) . BeEquivalentTo ( new DataQueryResult {
118+ RowId = 3 ,
119+ Results = new List < KeyValuePair < string , string > > { new KeyValuePair < string , string > ( "sqlId" , "3" ) ,
120+ }
121+ } ) ;
122+
123+ _ = records . Last ( ) . Results . Should ( ) . HaveCount ( 1 ) ;
124+ }
125+
126+ [ TestMethod ]
127+ public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_TwoFieldDataSet_withOneFieldFromDbForBinder ( ) {
128+ _config . DBConnectionString = "Data Source=(LocalDB)\\ MSSQLLocalDB;AttachDbFilename=C:\\ code\\ cpoDesign\\ APITestingRunner\\ APITestingRunner.Unit.Tests\\ SampleDb.mdf;Integrated Security=True" ;
129+
130+ _config . DBFields = new List < Param >
131+ {
132+ new Param ( "sqlId" , "sqlId" ) ,
133+ new Param ( "fieldName" , "fieldName" )
134+ } ;
135+
136+ _config . DBQuery = "select id as sqlId, name as fieldName from dbo.sampleTable" ;
137+ var data = new DataAccess ( _config , new Mock < Logger > ( ) . Object ) ;
138+
139+ var records = await data . FetchDataForRunnerAsync ( ) ;
140+
141+ _ = records . Should ( ) . NotBeEmpty ( ) ;
142+ _ = records . Should ( ) . HaveCount ( 3 ) ;
143+
144+ _ = records . Last ( ) . RowId . Should ( ) . Be ( 3 ) ;
145+ _ = records . Last ( ) . Results . Should ( ) . HaveCount ( 2 ) ;
146+ _ = records . Last ( ) . Results . First ( ) . Should ( ) . BeEquivalentTo ( new KeyValuePair < string , string > ( "sqlId" , "3" ) ) ;
147+ _ = records . Last ( ) . Results . Last ( ) . Should ( ) . BeEquivalentTo ( new KeyValuePair < string , string > ( "fieldName" , "Linux" ) , because : "" ) ;
148+ }
73149 }
74150}
0 commit comments