Skip to content

Commit 37b266e

Browse files
lowlydbaJohn McCallAppveyor
authored
Improve unit tests (#151)
* Create sp_predindex.sql * alpha version of sp_estindex * fix linting error * more md linting fixes * Update sp_estindex.md * mvp version * add extended properties * use label for repeatable code block * update docs * Update sp_estindex.md * Update sp_estindex.md * add sp_estindex * alphabetize scripts * CI produced files * update .gitattributes * encode file to utf8 * unit test stub * CI produced files * fix index drop for < 2016 sql * CI produced files * remove drop if * CI produced files * actually remove if * CI produced files * fix tsqllint errors * CI produced files * force build error for lint error * build tests for sp_estindex * add verbose mode test * increase test cov for sp_estindex to 100% * Update sp_estindex_tests.sql * formatting * CI produced files * @verbose documentation * remove spt usage for azure compatibility * improve linting, readability, general quality of unit tests * CI produced files * add @verbose to documentation * CI produced files Co-authored-by: John McCall <[email protected]> Co-authored-by: Appveyor <[email protected]>
1 parent b25eac7 commit 37b266e

File tree

11 files changed

+959
-672
lines changed

11 files changed

+959
-672
lines changed

appveyor/sqlcover/Coverage.opencoverxml

Lines changed: 568 additions & 564 deletions
Large diffs are not rendered by default.

appveyor/sqlcover/[dbo].[sp_doc]

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CREATE PROCEDURE [dbo].[sp_doc]
44
,@ExtendedPropertyName SYSNAME = 'Description'
55
,@LimitStoredProcLength BIT = 1
66
,@Emojis BIT = 0
7+
,@Verbose BIT = 1
78
/* Parameters defined here for testing only */
89
,@SqlMajorVersion TINYINT = 0
910
,@SqlMinorVersion SMALLINT = 0
@@ -78,8 +79,11 @@ BEGIN
7879
IF (@DatabaseName IS NULL)
7980
BEGIN
8081
SET @DatabaseName = DB_NAME();
81-
SET @Msg = 'No database provided, assuming current database.';
82-
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
82+
IF (@Verbose = 1)
83+
BEGIN;
84+
SET @Msg = 'No database provided, assuming current database.';
85+
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
86+
END;
8387
END
8488
ELSE IF (DB_ID(@DatabaseName) IS NULL)
8589
BEGIN;

appveyor/sqlcover/[dbo].[sp_estindex]

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ BEGIN TRY
248248
WHERE COALESCE([equality_columns] + ', ', '') + [inequality_columns] = @QuotedKeyColumns
249249
AND ([included_columns] = @QuotedInclColumns OR [included_columns] IS NULL);
250250

251+
IF (SELECT COUNT(*) FROM ##TempMissingIndex) = 0 AND (@Verbose = 1)
252+
BEGIN;
253+
SET @Msg = 'No matching missing index statistics found.';
254+
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
255+
END;
256+
251257
DROP TABLE ##TempMissingIndex;
252258

253259
/************************************************/

docs/sp_doc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ and plays nice with:
5656
| @ExtendedPropertyName | SYSNAME(128) | no | Key for extended properties on objects. Default is 'Description'. |
5757
| @LimitStoredProcLength | BIT | no | Limit stored procedure contents to 8000 characters, to avoid memory issues with some IDEs. Default is 1. |
5858
| @Emojis | BIT | no | Use emojis when generating documentation. Default is 0. |
59+
| @Verbose | BIT | no | Whether or not to print additional information during the script run. Default is 0. |
5960
| @SqlMajorVersion | TINYINT | no | Used for unit testing purposes only. |
6061
| @SqlMinorVersion | SMALLINT | no | Used for unit testing purposes only. |
6162

install_dba-multitool.sql

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ IF EXISTS (SELECT * FROM sys.fn_listextendedproperty(N'@Emojis' , N'SCHEMA',N'd
4646
END
4747
GO
4848

49+
IF EXISTS (SELECT * FROM sys.fn_listextendedproperty(N'@Verbose' , N'SCHEMA',N'dbo', N'PROCEDURE',N'sp_doc', NULL,NULL))
50+
BEGIN;
51+
EXEC sys.sp_dropextendedproperty @name=N'@Verbose' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_doc';
52+
END
53+
GO
54+
4955
/***************************/
5056
/* Create stored procedure */
5157
/***************************/
@@ -60,6 +66,7 @@ ALTER PROCEDURE [dbo].[sp_doc]
6066
,@ExtendedPropertyName SYSNAME = 'Description'
6167
,@LimitStoredProcLength BIT = 1
6268
,@Emojis BIT = 0
69+
,@Verbose BIT = 1
6370
/* Parameters defined here for testing only */
6471
,@SqlMajorVersion TINYINT = 0
6572
,@SqlMinorVersion SMALLINT = 0
@@ -134,8 +141,11 @@ BEGIN
134141
IF (@DatabaseName IS NULL)
135142
BEGIN
136143
SET @DatabaseName = DB_NAME();
137-
SET @Msg = 'No database provided, assuming current database.';
138-
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
144+
IF (@Verbose = 1)
145+
BEGIN;
146+
SET @Msg = 'No database provided, assuming current database.';
147+
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
148+
END;
139149
END
140150
ELSE IF (DB_ID(@DatabaseName) IS NULL)
141151
BEGIN;
@@ -1175,6 +1185,10 @@ GO
11751185

11761186
EXEC sys.sp_addextendedproperty @name=N'@Emojis', @value=N'Use emojis when generating documentation. Default is 0.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_doc';
11771187
GO
1188+
1189+
EXEC sys.sp_addextendedproperty @name=N'@Verbose', @value=N'Whether or not to print additional information during the script run. Default is 0.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_doc';
1190+
GO
1191+
11781192
SET ANSI_NULLS ON;
11791193
GO
11801194

@@ -1505,6 +1519,12 @@ BEGIN TRY
15051519
WHERE COALESCE([equality_columns] + ', ', '') + [inequality_columns] = @QuotedKeyColumns
15061520
AND ([included_columns] = @QuotedInclColumns OR [included_columns] IS NULL);
15071521

1522+
IF (SELECT COUNT(*) FROM ##TempMissingIndex) = 0 AND (@Verbose = 1)
1523+
BEGIN;
1524+
SET @Msg = 'No matching missing index statistics found.';
1525+
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
1526+
END;
1527+
15081528
DROP TABLE ##TempMissingIndex;
15091529

15101530
/************************************************/

sp_doc.sql

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ IF EXISTS (SELECT * FROM sys.fn_listextendedproperty(N'@Emojis' , N'SCHEMA',N'd
4646
END
4747
GO
4848

49+
IF EXISTS (SELECT * FROM sys.fn_listextendedproperty(N'@Verbose' , N'SCHEMA',N'dbo', N'PROCEDURE',N'sp_doc', NULL,NULL))
50+
BEGIN;
51+
EXEC sys.sp_dropextendedproperty @name=N'@Verbose' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_doc';
52+
END
53+
GO
54+
4955
/***************************/
5056
/* Create stored procedure */
5157
/***************************/
@@ -60,6 +66,7 @@ ALTER PROCEDURE [dbo].[sp_doc]
6066
,@ExtendedPropertyName SYSNAME = 'Description'
6167
,@LimitStoredProcLength BIT = 1
6268
,@Emojis BIT = 0
69+
,@Verbose BIT = 1
6370
/* Parameters defined here for testing only */
6471
,@SqlMajorVersion TINYINT = 0
6572
,@SqlMinorVersion SMALLINT = 0
@@ -134,8 +141,11 @@ BEGIN
134141
IF (@DatabaseName IS NULL)
135142
BEGIN
136143
SET @DatabaseName = DB_NAME();
137-
SET @Msg = 'No database provided, assuming current database.';
138-
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
144+
IF (@Verbose = 1)
145+
BEGIN;
146+
SET @Msg = 'No database provided, assuming current database.';
147+
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
148+
END;
139149
END
140150
ELSE IF (DB_ID(@DatabaseName) IS NULL)
141151
BEGIN;
@@ -1175,3 +1185,7 @@ GO
11751185

11761186
EXEC sys.sp_addextendedproperty @name=N'@Emojis', @value=N'Use emojis when generating documentation. Default is 0.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_doc';
11771187
GO
1188+
1189+
EXEC sys.sp_addextendedproperty @name=N'@Verbose', @value=N'Whether or not to print additional information during the script run. Default is 0.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_doc';
1190+
GO
1191+

sp_estindex.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,12 @@ BEGIN TRY
328328
WHERE COALESCE([equality_columns] + ', ', '') + [inequality_columns] = @QuotedKeyColumns
329329
AND ([included_columns] = @QuotedInclColumns OR [included_columns] IS NULL);
330330

331+
IF (SELECT COUNT(*) FROM ##TempMissingIndex) = 0 AND (@Verbose = 1)
332+
BEGIN;
333+
SET @Msg = 'No matching missing index statistics found.';
334+
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
335+
END;
336+
331337
DROP TABLE ##TempMissingIndex;
332338

333339
/************************************************/

tests/build/sp_doc_tests.sql

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
SET NOCOUNT ON;
2+
SET ANSI_NULLS ON;
3+
SET QUOTED_IDENTIFIER ON;
4+
15
/************************************
26
Begin sp_doc tests
37
*************************************/
@@ -16,8 +20,11 @@ CREATE PROCEDURE [sp_doc].[test sp succeeds on create]
1620
AS
1721
BEGIN;
1822

23+
DECLARE @ObjectName NVARCHAR(1000) = N'dbo.sp_doc';
24+
DECLARE @ErrorMessage NVARCHAR(MAX) = N'Stored procedure sp_doc does not exist.';
25+
1926
--Assert
20-
EXEC tSQLt.AssertObjectExists @objectName = 'dbo.sp_doc', @message = 'Stored procedure sp_doc does not exist.';
27+
EXEC tSQLt.AssertObjectExists @objectName = @objectName, @message = @ErrorMessage;
2128

2229
END;
2330
GO
@@ -80,23 +87,25 @@ CREATE PROCEDURE [sp_doc].[test sp fails on invalid db]
8087
AS
8188
BEGIN;
8289

83-
DECLARE @db SYSNAME = 'StarshipVoyager';
90+
DECLARE @DatabaseName SYSNAME = 'StarshipVoyager';
91+
DECLARE @ExpectedMessage NVARCHAR(MAX) = N'Database not available.';
8492

8593
--Assert
86-
EXEC [tSQLt].[ExpectException] @ExpectedMessage = N'Database not available.';
87-
EXEC [dbo].[sp_doc] @DatabaseName = @db;
94+
EXEC [tSQLt].[ExpectException] @ExpectedMessage = @ExpectedMessage;
95+
EXEC [dbo].[sp_doc] @DatabaseName = @DatabaseName;
8896

8997
END;
9098
GO
9199

92100
/*
93-
test sp_doc can assume current db if none given
101+
test sp_doc succeeds on assume current db if none given
94102
*/
95103
CREATE PROCEDURE [sp_doc].[test sp succeeds on current db if none given]
96104
AS
97105
BEGIN;
98106

99-
DECLARE @command NVARCHAR(MAX) = '[dbo].[sp_doc];';
107+
DECLARE @Verbose BIT = 0;
108+
DECLARE @command NVARCHAR(MAX) = CONCAT('[dbo].[sp_doc] @Verbose = ', @Verbose, ';');
100109

101110
--Assert
102111
EXEC [tSQLt].[ExpectNoException];
@@ -106,30 +115,32 @@ END;
106115
GO
107116

108117
/*
109-
test sp_doc errors on unsupported SQL Server < v12
118+
test sp_doc fails on unsupported SQL Server < v12
110119
*/
111120
CREATE PROCEDURE [sp_doc].[test sp fails on unsupported version]
112121
AS
113122
BEGIN;
114123

115124
DECLARE @version TINYINT = 10;
125+
DECLARE @ExpectedMessage NVARCHAR(MAX) = N'SQL Server versions below 2012 are not supported, sorry!';
116126

117127
--Assert
118-
EXEC [tSQLt].[ExpectException] @ExpectedMessage = N'SQL Server versions below 2012 are not supported, sorry!';
128+
EXEC [tSQLt].[ExpectException] @ExpectedMessage = @ExpectedMessage;
119129
EXEC [dbo].[sp_doc] @SqlMajorVersion = @version;
120130

121131
END;
122132
GO
123133

124134
/*
125-
test sp_doc works on supported SQL Server >= v12
135+
test sp_doc succeeds on supported SQL Server >= v12
126136
*/
127137
CREATE PROCEDURE [sp_doc].[test sp succeeds on supported version]
128138
AS
129139
BEGIN;
130140

131141
DECLARE @version TINYINT = 13;
132-
DECLARE @command NVARCHAR(MAX) = '[dbo].[sp_doc] @SqlMajorVersion = ' + CAST(@version AS NVARCHAR(4)) + ';';
142+
DECLARE @Verbose BIT = 0;
143+
DECLARE @command NVARCHAR(MAX) = CONCAT('[dbo].[sp_doc] @SqlMajorVersion = ', @version, ', @Verbose = ', @Verbose, ';');
133144

134145
--Assert
135146
EXEC [tSQLt].[ExpectNoException];
@@ -147,7 +158,7 @@ BEGIN;
147158

148159
EXEC tSQLt.AssertResultSetsHaveSameMetaData
149160
'SELECT CAST(''test'' AS NVARCHAR(MAX)) as [value]',
150-
'EXEC sp_doc';
161+
'EXEC [dbo].[sp_doc] @Verbose = 0';
151162

152163
END;
153164
GO
@@ -162,19 +173,20 @@ BEGIN;
162173
--Rows returned from empty database
163174
DECLARE @TargetRows SMALLINT = 22;
164175
DECLARE @ReturnedRows BIGINT;
176+
DECLARE @FailMessage NVARCHAR(MAX) = N'Minimum number of rows were not returned.';
177+
DECLARE @Verbose BIT = 0;
165178

166-
EXEC sp_doc;
179+
EXEC [dbo].[sp_doc] @Verbose = @Verbose;
167180
SET @ReturnedRows = @@ROWCOUNT;
168181

169182
IF (@TargetRows > @ReturnedRows)
170183
BEGIN;
171-
EXEC tSQLt.Fail 'Minimum number of rows were not returned.', @ReturnedRows;
184+
EXEC tSQLt.Fail @FailMessage, @ReturnedRows;
172185
END;
173186

174187
END;
175188
GO
176189

177-
178190
/************************************
179191
End sp_doc tests
180192
*************************************/

0 commit comments

Comments
 (0)