Skip to content
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

Renaming Box to Factory #614

Merged
merged 1 commit into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions spec/array_column_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ end

describe "Array Columns" do
it "fails when passing a single value to an array query" do
BucketBox.new.numbers([1, 2, 3]).create
BucketFactory.new.numbers([1, 2, 3]).create
expect_raises(PQ::PQError) do
BucketQuery.new.numbers(1).select_count
end
end

it "returns no results when passing in a proper query that doesn't match" do
BucketBox.new.numbers([1, 2, 3]).create
BucketFactory.new.numbers([1, 2, 3]).create
BucketQuery.new.numbers([1]).select_count.should eq 0
end

it "handles Array(Float64)" do
BucketBox.create &.floaty_numbers([1.1, 2.2, 3.3, 4.4])
BucketFactory.create &.floaty_numbers([1.1, 2.2, 3.3, 4.4])
bucket = BucketQuery.new.last
bucket.floaty_numbers.should eq([1.1, 2.2, 3.3, 4.4])
end

it "handles Array(UUID)" do
BucketBox.create &.oody_things([UUID.new("40435254-4e21-45a6-9a1b-a1b9e7f5b444")])
BucketFactory.create &.oody_things([UUID.new("40435254-4e21-45a6-9a1b-a1b9e7f5b444")])
bucket = BucketQuery.new.last
bucket.oody_things.should eq([UUID.new("40435254-4e21-45a6-9a1b-a1b9e7f5b444")])
end

it "handles optional Array" do
BucketBox.create &.numbers(nil)
BucketFactory.create &.numbers(nil)
bucket = BucketQuery.new.last
bucket.numbers.should be_nil
bucket = SaveBucket.update!(bucket, numbers: [1, 2, 3])
Expand Down
72 changes: 36 additions & 36 deletions spec/associations_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ require "./spec_helper"
describe Avram::Model do
describe "has_many" do
it "gets the related records" do
post = PostBox.create
post2 = PostBox.create
comment = CommentBox.new.post_id(post.id).create
post = PostFactory.create
post2 = PostFactory.create
comment = CommentFactory.new.post_id(post.id).create

post = Post::BaseQuery.new.find(post.id)

Expand All @@ -15,9 +15,9 @@ describe Avram::Model do
end

it "gets the related records for nilable association that exists" do
manager = ManagerBox.create
employee = EmployeeBox.new.manager_id(manager.id).create
customer = CustomerBox.new.employee_id(employee.id).create
manager = ManagerFactory.create
employee = EmployeeFactory.new.manager_id(manager.id).create
customer = CustomerFactory.new.employee_id(employee.id).create

manager = Manager::BaseQuery.new.find(manager.id)

Expand All @@ -28,14 +28,14 @@ describe Avram::Model do
end

it "returns nil for nilable association that doesn't exist" do
employee = EmployeeBox.create
employee = EmployeeFactory.create
employee.manager.should eq nil
end

it "accepts a foreign_key" do
user = UserBox.create
cred_1 = SignInCredentialBox.new.user_id(user.id).create
cred_2 = SignInCredentialBox.new.user_id(user.id).create
user = UserFactory.create
cred_1 = SignInCredentialFactory.new.user_id(user.id).create
cred_2 = SignInCredentialFactory.new.user_id(user.id).create

key_holder = KeyHolderQuery.new.first

Expand All @@ -45,29 +45,29 @@ describe Avram::Model do

describe "has_many through" do
it "joins the two associations" do
tag = TagBox.create
post = PostBox.create
post2 = PostBox.create
TagBox.create
TaggingBox.new.tag_id(tag.id).post_id(post.id).create
tag = TagFactory.create
post = PostFactory.create
post2 = PostFactory.create
TagFactory.create
TaggingFactory.new.tag_id(tag.id).post_id(post.id).create

post.tags.should eq [tag]
post2.tags.size.should eq 0
end

it "counts has_many through belongs_to associations" do
tag = TagBox.create
post = PostBox.create
TagBox.create
TaggingBox.new.tag_id(tag.id).post_id(post.id).create
tag = TagFactory.create
post = PostFactory.create
TagFactory.create
TaggingFactory.new.tag_id(tag.id).post_id(post.id).create

post.tags_count.should eq 1
end

it "counts has_many through has_many associations" do
manager = ManagerBox.create
employee = EmployeeBox.new.manager_id(manager.id).create
CustomerBox.new.employee_id(employee.id).create
manager = ManagerFactory.create
employee = EmployeeFactory.new.manager_id(manager.id).create
CustomerFactory.new.employee_id(employee.id).create

manager.customers_count.should eq 1
end
Expand All @@ -76,22 +76,22 @@ describe Avram::Model do
describe "has_one" do
context "missing association" do
it "raises if association is not nilable" do
credentialed = AdminBox.create
credentialed = AdminFactory.create
expect_raises Exception, "Could not find first record in sign_in_credentials" do
credentialed.sign_in_credential
end
end

it "returns nil if association is nilable" do
possibly_credentialed = UserBox.create
possibly_credentialed = UserFactory.create
possibly_credentialed.sign_in_credential.should be_nil
end
end

context "existing association" do
it "returns associated model" do
user = UserBox.create
credentials = SignInCredentialBox.new.user_id(user.id).create
user = UserFactory.create
credentials = SignInCredentialFactory.new.user_id(user.id).create
user.sign_in_credential.should eq credentials
end
end
Expand All @@ -100,16 +100,16 @@ describe Avram::Model do
context "uuid backed models" do
describe "has_one" do
it "returns associated model" do
item = LineItemBox.create
price = PriceBox.new.line_item_id(item.id).create
item = LineItemFactory.create
price = PriceFactory.new.line_item_id(item.id).create
item.price.should eq price
end
end

describe "belongs_to" do
it "returns associated model" do
item = LineItemBox.create
price = PriceBox.new.line_item_id(item.id).create
item = LineItemFactory.create
price = PriceFactory.new.line_item_id(item.id).create
price.line_item.should eq item
end

Expand All @@ -125,24 +125,24 @@ describe Avram::Model do

describe "has_many" do
it "gets the related records" do
item = LineItemBox.create
scan = ScanBox.new.line_item_id(item.id).create
item = LineItemFactory.create
scan = ScanFactory.new.line_item_id(item.id).create

LineItemQuery.new.find(item.id).scans.should eq [scan]
end

it "gets amount of records" do
item = LineItemBox.create
ScanBox.new.line_item_id(item.id).create
item = LineItemFactory.create
ScanFactory.new.line_item_id(item.id).create

item.scans_count.should eq 1
end
end

describe "has_many through a join table" do
it "gets the related records" do
item = LineItemBox.create
scan = ScanBox.new.line_item_id(item.id).create
item = LineItemFactory.create
scan = ScanFactory.new.line_item_id(item.id).create

LineItemQuery.new.find(item.id).scans.should eq [scan]
end
Expand Down
30 changes: 15 additions & 15 deletions spec/box_spec.cr
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
require "./spec_helper"

describe Avram::Box do
describe Avram::Factory do
it "can create a model without additional columns" do
PlainModelBox.create.id.should_not be_nil
PlainModelFactory.create.id.should_not be_nil
end

describe "build_attributes" do
it "generate a named_tuple with attributes" do
BaseBox::SEQUENCES["name"] = 0
attributes = TagBox.build_attributes
BaseFactory::SEQUENCES["name"] = 0
attributes = TagFactory.build_attributes
attributes.should eq({custom_id: nil, created_at: nil, updated_at: nil, name: "name-1"})
end

it "overwrite attributes using a block" do
attributes = TagBox.build_attributes(&.name("new name"))
attributes = TagFactory.build_attributes(&.name("new name"))
attributes.should eq({custom_id: nil, created_at: nil, updated_at: nil, name: "new name"})
end
end

describe "Sequences" do
it "increases a value every time it's called" do
BaseBox::SEQUENCES["name"] = 0
BaseFactory::SEQUENCES["name"] = 0

tag1 = TagBox.create
tag2 = TagBox.create
tag1 = TagFactory.create
tag2 = TagFactory.create

tag1.name.should eq("name-1")
tag2.name.should eq("name-2")
end

it "can be overridden" do
tag = TagBox.create(&.name("not-a-sequence"))
tag = TagFactory.create(&.name("not-a-sequence"))

tag.name.should eq("not-a-sequence")
end
end

describe "create_pair" do
it "creates 2 tags" do
tags = TagBox.create_pair
tags = TagFactory.create_pair
tags.size.should eq 2
tags.class.name.should eq "Array(Tag)"
end

it "yields the block to both boxes" do
users = UserBox.create_pair do |box|
box.age(30)
it "yields the block to both Factoryes" do
users = UserFactory.create_pair do |factory|
factory.age(30)
end
users.first.age.should eq 30
users.last.age.should eq 30
end

it "works with sequences" do
tags = TagBox.create_pair do |box|
box.name(box.sequence("new-tag"))
tags = TagFactory.create_pair do |factory|
factory.name(factory.sequence("new-tag"))
end
tags.first.name.should eq "new-tag-1"
tags.last.name.should eq "new-tag-2"
Expand Down
2 changes: 1 addition & 1 deletion spec/case_insensitive_column_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ end

describe "Case insensitive columns" do
it "fails uniqueness validation" do
existing = EmailAddressBox.create
existing = EmailAddressFactory.create

SaveEmailAddress.create(address: existing.address) do |operation, _result|
operation.valid?.should be_false
Expand Down
2 changes: 1 addition & 1 deletion spec/database_cleaner_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "./spec_helper"
describe "DatabaseCleaner" do
describe "delete strategy" do
it "deletes all records" do
10.times { UserBox.create }
10.times { UserFactory.create }
UserQuery.new.select_count.should eq 10
TestDatabase.delete
UserQuery.new.select_count.should eq 0
Expand Down
8 changes: 4 additions & 4 deletions spec/json_column_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class SaveBlob < Blob::SaveOperation
end

describe "JSON Columns" do
it "should work in boxes" do
BlobBox.create
it "should work in factories" do
BlobFactory.create
blob = BlobQuery.new.first
blob.doc.should eq JSON::Any.new({"foo" => JSON::Any.new("bar")})

blob2 = BlobBox.new.doc(JSON::Any.new(42_i64)).create
blob2 = BlobFactory.new.doc(JSON::Any.new(42_i64)).create
blob2.doc.should eq JSON::Any.new(42_i64)
end

it "should be nullable" do
blob = BlobBox.create
blob = BlobFactory.create
SaveBlob.update!(blob, doc: nil)
blob = BlobQuery.new.first
blob.doc.should eq nil
Expand Down
22 changes: 11 additions & 11 deletions spec/lazy_loading_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@ include LazyLoadHelpers

describe "Lazy loading associations" do
it "can lazy load has_many and has_many through" do
post = PostBox.create
comment = CommentBox.new.post_id(post.id).create
tag = TagBox.create
TaggingBox.new.post_id(post.id).tag_id(tag.id).create
post = PostFactory.create
comment = CommentFactory.new.post_id(post.id).create
tag = TagFactory.create
TaggingFactory.new.post_id(post.id).tag_id(tag.id).create

post.comments!.should eq([comment])
post.tags!.should eq([tag])
end

it "can lazy load has_one" do
# to verify it is loading the correct association, not just the first
SignInCredentialBox.new.user_id(AdminBox.create.id).create
SignInCredentialFactory.new.user_id(AdminFactory.create.id).create

admin = AdminBox.create
sign_in_credential = SignInCredentialBox.new.user_id(admin.id).create
admin = AdminFactory.create
sign_in_credential = SignInCredentialFactory.new.user_id(admin.id).create
admin.sign_in_credential!.should eq(sign_in_credential)
end

it "can lazy load optional has_one" do
user = UserBox.create
user = UserFactory.create
user.sign_in_credential!.should be_nil
end

it "can lazy load belongs_to" do
post = PostBox.create
comment = CommentBox.new.post_id(post.id).create
post = PostFactory.create
comment = CommentFactory.new.post_id(post.id).create
comment.post!.should eq(post)
end

it "can lazy load optional belongs_to" do
employee = EmployeeBox.create
employee = EmployeeFactory.create
employee.manager!.should be_nil
end
end
Loading