-
Notifications
You must be signed in to change notification settings - Fork 0
/
4 - TestDataBuilders.test.sql
108 lines (102 loc) · 4.66 KB
/
4 - TestDataBuilders.test.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
IF NOT EXISTS (
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = 'TestDataBuilder'
)
BEGIN
EXEC sys.sp_executesql N'CREATE SCHEMA [TestDataBuilder]'
END
GO
CREATE PROCEDURE [TestDataBuilder].[CustomerBuilder]
(
@FirstName NVARCHAR(40) = N'',
@LastName NVARCHAR(20) = N'',
@Company NVARCHAR(80) = N'',
@Address NVARCHAR(70) = N'',
@City NVARCHAR(40) = N'',
@State NVARCHAR(40) = N'',
@Country NVARCHAR(40) = N'',
@PostalCode NVARCHAR(10) = N'',
@Phone NVARCHAR(24) = N'',
@Fax NVARCHAR(24) = N'',
@Email NVARCHAR(60) = N'',
@SupportRepId INT = 0,
@CustomerId INT = 0 OUT
)
AS
BEGIN
DECLARE @returnValue INT = 0;
BEGIN TRY
IF OBJECTPROPERTY(OBJECT_ID(N'[dbo].[Customer]'), N'TableHasIdentity') = 1
BEGIN
-- If we're using the real table, we have to gather the IDENTITY value to
-- output it from the SPROC.
INSERT INTO [dbo].[Customer] (
[FirstName],
[LastName],
[Company],
[Address],
[City],
[State],
[Country],
[PostalCode],
[Phone],
[Fax],
[Email]
) VALUES (
@FirstName,
@LastName,
@Company,
@Address,
@City,
@State,
@Country,
@PostalCode,
@Phone,
@Fax,
@Email
)
SET @CustomerId = SCOPE_IDENTITY();
END
ELSE
BEGIN
-- If we're using a fake table, it may not have IDENTITY set so we need
-- to generate a new ID and output it
SET @CustomerId = COALESCE(@CustomerId, (SELECT MAX(CustomerId) FROM [dbo].[Customer]) + 1, 1);
INSERT INTO [dbo].[Customer] (
[FirstName],
[LastName],
[Company],
[Address],
[City],
[State],
[Country],
[PostalCode],
[Phone],
[Fax],
[Email]
) VALUES (
@FirstName,
@LastName,
@Company,
@Address,
@City,
@State,
@Country,
@PostalCode,
@Phone,
@Fax,
@Email
)
END
END TRY
BEGIN CATCH
-- Report any errors via a tSQLt failure.
DECLARE @ErrorMessage nvarchar(2000) = '[TestDataBuilder].[CustomerBuilder] - ERROR: ' + ERROR_MESSAGE();
SET @ReturnValue = ERROR_NUMBER()
EXEC tSQLt.Fail @ErrorMessage;
END CATCH
RETURN @returnValue;
END;
EXEC TestDataBuilder.CustomerBuilder @FirstName = N'Joe';
GO