-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-4-testing-constraint.sql
51 lines (33 loc) · 1.11 KB
/
example-4-testing-constraint.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Example 5
* Testing a constraint
*
*/
-- (Show constraint)
--select * from Production.WorkOrder
IF OBJECT_ID('[tSQLtDemo].[Test EndDate cannot be before StartDate]','P') IS NOT NULL
BEGIN
DROP PROCEDURE [tSQLtDemo].[Test EndDate cannot be before StartDate];
END
GO
CREATE PROCEDURE [tSQLtDemo].[Test EndDate cannot be before StartDate] AS BEGIN
-- Can be falsely triggered by other exceptions
--EXEC tSQLt.ExpectException
EXEC tSQLt.FakeTable 'Production.WorkOrder', @Identity = 0, @ComputedColumns = 1
EXEC tSQLt.ApplyConstraint 'Production.WorkOrder','CK_WorkOrder_EndDate'
DECLARE @ErrorMessage VARCHAR(MAX);
begin try
INSERT INTO Production.WorkOrder (StartDate, EndDate)
VALUES ('2016-12-31', '2016-09-25' )
end try
begin catch
SET @ErrorMessage = ERROR_MESSAGE()
end catch
-- Make sure our constraint is the reason for the error message
IF @ErrorMessage is NULL or @ErrorMessage NOT LIKE '%CK_WorkOrder_EndDate%'
BEGIN
select @ErrorMessage
EXEC tSQLt.Fail 'Expected error message containing ''CK_WorkOrder_EndDate'' but got: ',@ErrorMessage,'!';
END
END;
GO