Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cwl_tsql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Assumes a local server (see the run_tests.sh to configure server and password). Make sure you don't use sa for this very important data.

To run the tests:
```
cd sql
./run_tests.sh
```
4 changes: 4 additions & 0 deletions cwl_tsql/sql/DDL/001-create_table_product.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE Product (
Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
[Name] NVARCHAR(50)
)
5 changes: 5 additions & 0 deletions cwl_tsql/sql/DDL/002-create_table_basket.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE Basket (
Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
ProductId INT NOT NULL
CONSTRAINT FK_Basket_Product FOREIGN KEY (ProductId) REFERENCES Product(Id)
)
3 changes: 3 additions & 0 deletions cwl_tsql/sql/DDL/003-alter_table_basket_customer_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE basket
ADD CustomerId VARCHAR(100) NOT NULL
;
2 changes: 2 additions & 0 deletions cwl_tsql/sql/DDL/004-alter_table_product_add_price.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE dbo.Product
ADD Price DECIMAL(10,2) NOT NULL DEFAULT(0)
11 changes: 11 additions & 0 deletions cwl_tsql/sql/DML/generate_receipt.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SELECT
CONCAT(i, ') ', [Name], '. ', quantity, ' @ £', price, " = £", price * quantity)
FROM (
SELECT
p.[Name],
p.Price,
COUNT(1) as quantity,
DENSE_RANK() OVER (ORDER BY p.[Name]) AS i
FROM dbo.Basket b JOIN dbo.Product p ON b.ProductId = p.Id
GROUP BY p.[Name], p.Price
) x
2 changes: 2 additions & 0 deletions cwl_tsql/sql/DML/scan_product.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INSERT INTO dbo.Basket (ProductId, CustomerId)
VALUES (@ProductId, @CustomerId)
4 changes: 4 additions & 0 deletions cwl_tsql/sql/fixtures/_begin_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DECLARE @msg NVARCHAR(MAX);

BEGIN TRANSACTION test;
BEGIN TRY
10 changes: 10 additions & 0 deletions cwl_tsql/sql/fixtures/_end_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

ROLLBACK TRANSACTION test;

END TRY
BEGIN CATCH

ROLLBACK TRANSACTION test;
THROW;

END CATCH
5 changes: 5 additions & 0 deletions cwl_tsql/sql/fixtures/assert_actual_eq_expected.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
IF (@actual <> @expected)
BEGIN
PRINT FORMATMESSAGE(N'Expected: %s Actual: %s', COALESCE(@expected, '<null>'), COALESCE(@actual, '<null>'));
THROW 50000, @msg, 1;
END
5 changes: 5 additions & 0 deletions cwl_tsql/sql/fixtures/assert_actual_like_expected.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
IF NOT(@actual LIKE @expected)
BEGIN
PRINT FORMATMESSAGE(N'Expected: %s Actual: %s', COALESCE(@expected, '<null>'), COALESCE(@actual, '<null>'));
THROW 50000, @msg, 1;
END
17 changes: 17 additions & 0 deletions cwl_tsql/sql/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
PASSWORD='Password1!'
SERVER='::1'
TEST_DIR="./tests"

for test_file in "$TEST_DIR"/*.sql; do
test_name=$(basename "$test_file")

# Run test
if sqlcmd -S "$SERVER" -d supermarket -C -U sa -P "$PASSWORD" -b -i "$test_file" > /tmp/sql_test_output 2>&1; then
echo "_/ PASS: $test_name"
else
echo " X FAIL: $test_name"
sed 's/^/ /' /tmp/sql_test_output | tail -n 10 # show last few lines of output
fi

echo
done
24 changes: 24 additions & 0 deletions cwl_tsql/sql/seed/merge_products.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
DECLARE @SummaryOfChanges TABLE (Change VARCHAR(20))
;

MERGE INTO
dbo.Product AS tgt
USING
(VALUES
('Milk', 1.5),
('Bread', 3.5)
) AS src([Name], Price)
ON tgt.[Name] = src.[Name]
WHEN MATCHED THEN
UPDATE SET Price = src.Price
WHEN NOT MATCHED BY TARGET THEN
INSERT ([Name], Price)
VALUES (src.[Name], src.Price)
OUTPUT $action
INTO @SummaryOfChanges
;

SELECT Change, COUNT(*) as ChangeCount
FROM @SummaryOfChanges
GROUP BY Change
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- ARRANGE
SET NOCOUNT ON;

DECLARE @CustomerId UNIQUEIDENTIFIER = NEWID()
DECLARE @MilkId INT
DECLARE @BreadId INT
DECLARE @ProductId INT

SELECT @MilkId = p.Id FROM dbo.Product p WHERE p.[Name] = 'Milk'
SELECT @BreadId = p.Id FROM dbo.Product p WHERE p.[Name] = 'Bread'

DECLARE @actual NVARCHAR(MAX), @expected NVARCHAR(MAX)
CREATE TABLE #result ( x NVARCHAR(MAX) )

:r ./fixtures/_begin_test.sql

-- ACT
SET @ProductId = @MilkId
:r ./DML/scan_product.sql

SET @ProductId = @BreadId
:r ./DML/scan_product.sql

INSERT INTO #result
:r ./DML/generate_receipt.sql

-- ASSERT
SELECT @actual = STRING_AGG(x, '\n') FROM #result
SELECT @expected = '%1) Bread.%2) Milk.%'
:r ./fixtures/assert_actual_like_expected.sql

:r ./fixtures/_end_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- ARRANGE
SET NOCOUNT ON;

DECLARE @CustomerId UNIQUEIDENTIFIER = NEWID()
DECLARE @MilkId INT
DECLARE @BreadId INT
DECLARE @ProductId INT

SELECT @MilkId = p.Id FROM dbo.Product p WHERE p.[Name] = 'Milk'
SELECT @BreadId = p.Id FROM dbo.Product p WHERE p.[Name] = 'Bread'

DECLARE @actual NVARCHAR(MAX), @expected NVARCHAR(MAX)
CREATE TABLE #result ( x NVARCHAR(MAX) )

:r ./fixtures/_begin_test.sql

-- ACT
SET @ProductId = @MilkId
:r ./DML/scan_product.sql
:r ./DML/scan_product.sql

SET @ProductId = @BreadId
:r ./DML/scan_product.sql

INSERT INTO #result
:r ./DML/generate_receipt.sql

-- ASSERT
SELECT @actual = STRING_AGG(x, '\n') FROM #result
SELECT @expected = '%Bread. 1%'
:r ./fixtures/assert_actual_like_expected.sql


SELECT @expected = '%Milk. 2%'
:r ./fixtures/assert_actual_like_expected.sql


:r ./fixtures/_end_test.sql
38 changes: 38 additions & 0 deletions cwl_tsql/sql/tests/003 - generate_receipt_should_include_price.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- ARRANGE
SET NOCOUNT ON;

DECLARE @CustomerId UNIQUEIDENTIFIER = NEWID()
DECLARE @MilkId INT
DECLARE @BreadId INT
DECLARE @ProductId INT

SELECT @MilkId = p.Id FROM dbo.Product p WHERE p.[Name] = 'Milk'
SELECT @BreadId = p.Id FROM dbo.Product p WHERE p.[Name] = 'Bread'

DECLARE @actual NVARCHAR(MAX), @expected NVARCHAR(MAX)
CREATE TABLE #result ( x NVARCHAR(MAX) )

:r ./fixtures/_begin_test.sql

-- ACT
SET @ProductId = @MilkId
:r ./DML/scan_product.sql
:r ./DML/scan_product.sql

SET @ProductId = @BreadId
:r ./DML/scan_product.sql

INSERT INTO #result
:r ./DML/generate_receipt.sql

-- ASSERT
SELECT @actual = STRING_AGG(x, '\n') FROM #result
SELECT @expected = '%Bread. 1 @ £3.50%'
:r ./fixtures/assert_actual_like_expected.sql


SELECT @expected = '%Milk. 2 @ £1.50%'
:r ./fixtures/assert_actual_like_expected.sql


:r ./fixtures/_end_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- ARRANGE
SET NOCOUNT ON;

DECLARE @CustomerId UNIQUEIDENTIFIER = NEWID()
DECLARE @MilkId INT
DECLARE @BreadId INT
DECLARE @ProductId INT

SELECT @MilkId = p.Id FROM dbo.Product p WHERE p.[Name] = 'Milk'
SELECT @BreadId = p.Id FROM dbo.Product p WHERE p.[Name] = 'Bread'

DECLARE @actual NVARCHAR(MAX), @expected NVARCHAR(MAX)
CREATE TABLE #result ( x NVARCHAR(MAX) )

:r ./fixtures/_begin_test.sql

-- ACT
SET @ProductId = @MilkId
:r ./DML/scan_product.sql
:r ./DML/scan_product.sql

SET @ProductId = @BreadId
:r ./DML/scan_product.sql

INSERT INTO #result
:r ./DML/generate_receipt.sql

-- ASSERT
SELECT @actual = STRING_AGG(x, '\n') FROM #result
SELECT @expected = '%Bread. 1 @ £3.50 = £3.50%'
:r ./fixtures/assert_actual_like_expected.sql


SELECT @expected = '%Milk. 2 @ £1.50 = £3.00%'
:r ./fixtures/assert_actual_like_expected.sql


:r ./fixtures/_end_test.sql