---
title: 'This Week in #dev (Feb 9, 2024)'
teaser: 'Speed up your tests, a nice SQL trick, and a Jest global that you might not
  know about.

  '
tags: this week in dev,databases,rails,testing,jest,performance
author: thoughtbot
published_on: 2024-02-19
---

Welcome to another edition of [This Week in #dev](https://thoughtbot.com/blog/tags/this-week-in-dev),
a series of posts where we bring some of our most interesting Slack conversations to the public.

## Inverting `where` Clauses in Rails

[Fer Perales][fer] learned about [the `invert_where` method] in Rails' Active
Record.

[fer]: https://github.com/ferperales
[the `invert_where` method]: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-invert_where

```rb
class User
  scope :active, -> { where(accepted: true, locked: false) }
end

User.active
# WHERE `accepted` = 1 AND `locked` = 0

User.active.invert_where
# WHERE NOT (`accepted` = 1 AND `locked` = 0)
```

<aside class="warn">
  <p>
    Be careful with the use of <code>invert_where</code> because it inverts all conditions before its call. This can lead to unexpected results:
  </p>

<div class="highlight"><pre class="highlight ruby"><code><span class="k">class</span> <span class="nc">User</span>
  <span class="n">scope</span> <span class="ss">:active</span><span class="p">,</span> <span class="o">-&gt;</span> <span class="p">{</span> <span class="n">where</span><span class="p">(</span><span class="ss">accepted: </span><span class="kp">true</span><span class="p">,</span> <span class="ss">locked: </span><span class="kp">false</span><span class="p">)</span> <span class="p">}</span>
  <span class="n">scope</span> <span class="ss">:inactive</span><span class="p">,</span> <span class="o">-&gt;</span> <span class="p">{</span> <span class="n">active</span><span class="p">.</span><span class="nf">invert_where</span> <span class="p">}</span> <span class="c1"># Do not attempt it</span>
<span class="k">end</span>

<span class="c1"># It also inverts `where(role: 'admin')` unexpectedly.</span>
<span class="no">User</span><span class="p">.</span><span class="nf">where</span><span class="p">(</span><span class="ss">role: </span><span class="s1">'admin'</span><span class="p">).</span><span class="nf">inactive</span>
<span class="c1"># WHERE NOT (`role` = 'admin' AND `accepted` = 1 AND `locked` = 0)</span>
</code></pre></div>
</aside>

## Jest's `test.each` Global

[Stefanni Brasil][stefanni] shared a TIL about [Jest's `test.each` global]. Use it if you keep duplicating the same test suites with different data. `test.each` allows you to write the test suite once and pass data in.

Here's a simple example using the table syntax:

```js
test.each`
  a    | b    | expected
  ${1} | ${1} | ${2}
  ${1} | ${2} | ${3}
  ${2} | ${1} | ${3}
`('returns $expected when $a is added to $b', ({a, b, expected}) => {
  expect(a + b).toBe(expected);
});
```

[stefanni]: https://www.hexdevs.com/
[Jest's `test.each` global]: https://jestjs.io/docs/api#describeeachtablename-fn-timeout

## SQL `ALL` and `ANY`

SQL's `ALL` and `ANY` were this week's learning from [Mike Burns][mburns]. These
operators allow you to compare a value with all values in a set returned by a
subquery.

For instance, if you want to find all authors who have only published books, you
could use the following SQL query:

```sql
SELECT authors.*
FROM authors
WHERE 'published' = ALL (
  SELECT status
  FROM books
  WHERE author_id = authors.id
);
```

[mburns]: https://thoughtbot.com/blog/authors/mike-burns

## Less Logs, More Speed in Your Tests

[Sarah Lima][sarah.lima] learned about unlogged tables in PostgreSQL. They can
improve write performance by preventing tables from generating WAL (Write Ahead
Log) information. You can enable this on your tests to improve performance:

```rb
# config/environments/test.rb

ActiveSupport.on_load(:active_record_postgresqladapter) do
  self.create_unlogged_tables = true
end
```

Here are [some other non-durable PostgreSQL settings] which can help us trade off durability
for performance.

[sarah.lima]: https://thoughtbot.com/blog/authors/sarah-lima
[some other non-durable PostgreSQL settings]: https://www.postgresql.org/docs/current/non-durability.html

## Thanks

This edition was brought to you by [Fer Perales][fer], [Mike Burns][mburns],
[Sarah Lima][sarah.lima], and [Stefanni Brasil][stefanni]. Thanks
to all contributors! 🎉
