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

GilWright
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
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
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
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: