@@ -7701,6 +7701,37 @@ database in largely the same condition as it was in when you started it (the
77017701one exception I'm aware of being sequences, which are not rolled back to the
77027702value 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+
77047735Compose Yourself
77057736================
77067737
0 commit comments