Skip to content

Commit 1374245

Browse files
committed
Add tips and tricks to the docs.
1 parent eb27605 commit 1374245

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Changes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Revision history for pgTAP
44
0.99.0
55
---------------------------
66
* Updated introductory documentation. A lot has changed over the years!
7+
* Added the "Secrets of the pgTAP Mavens" section of the documentation.
8+
Contributions wanted!
79

810
0.98.0 2017-11-06T22:30:54Z
911
---------------------------

doc/pgtap.mmd

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7701,6 +7701,37 @@ database in largely the same condition as it was in when you started it (the
77017701
one exception I'm aware of being sequences, which are not rolled back to the
77027702
value used at the beginning of a rolled-back transaction).
77037703

7704+
Secrets of the pgTAP Mavens
7705+
===========================
7706+
7707+
Over the years, a number of techniques have evolved to make all of our pgTAP
7708+
testing lives easier. Here are some of them.
7709+
7710+
Relational-style Loops
7711+
----------------------
7712+
7713+
Need to test a bunch of objects and find yourself looking for some kind of
7714+
`for` loop to [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) off
7715+
with? SQL doesn't have one, of course, but that's because it doesn't need one:
7716+
the whole language is built around doing things to a bunch of rows. So take
7717+
advantage of it: build relations with the
7718+
[`VALUES`](https://www.postgresql.org/docs/current/static/sql-values.html)
7719+
command! For example, to make sure you have a table in a defined list of
7720+
schemas, try something like this:
7721+
7722+
SELECT has_table(sch, 'widgets', format('Has %I.widgets', sch))
7723+
FROM (VALUES('amazon'), ('starbucks'), ('boeing')) F(sch);
7724+
7725+
Note the use of the
7726+
[`format` function](https://www.postgresql.org/docs/current/static/functions-string.html#functions-string-format)
7727+
to make a nice test description, too. Here's a more complicated example that
7728+
uses a cross join to test that various columns are `NOT NULL` in a specific
7729+
table in a bunch of schemas:
7730+
7731+
SELECT col_not_null(sch, 'table1', col)
7732+
FROM (VALUES('schema1'), ('schema1')) AS stmp (sch)
7733+
CROSS JOIN (VALUES('col_pk'), ('col2'), ('col3')) AS ctmp (col);
7734+
77047735
Compose Yourself
77057736
================
77067737

0 commit comments

Comments
 (0)