go-mocket
go-mocket copied to clipboard
struggling with mock_catcher: check query and long queries with spaces
Great library. Works wonderfully, except when I am trying to match long multiline queries. copied the query recommended by the
2019/06/26 11:29:07 mock_catcher: check query:
SELECT c.week, c.count FROM (
SELECT date_trunc('week', scheduled_at) week, count(*)
FROM w
WHERE id=abf10e8d-5607-430e-acd0-f6bd6a201631
AND scheduled_at <> 0001-01-01 00:00:00 +0000 UTC
AND is_optional IS NOT true
AND type <> rest
GROUP BY week
ORDER BY week) s
JOIN
(
SELECT date_trunc('week', completed_at) week, count(*)
FROM w
WHERE id=abf10e8d-5607-430e-acd0-f6bd6a201631
AND completed_at <> 0001-01-01 00:00:00 +0000 UTC
AND is_optional IS NOT true
AND type <> rest
GROUP BY week
ORDER BY week
) completed
ON s.week=c.week
WHERE c.count>=s.count
Struggling to insert the query into my test and have it match. Returns [0 rows affected or returned ]
It seems very sensitive to spacing? Am I not copying the spaces properly? Is it possible to make it ignore spacing/tabs?
Any advice would be very appreciated. Thank you.
@lauraeci Have you tried using a multi-line string?
The nice thing about a multi-line string in GoLang is that you don't need to escape characters like single/double quotes.
With Go-Mocket that could be something like:
sqlJoin := `SELECT c.week, c.count FROM (
SELECT date_trunc('week', scheduled_at) week, count(*)
FROM w
WHERE id=abf10e8d-5607-430e-acd0-f6bd6a201631
AND scheduled_at <> 0001-01-01 00:00:00 +0000 UTC
AND is_optional IS NOT true
AND type <> rest
GROUP BY week
ORDER BY week) s
JOIN
(
SELECT date_trunc('week', completed_at) week, count(*)
FROM w
WHERE id=abf10e8d-5607-430e-acd0-f6bd6a201631
AND completed_at <> 0001-01-01 00:00:00 +0000 UTC
AND is_optional IS NOT true
AND type <> rest
GROUP BY week
ORDER BY week
) completed
ON s.week=c.week
WHERE c.count>=s.count`
dbReply := []map[string]interface{}{{"week":"35","count":"999"}}
go_mocket.Catcher.NewMock().WithQuery(sqlJoin).WithReply(dbReply)
Here's another guide on String formatting that I've found helpful - How to format strings in Go