diff --git a/spec/array_column_spec.cr b/spec/array_column_spec.cr index 73bfda195..0304fea8f 100644 --- a/spec/array_column_spec.cr +++ b/spec/array_column_spec.cr @@ -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]) diff --git a/spec/associations_spec.cr b/spec/associations_spec.cr index fe7fa64c3..ac811964b 100644 --- a/spec/associations_spec.cr +++ b/spec/associations_spec.cr @@ -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) @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -125,15 +125,15 @@ 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 @@ -141,8 +141,8 @@ describe Avram::Model do 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 diff --git a/spec/box_spec.cr b/spec/box_spec.cr index c4a0993d9..cc015ed06 100644 --- a/spec/box_spec.cr +++ b/spec/box_spec.cr @@ -1,36 +1,36 @@ 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 @@ -38,22 +38,22 @@ describe Avram::Box do 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" diff --git a/spec/case_insensitive_column_spec.cr b/spec/case_insensitive_column_spec.cr index ceb2a4e32..56ba22248 100644 --- a/spec/case_insensitive_column_spec.cr +++ b/spec/case_insensitive_column_spec.cr @@ -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 diff --git a/spec/database_cleaner_spec.cr b/spec/database_cleaner_spec.cr index 6c532357d..b22d14c85 100644 --- a/spec/database_cleaner_spec.cr +++ b/spec/database_cleaner_spec.cr @@ -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 diff --git a/spec/json_column_spec.cr b/spec/json_column_spec.cr index 6072355d0..598a00c69 100644 --- a/spec/json_column_spec.cr +++ b/spec/json_column_spec.cr @@ -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 diff --git a/spec/lazy_loading_spec.cr b/spec/lazy_loading_spec.cr index 9a6c0878a..27c2d448d 100644 --- a/spec/lazy_loading_spec.cr +++ b/spec/lazy_loading_spec.cr @@ -4,10 +4,10 @@ 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]) @@ -15,26 +15,26 @@ describe "Lazy loading associations" do 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 diff --git a/spec/model_spec.cr b/spec/model_spec.cr index 60c71bb28..f0746b81a 100644 --- a/spec/model_spec.cr +++ b/spec/model_spec.cr @@ -30,8 +30,8 @@ end describe Avram::Model do it "compares with id and model name, not just id" do - user = UserBox.create - post = PostBox.create + user = UserFactory.create + post = PostFactory.create user.id.should eq(post.id) user.should_not eq(post) @@ -94,7 +94,7 @@ describe Avram::Model do describe "reload" do it "can reload a model" do - user = UserBox.create &.name("Original Name") + user = UserFactory.create &.name("Original Name") # Update returns a brand new user. It should have the new name newly_updated_user = User::SaveOperation.update!(user, name: "Updated Name") @@ -108,7 +108,7 @@ describe Avram::Model do it "can reload a model with a yielded query" do with_lazy_load(enabled: false) do - post = PostBox.create + post = PostFactory.create # If `preload_tags` doesn't work this will raise post.reload(&.preload_tags).tags.should be_empty @@ -141,7 +141,7 @@ describe Avram::Model do end it "can be deleted" do - UserBox.create + UserFactory.create user = UserQuery.new.first user.delete @@ -162,14 +162,14 @@ describe Avram::Model do end it "can be saved" do - LineItemBox.create + LineItemFactory.create item = LineItemQuery.new.first item.id.should be_a UUID end it "can be deleted" do - LineItemBox.create + LineItemFactory.create item = LineItemQuery.new.first item.delete diff --git a/spec/operations/define_attribute_spec.cr b/spec/operations/define_attribute_spec.cr index b23ed6f09..2bdad76a2 100644 --- a/spec/operations/define_attribute_spec.cr +++ b/spec/operations/define_attribute_spec.cr @@ -245,6 +245,6 @@ private def upload_save_operation(attrs = {} of String => Avram::Uploadable) end private def delete_operation(attrs = {} of String => String) - post = PostBox.create + post = PostFactory.create DeleteOperationWithAttributes.new(post, Avram::Params.new(attrs)) end diff --git a/spec/operations/delete_operation_callbacks_spec.cr b/spec/operations/delete_operation_callbacks_spec.cr index 81c020d5f..520a2bd90 100644 --- a/spec/operations/delete_operation_callbacks_spec.cr +++ b/spec/operations/delete_operation_callbacks_spec.cr @@ -33,7 +33,7 @@ end describe "Avram::DeleteOperation callbacks" do it "runs before_delete and after_delete callbacks" do - user = UserBox.create &.name("Jerry") + user = UserFactory.create &.name("Jerry") DeleteOperationWithCallbacks.destroy(user) do |operation, deleted_user| deleted_user.not_nil!.name.should eq "Jerry" diff --git a/spec/operations/delete_operation_spec.cr b/spec/operations/delete_operation_spec.cr index 6c6d6e4a9..719857ff5 100644 --- a/spec/operations/delete_operation_spec.cr +++ b/spec/operations/delete_operation_spec.cr @@ -26,7 +26,7 @@ end describe "Avram::DeleteOperation" do describe "destroy" do it "deletes the specified record" do - user = UserBox.create + user = UserFactory.create BasicDeleteUser.destroy(user) do |operation, deleted_user| operation.valid?.should be_true @@ -37,7 +37,7 @@ describe "Avram::DeleteOperation" do end it "does not delete if the operation is invalid" do - user = UserBox.create + user = UserFactory.create FailedToDeleteUser.destroy(user) do |operation, deleted_user| operation.valid?.should be_false @@ -51,7 +51,7 @@ describe "Avram::DeleteOperation" do describe "destroy!" do it "deletes the specified record" do - user = UserBox.create + user = UserFactory.create deleted_user = BasicDeleteUser.destroy!(user) deleted_user.name.should eq user.name @@ -59,7 +59,7 @@ describe "Avram::DeleteOperation" do end it "raises an exception when unable to delete" do - user = UserBox.create + user = UserFactory.create expect_raises(Avram::InvalidOperationError) do FailedToDeleteUser.destroy!(user) @@ -69,7 +69,7 @@ describe "Avram::DeleteOperation" do describe "soft deletes" do it "returns a soft deleted object" do - item = SoftDeletableItemBox.create + item = SoftDeletableItemFactory.create deleted_item = SoftDeleteItem.destroy!(item) @@ -80,8 +80,8 @@ describe "Avram::DeleteOperation" do describe "cascade deletes" do it "deletes the object and associated" do - business = BusinessBox.create - EmailAddressBox.create &.business_id(business.id) + business = BusinessFactory.create + EmailAddressFactory.create &.business_id(business.id) EmailAddress::BaseQuery.new.select_count.should eq(1) @@ -99,7 +99,7 @@ describe "Avram::DeleteOperation" do events << event end - user = UserBox.create + user = UserFactory.create BasicDeleteUser.destroy!(user) events.map(&.operation_class).should contain("BasicDeleteUser") @@ -111,7 +111,7 @@ describe "Avram::DeleteOperation" do events << event end - user = UserBox.create + user = UserFactory.create expect_raises(Avram::InvalidOperationError) do FailedToDeleteUser.destroy!(user) @@ -126,7 +126,7 @@ describe "Avram::DeleteOperation" do context "using the model for conditional deletes" do it "adds the error and fails to save" do - post = PostBox.create &.title("sandbox") + post = PostFactory.create &.title("sandbox") DeleteOperationWithAccessToModelValues.destroy(post) do |operation, _deleted_post| operation.deleted?.should be_false diff --git a/spec/operations/operation_needs_spec.cr b/spec/operations/operation_needs_spec.cr index b6fd230cb..4f22d34e3 100644 --- a/spec/operations/operation_needs_spec.cr +++ b/spec/operations/operation_needs_spec.cr @@ -72,7 +72,7 @@ end describe "Avram::SaveOperation needs" do it "sets up a method arg for save, update, and new" do params = Avram::Params.new({"name" => "Paul"}) - UserBox.create + UserFactory.create user = UserQuery.new.first NeedsSaveOperation.create(params, nilable_value: "not nil", optional: "bar", created_by: "Jane") do |operation, _record| @@ -104,8 +104,8 @@ end describe "Avram::DeleteOperation needs" do it "sets up a method arg for destroy" do - user = UserBox.create - post = PostBox.create + user = UserFactory.create + post = PostFactory.create NeedyDeleteOperation.destroy(post, user: user, notification_message: "is this thing on?") do |operation, _record| operation.notification_message.should eq("is this thing on?") @@ -116,8 +116,8 @@ describe "Avram::DeleteOperation needs" do it "also generates named args for other attributes" do params = Avram::Params.new({"confirm_delete" => "yeah, do it"}) - user = UserBox.create - post = PostBox.create + user = UserFactory.create + post = PostFactory.create NeedyDeleteOperation.destroy(post, params, user: user, notification_message: nil) do |operation, _record| operation.notification_message.should be_nil diff --git a/spec/operations/operation_spec.cr b/spec/operations/operation_spec.cr index b990ee164..f88962c8e 100644 --- a/spec/operations/operation_spec.cr +++ b/spec/operations/operation_spec.cr @@ -116,7 +116,7 @@ describe Avram::Operation do operation.password.value = "p@ssword" end - user = UserBox.create + user = UserFactory.create UserWithVirtual.update(user, password: "p@ssword") do |operation, _user| operation.password.value = "p@ssword" end diff --git a/spec/operations/save_operation_callbacks_spec.cr b/spec/operations/save_operation_callbacks_spec.cr index 6cf2bdaa4..c9d276cc2 100644 --- a/spec/operations/save_operation_callbacks_spec.cr +++ b/spec/operations/save_operation_callbacks_spec.cr @@ -188,7 +188,7 @@ describe "Avram::SaveOperation callbacks" do end it "runs all callbacks when saving successfully" do - post = PostBox.create + post = PostFactory.create operation = CallbacksSaveOperation.new(post) operation.callbacks_that_ran.should eq([] of String) @@ -205,7 +205,7 @@ describe "Avram::SaveOperation callbacks" do end it "does not run after_commit if rolled back" do - post = PostBox.create + post = PostFactory.create operation = CallbacksSaveOperation.new(post, rollback: true) operation.callbacks_that_ran.should eq([] of String) @@ -239,7 +239,7 @@ describe "Avram::SaveOperation callbacks" do end it "skips running specified callbacks" do - post = PostBox.create &.title("Existing Post") + post = PostFactory.create &.title("Existing Post") params = Avram::Params.new({"title" => "A fancy post"}) UpdateOperationWithSkipCallbacks.update(post, params) do |operation, updated_post| @@ -255,7 +255,7 @@ describe "Avram::SaveOperation callbacks" do end it "runs callbacks even when nothing is being updated" do - post = PostBox.create + post = PostFactory.create UpdateOperationWithNoUpdates.update(post) do |operation, _updated_post| operation.callbacks_that_ran.should eq([ "after_save_called", diff --git a/spec/operations/save_operation_spec.cr b/spec/operations/save_operation_spec.cr index 4c2c9eb6e..76f1a25f2 100644 --- a/spec/operations/save_operation_spec.cr +++ b/spec/operations/save_operation_spec.cr @@ -80,7 +80,7 @@ describe "Avram::SaveOperation" do end it "can save empty arrays" do - bucket = BucketBox.create + bucket = BucketFactory.create bucket = Bucket::SaveOperation.update!(bucket, names: [] of String) @@ -119,7 +119,7 @@ describe "Avram::SaveOperation" do describe ".update" do it "sets params if passed it" do joined_at = 1.day.ago.at_beginning_of_minute.to_utc - user = UserBox.new.name("New").age(20).joined_at(Time.utc).create + user = UserFactory.new.name("New").age(20).joined_at(Time.utc).create user = SaveUser.update!(user, name: "New", age: 20, joined_at: joined_at) user.name.should eq "New" user.age.should eq 20 @@ -127,10 +127,10 @@ describe "Avram::SaveOperation" do end it "passes params to the block" do - user_box = UserBox.new.name("New").age(20).joined_at(Time.utc).create + user_factory = UserFactory.new.name("New").age(20).joined_at(Time.utc).create joined_at = 1.day.ago.at_beginning_of_minute.to_utc - SaveUser.update(user_box, name: "New", age: 20, joined_at: joined_at) do |_operation, user| + SaveUser.update(user_factory, name: "New", age: 20, joined_at: joined_at) do |_operation, user| user.name.should eq "New" user.age.should eq 20 user.joined_at.should eq joined_at @@ -149,7 +149,7 @@ describe "Avram::SaveOperation" do end it "treats nil changes as nil and not an empty string" do - user = UserBox.build + user = UserFactory.build operation = SaveUser.new(user) operation.name.value = nil @@ -196,7 +196,7 @@ describe "Avram::SaveOperation" do describe "initializer" do it "works with a record and named args" do - UserBox.new.name("Old Name").create + UserFactory.new.name("Old Name").create params = Avram::Params.new(name: "New Name") user = UserQuery.new.first @@ -264,7 +264,7 @@ describe "Avram::SaveOperation" do end it "returns the value from params for updates" do - user = UserBox.build + user = UserFactory.build params = {"name" => "New Name From Params"} avram_params = Avram::Params.new(params) @@ -287,7 +287,7 @@ describe "Avram::SaveOperation" do end it "uses the value if param is empty" do - user = UserBox.build + user = UserFactory.build operation = SaveUser.new(user, Avram::Params.new({} of String => String)) @@ -348,7 +348,7 @@ describe "Avram::SaveOperation" do end it "allows overriding updated_at and created_at on create" do - user = UserBox.new + user = UserFactory.new .created_at(Time.utc(2018, 1, 1, 10, 20, 30)) .updated_at(Time.utc(2018, 1, 1, 20, 30, 40)) .create @@ -498,7 +498,7 @@ describe "Avram::SaveOperation" do describe "updating with no changes" do it "works when there are no changes" do - UserBox.new.name("Old Name").create + UserFactory.new.name("Old Name").create user = UserQuery.new.first params = Avram::Params.new({} of String => String) SaveUser.update user, with: params do |operation, _record| @@ -507,7 +507,7 @@ describe "Avram::SaveOperation" do end it "returns true when there are no changes" do - UserBox.new.name("Old Name").create + UserFactory.new.name("Old Name").create user = UserQuery.new.first SaveUser.new(user).tap do |operation| operation.save.should be_true @@ -517,7 +517,7 @@ describe "Avram::SaveOperation" do describe ".update" do it "can create without params" do - post = PostBox.new.title("Original Title").create + post = PostFactory.new.title("Original Title").create ValidSaveOperationWithoutParams.update(post) do |operation, record| operation.saved?.should be_true record.title.should eq "My Title" @@ -526,7 +526,7 @@ describe "Avram::SaveOperation" do context "on success" do it "yields the operation and the updated record" do - UserBox.new.name("Old Name").create + UserFactory.new.name("Old Name").create user = UserQuery.new.first params = Avram::Params.new({"name" => "New Name"}) SaveUser.update user, with: params do |operation, record| @@ -536,7 +536,7 @@ describe "Avram::SaveOperation" do end it "updates updated_at" do - user = UserBox.new.updated_at(1.day.ago).create + user = UserFactory.new.updated_at(1.day.ago).create params = Avram::Params.new({"name" => "New Name"}) SaveUser.update user, with: params do |operation, record| operation.saved?.should be_true @@ -547,7 +547,7 @@ describe "Avram::SaveOperation" do context "on failure" do it "yields the operation and nil" do - UserBox.new.name("Old Name").create + UserFactory.new.name("Old Name").create user = UserQuery.new.first params = Avram::Params.new({"name" => ""}) SaveUser.update user, with: params do |operation, record| @@ -557,7 +557,7 @@ describe "Avram::SaveOperation" do end it "logs the failure" do - UserBox.new.name("Old Name").create + UserFactory.new.name("Old Name").create user = UserQuery.new.first Avram::SaveFailedLog.dexter.temp_config do |log_io| @@ -569,7 +569,7 @@ describe "Avram::SaveOperation" do context "with a uuid backed model" do it "doesn't generate a new uuid" do - line_item = LineItemBox.create + line_item = LineItemFactory.create SaveLineItem.update(line_item, Avram::Params.new({"name" => "Another pair of shoes"})) do |operation, record| operation.saved?.should be_true record.id.should eq line_item.id @@ -579,7 +579,7 @@ describe "Avram::SaveOperation" do context "when the default is false and the field is required" do it "is valid since 'false' is a valid Boolean value" do - user = UserBox.create &.nickname("oopsie").available_for_hire(false) + user = UserFactory.create &.nickname("oopsie").available_for_hire(false) params = Avram::Params.new({"nickname" => "falsey mcfalserson"}) SaveUserWithFalseValueValidations.update(user, params) do |operation, record| record.should_not eq nil @@ -594,14 +594,14 @@ describe "Avram::SaveOperation" do describe ".update!" do it "can create without params" do - post = PostBox.new.title("Original Title").create + post = PostFactory.new.title("Original Title").create post = ValidSaveOperationWithoutParams.update!(post) post.title.should eq "My Title" end context "on success" do it "updates and returns the record" do - UserBox.new.name("Old Name").create + UserFactory.new.name("Old Name").create user = UserQuery.new.first params = Avram::Params.new({"name" => "New Name"}) @@ -614,7 +614,7 @@ describe "Avram::SaveOperation" do context "on failure" do it "raises an exception" do - UserBox.new.name("Old Name").create + UserFactory.new.name("Old Name").create user = UserQuery.new.first params = Avram::Params.new({"name" => ""}) diff --git a/spec/preloading/preloading_belongs_to_spec.cr b/spec/preloading/preloading_belongs_to_spec.cr index 8a07019f9..f2ccfe3ce 100644 --- a/spec/preloading/preloading_belongs_to_spec.cr +++ b/spec/preloading/preloading_belongs_to_spec.cr @@ -10,8 +10,8 @@ describe "Preloading belongs_to associations" do it "works" do with_lazy_load(enabled: false) do Post::BaseQuery.times_called = 0 - post = PostBox.create - CommentBox.create &.post_id(post.id) + post = PostFactory.create + CommentFactory.create &.post_id(post.id) comments = Comment::BaseQuery.new.preload_post @@ -22,8 +22,8 @@ describe "Preloading belongs_to associations" do it "works with optional association" do with_lazy_load(enabled: false) do - employee = EmployeeBox.create - manager = ManagerBox.create + employee = EmployeeFactory.create + manager = ManagerFactory.create employees = Employee::BaseQuery.new.preload_manager employees.first.manager.should be_nil @@ -39,8 +39,8 @@ describe "Preloading belongs_to associations" do it "raises error if accessing association without preloading first" do with_lazy_load(enabled: false) do - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) comment = Comment::BaseQuery.find(comment.id) @@ -52,9 +52,9 @@ describe "Preloading belongs_to associations" do it "works with nested preloads" do with_lazy_load(enabled: false) do - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) - comment2 = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) + comment2 = CommentFactory.create &.post_id(post.id) comment = Comment::BaseQuery.new.preload_post(Post::BaseQuery.new.preload_comments).find(comment.id) @@ -63,8 +63,8 @@ describe "Preloading belongs_to associations" do end it "does not fail when getting results multiple times" do - post = PostBox.create - CommentBox.create &.post_id(post.id) + post = PostFactory.create + CommentFactory.create &.post_id(post.id) query = Comment::BaseQuery.new.preload_post @@ -72,15 +72,15 @@ describe "Preloading belongs_to associations" do end it "works with uuid foreign keys" do - item = LineItemBox.create - PriceBox.new.line_item_id(item.id).create + item = LineItemFactory.create + PriceFactory.new.line_item_id(item.id).create PriceQuery.new.preload_line_item.first.line_item.should eq item end it "lazy loads if nothing is preloaded" do - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) comment = Comment::BaseQuery.find(comment.id) @@ -98,8 +98,8 @@ describe "Preloading belongs_to associations" do context "with existing record" do it "works" do with_lazy_load(enabled: false) do - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) comment = Comment::BaseQuery.preload_post(comment) @@ -109,9 +109,9 @@ describe "Preloading belongs_to associations" do it "works with multiple" do with_lazy_load(enabled: false) do - post = PostBox.create - comment1 = CommentBox.create &.post_id(post.id) - comment2 = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment1 = CommentFactory.create &.post_id(post.id) + comment2 = CommentFactory.create &.post_id(post.id) comments = Comment::BaseQuery.preload_post([comment1, comment2]) @@ -122,9 +122,9 @@ describe "Preloading belongs_to associations" do it "works with custom query" do with_lazy_load(enabled: false) do - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) - comment2 = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) + comment2 = CommentFactory.create &.post_id(post.id) comment = Comment::BaseQuery.preload_post(comment, Post::BaseQuery.new.preload_comments) @@ -134,8 +134,8 @@ describe "Preloading belongs_to associations" do it "does not modify original record" do with_lazy_load(enabled: false) do - post = PostBox.create - original_comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + original_comment = CommentFactory.create &.post_id(post.id) Comment::BaseQuery.preload_post(original_comment) diff --git a/spec/preloading/preloading_has_many_spec.cr b/spec/preloading/preloading_has_many_spec.cr index 3f10fc62c..1bc908a03 100644 --- a/spec/preloading/preloading_has_many_spec.cr +++ b/spec/preloading/preloading_has_many_spec.cr @@ -10,8 +10,8 @@ describe "Preloading has_many associations" do it "works" do with_lazy_load(enabled: false) do Comment::BaseQuery.times_called = 0 - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) posts = Post::BaseQuery.new.preload_comments @@ -22,11 +22,11 @@ describe "Preloading has_many associations" do it "preserves additional criteria when used after adding a preload" do with_lazy_load(enabled: false) do - post = PostBox.create - another_post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + another_post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) - CommentBox.create &.post_id(another_post.id) # should not be preloaded + CommentFactory.create &.post_id(another_post.id) # should not be preloaded posts = Post::BaseQuery.new.preload_comments.limit(1) @@ -38,8 +38,8 @@ describe "Preloading has_many associations" do it "works with custom query" do with_lazy_load(enabled: false) do - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) posts = Post::BaseQuery.new.preload_comments( Comment::BaseQuery.new.id.not.eq(comment.id) @@ -51,8 +51,8 @@ describe "Preloading has_many associations" do it "works with UUID foreign keys" do with_lazy_load(enabled: false) do - item = LineItemBox.create - scan = ScanBox.create &.line_item_id(item.id) + item = LineItemFactory.create + scan = ScanFactory.create &.line_item_id(item.id) items = LineItem::BaseQuery.new.preload_scans @@ -62,8 +62,8 @@ describe "Preloading has_many associations" do it "works with nested preloads" do with_lazy_load(enabled: false) do - post = PostBox.create - CommentBox.create &.post_id(post.id) + post = PostFactory.create + CommentFactory.create &.post_id(post.id) posts = Post::BaseQuery.new.preload_comments( Comment::BaseQuery.new.preload_post @@ -75,7 +75,7 @@ describe "Preloading has_many associations" do it "raises error if accessing association without preloading first" do with_lazy_load(enabled: false) do - post = PostBox.create + post = PostFactory.create expect_raises Avram::LazyLoadError do post.comments @@ -85,7 +85,7 @@ describe "Preloading has_many associations" do it "uses an empty array if there are no associated records" do with_lazy_load(enabled: false) do - PostBox.create + PostFactory.create posts = Post::BaseQuery.new.preload_comments @@ -94,7 +94,7 @@ describe "Preloading has_many associations" do end it "does not fail when getting results multiple times" do - PostBox.create + PostFactory.create posts = Post::BaseQuery.new.preload_comments @@ -102,9 +102,9 @@ describe "Preloading has_many associations" do end it "does not fail when getting results multiple times with custom query" do - post = PostBox.create - _another_post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + _another_post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) posts = Post::BaseQuery.new.preload_comments( Comment::BaseQuery.new.id.not.eq(comment.id) @@ -115,8 +115,8 @@ describe "Preloading has_many associations" do it "uses preloaded records if available, even if lazy load is enabled" do with_lazy_load(enabled: true) do - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) posts = Post::BaseQuery.new.preload_comments( Comment::BaseQuery.new.id.not.eq(comment.id) @@ -127,8 +127,8 @@ describe "Preloading has_many associations" do end it "lazy loads if nothing is preloaded" do - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) posts = Post::BaseQuery.new @@ -146,8 +146,8 @@ describe "Preloading has_many associations" do context "with existing record" do it "works" do with_lazy_load(enabled: false) do - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) post = Post::BaseQuery.preload_comments(post) @@ -157,10 +157,10 @@ describe "Preloading has_many associations" do it "works with multiple" do with_lazy_load(enabled: false) do - post1 = PostBox.create - post2 = PostBox.create - comment1 = CommentBox.create &.post_id(post1.id) - comment2 = CommentBox.create &.post_id(post2.id) + post1 = PostFactory.create + post2 = PostFactory.create + comment1 = CommentFactory.create &.post_id(post1.id) + comment2 = CommentFactory.create &.post_id(post2.id) posts = Post::BaseQuery.preload_comments([post1, post2]) @@ -171,9 +171,9 @@ describe "Preloading has_many associations" do it "works with custom query" do with_lazy_load(enabled: false) do - post = PostBox.create - comment1 = CommentBox.create &.post_id(post.id).body("CUSTOM BODY") - CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment1 = CommentFactory.create &.post_id(post.id).body("CUSTOM BODY") + CommentFactory.create &.post_id(post.id) post = Post::BaseQuery.preload_comments(post, Comment::BaseQuery.new.body("CUSTOM BODY")) @@ -183,8 +183,8 @@ describe "Preloading has_many associations" do it "does not modify original record" do with_lazy_load(enabled: false) do - original_post = PostBox.create - CommentBox.create &.post_id(original_post.id) + original_post = PostFactory.create + CommentFactory.create &.post_id(original_post.id) Post::BaseQuery.preload_comments(original_post) diff --git a/spec/preloading/preloading_has_many_through_spec.cr b/spec/preloading/preloading_has_many_through_spec.cr index acb6a41db..40ec9e23c 100644 --- a/spec/preloading/preloading_has_many_through_spec.cr +++ b/spec/preloading/preloading_has_many_through_spec.cr @@ -10,12 +10,12 @@ describe "Preloading has_many through associations" do context "through is a has_many association that has a belongs_to relationship to target" do it "works" do with_lazy_load(enabled: false) do - tag = TagBox.create - TagBox.create # unused tag - post = PostBox.create - other_post = PostBox.create - TaggingBox.create &.tag_id(tag.id).post_id(post.id) - TaggingBox.create &.tag_id(tag.id).post_id(other_post.id) + tag = TagFactory.create + TagFactory.create # unused tag + post = PostFactory.create + other_post = PostFactory.create + TaggingFactory.create &.tag_id(tag.id).post_id(post.id) + TaggingFactory.create &.tag_id(tag.id).post_id(other_post.id) post_tags = Post::BaseQuery.new.preload_tags.results.first.tags @@ -26,12 +26,12 @@ describe "Preloading has_many through associations" do it "works with uuid foreign keys" do with_lazy_load(enabled: false) do - item = LineItemBox.create - other_item = LineItemBox.create - product = ProductBox.create - ProductBox.create # unused product - LineItemProductBox.create &.line_item_id(item.id).product_id(product.id) - LineItemProductBox.create &.line_item_id(other_item.id).product_id(product.id) + item = LineItemFactory.create + other_item = LineItemFactory.create + product = ProductFactory.create + ProductFactory.create # unused product + LineItemProductFactory.create &.line_item_id(item.id).product_id(product.id) + LineItemProductFactory.create &.line_item_id(other_item.id).product_id(product.id) item_products = LineItemQuery.new.preload_associated_products.results.first.associated_products @@ -41,7 +41,7 @@ describe "Preloading has_many through associations" do end it "does not fail when getting results multiple times" do - PostBox.create + PostFactory.create posts = Post::BaseQuery.new.preload_tags @@ -52,9 +52,9 @@ describe "Preloading has_many through associations" do context "through is a has_many association that has a has_many relationship to target" do it "works" do with_lazy_load(enabled: false) 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 customers = Manager::BaseQuery.new.preload_customers.find(manager.id).customers @@ -67,9 +67,9 @@ describe "Preloading has_many through associations" do context "through is a belongs_to association that has a belongs_to relationship to target" do it "works" do with_lazy_load(enabled: false) 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 managers = Customer::BaseQuery.new.preload_managers.find(customer.id).managers @@ -82,9 +82,9 @@ describe "Preloading has_many through associations" do context "with existing record" do it "works" do with_lazy_load(enabled: false) do - tag = TagBox.create - post = PostBox.create - TaggingBox.create &.tag_id(tag.id).post_id(post.id) + tag = TagFactory.create + post = PostFactory.create + TaggingFactory.create &.tag_id(tag.id).post_id(post.id) post = Post::BaseQuery.preload_tags(post) @@ -94,12 +94,12 @@ describe "Preloading has_many through associations" do it "works with multiple" do with_lazy_load(enabled: false) do - tag1 = TagBox.create - tag2 = TagBox.create - post1 = PostBox.create - post2 = PostBox.create - TaggingBox.create &.tag_id(tag1.id).post_id(post1.id) - TaggingBox.create &.tag_id(tag2.id).post_id(post2.id) + tag1 = TagFactory.create + tag2 = TagFactory.create + post1 = PostFactory.create + post2 = PostFactory.create + TaggingFactory.create &.tag_id(tag1.id).post_id(post1.id) + TaggingFactory.create &.tag_id(tag2.id).post_id(post2.id) posts = Post::BaseQuery.preload_tags([post1, post2]) @@ -110,12 +110,12 @@ describe "Preloading has_many through associations" do it "works with custom query" do with_lazy_load(enabled: false) do - manager = ManagerBox.create - employee1 = EmployeeBox.new.manager_id(manager.id).create - employee2 = EmployeeBox.new.manager_id(manager.id).create - customer1 = CustomerBox.new.employee_id(employee1.id).create - CustomerBox.new.employee_id(employee2.id).create - customer3 = CustomerBox.new.employee_id(employee1.id).create + manager = ManagerFactory.create + employee1 = EmployeeFactory.new.manager_id(manager.id).create + employee2 = EmployeeFactory.new.manager_id(manager.id).create + customer1 = CustomerFactory.new.employee_id(employee1.id).create + CustomerFactory.new.employee_id(employee2.id).create + customer3 = CustomerFactory.new.employee_id(employee1.id).create manager = Manager::BaseQuery.preload_customers(manager, Customer::BaseQuery.new.employee_id(employee1.id)) @@ -126,9 +126,9 @@ describe "Preloading has_many through associations" do it "does not modify original record" do with_lazy_load(enabled: false) do - tag = TagBox.create - original_post = PostBox.create - TaggingBox.create &.tag_id(tag.id).post_id(original_post.id) + tag = TagFactory.create + original_post = PostFactory.create + TaggingFactory.create &.tag_id(tag.id).post_id(original_post.id) Post::BaseQuery.preload_tags(original_post) diff --git a/spec/preloading/preloading_has_one_spec.cr b/spec/preloading/preloading_has_one_spec.cr index 666f02fae..2689a4592 100644 --- a/spec/preloading/preloading_has_one_spec.cr +++ b/spec/preloading/preloading_has_one_spec.cr @@ -9,8 +9,8 @@ end describe "Preloading has_one associations" do it "works" do with_lazy_load(enabled: false) do - admin = AdminBox.create - sign_in_credential = SignInCredentialBox.create &.user_id(admin.id) + admin = AdminFactory.create + sign_in_credential = SignInCredentialFactory.create &.user_id(admin.id) admin = Admin::BaseQuery.new.preload_sign_in_credential @@ -21,8 +21,8 @@ describe "Preloading has_one associations" do it "works with custom query and nested preload" do with_lazy_load(enabled: false) do SignInCredential::BaseQuery.times_called = 0 - user = UserBox.create - SignInCredentialBox.create &.user_id(user.id) + user = UserFactory.create + SignInCredentialFactory.create &.user_id(user.id) user = User::BaseQuery.new.preload_sign_in_credential( SignInCredential::BaseQuery.new.preload_user @@ -35,11 +35,11 @@ describe "Preloading has_one associations" do it "works with optional association" do with_lazy_load(enabled: false) do - UserBox.create + UserFactory.create user = User::BaseQuery.new.preload_sign_in_credential.first user.sign_in_credential.should be_nil - sign_in_credential = SignInCredentialBox.new.user_id(user.id).create + sign_in_credential = SignInCredentialFactory.new.user_id(user.id).create user = User::BaseQuery.new.preload_sign_in_credential.first user.sign_in_credential.should eq sign_in_credential end @@ -47,8 +47,8 @@ describe "Preloading has_one associations" do it "raises error if accessing association without preloading first" do with_lazy_load(enabled: false) do - admin = AdminBox.create - SignInCredentialBox.create &.user_id(admin.id) + admin = AdminFactory.create + SignInCredentialFactory.create &.user_id(admin.id) expect_raises Avram::LazyLoadError do admin.sign_in_credential @@ -57,7 +57,7 @@ describe "Preloading has_one associations" do end it "does not fail when getting results multiple times" do - AdminBox.create + AdminFactory.create admin = Admin::BaseQuery.new.preload_sign_in_credential @@ -65,8 +65,8 @@ describe "Preloading has_one associations" do end it "lazy loads if nothing is preloaded" do - admin = AdminBox.create - sign_in_credential = SignInCredentialBox.create &.user_id(admin.id) + admin = AdminFactory.create + sign_in_credential = SignInCredentialFactory.create &.user_id(admin.id) admin.sign_in_credential.should eq sign_in_credential end @@ -82,8 +82,8 @@ describe "Preloading has_one associations" do context "with existing record" do it "works" do with_lazy_load(enabled: false) do - admin = AdminBox.create - sign_in_credential = SignInCredentialBox.create &.user_id(admin.id) + admin = AdminFactory.create + sign_in_credential = SignInCredentialFactory.create &.user_id(admin.id) admin = Admin::BaseQuery.preload_sign_in_credential(admin) @@ -93,10 +93,10 @@ describe "Preloading has_one associations" do it "works with multiple" do with_lazy_load(enabled: false) do - admin = AdminBox.create - sign_in_credential = SignInCredentialBox.create &.user_id(admin.id) - admin2 = AdminBox.create - sign_in_credential2 = SignInCredentialBox.create &.user_id(admin2.id) + admin = AdminFactory.create + sign_in_credential = SignInCredentialFactory.create &.user_id(admin.id) + admin2 = AdminFactory.create + sign_in_credential2 = SignInCredentialFactory.create &.user_id(admin2.id) admins = Admin::BaseQuery.preload_sign_in_credential([admin, admin2]) @@ -107,8 +107,8 @@ describe "Preloading has_one associations" do it "works with custom query" do with_lazy_load(enabled: false) do - user = UserBox.create - sign_in_credential = SignInCredentialBox.create &.user_id(user.id) + user = UserFactory.create + sign_in_credential = SignInCredentialFactory.create &.user_id(user.id) user = UserQuery.preload_sign_in_credential(user, SignInCredential::BaseQuery.new.id.not.eq(sign_in_credential.id)) @@ -118,8 +118,8 @@ describe "Preloading has_one associations" do it "does not modify original record" do with_lazy_load(enabled: false) do - admin = AdminBox.create - SignInCredentialBox.create &.user_id(admin.id) + admin = AdminFactory.create + SignInCredentialFactory.create &.user_id(admin.id) Admin::BaseQuery.preload_sign_in_credential(admin) diff --git a/spec/query_associations_spec.cr b/spec/query_associations_spec.cr index a122138c4..9a7055512 100644 --- a/spec/query_associations_spec.cr +++ b/spec/query_associations_spec.cr @@ -36,14 +36,14 @@ end describe "Query associations" do it "can query associations" do - post_with_matching_comment = PostBox.create - CommentBox.new + post_with_matching_comment = PostFactory.create + CommentFactory.new .body("matching") .post_id(post_with_matching_comment.id) .create - post_without_matching_comment = PostBox.create - CommentBox + post_without_matching_comment = PostFactory.create + CommentFactory .new .body("not-matching") .post_id(post_without_matching_comment.id) @@ -59,14 +59,14 @@ describe "Query associations" do end it "can query associations with inner_join specified" do - post_with_matching_comment = PostBox.create - CommentBox.new + post_with_matching_comment = PostFactory.create + CommentFactory.new .body("matching") .post_id(post_with_matching_comment.id) .create - post_without_matching_comment = PostBox.create - CommentBox.new + post_without_matching_comment = PostFactory.create + CommentFactory.new .body("not-matching") .post_id(post_without_matching_comment.id) .create @@ -79,14 +79,14 @@ describe "Query associations" do end it "can query associations with left_join specified" do - post_with_matching_comment = PostBox.create - CommentBox.new + post_with_matching_comment = PostFactory.create + CommentFactory.new .body("matching") .post_id(post_with_matching_comment.id) .create - post_without_matching_comment = PostBox.create - CommentBox.new + post_without_matching_comment = PostFactory.create + CommentFactory.new .body("not-matching") .post_id(post_without_matching_comment.id) .create @@ -99,14 +99,14 @@ describe "Query associations" do end it "can query associations with right_join specified" do - post_with_matching_comment = PostBox.create - CommentBox.new + post_with_matching_comment = PostFactory.create + CommentFactory.new .body("matching") .post_id(post_with_matching_comment.id) .create - post_without_matching_comment = PostBox.create - CommentBox.new + post_without_matching_comment = PostFactory.create + CommentFactory.new .body("not-matching") .post_id(post_without_matching_comment.id) .create @@ -119,14 +119,14 @@ describe "Query associations" do end it "can query associations with full_join specified" do - post_with_matching_comment = PostBox.create - CommentBox.new + post_with_matching_comment = PostFactory.create + CommentFactory.new .body("matching") .post_id(post_with_matching_comment.id) .create - post_without_matching_comment = PostBox.create - CommentBox.new + post_without_matching_comment = PostFactory.create + CommentFactory.new .body("not-matching") .post_id(post_without_matching_comment.id) .create @@ -148,9 +148,9 @@ describe "Query associations" do end it "handles potential joins over the table queried" do - item = LineItemBox.create - product = ProductBox.create - line_item_product = LineItemProductBox.create &.line_item_id(item.id).product_id(product.id) + item = LineItemFactory.create + product = ProductFactory.create + line_item_product = LineItemProductFactory.create &.line_item_id(item.id).product_id(product.id) line_item_query = LineItemQuery.new .id(item.id) @@ -163,9 +163,9 @@ describe "Query associations" do end it "handles duplicate joins" do - item = LineItemBox.create - product = ProductBox.create - line_item_product = LineItemProductBox.create &.line_item_id(item.id).product_id(product.id) + item = LineItemFactory.create + product = ProductFactory.create + line_item_product = LineItemProductFactory.create &.line_item_id(item.id).product_id(product.id) line_item_query = LineItemQuery.new .id(item.id) diff --git a/spec/queryable_spec.cr b/spec/queryable_spec.cr index ee7f49635..0dac8b971 100644 --- a/spec/queryable_spec.cr +++ b/spec/queryable_spec.cr @@ -99,10 +99,10 @@ describe Avram::Queryable do describe "#distinct_on" do it "selects distinct on a specific column" do - UserBox.new.name("Purcell").age(22).create - UserBox.new.name("Purcell").age(84).create - UserBox.new.name("Griffiths").age(55).create - UserBox.new.name("Griffiths").age(75).create + UserFactory.new.name("Purcell").age(22).create + UserFactory.new.name("Purcell").age(84).create + UserFactory.new.name("Griffiths").age(55).create + UserFactory.new.name("Griffiths").age(75).create queryable = UserQuery.new.distinct_on(&.name).order_by(:name, :asc).order_by(:age, :asc) query = queryable.query @@ -129,8 +129,8 @@ describe Avram::Queryable do describe ".first" do it "gets the first row from the database" do - UserBox.new.name("First").create - UserBox.new.name("Last").create + UserFactory.new.name("First").create + UserFactory.new.name("Last").create UserQuery.first.name.should eq "First" end @@ -144,8 +144,8 @@ describe Avram::Queryable do describe "#first" do it "gets the first row from the database" do - UserBox.new.name("First").create - UserBox.new.name("Last").create + UserFactory.new.name("First").create + UserFactory.new.name("Last").create UserQuery.new.first.name.should eq "First" end @@ -159,8 +159,8 @@ describe Avram::Queryable do describe ".first?" do it "gets the first row from the database" do - UserBox.new.name("First").create - UserBox.new.name("Last").create + UserFactory.new.name("First").create + UserFactory.new.name("Last").create user = UserQuery.first? user.should_not be_nil @@ -174,8 +174,8 @@ describe Avram::Queryable do describe "#first?" do it "gets the first row from the database" do - UserBox.new.name("First").create - UserBox.new.name("Last").create + UserFactory.new.name("First").create + UserFactory.new.name("Last").create user = UserQuery.new.first? user_query = Avram::Events::QueryEvent.logged_events.last.query @@ -201,8 +201,8 @@ describe Avram::Queryable do describe ".last" do it "gets the last row from the database" do - UserBox.new.name("First").create - UserBox.new.name("Last").create + UserFactory.new.name("First").create + UserFactory.new.name("Last").create UserQuery.last.name.should eq "Last" end @@ -216,16 +216,16 @@ describe Avram::Queryable do describe "#last" do it "gets the last row from the database" do - UserBox.new.name("First").create - UserBox.new.name("Last").create + UserFactory.new.name("First").create + UserFactory.new.name("Last").create UserQuery.new.last.name.should eq "Last" end it "reverses the order of ordered queries" do - UserBox.new.name("Alpha").create - UserBox.new.name("Charlie").create - UserBox.new.name("Bravo").create + UserFactory.new.name("Alpha").create + UserFactory.new.name("Charlie").create + UserFactory.new.name("Bravo").create UserQuery.new.order_by(:name, :desc).last.name.should eq "Alpha" end @@ -239,8 +239,8 @@ describe Avram::Queryable do describe ".last?" do it "gets the last row from the database" do - UserBox.new.name("First").create - UserBox.new.name("Last").create + UserFactory.new.name("First").create + UserFactory.new.name("Last").create user = UserQuery.last? @@ -255,8 +255,8 @@ describe Avram::Queryable do describe "#last?" do it "gets the last row from the database" do - UserBox.new.name("First").create - UserBox.new.name("Last").create + UserFactory.new.name("First").create + UserFactory.new.name("Last").create user = UserQuery.new.last? user_query = Avram::Events::QueryEvent.logged_events.last.query @@ -282,7 +282,7 @@ describe Avram::Queryable do describe ".find" do it "gets the record with the given id" do - UserBox.create + UserFactory.create user = UserQuery.first UserQuery.find(user.id).should eq user @@ -309,7 +309,7 @@ describe Avram::Queryable do describe "#find" do it "gets the record with the given id" do - UserBox.create + UserFactory.create user = UserQuery.new.first UserQuery.new.find(user.id).should eq user @@ -334,7 +334,7 @@ describe Avram::Queryable do end it "doesn't mutate the query" do - user = UserBox.new.create + user = UserFactory.new.create query = UserQuery.new original_query_sql = query.to_sql @@ -360,7 +360,7 @@ describe Avram::Queryable do end it "accepts raw sql with bindings and chains with itself" do - user = UserBox.new.name("Mikias Abera").age(26).nickname("miki").create + user = UserFactory.new.name("Mikias Abera").age(26).nickname("miki").create users = UserQuery.new.where("name = ? AND age = ?", "Mikias Abera", 26).where(:nickname, "miki") users.query.statement.should eq "SELECT #{User::COLUMN_SQL} FROM users WHERE name = 'Mikias Abera' AND age = 26 AND nickname = $1" @@ -409,8 +409,8 @@ describe Avram::Queryable do end it "works while chaining" do - UserBox.create - UserBox.create + UserFactory.create + UserFactory.create users = UserQuery.new.name.desc_order.limit(1) users.query.statement.should eq "SELECT #{User::COLUMN_SQL} FROM users ORDER BY users.name DESC LIMIT 1" @@ -470,7 +470,7 @@ describe Avram::Queryable do describe "#none" do it "returns 0 records" do - UserBox.create + UserFactory.create query = UserQuery.new.none @@ -489,17 +489,17 @@ describe Avram::Queryable do describe "#select_min" do it "returns the minimum" do - UserBox.create &.age(2) - UserBox.create &.age(1) - UserBox.create &.age(3) + UserFactory.create &.age(2) + UserFactory.create &.age(1) + UserFactory.create &.age(3) min = UserQuery.new.age.select_min min.should eq 1 end it "works with chained where clauses" do - UserBox.create &.age(2) - UserBox.create &.age(1) - UserBox.create &.age(3) + UserFactory.create &.age(2) + UserFactory.create &.age(1) + UserFactory.create &.age(3) min = UserQuery.new.age.gte(2).age.select_min min.should eq 2 end @@ -521,17 +521,17 @@ describe Avram::Queryable do describe "#select_max" do it "returns the maximum" do - UserBox.create &.age(2) - UserBox.create &.age(1) - UserBox.create &.age(3) + UserFactory.create &.age(2) + UserFactory.create &.age(1) + UserFactory.create &.age(3) max = UserQuery.new.age.select_max max.should eq 3 end it "works with chained where clauses" do - UserBox.create &.age(2) - UserBox.create &.age(1) - UserBox.create &.age(3) + UserFactory.create &.age(2) + UserFactory.create &.age(1) + UserFactory.create &.age(3) max = UserQuery.new.age.lte(2).age.select_max max.should eq 2 end @@ -553,9 +553,9 @@ describe Avram::Queryable do describe "#select_average" do it "returns the average" do - UserBox.create &.age(2) - UserBox.create &.age(1) - UserBox.create &.age(3) + UserFactory.create &.age(2) + UserFactory.create &.age(1) + UserFactory.create &.age(3) average = UserQuery.new.age.select_average average.should eq 2 average.should be_a Float64 @@ -567,9 +567,9 @@ describe Avram::Queryable do end it "works with chained where clauses" do - UserBox.create &.age(2) - UserBox.create &.age(1) - UserBox.create &.age(3) + UserFactory.create &.age(2) + UserFactory.create &.age(1) + UserFactory.create &.age(3) average = UserQuery.new.age.gte(2).age.select_average average.should eq 2.5 end @@ -594,9 +594,9 @@ describe Avram::Queryable do describe "#select_sum" do it "works with chained where clauses" do - UserBox.create &.total_score(2000) - UserBox.create &.total_score(1000) - UserBox.create &.total_score(3000) + UserFactory.create &.total_score(2000) + UserFactory.create &.total_score(1000) + UserFactory.create &.total_score(3000) sum = UserQuery.new.total_score.gte(2000).total_score.select_sum sum.should eq 5000 end @@ -618,9 +618,9 @@ describe Avram::Queryable do describe "#select_sum for Int64 column" do it "returns the sum" do - UserBox.create &.total_score(2000) - UserBox.create &.total_score(1000) - UserBox.create &.total_score(3000) + UserFactory.create &.total_score(2000) + UserFactory.create &.total_score(1000) + UserFactory.create &.total_score(3000) sum = UserQuery.new.total_score.select_sum sum.should eq 6000 sum.should be_a Int64 @@ -637,9 +637,9 @@ describe Avram::Queryable do describe "#select_sum for Int32 column" do it "returns the sum" do - UserBox.create &.age(2) - UserBox.create &.age(1) - UserBox.create &.age(3) + UserFactory.create &.age(2) + UserFactory.create &.age(1) + UserFactory.create &.age(3) query_sum = UserQuery.new.age.select_sum query_sum.should eq 6 query_sum.should be_a Int64 @@ -656,8 +656,8 @@ describe Avram::Queryable do describe "#select_sum for Int16 column" do it "returns the sum" do - UserBox.create &.year_born(1990_i16) - UserBox.create &.year_born(1995_i16) + UserFactory.create &.year_born(1990_i16) + UserFactory.create &.year_born(1995_i16) query_sum = UserQuery.new.year_born.select_sum query_sum.should eq 3985 query_sum.should be_a Int64 @@ -676,7 +676,7 @@ describe Avram::Queryable do it "returns the sum" do scores = [100.4, 123.22] sum = scores.sum - scores.each { |score| UserBox.create &.average_score(score) } + scores.each { |score| UserFactory.create &.average_score(score) } query_sum = UserQuery.new.average_score.select_sum query_sum.should eq sum sum.should be_a Float64 @@ -696,13 +696,13 @@ describe Avram::Queryable do count = UserQuery.new.select_count count.should eq 0 - UserBox.create + UserFactory.create count = UserQuery.new.select_count count.should eq 1 end it "works with ORDER BY by removing the ordering" do - UserBox.create + UserFactory.create query = UserQuery.new.name.desc_order @@ -710,8 +710,8 @@ describe Avram::Queryable do end it "works with chained where" do - UserBox.new.age(30).create - UserBox.new.age(31).create + UserFactory.new.age(30).create + UserFactory.new.age(31).create query = UserQuery.new.age.gte(31) @@ -746,7 +746,7 @@ describe Avram::Queryable do describe "#not" do context "with an argument" do it "negates the given where condition as 'equal'" do - UserBox.new.name("Paul").create + UserFactory.new.name("Paul").create results = UserQuery.new.name.not.eq("not existing").results results.should eq UserQuery.new.results @@ -754,8 +754,8 @@ describe Avram::Queryable do results = UserQuery.new.name.not.eq("Paul").results results.should eq [] of User - UserBox.new.name("Alex").create - UserBox.new.name("Sarah").create + UserFactory.new.name("Alex").create + UserFactory.new.name("Sarah").create results = UserQuery.new.name.lower.not.eq("alex").results results.map(&.name).should eq ["Paul", "Sarah"] end @@ -763,15 +763,15 @@ describe Avram::Queryable do context "with no arguments" do it "negates any previous condition" do - UserBox.new.name("Paul").create + UserFactory.new.name("Paul").create results = UserQuery.new.name.not.eq("Paul").results results.should eq [] of User end it "can be used with operators" do - UserBox.new.age(33).name("Joyce").create - UserBox.new.age(34).name("Jil").create + UserFactory.new.age(33).name("Joyce").create + UserFactory.new.age(34).name("Jil").create results = UserQuery.new.age.not.gt(33).results results.map(&.name).should eq ["Joyce"] @@ -790,7 +790,7 @@ describe Avram::Queryable do describe "#in" do it "gets records with ids in an array" do - UserBox.new.name("Mikias").create + UserFactory.new.name("Mikias").create user = UserQuery.new.first results = UserQuery.new.id.in([user.id]) @@ -798,7 +798,7 @@ describe Avram::Queryable do end it "gets records with name not in an array" do - UserBox.new.name("Mikias") + UserFactory.new.name("Mikias") results = UserQuery.new.name.not.in(["Mikias"]) results.map(&.name).should eq [] of String @@ -816,8 +816,8 @@ describe Avram::Queryable do describe "#join methods for associations" do it "inner join on belongs to" do - post = PostBox.create - CommentBox.new.post_id(post.id).create + post = PostFactory.create + CommentFactory.new.post_id(post.id).create query = Comment::BaseQuery.new.join_post query.to_sql.should eq ["SELECT comments.custom_id, comments.created_at, comments.updated_at, comments.body, comments.post_id FROM comments INNER JOIN posts ON comments.post_id = posts.custom_id"] @@ -836,8 +836,8 @@ describe Avram::Queryable do end it "inner join on has many" do - post = PostBox.create - comment = CommentBox.new.post_id(post.id).create + post = PostFactory.create + comment = CommentFactory.new.post_id(post.id).create query = Post::BaseQuery.new.join_comments query.to_sql.should eq ["SELECT posts.custom_id, posts.created_at, posts.updated_at, posts.title, posts.published_at FROM posts INNER JOIN comments ON posts.custom_id = comments.post_id"] @@ -856,9 +856,9 @@ describe Avram::Queryable do end it "multiple inner joins on has many through" do - post = PostBox.create - tag = TagBox.create - TaggingBox.new.post_id(post.id).tag_id(tag.id).create + post = PostFactory.create + tag = TagFactory.create + TaggingFactory.new.post_id(post.id).tag_id(tag.id).create query = Post::BaseQuery.new.join_tags query.to_sql.should eq ["SELECT posts.custom_id, posts.created_at, posts.updated_at, posts.title, posts.published_at FROM posts INNER JOIN taggings ON posts.custom_id = taggings.post_id INNER JOIN tags ON taggings.tag_id = tags.custom_id"] @@ -879,7 +879,7 @@ describe Avram::Queryable do describe "#left_join methods for associations" do it "left join on belongs to" do - employee = EmployeeBox.create + employee = EmployeeFactory.create query = Employee::BaseQuery.new.left_join_manager query.to_sql.should eq ["SELECT employees.id, employees.created_at, employees.updated_at, employees.name, employees.manager_id FROM employees LEFT JOIN managers ON employees.manager_id = managers.id"] @@ -898,7 +898,7 @@ describe Avram::Queryable do end it "left join on has many" do - post = PostBox.create + post = PostFactory.create query = Post::BaseQuery.new.left_join_comments query.to_sql.should eq ["SELECT posts.custom_id, posts.created_at, posts.updated_at, posts.title, posts.published_at FROM posts LEFT JOIN comments ON posts.custom_id = comments.post_id"] @@ -917,7 +917,7 @@ describe Avram::Queryable do end it "multiple left joins on has many through" do - post = PostBox.create + post = PostFactory.create query = Post::BaseQuery.new.left_join_tags query.to_sql.should eq ["SELECT posts.custom_id, posts.created_at, posts.updated_at, posts.title, posts.published_at FROM posts LEFT JOIN taggings ON posts.custom_id = taggings.post_id LEFT JOIN tags ON taggings.tag_id = tags.custom_id"] @@ -939,7 +939,7 @@ describe Avram::Queryable do context "when querying jsonb" do describe "simple where query" do it "returns 1 result" do - blob = BlobBox.new.doc(JSON::Any.new({"foo" => JSON::Any.new("bar")})).create + blob = BlobFactory.new.doc(JSON::Any.new({"foo" => JSON::Any.new("bar")})).create query = JSONQuery.new.static_foo query.to_sql.should eq ["SELECT blobs.id, blobs.created_at, blobs.updated_at, blobs.doc FROM blobs WHERE blobs.doc = $1", "{\"foo\":\"bar\"}"] @@ -963,7 +963,7 @@ describe Avram::Queryable do context "when querying arrays" do describe "simple where query" do it "returns 1 result" do - bucket = BucketBox.new.names(["pumpkin", "zucchini"]).create + bucket = BucketFactory.new.names(["pumpkin", "zucchini"]).create query = BucketQuery.new.names(["pumpkin", "zucchini"]) query.to_sql.should eq ["SELECT #{Bucket::COLUMN_SQL} FROM buckets WHERE buckets.names = $1", "{\"pumpkin\",\"zucchini\"}"] @@ -975,7 +975,7 @@ describe Avram::Queryable do describe ".truncate" do it "truncates the table" do - 10.times { UserBox.create } + 10.times { UserFactory.create } UserQuery.new.select_count.should eq 10 UserQuery.truncate UserQuery.new.select_count.should eq 0 @@ -984,8 +984,8 @@ describe Avram::Queryable do describe "#update" do it "updates records when wheres are added" do - UserBox.create &.available_for_hire(false) - UserBox.create &.available_for_hire(false) + UserFactory.create &.available_for_hire(false) + UserFactory.create &.available_for_hire(false) updated_count = ChainedQuery.new.update(available_for_hire: true) @@ -996,8 +996,8 @@ describe Avram::Queryable do end it "updates some records when wheres are added" do - helen = UserBox.create &.available_for_hire(false).name("Helen") - kate = UserBox.create &.available_for_hire(false).name("Kate") + helen = UserFactory.create &.available_for_hire(false).name("Helen") + kate = UserFactory.create &.available_for_hire(false).name("Kate") updated_count = ChainedQuery.new.name("Helen").update(available_for_hire: true) @@ -1007,7 +1007,7 @@ describe Avram::Queryable do end it "only sets columns to `nil` if explicitly set" do - richard = UserBox.create &.name("Richard").nickname("Rich") + richard = UserFactory.create &.name("Richard").nickname("Rich") updated_count = ChainedQuery.new.update(nickname: nil) @@ -1018,7 +1018,7 @@ describe Avram::Queryable do end it "works with JSON" do - blob = BlobBox.create &.doc(JSON::Any.new({"updated" => JSON::Any.new(true)})) + blob = BlobFactory.create &.doc(JSON::Any.new({"updated" => JSON::Any.new(true)})) updated_doc = JSON::Any.new({"updated" => JSON::Any.new(false)}) updated_count = Blob::BaseQuery.new.update(doc: updated_doc) @@ -1029,7 +1029,7 @@ describe Avram::Queryable do end it "works with arrays" do - bucket = BucketBox.create &.names(["Luke"]) + bucket = BucketFactory.create &.names(["Luke"]) updated_count = Bucket::BaseQuery.new.update(names: ["Rey"]) @@ -1041,10 +1041,10 @@ describe Avram::Queryable do describe "#delete" do it "deletes user records that are young" do - UserBox.new.name("Tony").age(48).create - UserBox.new.name("Peter").age(15).create - UserBox.new.name("Bruce").age(49).create - UserBox.new.name("Wanda").age(17).create + UserFactory.new.name("Tony").age(48).create + UserFactory.new.name("Peter").age(15).create + UserFactory.new.name("Bruce").age(49).create + UserFactory.new.name("Wanda").age(17).create ChainedQuery.new.select_count.should eq 4 # use the glove to remove half of them @@ -1054,8 +1054,8 @@ describe Avram::Queryable do end it "delete all records since no where clause is specified" do - UserBox.new.name("Steve").age(90).create - UserBox.new.name("Nick").age(66).create + UserFactory.new.name("Steve").age(90).create + UserFactory.new.name("Nick").age(66).create result = ChainedQuery.new.delete result.should eq 2 @@ -1112,10 +1112,10 @@ describe Avram::Queryable do end it "returns separate results than the original query" do - UserBox.create &.name("Purcell").age(22) - UserBox.create &.name("Purcell").age(84) - UserBox.create &.name("Griffiths").age(55) - UserBox.create &.name("Griffiths").age(75) + UserFactory.create &.name("Purcell").age(22) + UserFactory.create &.name("Purcell").age(84) + UserFactory.create &.name("Griffiths").age(55) + UserFactory.create &.name("Griffiths").age(75) original_query = ChainedQuery.new.named("Purcell") new_query = original_query.clone.age.gt(30) @@ -1125,8 +1125,8 @@ describe Avram::Queryable do end it "clones joined queries" do - post = PostBox.create - CommentBox.create &.post_id(post.id) + post = PostFactory.create + CommentFactory.create &.post_id(post.id) original_query = Post::BaseQuery.new.where_comments(Comment::BaseQuery.new.created_at.asc_order) new_query = original_query.clone.select_count @@ -1137,8 +1137,8 @@ describe Avram::Queryable do it "clones preloads" do Avram.temp_config(lazy_load_enabled: false) do - post = PostBox.create - comment = CommentBox.create &.post_id(post.id) + post = PostFactory.create + comment = CommentFactory.create &.post_id(post.id) posts = Post::BaseQuery.new.preload_comments cloned_posts = posts.clone posts.first.comments.should eq([comment]) @@ -1148,10 +1148,10 @@ describe Avram::Queryable do it "clones nested preloads" do Avram.temp_config(lazy_load_enabled: false) do - item = LineItemBox.create - PriceBox.create &.line_item_id(item.id).in_cents(500) - product = ProductBox.create - LineItemProductBox.create &.line_item_id(item.id).product_id(product.id) + item = LineItemFactory.create + PriceFactory.create &.line_item_id(item.id).in_cents(500) + product = ProductFactory.create + LineItemProductFactory.create &.line_item_id(item.id).product_id(product.id) products = Product::BaseQuery.new .preload_line_items(LineItem::BaseQuery.new.preload_price) @@ -1173,7 +1173,7 @@ describe Avram::Queryable do it "queries between dates" do start_date = 1.week.ago end_date = 1.day.ago - post = PostBox.create &.published_at(3.days.ago) + post = PostFactory.create &.published_at(3.days.ago) posts = Post::BaseQuery.new.published_at.between(start_date, end_date) posts.query.statement.should eq "SELECT posts.custom_id, posts.created_at, posts.updated_at, posts.title, posts.published_at FROM posts WHERE posts.published_at >= $1 AND posts.published_at <= $2" @@ -1182,7 +1182,7 @@ describe Avram::Queryable do end it "queries between numbers" do - company = CompanyBox.create &.sales(50) + company = CompanyFactory.create &.sales(50) companies = Company::BaseQuery.new.sales.between(1, 100) companies.query.statement.should eq "SELECT companies.id, companies.created_at, companies.updated_at, companies.sales, companies.earnings FROM companies WHERE companies.sales >= $1 AND companies.sales <= $2" @@ -1191,7 +1191,7 @@ describe Avram::Queryable do end it "queries between floats" do - company = CompanyBox.create &.earnings(300.45) + company = CompanyFactory.create &.earnings(300.45) companies = Company::BaseQuery.new.earnings.between(123.45, 678.901) companies.query.statement.should eq "SELECT companies.id, companies.created_at, companies.updated_at, companies.sales, companies.earnings FROM companies WHERE companies.earnings >= $1 AND companies.earnings <= $2" @@ -1211,9 +1211,9 @@ describe Avram::Queryable do describe "#group" do it "groups" do - UserBox.create &.age(25).name("Michael") - UserBox.create &.age(25).name("Dwight") - UserBox.create &.age(21).name("Jim") + UserFactory.create &.age(25).name("Michael") + UserFactory.create &.age(25).name("Dwight") + UserFactory.create &.age(21).name("Jim") users = UserQuery.new.group(&.age).group(&.id) users.query.statement.should eq "SELECT #{User::COLUMN_SQL} FROM users GROUP BY users.age, users.id" @@ -1241,8 +1241,8 @@ describe Avram::Queryable do context "queries joining with has_one" do describe "when you query from the belongs_to side" do it "returns a record" do - line_item = LineItemBox.create &.name("Thing 1") - price = PriceBox.create &.in_cents(100).line_item_id(line_item.id) + line_item = LineItemFactory.create &.name("Thing 1") + price = PriceFactory.create &.in_cents(100).line_item_id(line_item.id) query = PriceQuery.new.where_line_item(LineItemQuery.new.name("Thing 1")) query.first.should eq price @@ -1251,8 +1251,8 @@ describe Avram::Queryable do describe "when you query from the has_one side" do it "returns a record" do - line_item = LineItemBox.create &.name("Thing 1") - PriceBox.create &.in_cents(100).line_item_id(line_item.id) + line_item = LineItemFactory.create &.name("Thing 1") + PriceFactory.create &.in_cents(100).line_item_id(line_item.id) query = LineItemQuery.new.where_price(PriceQuery.new.in_cents(100)) query.first.should eq line_item diff --git a/spec/soft_delete_spec.cr b/spec/soft_delete_spec.cr index 21c5dc6c3..74d503841 100644 --- a/spec/soft_delete_spec.cr +++ b/spec/soft_delete_spec.cr @@ -7,7 +7,7 @@ end describe "Avram soft delete" do describe "models" do it "allows soft deleting a record" do - item = SoftDeletableItemBox.create &.kept + item = SoftDeletableItemFactory.create &.kept item = item.soft_delete @@ -15,7 +15,7 @@ describe "Avram soft delete" do end it "allows restoring a soft deleted record" do - item = SoftDeletableItemBox.create &.soft_deleted + item = SoftDeletableItemFactory.create &.soft_deleted item = item.restore @@ -23,7 +23,7 @@ describe "Avram soft delete" do end it "allows checking if a record is soft deleted" do - item = SoftDeletableItemBox.create &.kept + item = SoftDeletableItemFactory.create &.kept item.soft_deleted?.should be_false item = item.soft_delete @@ -34,8 +34,8 @@ describe "Avram soft delete" do describe "queries" do it "can get only kept records" do - kept_item = SoftDeletableItemBox.create &.kept - SoftDeletableItemBox.create &.soft_deleted + kept_item = SoftDeletableItemFactory.create &.kept + SoftDeletableItemFactory.create &.soft_deleted SoftDeletableItemQuery.new.only_soft_deleted.only_kept.results.should eq([ kept_item, @@ -43,8 +43,8 @@ describe "Avram soft delete" do end it "can get only soft deleted records" do - SoftDeletableItemBox.create &.kept - soft_deleted_item = SoftDeletableItemBox.create &.soft_deleted + SoftDeletableItemFactory.create &.kept + soft_deleted_item = SoftDeletableItemFactory.create &.soft_deleted SoftDeletableItemQuery.new.only_kept.only_soft_deleted.results.should eq([ soft_deleted_item, @@ -52,8 +52,8 @@ describe "Avram soft delete" do end it "can get soft deleted and kept records" do - kept_item = SoftDeletableItemBox.create &.kept - soft_deleted_item = SoftDeletableItemBox.create &.soft_deleted + kept_item = SoftDeletableItemFactory.create &.kept + soft_deleted_item = SoftDeletableItemFactory.create &.soft_deleted SoftDeletableItemQuery.new.only_kept.with_soft_deleted.results.should eq([ kept_item, @@ -62,8 +62,8 @@ describe "Avram soft delete" do end it "can bulk soft delete" do - kept_item = SoftDeletableItemBox.create &.kept - soft_deleted_item = SoftDeletableItemBox.create &.soft_deleted + kept_item = SoftDeletableItemFactory.create &.kept + soft_deleted_item = SoftDeletableItemFactory.create &.soft_deleted num_restored = SoftDeletableItemQuery.new.soft_delete @@ -73,8 +73,8 @@ describe "Avram soft delete" do end it "can bulk restore" do - kept_item = SoftDeletableItemBox.create &.kept - soft_deleted_item = SoftDeletableItemBox.create &.soft_deleted + kept_item = SoftDeletableItemFactory.create &.kept + soft_deleted_item = SoftDeletableItemFactory.create &.soft_deleted num_restored = SoftDeletableItemQuery.new.restore diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 7500e3bb7..8aaba2726 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -3,8 +3,8 @@ require "spec" require "../src/avram" require "./support/models/base_model" require "./support/models/**" -require "./support/boxes/base_box" -require "./support/boxes/**" +require "./support/factories/base_factory" +require "./support/factories/**" require "./support/**" require "../config/*" require "../db/migrations/**" diff --git a/spec/support/boxes/line_items_products_box.cr b/spec/support/boxes/line_items_products_box.cr deleted file mode 100644 index bfc066e04..000000000 --- a/spec/support/boxes/line_items_products_box.cr +++ /dev/null @@ -1,4 +0,0 @@ -class LineItemProductBox < BaseBox - def initialize - end -end diff --git a/spec/support/boxes/plain_model_box.cr b/spec/support/boxes/plain_model_box.cr deleted file mode 100644 index 140477075..000000000 --- a/spec/support/boxes/plain_model_box.cr +++ /dev/null @@ -1,4 +0,0 @@ -class PlainModelBox < BaseBox - def initialize - end -end diff --git a/spec/support/boxes/product_box.cr b/spec/support/boxes/product_box.cr deleted file mode 100644 index 5f1346336..000000000 --- a/spec/support/boxes/product_box.cr +++ /dev/null @@ -1,4 +0,0 @@ -class ProductBox < BaseBox - def initialize - end -end diff --git a/spec/support/boxes/sign_in_credential_box.cr b/spec/support/boxes/sign_in_credential_box.cr deleted file mode 100644 index 1b8c9a690..000000000 --- a/spec/support/boxes/sign_in_credential_box.cr +++ /dev/null @@ -1,4 +0,0 @@ -class SignInCredentialBox < BaseBox - def initialize - end -end diff --git a/spec/support/boxes/tagging_box.cr b/spec/support/boxes/tagging_box.cr deleted file mode 100644 index a6f7c65d6..000000000 --- a/spec/support/boxes/tagging_box.cr +++ /dev/null @@ -1,2 +0,0 @@ -class TaggingBox < BaseBox -end diff --git a/spec/support/boxes/admin_box.cr b/spec/support/factories/admin_factory.cr similarity index 57% rename from spec/support/boxes/admin_box.cr rename to spec/support/factories/admin_factory.cr index 93a0b24d1..fc1ac07d3 100644 --- a/spec/support/boxes/admin_box.cr +++ b/spec/support/factories/admin_factory.cr @@ -1,4 +1,4 @@ -class AdminBox < BaseBox +class AdminFactory < BaseFactory def initialize name "Admin" end diff --git a/spec/support/boxes/base_box.cr b/spec/support/factories/base_factory.cr similarity index 73% rename from spec/support/boxes/base_box.cr rename to spec/support/factories/base_factory.cr index 755f8c012..c536d826d 100644 --- a/spec/support/boxes/base_box.cr +++ b/spec/support/factories/base_factory.cr @@ -1,4 +1,4 @@ -abstract class BaseBox < Avram::Box +abstract class BaseFactory < Avram::Factory def self.build new.build end diff --git a/spec/support/boxes/blob_box.cr b/spec/support/factories/blob_factory.cr similarity index 71% rename from spec/support/boxes/blob_box.cr rename to spec/support/factories/blob_factory.cr index 71f7d0738..5ba355775 100644 --- a/spec/support/boxes/blob_box.cr +++ b/spec/support/factories/blob_factory.cr @@ -1,4 +1,4 @@ -class BlobBox < BaseBox +class BlobFactory < BaseFactory def initialize doc JSON::Any.new({"foo" => JSON::Any.new("bar")}) end diff --git a/spec/support/boxes/bucket_box.cr b/spec/support/factories/bucket_factory.cr similarity index 87% rename from spec/support/boxes/bucket_box.cr rename to spec/support/factories/bucket_factory.cr index fceb38b98..c5f3d030c 100644 --- a/spec/support/boxes/bucket_box.cr +++ b/spec/support/factories/bucket_factory.cr @@ -1,4 +1,4 @@ -class BucketBox < BaseBox +class BucketFactory < BaseFactory def initialize bools [true, false] small_numbers [1_i16, 2_i16] diff --git a/spec/support/boxes/business_box.cr b/spec/support/factories/business_factory.cr similarity index 55% rename from spec/support/boxes/business_box.cr rename to spec/support/factories/business_factory.cr index a3339b297..4f6d626f6 100644 --- a/spec/support/boxes/business_box.cr +++ b/spec/support/factories/business_factory.cr @@ -1,4 +1,4 @@ -class BusinessBox < BaseBox +class BusinessFactory < BaseFactory def initialize name "My Biz" end diff --git a/spec/support/boxes/comment_box.cr b/spec/support/factories/comment_factory.cr similarity index 61% rename from spec/support/boxes/comment_box.cr rename to spec/support/factories/comment_factory.cr index 85f3cc3b0..3f0a8b235 100644 --- a/spec/support/boxes/comment_box.cr +++ b/spec/support/factories/comment_factory.cr @@ -1,4 +1,4 @@ -class CommentBox < BaseBox +class CommentFactory < BaseFactory def initialize body "Best comment ever" end diff --git a/spec/support/boxes/company_box.cr b/spec/support/factories/company_factory.cr similarity index 65% rename from spec/support/boxes/company_box.cr rename to spec/support/factories/company_factory.cr index 607826612..a22d0cef1 100644 --- a/spec/support/boxes/company_box.cr +++ b/spec/support/factories/company_factory.cr @@ -1,4 +1,4 @@ -class CompanyBox < BaseBox +class CompanyFactory < BaseFactory def initialize sales Int64::MAX earnings 1.0 diff --git a/spec/support/boxes/customer_box.cr b/spec/support/factories/customer_factory.cr similarity index 63% rename from spec/support/boxes/customer_box.cr rename to spec/support/factories/customer_factory.cr index 94281d3d6..14757512b 100644 --- a/spec/support/boxes/customer_box.cr +++ b/spec/support/factories/customer_factory.cr @@ -1,4 +1,4 @@ -class CustomerBox < Avram::Box +class CustomerFactory < BaseFactory def initialize name sequence("test-customer") end diff --git a/spec/support/boxes/email_address_box.cr b/spec/support/factories/email_address_factory.cr similarity index 56% rename from spec/support/boxes/email_address_box.cr rename to spec/support/factories/email_address_factory.cr index ef379f579..7f8abb83e 100644 --- a/spec/support/boxes/email_address_box.cr +++ b/spec/support/factories/email_address_factory.cr @@ -1,4 +1,4 @@ -class EmailAddressBox < BaseBox +class EmailAddressFactory < BaseFactory def initialize address "foo@bar.com" end diff --git a/spec/support/boxes/employee_box.cr b/spec/support/factories/employee_factory.cr similarity index 60% rename from spec/support/boxes/employee_box.cr rename to spec/support/factories/employee_factory.cr index 913943032..5f81f23fd 100644 --- a/spec/support/boxes/employee_box.cr +++ b/spec/support/factories/employee_factory.cr @@ -1,4 +1,4 @@ -class EmployeeBox < BaseBox +class EmployeeFactory < BaseFactory def initialize name "Humble Employee" end diff --git a/spec/support/boxes/issue_box.cr b/spec/support/factories/issue_factory.cr similarity index 85% rename from spec/support/boxes/issue_box.cr rename to spec/support/factories/issue_factory.cr index 76fb5e3be..8f3d4d0d0 100644 --- a/spec/support/boxes/issue_box.cr +++ b/spec/support/factories/issue_factory.cr @@ -1,4 +1,4 @@ -class IssueBox < BaseBox +class IssueFactory < BaseFactory def initialize status Issue::Status.new(:opened) role Issue::Role.new(:issue) diff --git a/spec/support/boxes/line_item_box.cr b/spec/support/factories/line_item_factory.cr similarity index 60% rename from spec/support/boxes/line_item_box.cr rename to spec/support/factories/line_item_factory.cr index e5673b120..76ac01c56 100644 --- a/spec/support/boxes/line_item_box.cr +++ b/spec/support/factories/line_item_factory.cr @@ -1,4 +1,4 @@ -class LineItemBox < BaseBox +class LineItemFactory < BaseFactory def initialize name "A pair of shoes" end diff --git a/spec/support/factories/line_items_products_factory.cr b/spec/support/factories/line_items_products_factory.cr new file mode 100644 index 000000000..b406a2966 --- /dev/null +++ b/spec/support/factories/line_items_products_factory.cr @@ -0,0 +1,4 @@ +class LineItemProductFactory < BaseFactory + def initialize + end +end diff --git a/spec/support/boxes/manager_box.cr b/spec/support/factories/manager_factory.cr similarity index 58% rename from spec/support/boxes/manager_box.cr rename to spec/support/factories/manager_factory.cr index f8b8c9dcf..b93ab0224 100644 --- a/spec/support/boxes/manager_box.cr +++ b/spec/support/factories/manager_factory.cr @@ -1,4 +1,4 @@ -class ManagerBox < BaseBox +class ManagerFactory < BaseFactory def initialize name "Mr. Manager" end diff --git a/spec/support/boxes/menu_option_box.cr b/spec/support/factories/menu_option_factory.cr similarity index 64% rename from spec/support/boxes/menu_option_box.cr rename to spec/support/factories/menu_option_factory.cr index d93b2329f..7b21eaeee 100644 --- a/spec/support/boxes/menu_option_box.cr +++ b/spec/support/factories/menu_option_factory.cr @@ -1,4 +1,4 @@ -class MenuOptionBox < BaseBox +class MenuOptionFactory < BaseFactory def initialize title "Option" option_value 1_i16 diff --git a/spec/support/factories/plain_model_factory.cr b/spec/support/factories/plain_model_factory.cr new file mode 100644 index 000000000..764a28d06 --- /dev/null +++ b/spec/support/factories/plain_model_factory.cr @@ -0,0 +1,4 @@ +class PlainModelFactory < BaseFactory + def initialize + end +end diff --git a/spec/support/boxes/post_box.cr b/spec/support/factories/post_factory.cr similarity index 62% rename from spec/support/boxes/post_box.cr rename to spec/support/factories/post_factory.cr index 315f1505b..55061c80c 100644 --- a/spec/support/boxes/post_box.cr +++ b/spec/support/factories/post_factory.cr @@ -1,4 +1,4 @@ -class PostBox < BaseBox +class PostFactory < BaseFactory def initialize title "My Cool Title" end diff --git a/spec/support/boxes/price_box.cr b/spec/support/factories/price_factory.cr similarity index 56% rename from spec/support/boxes/price_box.cr rename to spec/support/factories/price_factory.cr index 790c6b262..d55c0b5d4 100644 --- a/spec/support/boxes/price_box.cr +++ b/spec/support/factories/price_factory.cr @@ -1,4 +1,4 @@ -class PriceBox < BaseBox +class PriceFactory < BaseFactory def initialize in_cents 99 end diff --git a/spec/support/factories/product_factory.cr b/spec/support/factories/product_factory.cr new file mode 100644 index 000000000..fb1718061 --- /dev/null +++ b/spec/support/factories/product_factory.cr @@ -0,0 +1,4 @@ +class ProductFactory < BaseFactory + def initialize + end +end diff --git a/spec/support/boxes/scan_box.cr b/spec/support/factories/scan_factory.cr similarity index 61% rename from spec/support/boxes/scan_box.cr rename to spec/support/factories/scan_factory.cr index 2f8beb1ef..784530641 100644 --- a/spec/support/boxes/scan_box.cr +++ b/spec/support/factories/scan_factory.cr @@ -1,4 +1,4 @@ -class ScanBox < BaseBox +class ScanFactory < BaseFactory def initialize scanned_at Time.utc end diff --git a/spec/support/factories/sign_in_credential_factory.cr b/spec/support/factories/sign_in_credential_factory.cr new file mode 100644 index 000000000..c39a6b4fd --- /dev/null +++ b/spec/support/factories/sign_in_credential_factory.cr @@ -0,0 +1,4 @@ +class SignInCredentialFactory < BaseFactory + def initialize + end +end diff --git a/spec/support/boxes/soft_deletable_item_box.cr b/spec/support/factories/soft_deletable_item_factory.cr similarity index 68% rename from spec/support/boxes/soft_deletable_item_box.cr rename to spec/support/factories/soft_deletable_item_factory.cr index b2f475677..9d96b8e99 100644 --- a/spec/support/boxes/soft_deletable_item_box.cr +++ b/spec/support/factories/soft_deletable_item_factory.cr @@ -1,4 +1,4 @@ -class SoftDeletableItemBox < BaseBox +class SoftDeletableItemFactory < BaseFactory def kept soft_deleted_at nil end diff --git a/spec/support/boxes/tag_box.cr b/spec/support/factories/tag_factory.cr similarity index 63% rename from spec/support/boxes/tag_box.cr rename to spec/support/factories/tag_factory.cr index 27227d6d2..1fde3a867 100644 --- a/spec/support/boxes/tag_box.cr +++ b/spec/support/factories/tag_factory.cr @@ -1,4 +1,4 @@ -class TagBox < BaseBox +class TagFactory < BaseFactory def initialize name sequence("name") end diff --git a/spec/support/factories/tagging_factory.cr b/spec/support/factories/tagging_factory.cr new file mode 100644 index 000000000..8c95f5ed5 --- /dev/null +++ b/spec/support/factories/tagging_factory.cr @@ -0,0 +1,2 @@ +class TaggingFactory < BaseFactory +end diff --git a/spec/support/boxes/user_box.cr b/spec/support/factories/user_factory.cr similarity index 92% rename from spec/support/boxes/user_box.cr rename to spec/support/factories/user_factory.cr index 9fa58c924..9be3bafb4 100644 --- a/spec/support/boxes/user_box.cr +++ b/spec/support/factories/user_factory.cr @@ -1,4 +1,4 @@ -class UserBox < BaseBox +class UserFactory < BaseFactory def initialize name "Paul Smith" joined_at Time.utc diff --git a/spec/transaction_spec.cr b/spec/transaction_spec.cr index 263bcd5c4..4bd7eae71 100644 --- a/spec/transaction_spec.cr +++ b/spec/transaction_spec.cr @@ -23,8 +23,8 @@ describe "Avram::SaveOperation" do describe "wrapping multiple saves in a transaction" do it "rolls them all back" do TestDatabase.transaction do - UserBox.create - PostBox.create + UserFactory.create + PostFactory.create TestDatabase.rollback end.should be_false @@ -36,7 +36,7 @@ describe "Avram::SaveOperation" do describe "updating" do it "runs in a transaction" do params = Avram::Params.new({"title" => "New Title"}) - post = PostBox.new.title("Old Title").create + post = PostFactory.new.title("Old Title").create Post::BaseQuery.new.first.title.should eq "Old Title" PostTransactionSaveOperation.update(post, params, rollback_after_save: true) do |operation, _post| diff --git a/spec/type_extension_spec.cr b/spec/type_extension_spec.cr index 34fefb527..88ff24020 100644 --- a/spec/type_extension_spec.cr +++ b/spec/type_extension_spec.cr @@ -17,13 +17,13 @@ class MenuOptionQuery < MenuOption::BaseQuery end describe "TypeExtensions" do - it "should work in boxes" do - CompanyBox.create + it "should work in factories" do + CompanyFactory.create company = CompanyQuery.new.first company.sales.should eq Int64::MAX company.earnings.should eq 1.0 - company2 = CompanyBox.create &.sales(10_i64).earnings(2.0) + company2 = CompanyFactory.create &.sales(10_i64).earnings(2.0) company2.sales.should eq 10_i64 company2.earnings.should eq 2.0 end @@ -35,7 +35,7 @@ describe "TypeExtensions" do end it "Int64 and Float64 should allow querying with Int32" do - CompanyBox.create &.sales(10).earnings(1.0) + CompanyFactory.create &.sales(10).earnings(1.0) using_sales = CompanyQuery.new.sales(10).first using_sales.sales.should eq 10_i64 @@ -44,7 +44,7 @@ describe "TypeExtensions" do end it "Int16 should allow querying with Int32" do - MenuOptionBox.create &.title("test").option_value(4_i16) + MenuOptionFactory.create &.title("test").option_value(4_i16) opt = MenuOptionQuery.new.option_value(4).first opt.option_value.should eq 4_i16 diff --git a/spec/type_extensions/enum_spec.cr b/spec/type_extensions/enum_spec.cr index 2c394fb37..992cd80db 100644 --- a/spec/type_extensions/enum_spec.cr +++ b/spec/type_extensions/enum_spec.cr @@ -4,14 +4,14 @@ include LazyLoadHelpers describe "Enum" do it "check enum" do - issue = IssueBox.create + issue = IssueFactory.create issue.status.enum.should eq(Issue::AvramStatus::Opened) issue.role.enum.should eq(Issue::AvramRole::Issue) end it "update enum" do - issue = IssueBox.create + issue = IssueFactory.create updated_issue = Issue::SaveOperation.update!(issue, status: Issue::Status.new(:closed)) @@ -20,14 +20,14 @@ describe "Enum" do end it "access enum methods" do - issue = IssueBox.create + issue = IssueFactory.create issue.status.opened?.should eq(true) issue.status.value.should eq(0) end it "access enum to_s and to_i" do - issue = IssueBox.create + issue = IssueFactory.create issue.status.to_s.should eq("Opened") issue.status.to_i.should eq(0) diff --git a/spec/validations_spec.cr b/spec/validations_spec.cr index 3c988c2ce..3ee081d24 100644 --- a/spec/validations_spec.cr +++ b/spec/validations_spec.cr @@ -106,7 +106,7 @@ describe Avram::Validations do describe "validate_uniqueness_of" do it "validates that a new record is unique with a query or without one" do - existing_user = UserBox.new.name("Sally").nickname("Sal").create + existing_user = UserFactory.new.name("Sally").nickname("Sal").create operation = UniquenessSaveOperation.new operation.name.value = existing_user.name operation.nickname.value = existing_user.nickname.not_nil!.downcase @@ -118,7 +118,7 @@ describe Avram::Validations do end it "ignores the existing record on update" do - existing_user = UserBox.new.name("Sally").create + existing_user = UserFactory.new.name("Sally").create operation = UniquenessSaveOperation.new(existing_user) operation.name.value = existing_user.name @@ -138,7 +138,7 @@ describe Avram::Validations do end it "validates custom message for validate_uniqueness_of" do - existing_user = UserBox.create + existing_user = UserFactory.create UniquenessWithCustomMessageSaveOperation.create(name: existing_user.name) do |operation, _user| operation.name.errors.should eq(["cannot be used"]) end diff --git a/spec/view_spec.cr b/spec/view_spec.cr index c0fd4bb3c..1662db5e9 100644 --- a/spec/view_spec.cr +++ b/spec/view_spec.cr @@ -4,17 +4,17 @@ include LazyLoadHelpers describe "views" do it "works with a primary key" do - user = UserBox.create - AdminBox.new.name(user.name).create + user = UserFactory.create + AdminFactory.new.name(user.name).create admin_user = AdminUser::BaseQuery.find(user.id) admin_user.name.should eq user.name end it "works without a primary key" do - UserBox.new.nickname("Johnny").create - UserBox.new.nickname("Johnny").create - UserBox.new.nickname("Johnny").create + UserFactory.new.nickname("Johnny").create + UserFactory.new.nickname("Johnny").create + UserFactory.new.nickname("Johnny").create nickname_info = NicknameInfo::BaseQuery.first nickname_info.nickname.should eq "Johnny" diff --git a/src/avram/box.cr b/src/avram/factory.cr similarity index 75% rename from src/avram/box.cr rename to src/avram/factory.cr index e907745d4..3a1c3edaf 100644 --- a/src/avram/box.cr +++ b/src/avram/factory.cr @@ -1,11 +1,17 @@ abstract class Avram::Box + macro inherited + {% raise "Avram::Box has been renamed to Avram::Factory" %} + end +end + +abstract class Avram::Factory getter operation SEQUENCES = {} of String => Int32 macro inherited {% unless @type.abstract? %} - {% operation = @type.name.gsub(/Box/, "::SaveOperation").id %} + {% operation = @type.name.gsub(/Factory$/, "::SaveOperation").id %} @operation : {{ operation }} = {{ operation }}.new setup_attribute_shortcuts({{ operation }}) setup_attributes({{ operation }}) @@ -31,10 +37,6 @@ abstract class Avram::Box end end - def self.save - {% raise "'Box.save' has been renamed to 'Box.create' to match 'SaveOperation.create'" %} - end - def self.build_attributes yield(new).attributes end @@ -55,34 +57,34 @@ abstract class Avram::Box operation.save! end - # Returns an array with 2 instances of the model from the Box. + # Returns an array with 2 instances of the model from the Factory. # # Usage: # # ```crystal - # tags = TagBox.create_pair + # tags = TagFactory.create_pair # typeof(tags) # => Array(Tag) # tags.size # => 2 # ``` def self.create_pair - create_pair { |box| box } + create_pair { |factory| factory } end - # Similar to `create_pair`, but accepts a block which yields the box instance. + # Similar to `create_pair`, but accepts a block which yields the factory instance. # - # Both boxes receive the same argument values. + # Both factories receive the same argument values. # # Usage: # # ```crystal - # TagBox.create_pair do |box| - # # set both boxes name to "test" - # box.name("test") + # TagFactory.create_pair do |factory| + # # set both factories name to "test" + # factory.name("test") # end # ``` def self.create_pair [1, 2].map do |n| - self.create { |box| yield(box) } + self.create { |factory| yield(factory) } end end @@ -91,7 +93,7 @@ abstract class Avram::Box # Usage: # # ```crystal - # class UserBox < Avram::Box + # class UserFactory < Avram::Factory # def initialize # username sequence("username") # => username-1, username-2, etc. # email "#{sequence("email")}@example.com" # => email-1@example.com, email-2@example.com, etc.