Skip to content

Commit

Permalink
fix up tests now that we have isolation helper
Browse files Browse the repository at this point in the history
  • Loading branch information
stathis-alexander committed Dec 3, 2024
1 parent 8ea8bfc commit 9bd7fdc
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 142 deletions.
58 changes: 29 additions & 29 deletions spec/boba/active_record/attribute_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,18 @@ class AttributeServiceSpec < ::Minitest::Spec

before do
::ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

add_ruby_file("schema.rb", <<~RUBY)
::ActiveRecord::Migration.suppress_messages do
::ActiveRecord::Schema.define do
create_table(:posts) do |t|
t.integer(:author_id, null: false)
t.string(:title)
t.string(:subject)
t.string(:body)
end
create_table(:author) do |t|
end
::ActiveRecord::Migration.suppress_messages do
::ActiveRecord::Schema.define do
create_table(:posts) do |t|
t.integer(:author_id, null: false)
t.string(:title)
t.string(:subject)
t.string(:body)
end
create_table(:author) do |t|
end
end
RUBY

add_ruby_file("author.rb", <<~RUBY)
class Author < ::ActiveRecord::Base; end
RUBY

add_ruby_file("post.rb", <<~RUBY)
class Post < ::ActiveRecord::Base
validates :title, presence: true
validates :subject, presence: true, if: ->() { true }
end
RUBY
end
end

after do
Expand All @@ -49,37 +35,51 @@ class Post < ::ActiveRecord::Base
describe "Boba::ActiveRecord::AttributeService" do
describe "nilable_attribute?" do
it "returns true if column is virtual" do
class Author < ::ActiveRecord::Base; end

assert_equal(
true,
Boba::ActiveRecord::AttributeService.nilable_attribute?("Author".constantize, "name"),
Boba::ActiveRecord::AttributeService.nilable_attribute?(Author, "name"),
)
end

it "returns true if no constraints or validators" do
class Post < ::ActiveRecord::Base; end

assert_equal(
true,
Boba::ActiveRecord::AttributeService.nilable_attribute?("Post".constantize, "body"),
Boba::ActiveRecord::AttributeService.nilable_attribute?(Post, "body"),
)
end

it "returns false if non-null constraint on column" do
class Post < ::ActiveRecord::Base; end

assert_equal(
false,
Boba::ActiveRecord::AttributeService.nilable_attribute?("Post".constantize, "author_id"),
Boba::ActiveRecord::AttributeService.nilable_attribute?(Post, "author_id"),
)
end

it "returns true if presence validator on column" do
class Post < ::ActiveRecord::Base
validates :title, presence: true
end

assert_equal(
false,
Boba::ActiveRecord::AttributeService.nilable_attribute?("Post".constantize, "title"),
Boba::ActiveRecord::AttributeService.nilable_attribute?(Post, "title"),
)
end

it "returns true if presence validator on column is conditional" do
class Post < ::ActiveRecord::Base
validates :subject, presence: true, if: ->() { true }
end

assert_equal(
true,
Boba::ActiveRecord::AttributeService.nilable_attribute?("Post".constantize, "subject"),
Boba::ActiveRecord::AttributeService.nilable_attribute?(Post, "subject"),
)
end
end
Expand Down
180 changes: 67 additions & 113 deletions spec/boba/active_record/reflection_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@ class ReflectionServiceSpec < ::Minitest::Spec
before do
::ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

add_ruby_file("schema.rb", <<~RUBY)
::ActiveRecord::Migration.suppress_messages do
::ActiveRecord::Schema.define do
create_table(:posts) do |t|
t.integer(:author_id)
end
create_table(:comments) do |t|
t.integer(:author_id, null: false)
end
create_table(:author) do |t|
end
::ActiveRecord::Migration.suppress_messages do
::ActiveRecord::Schema.define do
create_table(:posts) do |t|
t.integer(:author_id)
end
create_table(:comments) do |t|
t.integer(:author_id, null: false)
end
create_table(:author) do |t|
end
end
RUBY
end
end

after do
Expand All @@ -39,65 +37,49 @@ class ReflectionServiceSpec < ::Minitest::Spec
describe "required_reflection?" do
describe "with has_one associations" do
it "returns false if it is not a has_one association" do
add_ruby_file("author.rb", <<~RUBY)
class AuthorWithManyPosts < ::ActiveRecord::Base
self.table_name = "authors"
has_many :posts
end
RUBY
class Author < ::ActiveRecord::Base
has_many :posts
end

reflection = "AuthorWithManyPosts".constantize.reflect_on_association(:posts)
reflection = Author.reflect_on_association(:posts)
assert_equal(
false,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
)
end

it "returns false if the association is not required" do
add_ruby_file("author.rb", <<~RUBY)
class AuthorWithOneOptionalPost < ::ActiveRecord::Base
self.table_name = "authors"
has_one :post
end
RUBY
class Author < ::ActiveRecord::Base
has_one :post
end

reflection = "AuthorWithOneOptionalPost".constantize.reflect_on_association(:post)
reflection = Author.reflect_on_association(:post)
assert_equal(
false,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
)
end

it "returns true if the association is required" do
add_ruby_file("author.rb", <<~RUBY)
class AuthorWithRequiredPost < ::ActiveRecord::Base
self.table_name = "authors"
has_one :post, required: true
end
RUBY
class Author < ::ActiveRecord::Base
has_one :post, required: true
end

reflection = "AuthorWithRequiredPost".constantize.reflect_on_association(:post)
reflection = Author.reflect_on_association(:post)
assert_equal(
true,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
)
end

it "returns true if the attribute has a presence validation" do
add_ruby_file("author.rb", <<~RUBY)
class AuthorWithPresenceValidatedPost < ::ActiveRecord::Base
self.table_name = "authors"
class Author < ::ActiveRecord::Base
has_one :post, foreign_key: :author_id

has_one :post, foreign_key: :author_id
validates :post, presence: true
end
RUBY
validates :post, presence: true
end

reflection = "AuthorWithPresenceValidatedPost".constantize.reflect_on_association(:post)
reflection = Author.reflect_on_association(:post)
assert_equal(
true,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
Expand All @@ -107,123 +89,95 @@ class AuthorWithPresenceValidatedPost < ::ActiveRecord::Base

describe "with belongs_to associations" do
it "returns false if it is not a belongs_to association" do
add_ruby_file("post.rb", <<~RUBY)
class PostWithComment < ::ActiveRecord::Base
self.table_name = "posts"
has_one :comment
end
RUBY
class Post < ::ActiveRecord::Base
has_one :comment
end

reflection = "PostWithComment".constantize.reflect_on_association(:comment)
reflection = Post.reflect_on_association(:comment)
assert_equal(
false,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
)
end

it "returns false if the association is optional" do
add_ruby_file("post.rb", <<~RUBY)
class PostWithNonRequiredAuthor < ::ActiveRecord::Base
self.table_name = "posts"
has_one :comment, required: false
end
RUBY
class Post < ::ActiveRecord::Base
has_one :comment, required: false
end

reflection = "PostWithNonRequiredAuthor".constantize.reflect_on_association(:comment)
reflection = Post.reflect_on_association(:comment)
assert_equal(
false,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
)
end

it "returns true if the association is not optional" do
add_ruby_file("post.rb", <<~RUBY)
class PostWithRequiredAuthor < ::ActiveRecord::Base
self.table_name = "posts"
has_one :comment, required: true
end
RUBY
class Post < ::ActiveRecord::Base
has_one :comment, required: true
end

reflection = "PostWithRequiredAuthor".constantize.reflect_on_association(:comment)
reflection = Post.reflect_on_association(:comment)
assert_equal(
true,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
)
end

it "returns true if there is a non-null constraint on the fk" do
add_ruby_file("comment.rb", <<~RUBY)
class CommentWithNonNullAuthorFK < ::ActiveRecord::Base
self.table_name = "comments"
belongs_to :author
end
RUBY
class Comment < ::ActiveRecord::Base
belongs_to :author
end

reflection = "CommentWithNonNullAuthorFK".constantize.reflect_on_association(:author)
reflection = Comment.reflect_on_association(:author)
assert_equal(
true,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
)
end

it "returns true if there is a presence validation on the attribute" do
add_ruby_file("post.rb", <<~RUBY)
class PostWithValidatedAuthor < ::ActiveRecord::Base
self.table_name = "posts"
belongs_to :author
class Post < ::ActiveRecord::Base
belongs_to :author

validates :author, presence: true
end
RUBY
validates :author, presence: true
end

reflection = "PostWithValidatedAuthor".constantize.reflect_on_association(:author)
reflection = Post.reflect_on_association(:author)
assert_equal(
true,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
)
end

it "falls back to the default active record config if nothing is defined" do
add_ruby_file("post.rb", <<~RUBY)
class PostWithOptionalDefault < ::ActiveRecord::Base
class << self
extend T::Sig
sig { returns(T::Boolean) }
def belongs_to_required_by_default = false
end
class Post < ::ActiveRecord::Base
class << self
extend T::Sig

self.table_name = "posts"
belongs_to :author
end
class PostWithRequiredDefault < ::ActiveRecord::Base
class << self
extend T::Sig
sig { returns(T::Boolean) }
def belongs_to_required_by_default = true
end
self.table_name = "posts"
belongs_to :author
sig { returns(T::Boolean) }
def belongs_to_required_by_default = false
end
RUBY

reflection = "PostWithOptionalDefault".constantize.reflect_on_association(:author)
belongs_to :author
end

reflection = Post.reflect_on_association(:author)
assert_equal(
false,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
)

reflection = "PostWithRequiredDefault".constantize.reflect_on_association(:author)
class Post < ::ActiveRecord::Base
class << self
extend T::Sig

sig { returns(T::Boolean) }
def belongs_to_required_by_default = true
end
end

reflection = Post.reflect_on_association(:author)
assert_equal(
true,
Boba::ActiveRecord::ReflectionService.required_reflection?(reflection),
Expand Down
6 changes: 6 additions & 0 deletions spec/tapioca/dsl/compilers/money_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class MoneyRailsSpec < ::DslSpec
end

describe "Tapioca::Dsl::Compilers::MoneyRails" do
describe "initialize" do
it "gathers no constants if there are no ActiveRecord classes" do
assert_equal(gathered_constants, ["ActiveRecord::Base"])
end
end

describe "decorate" do
describe "when compiled with the persisted option" do
it "generates RBI files for classes that use the `monetize` method provided by the `money-rails` gem" do
Expand Down

0 comments on commit 9bd7fdc

Please sign in to comment.