flavius-popan

flavius-popan

Programming Phoenix LiveView: Hundreds of failing tests after using Catalog generator due to long usernames (p74, B12)

After running mix phx.gen.live Catalog Product products name:string \ description:string unit_price:float sku:integer:unique to create the Catalog and updating router.ex, running mix test results in hundreds of failed tests (113) due to usernames being too long (+20 chars).

Example error:

     ** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{username: "user-576460752303421560", password: "**redacted**", email: "user-576460752303421592@example.com"}, errors: [username: {"should be at most %{count} character(s)", [count: 20, validation: :length, kind: :max, type: :string]}], data: #Pento.Accounts.User<>, valid?: false, ...>}
     stacktrace:
       (pento 0.1.0) test/support/fixtures/accounts_fixtures.ex:20: Pento.AccountsFixtures.user_fixture/1
       test/pento/accounts_test.exs:336: Pento.AccountsTest.__ex_unit_setup_12_0/1
       Pento.AccountsTest.__ex_unit_describe_12/1

This is because the unique_username() function in accounts_fixtures.ex generates usernames like "user#{System.unique_integer()}", but System.unique_integer() can return very large numbers (ex: 576460752303421560). This creates usernames longer than 20 characters, but the User schema has a validation that limits usernames to a maximum of 20 characters:

|> validate_length(:username, min: 3, max: 20)

My fix involves truncating usernames & emails via rem like so:

def unique_user_email, do: "user#{System.unique_integer() |> rem(1_000_000)}@example.com"
def unique_username, do: "user#{System.unique_integer() |> rem(1_000_000)}"

I’m using the following versions:

elixir = "1.18.4-otp-27"
erlang = "27.3.4.1"

Where Next?

Popular Pragmatic Bookshelf topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New

Other popular topics Top

PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
PragmaticBookshelf
Tailwind CSS is an exciting new CSS framework that allows you to design your site by composing simple utility classes to create complex e...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New

Sub Categories: