Comments (5)
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.
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.
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.
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.
@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)
- def build/2 defines defaults multiple times HOT 2
- Use of `build( ... )` with Ecto 3.4+ leads to many association preload warnings HOT 7
- Compiler warning when using an a project using Elixir 1.11 HOT 3
- Insert with assocs fails constraint HOT 1
- Proposal: Introduce build_lazy/2 HOT 3
- Test-only implementation fails HOT 4
- Cast polymorphic embeds HOT 10
- Inserted data breaks manual test HOT 3
- Hundreds of depreciation compiler warnings when running tests HOT 4
- insert/2 doesn't work with compound primary keys HOT 3
- Allow sequences indices to be persisted between runs HOT 2
- sequence starting with 0
- Is this project maintained? HOT 17
- Sequence reset doesn't respect the `start_at` option
- sequence(:example_id, & &1) put chars instead of integer HOT 3
- Stringify `Ecto.Enum` values when using `string_params`/`string_params_with_assocs`
- Option to have `insert_list` leverage Ecto's `insert_all`
- an intermittent error on async false tests HOT 1
- nvim diagnostics error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ex_machina.