-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from pocke/Write_spec
Write spec
- Loading branch information
Showing
9 changed files
with
119 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe ActiveRecord::Originator do | ||
pending | ||
describe '#to_sql' do | ||
context 'with a WHERE clause' do | ||
it 'annotates single = operator' do | ||
expect(Post.hello.to_sql).to eq(<<~SQL) | ||
SELECT "posts".* FROM "posts" WHERE "posts"."title" = 'hello' /* /spec/support/post.rb:4 */ | ||
SQL | ||
end | ||
|
||
it 'annotates multiple = operator' do | ||
expect(Post.hello.draft.to_sql).to eq(<<~SQL) | ||
SELECT "posts".* FROM "posts" WHERE "posts"."title" = 'hello' /* /spec/support/post.rb:4 */ | ||
AND "posts"."state" = 'draft' /* /spec/support/post.rb:5 */ | ||
SQL | ||
end | ||
|
||
it 'annotates != operator' do | ||
expect(Post.not_eq.to_sql).to eq(<<~SQL) | ||
SELECT "posts".* FROM "posts" WHERE "posts"."title" != 'hello' /* /spec/support/post.rb:9 */ | ||
SQL | ||
end | ||
|
||
it 'annotates IN operator' do | ||
expect(Post.state_for(['draft', 'published']).to_sql).to eq(<<~SQL) | ||
SELECT "posts".* FROM "posts" WHERE "posts"."state" IN ('draft', 'published') /* /spec/support/post.rb:6 */ | ||
SQL | ||
end | ||
|
||
it 'annotates NOT IN operator' do | ||
expect(Post.not_state_for(['draft', 'published']).to_sql).to eq(<<~SQL) | ||
SELECT "posts".* FROM "posts" WHERE "posts"."state" NOT IN ('draft', 'published') /* /spec/support/post.rb:7 */ | ||
SQL | ||
end | ||
|
||
it 'annotates >= operator' do | ||
expect(Post.compare_id(42..).to_sql).to eq(<<~SQL) | ||
SELECT "posts".* FROM "posts" WHERE "posts"."id" >= 42 /* /spec/support/post.rb:8 */ | ||
SQL | ||
end | ||
|
||
it 'annotates >= operator' do | ||
expect(Post.compare_id(..42).to_sql).to eq(<<~SQL) | ||
SELECT "posts".* FROM "posts" WHERE "posts"."id" <= 42 /* /spec/support/post.rb:8 */ | ||
SQL | ||
end | ||
|
||
it 'annotates < operator' do | ||
expect(Post.compare_id(...42).to_sql).to eq(<<~SQL) | ||
SELECT "posts".* FROM "posts" WHERE "posts"."id" < 42 /* /spec/support/post.rb:8 */ | ||
SQL | ||
end | ||
end | ||
|
||
it 'annotates a ORDER BY asc clause' do | ||
expect(Post.order_by_title(:asc).to_sql).to eq(<<~SQL) | ||
SELECT "posts".* FROM "posts" ORDER BY "posts"."title" ASC /* /spec/support/post.rb:10 */ | ||
SQL | ||
end | ||
|
||
it 'annotates a ORDER BY desc clause' do | ||
expect(Post.order_by_title(:desc).to_sql).to eq(<<~SQL) | ||
SELECT "posts".* FROM "posts" ORDER BY "posts"."title" DESC /* /spec/support/post.rb:10 */ | ||
SQL | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | ||
ActiveRecord::Schema.define do | ||
create_table :posts, force: true do |t| | ||
t.string :title, null: false | ||
t.string :state, null: false | ||
end | ||
|
||
create_table :comments, force: true do |t| | ||
t.integer :post_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Comment < ActiveRecord::Base | ||
belongs_to :post | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Post < ActiveRecord::Base | ||
has_many :comments | ||
|
||
scope :hello, -> { where(title: "hello") } | ||
scope :draft, -> { where(state: "draft") } | ||
scope :state_for, ->(state) { where(state: state) } | ||
scope :not_state_for, ->(state) { where.not(state: state) } | ||
scope :compare_id, ->(range) { where(id: range) } | ||
scope :not_eq, -> { where.not(title: 'hello') } | ||
scope :order_by_title, ->(direction) { order(title: direction) } | ||
end |