Git Product home page Git Product logo

Comments (5)

kevinkirkup avatar kevinkirkup commented on May 31, 2024 1

ah, ok.
I was able to get everything working with this:

mythings =
  insert_list(mything_count, :mything,
    name: fn -> sequence(:name, &"Custom-Name-#{&1}") end
  )

def mything_factory(attrs) do
  name = Map.get(attrs, :name, sequence("Test-Thing"))

  %MyThing{
    name: name,
  }
  |> merge_attributes(attrs)
  |> evaluate_lazy_attributes()
end

Thanks for your help!

from ex_machina.

germsvel avatar germsvel commented on May 31, 2024

Hi @kevinkirkup, do you have any more information you can provide? Can you share your factory setup and your test setup?

Just looking at the error, it seems like for some reason, it doesn't get that the second argument to sequence/2 is a function:

** (BadFunctionError) expected a function, got: "Custom-Name-"

But name: sequence(:name, &"Custom-Name-#{&1}") should be a valid sequence. I tested that in the ExMachina tests and it works.

Usually, I'd use sequences in the factory definition, not when calling insert. Are you calling the insert_list/3 function in a test or inside the factory? Do you get a different result if you try inside the factory?

from ex_machina.

kevinkirkup avatar kevinkirkup commented on May 31, 2024

Hi @germsvel, here is the factory definition:

  def myFactory_factory(attrs) do
    %MyFactory{
      name: sequence("Test-MyFactory"),
    }
    |> merge_attributes(attrs)
  end

I'm calling insert_list/3 as part of the test setup.
Yeah, I've seen the main pattern is to use sequence in the factory, but in this case, the name has to be formatted a certain way for the test case.

from ex_machina.

kevinkirkup avatar kevinkirkup commented on May 31, 2024

I updated the factory and now I'm seeing a different behavior.

Changed the factory to:

  def myFactory_factory(attrs) do
    name = Map.get(attrs, :name, sequence("Test-MyFactory"))
    %MyFactory{
      name: name,
    }
    |> merge_attributes(attrs)
  end
     test/xxx_web/controllers/myfactory_controller_test.exs:42
     ** (Ecto.ConstraintError) constraint error when attempting to insert struct:

         * myfactory_id_name_index (unique_constraint)

     If you would like to stop this constraint violation from raising an
     exception and instead add it as an error to your changeset, please
     call `unique_constraint/3` on your changeset with the constraint
     `:name` as an option.

     The changeset has not defined any constraint.

from ex_machina.

germsvel avatar germsvel commented on May 31, 2024

@kevinkirkup for the last error (the unique constraint one) are you passing a sequence when calling insert?

In other words, are you still calling this in your test?

myList = insert_list(8, :myFactory, name: sequence(:name, &"Custom-Name-#{&1}"))

If so, the error is happening because you're generating the sequence first, and then passing that generated value into insert_list/3, which means all 8 of your records have the same name. In other words, this is the equivalent of what you're doing:

# generates a name
name = sequence(:name, &"Custom-Name-#{&1}")
# e.g. => "Custom-Name-1"

# passes that name to all 8 factories
myList = insert_list(8, :myFactory, name: name)

In order to get around that you can delay the evaluation of attributes by passing a function:

myList = insert_list(8, :myFactory, name: fn -> sequence(:name, &"Custom-Name-#{&1}") end)

All in all, I'd expect the code to look something like this:

# factory.ex
def myFactory_factory(attrs) do
    %MyFactory{
     # the default sequence
      name: sequence("some name")
    }
end

# some_test.exs
test "I can do something" do
  # overriding the name attribute
  a = insert_list(8, :myFactory, name: fn -> sequence(:name, &"Custom-Name-#{&1}") end)
end

That should solve your unique constraint error.

from ex_machina.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.