Skip to content

Conversation

rmoorman
Copy link
Contributor

@rmoorman rmoorman commented Sep 12, 2025

When the live generator is used with a schema name that is the same as the table name, the generated tests should not fail.

This change makes sure that the generated tests correctly target the collection item elements. We use the schema.collection by default and use schema.table when the primary key is not named :id.

The following generator calling variations should therefore work as intended:

  • same names, default pk name: mix phx.gen.live Bibliography Series series name:string
  • different names, default pk name: mix phx.gen.live Bibliography Author authors name:string
  • same names, custom pk name: mix phx.gen.live Bibliography Buyer buyer name:string --primary-key buyer_id
  • different names, custom pk name: mix phx.gen.live Bibliography Seller sellers name:string --primary-key seller_id

For context, before this change, calling the generator with the same word for schema name and table name (e.g. mix phx.gen.live Bibliography Series series name:string) resulted in failing tests with errors like:

  1) test Index updates series in listing (ExampleWeb.SeriesLiveTest)
     test/example_web/live/series_live_test.exs:52
     ** (ArgumentError) selector "#series_collection-c9129c62-f6b4-4749-854d-6d56f6a3b4b2 a" did not return any element within:

     <div id="phx-GGSpIya1xsOZigmM" data-phx-main="" data-phx-session="SFMyNTY.g2gDaAJhBnQAAAAIdwJpZG0AAAAUcGh4LUdHU3BJeWExeHNPWmlnbU13B3Nlc3Npb250AAAAAHcKcGFyZW50X3BpZHcDbmlsdwZ...
  ...

Reason being, that the stream used to generate the listing was named after the schema.collection which was in this case suffixed with _collection while the tests used the schema.plural value.

For reference see:

When the live generator is used with a schema name that is the same as
the table name, the generated tests should not fail.

This change makes sure that the generated tests correctly target the
collection item elements. We use the `schema.collection` by default and
use `schema.table` when the primary key is not named `:id`.

The following generator calling variations should therefore work as
intended:

- same names, default pk name: `mix phx.gen.live Bibliography Series series name:string`
- different names, default pk name: `mix phx.gen.live Bibliography Author authors name:string`
- same names, custom pk name: `mix phx.gen.live Bibliography Buyer buyer name:string --primary-key buyer_id`
- different names, custom pk name: `mix phx.gen.live Bibliography Seller sellers name:string --primary-key seller_id`

For context, before this change, calling the generator with the same
word for schema name and table name (e.g. `mix phx.gen.live Bibliography
Series series name:string`) resulted in failing tests with errors like:

```
  1) test Index updates series in listing (ExampleWeb.SeriesLiveTest)
     test/example_web/live/series_live_test.exs:52
     ** (ArgumentError) selector "#series_collection-c9129c62-f6b4-4749-854d-6d56f6a3b4b2 a" did not return any element within:

     <div id="phx-GGSpIya1xsOZigmM" data-phx-main="" data-phx-session="SFMyNTY.g2gDaAJhBnQAAAAIdwJpZG0AAAAUcGh4LUdHU3BJeWExeHNPWmlnbU13B3Nlc3Npb250AAAAAHcKcGFyZW50X3BpZHcDbmlsdwZ...
  ...
```

Reason being, that the stream used to generate the listing was named
after the `schema.collection` which was in this case suffixed with
`_collection` while the tests used the `schema.plural` value.

For reference see:
- <https://github.com/phoenixframework/phoenix/blob/v1.8.1/lib/mix/phoenix/schema.ex#L102>
- <https://github.com/phoenixframework/phoenix/blob/v1.8.1/priv/templates/phx.gen.live/index.ex#L54>
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.

1 participant