Permalink
Browse files

Add tests to it

  • Loading branch information...
1 parent ee7d0bf commit c15b2b551cae1a8f644b4fe1f9819a0192d8ccb8 @mauriciozaffari mauriciozaffari committed Sep 17, 2010
Showing with 163 additions and 26 deletions.
  1. +1 −0 .gitignore
  2. +6 −0 Gemfile
  3. +51 −0 Gemfile.lock
  4. +0 −18 Rakefile
  5. +3 −2 mongoid_search.gemspec
  6. +28 −0 spec/keywords_extractor_spec.rb
  7. +10 −0 spec/models/product.rb
  8. +6 −0 spec/models/tag.rb
  9. +36 −3 spec/mongoid_search_spec.rb
  10. +22 −3 spec/spec_helper.rb
View
@@ -19,3 +19,4 @@ rdoc
pkg
## PROJECT::SPECIFIC
+.bundle
View
@@ -0,0 +1,6 @@
+source 'http://rubygems.org'
+
+gem "jeweler"
+gem "database_cleaner"
+gem "mongoid", "2.0.0.beta.18"
+gem "rspec", "2.0.0.beta.22"
View
@@ -0,0 +1,51 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activemodel (3.0.0)
+ activesupport (= 3.0.0)
+ builder (~> 2.1.2)
+ i18n (~> 0.4.1)
+ activesupport (3.0.0)
+ bson (1.0.4)
+ builder (2.1.2)
+ database_cleaner (0.5.2)
+ diff-lcs (1.1.2)
+ gemcutter (0.6.1)
+ git (1.2.5)
+ i18n (0.4.1)
+ jeweler (1.4.0)
+ gemcutter (>= 0.1.0)
+ git (>= 1.2.5)
+ rubyforge (>= 2.0.0)
+ json_pure (1.4.6)
+ mongo (1.0.7)
+ bson (>= 1.0.4)
+ mongoid (2.0.0.beta.18)
+ activemodel (~> 3.0.0)
+ bson (= 1.0.4)
+ mongo (= 1.0.7)
+ tzinfo (~> 0.3.22)
+ will_paginate (~> 3.0.pre)
+ rspec (2.0.0.beta.22)
+ rspec-core (= 2.0.0.beta.22)
+ rspec-expectations (= 2.0.0.beta.22)
+ rspec-mocks (= 2.0.0.beta.22)
+ rspec-core (2.0.0.beta.22)
+ rspec-expectations (2.0.0.beta.22)
+ diff-lcs (>= 1.1.2)
+ rspec-mocks (2.0.0.beta.22)
+ rspec-core (= 2.0.0.beta.22)
+ rspec-expectations (= 2.0.0.beta.22)
+ rubyforge (2.0.4)
+ json_pure (>= 1.1.7)
+ tzinfo (0.3.23)
+ will_paginate (3.0.pre2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ database_cleaner
+ jeweler
+ mongoid (= 2.0.0.beta.18)
+ rspec (= 2.0.0.beta.22)
View
@@ -25,22 +25,4 @@ Spec::Rake::SpecTask.new(:spec) do |spec|
spec.spec_files = FileList['spec/**/*_spec.rb']
end
-Spec::Rake::SpecTask.new(:rcov) do |spec|
- spec.libs << 'lib' << 'spec'
- spec.pattern = 'spec/**/*_spec.rb'
- spec.rcov = true
-end
-
-task :spec => :check_dependencies
-
task :default => :spec
-
-require 'rake/rdoctask'
-Rake::RDocTask.new do |rdoc|
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
-
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = "mongoid_search #{version}"
- rdoc.rdoc_files.include('README*')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Mauricio Zaffari"]
- s.date = %q{2010-09-16}
+ s.date = %q{2010-09-17}
s.description = %q{Simple full text search implementation.}
s.email = %q{mauricio@papodenerd.net}
s.extra_rdoc_files = [
@@ -37,7 +37,8 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.7}
s.summary = %q{Search implementation for Mongoid ORM}
s.test_files = [
- "spec/mongoid_search_spec.rb",
+ "spec/keywords_extractor_spec.rb",
+ "spec/mongoid_search_spec.rb",
"spec/spec_helper.rb"
]
@@ -0,0 +1,28 @@
+# encoding: utf-8
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe KeywordsExtractor do
+ it "should return if no text is passed" do
+ KeywordsExtractor.extract("").should == nil
+ end
+
+ it "should return an array of keywords" do
+ KeywordsExtractor.extract("keyword").class.should == Array
+ end
+
+ it "should return an array of strings" do
+ KeywordsExtractor.extract("keyword").first.class.should == String
+ end
+
+ it "should remove accents from the text passed" do
+ KeywordsExtractor.extract("café").should == ["cafe"]
+ end
+
+ it "should downcase the text passed" do
+ KeywordsExtractor.extract("CaFé").should == ["cafe"]
+ end
+
+ it "should split whitespaces, hifens, dots and underlines" do
+ KeywordsExtractor.extract("CaFé-express.com delicious").should == ["cafe", "express", "com", "delicious"]
+ end
+end
@@ -0,0 +1,10 @@
+class Product
+ include Mongoid::Document
+ include Mongoid::Search
+ field :brand
+ field :name
+
+ references_many :tags
+
+ search_in :brand, :name, :tags => :name
+end
View
@@ -0,0 +1,6 @@
+class Tag
+ include Mongoid::Document
+ field :name
+
+ referenced_in :product
+end
@@ -1,7 +1,40 @@
+# encoding: utf-8
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-describe "MongoidSearch" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+describe Mongoid::Search do
+
+ before(:each) do
+ @product = Product.create :brand => "Apple", :name => "iPhone", :tags => ["Amazing", "Awesome", "Olé"].map { |tag| Tag.new(:name => tag) }
+ end
+
+ it "should set the _keywords field" do
+ @product._keywords.should == ["apple", "iphone", "amazing", "awesome", "ole"]
+ end
+
+ it "should return results in search" do
+ Product.search("apple").size.should == 1
+ end
+
+ it "should return results in search even searching a accented word" do
+ Product.search("Ole").size.should == 1
+ Product.search("Olé").size.should == 1
+ end
+
+ it "should return results in search even if the case doesn't match" do
+ Product.search("oLe").size.should == 1
+ end
+
+ it "should return results in search with a partial word" do
+ Product.search("iph").size.should == 1
+ end
+
+ it "should return results for any matching word with default search" do
+ Product.search("apple motorola").size.should == 1
+ end
+
+ it "should not return results when all words do not match, if using :match => :all" do
+ Product.match = :all
+ Product.search("apple motorola").size.should == 0
end
end
View
@@ -1,9 +1,28 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'mongoid'
+require 'database_cleaner'
require 'mongoid_search'
-require 'spec'
-require 'spec/autorun'
+
+Mongoid.configure do |config|
+ name = "mongoid_search_test"
+ config.master = Mongo::Connection.new.db(name)
+end
+
+Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |file| require file }
+
+DatabaseCleaner.orm = "mongoid"
Spec::Runner.configure do |config|
-
+ config.before(:all) do
+ DatabaseCleaner.strategy = :truncation
+ end
+
+ config.before(:each) do
+ DatabaseCleaner.start
+ end
+
+ config.after(:each) do
+ DatabaseCleaner.clean
+ end
end

0 comments on commit c15b2b5

Please sign in to comment.