From 55a242d1c5e7fc471d125e7209a20c8b0c175ce4 Mon Sep 17 00:00:00 2001 From: Hong ChulJu Date: Thu, 30 Oct 2014 16:22:42 +0900 Subject: [PATCH] test added: tag --- app/models/tag.rb | 5 ++--- app/models/user.rb | 4 ++-- spec/rails_helper.rb | 11 +++++++++++ spec/unit/models/tag_spec.rb | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 spec/unit/models/tag_spec.rb diff --git a/app/models/tag.rb b/app/models/tag.rb index 722b68d..d0ccedc 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -2,9 +2,8 @@ class Tag < ActiveRecord::Base has_many :taggings has_many :users, through: :taggings, source: :user - scope :fetch_list_by_tag_name, -> (tag_name){ + scope :fetch_list_by_tag_name, -> (tag_name) { tag_arel = Tag.arel_table where(tag_arel[:tag_name].matches("%#{tag_name}%")) } - -end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index d9eb568..77b7a01 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -15,10 +15,10 @@ class User < ActiveRecord::Base scope "responsed_#{status}", -> (notice) { responsed_to_notice(notice).merge(Response.where(status: status)) } end scope :responsed_not_to_notice, -> (notice) { - SQL = %{LEFT OUTER JOIN (SELECT * FROM responses WHERE responses.notice_id = #{notice.id} ) A + sql = %{LEFT OUTER JOIN (SELECT * FROM responses WHERE responses.notice_id = #{notice.id} ) A ON users.id = A.user_id WHERE A.status is null} - joins(SQL) } + joins(sql) } scope :order_by_responsed_at, -> {order('responses.created_at ASC')} scope :order_by_read_at, -> {order('read_activity_marks.created_at DESC')} diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 70813ad..b03ea97 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -30,6 +30,17 @@ config.include ShowMeTheCookies, :type => :feature + config.before(:suite) do + DatabaseCleaner.strategy = :transaction + DatabaseCleaner.clean_with(:truncation) + end + + config.around(:each) do |example| + DatabaseCleaner.cleaning do + example.run + end + end + # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. diff --git a/spec/unit/models/tag_spec.rb b/spec/unit/models/tag_spec.rb new file mode 100644 index 0000000..24e5a49 --- /dev/null +++ b/spec/unit/models/tag_spec.rb @@ -0,0 +1,14 @@ +require "rails_helper" + +RSpec.describe Tag, :type => :model do + describe "#fetch_list_by_tag_name" do + it "should fetch tag list where like name" do + tag1 = Tag.create!(tag_name: "Hello") + tag2 = Tag.create!(tag_name: "World") + tag3 = Tag.create!(tag_name: "Yellow") + + expect(Tag.fetch_list_by_tag_name("Worl")).to contain_exactly(tag2) + expect(Tag.fetch_list_by_tag_name("llo")).to contain_exactly(tag1, tag3) + end + end +end \ No newline at end of file