Skip to content

Commit 124fc78

Browse files
committed
Add mock GetCurrentState method to SqlAudit, SqlDatabasePermission, and SqlPermission tests
1 parent 3998c00 commit 124fc78

File tree

4 files changed

+227
-6
lines changed

4 files changed

+227
-6
lines changed

tests/Unit/Classes/SqlAudit.Tests.ps1

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ Describe 'SqlAudit\Set()' -Tag 'Set' {
261261
BeforeAll {
262262
InModuleScope -ScriptBlock {
263263
$script:mockSqlAuditInstance |
264+
# Mock method GetCurrentState() which is called by the base method Get()
265+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
266+
return [System.Collections.Hashtable] @{
267+
Name = 'MockAuditName'
268+
InstanceName = 'NamedInstance'
269+
Path = 'C:\Temp'
270+
}
271+
} -PassThru |
264272
# Mock method Compare() which is called by the base method Set()
265273
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
266274
return $null
@@ -284,6 +292,14 @@ Describe 'SqlAudit\Set()' -Tag 'Set' {
284292
BeforeAll {
285293
InModuleScope -ScriptBlock {
286294
$script:mockSqlAuditInstance |
295+
# Mock method GetCurrentState() which is called by the base method Get()
296+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
297+
return [System.Collections.Hashtable] @{
298+
Name = 'MockAuditName'
299+
InstanceName = 'NamedInstance'
300+
Path = 'C:\Path'
301+
}
302+
} -PassThru |
287303
# Mock method Compare() which is called by the base method Set()
288304
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
289305
return @{
@@ -323,7 +339,15 @@ Describe 'SqlAudit\Test()' -Tag 'Test' {
323339
BeforeAll {
324340
InModuleScope -ScriptBlock {
325341
$script:mockSqlAuditInstance |
326-
# Mock method Compare() which is called by the base method Set()
342+
# Mock method GetCurrentState() which is called by the base method Get()
343+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
344+
return [System.Collections.Hashtable] @{
345+
Name = 'MockAuditName'
346+
InstanceName = 'NamedInstance'
347+
Path = 'C:\Temp'
348+
}
349+
} -PassThru |
350+
# Mock method Compare() which is called by the base method Test()
327351
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
328352
return $null
329353
} -PassThru |
@@ -344,7 +368,15 @@ Describe 'SqlAudit\Test()' -Tag 'Test' {
344368
BeforeAll {
345369
InModuleScope -ScriptBlock {
346370
$script:mockSqlAuditInstance |
347-
# Mock method Compare() which is called by the base method Set()
371+
# Mock method GetCurrentState() which is called by the base method Get()
372+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
373+
return [System.Collections.Hashtable] @{
374+
Name = 'MockAuditName'
375+
InstanceName = 'NamedInstance'
376+
Path = 'C:\WrongFolder'
377+
}
378+
} -PassThru |
379+
# Mock method Compare() which is called by the base method Test()
348380
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
349381
<#
350382
Compare() method shall only return the properties NOT in

tests/Unit/Classes/SqlDatabasePermission.Tests.ps1

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,28 @@ Describe 'SqlDatabasePermission\Set()' -Tag 'Set' {
942942
BeforeAll {
943943
InModuleScope -ScriptBlock {
944944
$script:mockSqlDatabasePermissionInstance |
945+
# Mock method GetCurrentState() which is called by the base method Get()
946+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
947+
return [System.Collections.Hashtable] @{
948+
Name = 'MockUserName'
949+
DatabaseName = 'MockDatabaseName'
950+
InstanceName = 'NamedInstance'
951+
Permission = [DatabasePermission[]] @(
952+
[DatabasePermission] @{
953+
State = 'Grant'
954+
Permission = @('Connect')
955+
}
956+
[DatabasePermission] @{
957+
State = 'GrantWithGrant'
958+
Permission = @()
959+
}
960+
[DatabasePermission] @{
961+
State = 'Deny'
962+
Permission = @()
963+
}
964+
)
965+
}
966+
} -PassThru |
945967
# Mock method Compare() which is called by the base method Set()
946968
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
947969
return $null
@@ -962,6 +984,28 @@ Describe 'SqlDatabasePermission\Set()' -Tag 'Set' {
962984
BeforeAll {
963985
InModuleScope -ScriptBlock {
964986
$script:mockSqlDatabasePermissionInstance |
987+
# Mock method GetCurrentState() which is called by the base method Get()
988+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
989+
return [System.Collections.Hashtable] @{
990+
Name = 'MockUserName'
991+
DatabaseName = 'MockDatabaseName'
992+
InstanceName = 'NamedInstance'
993+
Permission = [DatabasePermission[]] @(
994+
[DatabasePermission] @{
995+
State = 'Grant'
996+
Permission = @('Connect')
997+
}
998+
[DatabasePermission] @{
999+
State = 'GrantWithGrant'
1000+
Permission = @()
1001+
}
1002+
[DatabasePermission] @{
1003+
State = 'Deny'
1004+
Permission = @()
1005+
}
1006+
)
1007+
}
1008+
} -PassThru |
9651009
# Mock method Compare() which is called by the base method Set()
9661010
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
9671011
return @{
@@ -1022,7 +1066,29 @@ Describe 'SqlDatabasePermission\Test()' -Tag 'Test' {
10221066
BeforeAll {
10231067
InModuleScope -ScriptBlock {
10241068
$script:mockSqlDatabasePermissionInstance |
1025-
# Mock method Compare() which is called by the base method Set()
1069+
# Mock method GetCurrentState() which is called by the base method Get()
1070+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
1071+
return [System.Collections.Hashtable] @{
1072+
Name = 'MockUserName'
1073+
DatabaseName = 'MockDatabaseName'
1074+
InstanceName = 'NamedInstance'
1075+
Permission = [DatabasePermission[]] @(
1076+
[DatabasePermission] @{
1077+
State = 'Grant'
1078+
Permission = @('Connect')
1079+
}
1080+
[DatabasePermission] @{
1081+
State = 'GrantWithGrant'
1082+
Permission = @()
1083+
}
1084+
[DatabasePermission] @{
1085+
State = 'Deny'
1086+
Permission = @()
1087+
}
1088+
)
1089+
}
1090+
} -PassThru |
1091+
# Mock method Compare() which is called by the base method Test()
10261092
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
10271093
return $null
10281094
}
@@ -1040,7 +1106,29 @@ Describe 'SqlDatabasePermission\Test()' -Tag 'Test' {
10401106
BeforeAll {
10411107
InModuleScope -ScriptBlock {
10421108
$script:mockSqlDatabasePermissionInstance |
1043-
# Mock method Compare() which is called by the base method Set()
1109+
# Mock method GetCurrentState() which is called by the base method Get()
1110+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
1111+
return [System.Collections.Hashtable] @{
1112+
Name = 'MockUserName'
1113+
DatabaseName = 'MockDatabaseName'
1114+
InstanceName = 'NamedInstance'
1115+
Permission = [DatabasePermission[]] @(
1116+
[DatabasePermission] @{
1117+
State = 'Grant'
1118+
Permission = @('Connect')
1119+
}
1120+
[DatabasePermission] @{
1121+
State = 'GrantWithGrant'
1122+
Permission = @()
1123+
}
1124+
[DatabasePermission] @{
1125+
State = 'Deny'
1126+
Permission = @()
1127+
}
1128+
)
1129+
}
1130+
} -PassThru |
1131+
# Mock method Compare() which is called by the base method Test()
10441132
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
10451133
return @{
10461134
Property = 'Permission'

tests/Unit/Classes/SqlPermission.Tests.ps1

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,27 @@ Describe 'SqlPermission\Set()' -Tag 'Set' {
920920
BeforeAll {
921921
InModuleScope -ScriptBlock {
922922
$script:mockSqlPermissionInstance |
923+
# Mock method GetCurrentState() which is called by the base method Get()
924+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
925+
return [System.Collections.Hashtable] @{
926+
Name = 'MockUserName'
927+
InstanceName = 'NamedInstance'
928+
Permission = [ServerPermission[]] @(
929+
[ServerPermission] @{
930+
State = 'Grant'
931+
Permission = @('ConnectSql')
932+
}
933+
[ServerPermission] @{
934+
State = 'GrantWithGrant'
935+
Permission = @()
936+
}
937+
[ServerPermission] @{
938+
State = 'Deny'
939+
Permission = @()
940+
}
941+
)
942+
}
943+
} -PassThru |
923944
# Mock method Compare() which is called by the base method Set()
924945
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
925946
return $null
@@ -940,6 +961,27 @@ Describe 'SqlPermission\Set()' -Tag 'Set' {
940961
BeforeAll {
941962
InModuleScope -ScriptBlock {
942963
$script:mockSqlPermissionInstance |
964+
# Mock method GetCurrentState() which is called by the base method Get()
965+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
966+
return [System.Collections.Hashtable] @{
967+
Name = 'MockUserName'
968+
InstanceName = 'NamedInstance'
969+
Permission = [ServerPermission[]] @(
970+
[ServerPermission] @{
971+
State = 'Grant'
972+
Permission = @('ConnectSql')
973+
}
974+
[ServerPermission] @{
975+
State = 'GrantWithGrant'
976+
Permission = @()
977+
}
978+
[ServerPermission] @{
979+
State = 'Deny'
980+
Permission = @()
981+
}
982+
)
983+
}
984+
} -PassThru |
943985
# Mock method Compare() which is called by the base method Set()
944986
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
945987
return @{
@@ -999,7 +1041,28 @@ Describe 'SqlPermission\Test()' -Tag 'Test' {
9991041
BeforeAll {
10001042
InModuleScope -ScriptBlock {
10011043
$script:mockSqlPermissionInstance |
1002-
# Mock method Compare() which is called by the base method Set()
1044+
# Mock method GetCurrentState() which is called by the base method Get()
1045+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
1046+
return [System.Collections.Hashtable] @{
1047+
Name = 'MockUserName'
1048+
InstanceName = 'NamedInstance'
1049+
Permission = [ServerPermission[]] @(
1050+
[ServerPermission] @{
1051+
State = 'Grant'
1052+
Permission = @('ConnectSql')
1053+
}
1054+
[ServerPermission] @{
1055+
State = 'GrantWithGrant'
1056+
Permission = @()
1057+
}
1058+
[ServerPermission] @{
1059+
State = 'Deny'
1060+
Permission = @()
1061+
}
1062+
)
1063+
}
1064+
} -PassThru |
1065+
# Mock method Compare() which is called by the base method Test()
10031066
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
10041067
return $null
10051068
}
@@ -1017,7 +1080,28 @@ Describe 'SqlPermission\Test()' -Tag 'Test' {
10171080
BeforeAll {
10181081
InModuleScope -ScriptBlock {
10191082
$script:mockSqlPermissionInstance |
1020-
# Mock method Compare() which is called by the base method Set()
1083+
# Mock method GetCurrentState() which is called by the base method Get()
1084+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
1085+
return [System.Collections.Hashtable] @{
1086+
Name = 'MockUserName'
1087+
InstanceName = 'NamedInstance'
1088+
Permission = [ServerPermission[]] @(
1089+
[ServerPermission] @{
1090+
State = 'Grant'
1091+
Permission = @('ConnectSql')
1092+
}
1093+
[ServerPermission] @{
1094+
State = 'GrantWithGrant'
1095+
Permission = @()
1096+
}
1097+
[ServerPermission] @{
1098+
State = 'Deny'
1099+
Permission = @()
1100+
}
1101+
)
1102+
}
1103+
} -PassThru |
1104+
# Mock method Compare() which is called by the base method Test()
10211105
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
10221106
return @{
10231107
Property = 'Permission'

tests/Unit/Stubs/SMO.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,14 +1344,31 @@ public void Create()
13441344
// Used by:
13451345
// SqlAGDatabase
13461346
// Invoke-SqlDscScalarQuery
1347+
// Connect-SQL
13471348
public class ServerConnection
13481349
{
13491350
public string TrueLogin;
13501351
public int StatementTimeout;
1352+
public string ServerInstance;
1353+
public int ConnectTimeout;
1354+
public string ApplicationName;
1355+
public bool EncryptConnection;
1356+
public bool LoginSecure;
1357+
public string Login;
1358+
public System.Security.SecureString SecurePassword;
1359+
public bool ConnectAsUser;
1360+
public string ConnectAsUserName;
1361+
public string ConnectAsUserPassword;
13511362

13521363
public void Create()
13531364
{}
13541365

1366+
public void Connect()
1367+
{}
1368+
1369+
public void Disconnect()
1370+
{}
1371+
13551372
// Method: ExecuteScalar
13561373
// Used for testing scalar query execution in Invoke-SqlDscScalarQuery
13571374
public object ExecuteScalar(string query)

0 commit comments

Comments
 (0)