-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from Shopify/add-test-factories
Add FactoryBot for test factories
- Loading branch information
Showing
13 changed files
with
251 additions
and
113 deletions.
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
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
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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :category do | ||
sequence(:id, 1) do | ||
if parent.nil? | ||
"aa" | ||
else | ||
"#{parent.id}-#{_1}" | ||
end | ||
end | ||
name { "Category #{id}" } | ||
end | ||
end |
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,10 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :property do | ||
sequence(:id, 1) | ||
name { "Property#{id}" } | ||
handle { name.downcase } | ||
friendly_id { handle } | ||
end | ||
end |
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,10 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :property_value do | ||
sequence(:id, 1) | ||
name { "Value#{id}" } | ||
handle { name.downcase } | ||
friendly_id { [primary_property&.handle, handle].compact.join("__") } | ||
end | ||
end |
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 |
---|---|---|
@@ -1,11 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
|
||
Bundler.require(:test) | ||
|
||
require "minitest/rails" | ||
require "minitest/pride" | ||
require_relative "../application" | ||
|
||
Application.establish_db_connection!(env: :test) | ||
Application.load_and_reset_schema! | ||
|
||
require "minitest/rails" | ||
require "minitest/pride" | ||
FactoryBot.find_definitions | ||
|
||
class ApplicationTestCase < ActiveSupport::TestCase | ||
include FactoryBot::Syntax::Methods | ||
include Minitest::Hooks | ||
|
||
def around_all | ||
ActiveRecord::Base.transaction do | ||
super | ||
end | ||
end | ||
end |
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
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,80 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../test_helper" | ||
|
||
class PropertyTest < ApplicationTestCase | ||
def teardown | ||
Property.delete_all | ||
end | ||
|
||
test "default ordering is alphabetical" do | ||
material = create(:property, name: "Material") | ||
size = create(:property, name: "size") | ||
color = create(:property, name: "Color") | ||
|
||
assert_equal [color, material, size], Property.all.to_a | ||
end | ||
|
||
test ".base returns base properties" do | ||
base_property.save! | ||
extended_property.save! | ||
|
||
assert_equal [base_property], Property.base | ||
end | ||
|
||
test ".extended returns properties based off others" do | ||
base_property.save! | ||
extended_property.save! | ||
|
||
assert_equal [extended_property], Property.extended | ||
end | ||
|
||
test "#gid returns a global id" do | ||
assert_equal "gid://shopify/TaxonomyAttribute/42", build(:property, id: 42).gid | ||
end | ||
|
||
test "#gid returns base_property.gid when extended" do | ||
refute_equal base_property.id, extended_property.id | ||
assert_equal base_property.gid, extended_property.gid | ||
end | ||
|
||
test "#base?" do | ||
assert_predicate base_property, :base? | ||
refute_predicate extended_property, :base? | ||
end | ||
|
||
test "#extended?" do | ||
refute_predicate base_property, :extended? | ||
assert_predicate extended_property, :extended? | ||
end | ||
|
||
test "#friendly_id must be unique" do | ||
create(:property, friendly_id: "material") | ||
another_material = build(:property, friendly_id: "material") | ||
|
||
refute_predicate another_material, :valid? | ||
end | ||
|
||
test "#property_values must match base_property#property_values" do | ||
value = build(:property_value) | ||
base_property.property_values = [value] | ||
extended_property.property_values = [value] | ||
|
||
assert_predicate base_property, :valid? | ||
assert_predicate extended_property, :valid? | ||
|
||
extended_property.property_values = [] | ||
|
||
refute_predicate extended_property, :valid? | ||
end | ||
|
||
private | ||
|
||
def base_property | ||
@base_property ||= build(:property) | ||
end | ||
|
||
def extended_property | ||
@extended_property ||= build(:property, base_property:) | ||
end | ||
end |
Oops, something went wrong.