-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[guides] Testing - how to set up rom-factories #275
Open
JulienAmoros
wants to merge
2
commits into
rom-rb:main
Choose a base branch
from
JulienAmoros:guides_testing-factories
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
title: Guides | ||
sections: | ||
- sql-quick-start | ||
- testing | ||
- sql-how-to | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
--- | ||
chapter: Testing | ||
title: Factories | ||
--- | ||
|
||
When using ROM, you will need to test your code, and testing your code implies generating entities and use Factories to | ||
create records on your database. To do all of that, you can use `rom-factory`, which provides more or less same | ||
functionnalities as `FactoryBot`. | ||
|
||
### Install RomFactory | ||
|
||
In your gemfile: | ||
|
||
```ruby | ||
gem 'rom-factory' | ||
``` | ||
|
||
### Setup ROMFactory | ||
|
||
Then on your spec root file (like `spec_helper`), you have to initialize your factory: | ||
|
||
```ruby | ||
# Build database configuration | ||
db_config = { | ||
adapter: 'mysql2', | ||
database: 'db_name', | ||
host: 'database.host.io', | ||
username: 'username', | ||
password: 'password', | ||
port: 3306 | ||
} | ||
|
||
# Init your ROM container | ||
rom = ROM.container(:sql, db_config)do |config| | ||
# Register ROM related files, especially Relations | ||
config.auto_registration('../app/') # if your sources are in app folder | ||
end | ||
|
||
# Declare your factory | ||
YourFactory = ROM::Factory.configure do |config| | ||
config.rom = rom | ||
end | ||
|
||
# Load your factories' files | ||
Dir[App.root + '/spec/factories/**/*.rb'].each { |f| require f } # if you plan to put your factory files `spec/factories` | ||
``` | ||
|
||
### Define your factory | ||
|
||
Then you can define your factories: | ||
|
||
```ruby | ||
YourFactory.define(:user) do |f| | ||
f.sequence(:name) { |n| "User#{n}" } | ||
f.website 'http://personal.website' | ||
f.some_integer 42 | ||
f.created_at Time.now.utc | ||
end | ||
``` | ||
|
||
You can define multiple builders on the same factory. | ||
In this example, the relation `users` (ie builder name pluralized) must exist in your ROM::RelationRegistry`. | ||
|
||
### Use your factory | ||
|
||
In your tests, you can either get an instance from your factory only in memory, or in memory but that has been | ||
persisted on your database. | ||
|
||
Let's suppose that `UserRepo` is your Users' repository and it has a relation to `users` SQL table. | ||
|
||
```ruby | ||
user1 = YourFactory[:user] | ||
p user1 | ||
#=> #<ROM::Struct::User id=84 name="User1" website="http://personnal.website" some_integer=42 created_at=2019-04-02 08:50:34 +0000> | ||
|
||
p UserRepo.users.by_pk(user1.id).one | ||
#=> #<ROM::Struct::User id=84 name="User1" website="http://personnal.website" some_integer=42 created_at=2019-04-02 08:50:34 +0000> | ||
|
||
user2 = YourFactory.structs[:user] | ||
p user2 | ||
#=> #<ROM::Struct::User id=85 name="User2" website="http://personnal.website" some_integer=42 created_at=2019-04-02 08:50:46 +0000> | ||
|
||
p UserRepo.users.by_pk(user2.id).one | ||
#=> nil | ||
|
||
user3 = YourFactory.structs[:user, name: "toto", website: nil] | ||
p user3 | ||
#=> #<ROM::Struct::User id=85 name="toto" website=nil some_integer=42 created_at=2019-04-02 08:51:20 +0000> | ||
``` | ||
|
||
### Going further | ||
|
||
#### Associations | ||
|
||
RomFactory supports associations that you declared in your Relations. | ||
```ruby | ||
YourFactory.define(:user) do |f| | ||
# this will use :team builder to create a team for a user | ||
f.association(:team) | ||
|
||
# this will create 2 posts for a user using :post builder | ||
f.association(:posts, count: 2) | ||
end | ||
``` | ||
|
||
#### Faker | ||
|
||
ROMFactory comes with Faker gem: | ||
|
||
```ruby | ||
YourFactory.define(:foo) do |f| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rename YourFactory to Factory |
||
f.bar { fake(:number, :between, 10, 100) } | ||
end | ||
``` | ||
|
||
#### Traits | ||
|
||
ROMFactory supports traits: | ||
|
||
```ruby | ||
YourFactory.define(:user) do |f| | ||
f.registered true | ||
|
||
f.trait :not_registered do |t| | ||
t.registered false | ||
end | ||
end | ||
|
||
p YourFactory[:user, :not_registered] | ||
#=> #<ROM::Struct::User id=130 registered=false> | ||
``` | ||
|
||
### Inheritance | ||
You can extend existing builders, see [Rom Factory annoncement](https://rom-rb.org/blog/announcing-rom-factory/) for an | ||
example. | ||
|
||
|
||
### Read more: | ||
* [Rom Factory annoncement](https://rom-rb.org/blog/announcing-rom-factory/) | ||
* [Rom Factory repository](https://github.com/rom-rb/rom-factory/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
chapter: SQL How To | ||
sections: | ||
- factories | ||
--- | ||
|
||
In this section you can learn some tricks to test your code when using ROM. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
--- | ||
chapter: Testing | ||
title: Factories | ||
--- | ||
|
||
When using ROM, you will need to test your code, and testing your code implies generating entities and use Factories to | ||
create records on your database. To do all of that, you can use `rom-factory`, which provides more or less same | ||
functionnalities as `FactoryBot`. | ||
|
||
### Install RomFactory | ||
|
||
In your gemfile: | ||
|
||
```ruby | ||
gem 'rom-factory' | ||
``` | ||
|
||
### Setup ROMFactory | ||
|
||
Then on your spec root file (like `spec_helper`), you have to initialize your factory: | ||
|
||
```ruby | ||
# Build database configuration | ||
db_config = { | ||
adapter: 'mysql2', | ||
database: 'db_name', | ||
host: 'database.host.io', | ||
username: 'username', | ||
password: 'password', | ||
port: 3306 | ||
} | ||
|
||
# Init your ROM container | ||
rom = ROM.container(:sql, db_config)do |config| | ||
# Register ROM related files, especially Relations | ||
config.auto_registration('../app/') # if your sources are in app folder | ||
end | ||
|
||
# Declare your factory | ||
YourFactory = ROM::Factory.configure do |config| | ||
config.rom = rom | ||
end | ||
|
||
# Load your factories' files | ||
Dir[App.root + '/spec/factories/**/*.rb'].each { |f| require f } # if you plan to put your factory files `spec/factories` | ||
``` | ||
|
||
### Define your factory | ||
|
||
Then you can define your factories: | ||
|
||
```ruby | ||
YourFactory.define(:user) do |f| | ||
f.sequence(:name) { |n| "User#{n}" } | ||
f.website 'http://personal.website' | ||
f.some_integer 42 | ||
f.created_at Time.now.utc | ||
end | ||
``` | ||
|
||
You can define multiple builders on the same factory. | ||
In this example, the relation `users` (ie builder name pluralized) must exist in your ROM::RelationRegistry`. | ||
|
||
### Use your factory | ||
|
||
In your tests, you can either get an instance from your factory only in memory, or in memory but that has been | ||
persisted on your database. | ||
|
||
Let's suppose that `UserRepo` is your Users' repository and it has a relation to `users` SQL table. | ||
|
||
```ruby | ||
user1 = YourFactory[:user] | ||
p user1 | ||
#=> #<ROM::Struct::User id=84 name="User1" website="http://personnal.website" some_integer=42 created_at=2019-04-02 08:50:34 +0000> | ||
|
||
p UserRepo.users.by_pk(user1.id).one | ||
#=> #<ROM::Struct::User id=84 name="User1" website="http://personnal.website" some_integer=42 created_at=2019-04-02 08:50:34 +0000> | ||
|
||
user2 = YourFactory.structs[:user] | ||
p user2 | ||
#=> #<ROM::Struct::User id=85 name="User2" website="http://personnal.website" some_integer=42 created_at=2019-04-02 08:50:46 +0000> | ||
|
||
p UserRepo.users.by_pk(user2.id).one | ||
#=> nil | ||
|
||
user3 = YourFactory.structs[:user, name: "toto", website: nil] | ||
p user3 | ||
#=> #<ROM::Struct::User id=85 name="toto" website=nil some_integer=42 created_at=2019-04-02 08:51:20 +0000> | ||
``` | ||
|
||
### Going further | ||
|
||
#### Associations | ||
|
||
RomFactory supports associations that you declared in your Relations. | ||
```ruby | ||
YourFactory.define(:user) do |f| | ||
# this will use :team builder to create a team for a user | ||
f.association(:team) | ||
|
||
# this will create 2 posts for a user using :post builder | ||
f.association(:posts, count: 2) | ||
end | ||
``` | ||
|
||
#### Faker | ||
|
||
ROMFactory comes with Faker gem: | ||
|
||
```ruby | ||
YourFactory.define(:foo) do |f| | ||
f.bar { fake(:number, :between, 10, 100) } | ||
end | ||
``` | ||
|
||
#### Traits | ||
|
||
ROMFactory supports traits: | ||
|
||
```ruby | ||
YourFactory.define(:user) do |f| | ||
f.registered true | ||
|
||
f.trait :not_registered do |t| | ||
t.registered false | ||
end | ||
end | ||
|
||
p YourFactory[:user, :not_registered] | ||
#=> #<ROM::Struct::User id=130 registered=false> | ||
``` | ||
|
||
### Inheritance | ||
You can extend existing builders, see [Rom Factory annoncement](https://rom-rb.org/blog/announcing-rom-factory/) for an | ||
example. | ||
|
||
|
||
### Read more: | ||
* [Rom Factory annoncement](https://rom-rb.org/blog/announcing-rom-factory/) | ||
* [Rom Factory repository](https://github.com/rom-rb/rom-factory/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
chapter: SQL How To | ||
sections: | ||
- factories | ||
--- | ||
|
||
In this section you can learn some tricks to test your code when using ROM. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make generating the time a block, so each created record has a new timestamp. This is more realistic in testing since records created later in the spec will have an increasing created_at