Skip to content

Commit

Permalink
Write spec
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuwatch committed Dec 5, 2015
1 parent aa81a5d commit 0976dc9
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
language: ruby
rvm:
- 2.1.5
- 2.2.3
before_install: gem install bundler -v 1.10.3
2 changes: 1 addition & 1 deletion lib/nazrin/active_record/data_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def results(client)
if @client.parameters[:size] && @client.parameters[:start]
round_correct_page

Nazrin.paginate_array(
Nazrin.paginated_array(
collections,
current_page: @current_page,
per_page: @client.parameters[:size],
Expand Down
2 changes: 1 addition & 1 deletion lib/nazrin/paginated_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def previous_page

# next page number in the collections
def next_page
current_page - 1 unless last_page? || out_of_bounds?
current_page + 1 unless last_page? || out_of_bounds?
end

# out of bounds of the collections?
Expand Down
1 change: 0 additions & 1 deletion lib/nazrin/search_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ def expr(expr)
end

def search
p @parameters
@client.search(@parameters)
end

Expand Down
6 changes: 4 additions & 2 deletions nazrin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ Gem::Specification.new do |spec|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.require_paths = ['lib']

spec.add_dependency 'activesupport'
spec.add_dependency 'activerecord'
spec.add_dependency 'aws-sdk', '~> 2'

spec.add_development_dependency 'bundler', '~> 1.9'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'kaminari'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'coveralls'
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'activerecord'
spec.add_development_dependency 'database_cleaner'
end
8 changes: 8 additions & 0 deletions spec/db/migrate/0_create_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :content
t.datetime :created_at
end
end
end
21 changes: 21 additions & 0 deletions spec/nazrin/active_record/searchable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

describe Nazrin::ActiveRecord::Searchable do
let!(:post) { Post.create(content: 'content', created_at: Time.now) }

it { expect(Post).to be_respond_to :search }
it { expect(Post).to be_respond_to :fields }
it { expect(Post).to be_respond_to :field }
it { expect(post).to be_respond_to :add_to_index }
it { expect(post).to be_respond_to :update_in_index }
it { expect(post).to be_respond_to :delete_from_index }

describe '#search' do
before { allow_any_instance_of(Nazrin::SearchClient).to receive(:search).and_return(FakeResponse.new) }

it { expect(Post.search.is_a?(Nazrin::SearchClient)).to eq true }
it { expect(Post.search.data_accessor.is_a?(Nazrin::ActiveRecord::DataAccessor)).to eq true }

it { expect(Post.search.size(1).start(0).execute).to eq [post] }
end
end
7 changes: 7 additions & 0 deletions spec/nazrin/document_client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'spec_helper'

describe Nazrin::DocumentClient do
let(:document_client) { described_class.new }
it { expect(document_client).to be_respond_to :client }
it { expect(document_client.client.class).to eq Aws::CloudSearchDomain::Client }
end
38 changes: 38 additions & 0 deletions spec/nazrin/nazrin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'

describe Nazrin do
describe 'configure' do
let(:config) { Nazrin.config }
it { expect(config.debug_mode).to be false }
it { expect(config.search_endpoint).to eq 'http://search' }
it { expect(config.document_endpoint).to eq 'http://document' }
it { expect(config.region).to eq :region }
it { expect(config.access_key_id).to eq :access_key_id }
it { expect(config.secret_access_key).to eq :secret_access_key }
it { expect(config.pagination).to eq 'kaminari' }
end

describe 'paginated_array' do
let(:paginated_array) do
Nazrin.paginated_array(
[1, 2, 3],
current_page: 1, per_page: 1, total_count: 3)
end

context 'kaminari' do
before { Nazrin.config.pagination = 'kaminari' }
it { expect(paginated_array.class).to eq Kaminari::PaginatableArray }
end

context 'nazrin' do
before { Nazrin.config.pagination = 'nazrin' }
it { expect(paginated_array.class).to eq Nazrin::PaginatedArray }
it { expect(paginated_array.first_page?).to eq true }
it { expect(paginated_array.last_page?).to eq false }
it { expect(paginated_array.total_pages).to eq 3 }
it { expect(paginated_array.previous_page).to eq nil }
it { expect(paginated_array.next_page).to eq 2 }
it { expect(paginated_array.out_of_bounds?).to eq false }
end
end
end
13 changes: 13 additions & 0 deletions spec/nazrin/search_client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe Nazrin::SearchClient do
let(:search_client) { described_class.new }
it { expect(search_client).to be_respond_to :parameters }
it { expect(search_client).to be_respond_to :data_accessor }

describe '#execute' do
before { allow_any_instance_of(Nazrin::SearchClient).to receive(:search).and_return(FakeResponse.new) }

it { expect(search_client.execute.is_a?(FakeResponse)).to eq true }
end
end
11 changes: 0 additions & 11 deletions spec/nazrin_spec.rb

This file was deleted.

74 changes: 74 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,76 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

require 'coveralls'
Coveralls.wear!

require 'kaminari'
require 'aws-sdk'
require 'active_record'
require 'nazrin'

Kaminari::Hooks.init

Nazrin.configure do |config|
config.debug_mode = false
config.search_endpoint = 'http://search'
config.document_endpoint = 'http://document'
config.region = :region
config.access_key_id = :access_key_id
config.secret_access_key = :secret_access_key
config.pagination = 'kaminari'
end

class FakeResponse
def data
self
end

def hits
self
end

def hit
[self]
end

def id
1
end

def found
1
end
end

ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:')

class Post < ActiveRecord::Base
include Nazrin::ActiveRecord::Searchable

searchable do
fields [:content]
field(:created_at) { created_at.utc.iso8601 }
end
end

ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.migrate File.expand_path('../db/migrate', __FILE__), nil

require 'database_cleaner'

RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
DatabaseCleaner.start
end

config.after(:each) do
DatabaseCleaner.clean
end
end

0 comments on commit 0976dc9

Please sign in to comment.