Skip to content

Commit ce42d6b

Browse files
SQLDBAWithABeardTylerLeonhardt
authored andcommitted
Added some snippets (#1297)
* Added data table snippet * added max length of datatable * spelling * Added Assertt Mock snippet * Added Pester Parameter test * added pester test for mandatory parameter
1 parent ab5ee30 commit ce42d6b

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

docs/community_snippets.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,41 @@ _To contribute, check out our [guide here](#contributing)._
1616

1717
| Table of Contents |
1818
|:------------------|
19+
| [AssertMock](#assertmock): _Creates assert mock Pester test_ |
1920
| [CalculatedProperty](#calculatedproperty): _Create a calculated property for use in a select-object call by @corbob_ |
21+
| [DataTable](#datatable): _Creates a DataTable_ |
2022
| [DateTimeWriteVerbose](#datetimewriteverbose): _Write-Verbose with the time and date pre-pended to your message by @ThmsRynr_ |
23+
| [IfShouldProcess](#ifshouldprocess): _Added If Should Process_ |
24+
| [MaxColumnLengthinDataTable](#maxcolumnlengthindatatable): _Gets the max length of string columns in datatables_ |
2125
| [Parameter-Credential](#parameter-credential): _Add a standard credential parameter to your function by @omniomi_ |
26+
| [PesterTestForMandatoryParameter](#pestertestformandatoryparameter): _Create Pester test for a mandatory parameter_ |
27+
| [PesterTestForParameter](#pestertestforparameter): _Create Pester test for parameter_ |
2228
| [PSCustomObject](#pscustomobject): _A simple PSCustomObject by @brettmillerb_ |
2329

30+
2431
## Snippets
2532

33+
### Assert Mock
34+
35+
Creates Assert Mock for Pester Tests y @SQLDBAWithABeard
36+
37+
#### Snippet
38+
39+
```json
40+
"AssertMock": {
41+
"prefix": "AssertMock",
42+
"body": [
43+
"$$assertMockParams = @{",
44+
"'CommandName' = '${1:Command}'",
45+
"'Times' = ${2:1}",
46+
"'Exactly' = $$true",
47+
"}",
48+
"Assert-MockCalled @assertMockParams"
49+
],
50+
"description": "AssertMock snippet for Pestering"
51+
}
52+
```
53+
2654
### CalculatedProperty
2755

2856
Create calculated property for use in Select Statements
@@ -39,6 +67,41 @@ Create calculated property for use in Select Statements
3967
}
4068
```
4169

70+
### DataTable
71+
72+
Quickly create a Data Table object by @SQLDBAWithABeard.
73+
74+
#### Snippet
75+
76+
```json
77+
"DataTable": {
78+
"prefix": "DataTable",
79+
"body": [
80+
"# Create DataTable Object",
81+
"$$table = New-Object system.Data.DataTable $$TableName",
82+
83+
"\r# Create Columns",
84+
"$$col1 = New-Object system.Data.DataColumn NAME1,([string])",
85+
"$$col2 = New-Object system.Data.DataColumn NAME2,([decimal])",
86+
87+
"\r#Add the Columns to the table",
88+
"$$table.columns.add($$col1)",
89+
"$$table.columns.add($$col2)",
90+
91+
"\r# Create a new Row",
92+
"$$row = $$table.NewRow() ",
93+
94+
"\r# Add values to new row",
95+
"$$row.Name1 = 'VALUE'",
96+
"$$row.NAME2 = 'VALUE'",
97+
98+
"\r#Add new row to table",
99+
"$$table.Rows.Add($$row)"
100+
],
101+
"description": "Creates a Data Table Object"
102+
}
103+
```
104+
42105
### DateTimeWriteVerbose
43106

44107
Quickly add a `Write-Verbose` with the current date and time inserted before the message you're going to write to the verbose stream, by @ThmsRynr.
@@ -55,6 +118,49 @@ Quickly add a `Write-Verbose` with the current date and time inserted before the
55118
}
56119
```
57120

121+
### IfShouldProcess
122+
123+
Add If Should Process with easy tab inputs
124+
125+
#### Snippet
126+
127+
```json
128+
"IfShouldProcess": {
129+
"prefix": "IfShouldProcess",
130+
"body": [
131+
"if ($$PSCmdlet.ShouldProcess(\"${1:The Item}\" , \"${2:The Change}\")) {",
132+
" # Place Code here",
133+
"}"
134+
],
135+
"description": "Creates an if should process"
136+
}
137+
```
138+
139+
### MaxColumnLengthinDataTable
140+
141+
Takes a datatable object and iterates through it to get the max length of the string columns - useful for data loads into a SQL Server table with fixed column widths by @SQLDBAWithABeard
142+
143+
#### Snippet
144+
145+
```json
146+
"Max Length of Datatable": {
147+
"prefix": "Max Length of Datatable",
148+
"body": [
149+
"$$columns = ($$datatable | Get-Member -MemberType Property).Name",
150+
"foreach($$column in $$Columns) {",
151+
" $$max = 0",
152+
" foreach ($$a in $$datatable){",
153+
" if($$max -lt $$a.$$column.length){",
154+
" $$max = $$a.$$column.length",
155+
" }",
156+
" }",
157+
" Write-Output \"$$column max length is $$max\"",
158+
"}"
159+
],
160+
"description": "Takes a datatable object and iterates through it to get the max length of the string columns - useful for data loads"
161+
}
162+
```
163+
58164
### Parameter-Credential
59165

60166
Add a `-Credential` parameter that supports a PSCredential object in a variable, `-Credential (Get-Credential)`, or `-Credential Username` (will prompt). Includes an empty PSCredential object as the default value but this is the first tabstop so pressing backspace after inserting the snippet removes it. by @omniomi
@@ -76,6 +182,41 @@ Add a `-Credential` parameter that supports a PSCredential object in a variable,
76182
}
77183
```
78184

185+
### PesterTestForMandatoryParameter
186+
187+
Quickly create a Pester Test for existence of a mandatory parameter by @SQLDBAWithABeard
188+
189+
#### Snippet
190+
191+
```json
192+
"Pester for Mandatory Pester": {
193+
"prefix": "mandatoryParamPester",
194+
"body": [
195+
"It \"${1:FunctionName} Should have a mandatory parameter ${2:ParameterName}\" {",
196+
" (Get-Command ${1:FunctionName}).Parameters['${2:ParameterName}'].Attributes.Mandatory | Should -BeTrue",
197+
"}"
198+
],
199+
"description": "Pester Test for Parameter"
200+
}
201+
```
202+
203+
### PesterTestForParameter
204+
205+
Quickly create a Pester Test for existence of a parameter by @SQLDBAWithABeard
206+
207+
#### Snippet
208+
209+
```json
210+
"Pester for Parameter": {
211+
"prefix": "Param Pester",
212+
"body": [
213+
"It \"${1:FunctionName} Should have a parameter ${2:ParameterName}\" {",
214+
" (Get-Command ${1:FunctionName}).Parameters['${2:ParameterName}'].Count | Should -Be 1",
215+
"}"
216+
],
217+
"description": "Pester Test for Parameter"
218+
}
219+
```
79220
### PSCustomObject
80221

81222
A simple PSCustomObject by @brettmillerb. It has 4 properties that you can tab through to quickly fill in.

0 commit comments

Comments
 (0)