Skip to content

Make the factories satisfy the constraints in the real database - #58

Open
kf8a wants to merge 1 commit into
masterfrom
test/factories-match-database-constraints
Open

kf8a wants to merge 1 commit into
masterfrom
test/factories-match-database-constraints

Conversation

@kf8a

@kf8a kf8a commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Summary

The database the app runs on has constraints the migration-built schema (db/schema.rb before 7f502da, and #57) doesn't. A schema dumped from it made the suite fail, because the factories made records that break those constraints:

  • datasets.website_id is a foreign key to websites that defaults to 1. The :dataset factory never set a website, so every dataset got website 1, which doesn't exist in a test database. This caused 53 of 53 failures.
  • units.name is unique (unit_names_key). The :unit factory always used 'per something', so a spec that made two variates failed. This caused the next 3.

Changes (tests only; no app or production code):

  • Dataset factory: website { Website.find_or_create_by!(name: 'lter') { |site| site.url = 'lter.kbs.msu.edu' } }. Each dataset gets the lter website, and it is created when the spec hasn't, so a spec that makes its own doesn't end up with a second.
  • Unit factory: the name is a sequence (per something 1, per something 2, ...).
  • spec/models/datatable_spec.rb: the #csv_headers specs created a second lter website after the factory had made one, and Website rejects duplicate names. The factory's dataset already has a site with that URL, so the redundant before block is removed.

If a new spec creates Website.create!(name: 'lter') after a factory dataset exists, it will hit the same duplicate-name error. Use find_or_create_by! there, or create the site first.

Testing

321 examples, 0 failures, 3 pending, on both:

  • the migration-built schema (the one in Restore a db/schema.rb that loads into a fresh database #57), and
  • the schema dumped from the real database (the 7f502da file with the extensions kept, minus barcode, spatial_ref_sys and the topology tables, and with two legacy sequence names changed to serial), loaded into a fresh PostgreSQL 17 database with PostGIS.

Master's current db/schema.rb still can't be loaded into a fresh database; that's #57. This PR doesn't depend on it and the two don't conflict.


Generated by Claude Code

The database the app runs on has constraints the migration-built schema
does not: datasets.website_id is a foreign key that defaults to 1, and
units.name is unique. With a schema dumped from that database 53 specs
failed on the first and 3 on the second.

- the dataset factory gives each dataset the lter website, creating it
  when the spec has not (find_or_create_by, so a spec that makes its own
  does not get a second one)
- the unit factory numbers its names
- the csv_headers spec no longer makes a second lter website; the factory's
  dataset already has one with that url

The suite passes on both the migration-built schema and the one from the
real database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants