rom-factory icon indicating copy to clipboard operation
rom-factory copied to clipboard

Allow usage of relations in dependant attributes

Open alexandru-calinoiu opened this issue 4 years ago • 4 comments

Noticed that when using a relation as dependency in creating an attribute the relation always comes in nil, any way I can achieve this?

I have tables that have some of the data denormalized and some attributes need to have the same value on the entity and on the relation.

alexandru-calinoiu avatar Oct 24 '19 05:10 alexandru-calinoiu

Can you show an example? I'm not sure I understand.

solnic avatar Oct 24 '19 07:10 solnic

Factory.define :scheduled_position do |f|
  # TODO: figure out a way to set position_id to shift_position.position_id
  f.association(:position)
end

In the above code, I expected scheduled_position#position_id to be set the position#id

Does this make it any clearer?

alexandru-calinoiu avatar Nov 24 '19 16:11 alexandru-calinoiu

@alexandru-calinoiu kind of yes, a sample setup that shows the problem would be more helpful though. Are you saying that in this case factory[:scheduled_position].position_id returns nil?

solnic avatar Dec 10 '19 09:12 solnic

I think the same problem, I want to create consistent entities for my tests.

Example:

  • Entity web_resource has the attribute uri
  • Entity web_resource_content has the attribute uri and the association to web_resource

In my tests, I would like to be able to create valid factories by default (without passing attributes, and entities) to the factory call. I would like to be able to create a web_resource_content and web_resource using the same uri like this.

web_resource_content = Factory[:web_resource_content]
web_resource_content.uri == web_resource_content.resource_content.uri
# => true

This is possible with FactoryBot like this:

factory :web_resource_content do
  uri { Faker::Internet.url }
  web_resource { association :web_resource, uri: uri }
end

I don't know if this is something that can be interested to have in this library. It could be using this syntax:

Factory.define(:web_resource_content) do |f|
  f.association(:web_resource) { |uri| { uri: uri } }
  f.uri { fake(:internet, :url) }
end

or

Factory.define(:web_resource_content) do |f|
  f.web_resource { |uri| association(:web_resource, attributes: { uri: uri }) }
  f.uri { fake(:internet, :url) }
end

I don't know the implementation details of the library, this is just an example from a consumer perspective. 🙂

masterT avatar Oct 31 '22 10:10 masterT