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

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
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
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
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
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
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
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
PragmaticBookshelf
Programming Ruby is the most complete book on Ruby, covering both the language itself and the standard library as well as commonly used t...
New
hilfordjames
There appears to have been an update that has changed the terminology for what has previously been known as the Taskbar Overflow - this h...
New

Sub Categories: