rom-rb / rom-factory

Data generator with support for persistence backends
MIT License
83 stars 42 forks source link

Allow usage of relations in dependant attributes #48

Open alexandru-calinoiu opened 4 years ago

alexandru-calinoiu commented 4 years ago

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.

solnic commented 4 years ago

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

alexandru-calinoiu commented 4 years ago
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?

solnic commented 4 years ago

@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?

masterT commented 1 year ago

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

Example:

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. 🙂