Skip to content

Commit

Permalink
Merge pull request #5 from fjuan/add_test_coverage_to_codeclimate
Browse files Browse the repository at this point in the history
Add specs and a minimum test coverage
  • Loading branch information
fjuan committed Jun 26, 2016
2 parents 6dc868a + 3d15c82 commit e32ae0b
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.gem
Gemfile.lock
.byebug_history

3 changes: 3 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ruby:
config_file: .ruby-style.yml

7 changes: 7 additions & 0 deletions .ruby-style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Style/StringLiterals:
Enabled: true
EnforcedStyle: single_quotes
Style/DotPosition:
Enabled: true
EnforcedStyle: leading

3 changes: 3 additions & 0 deletions acts_as_tokenizable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ Gem::Specification.new do |gem|

gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'sqlite3'
gem.add_development_dependency 'coveralls'
gem.add_development_dependency 'byebug'
gem.add_development_dependency 'pry-byebug'
end
12 changes: 2 additions & 10 deletions lib/acts_as_tokenizable.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
require 'acts_as_tokenizable/acts_as_tokenizable'
$LOAD_PATH << File.dirname(__FILE__)

module ActiveRecord
class Base
def self.acts_as_tokenizable(field_name = :token)
include ActsAsTokenizable
self.token_field_name = field_name
before_save :tokenize
end
end
end
require 'acts_as_tokenizable/base'
12 changes: 12 additions & 0 deletions lib/acts_as_tokenizable/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'active_record'
require 'acts_as_tokenizable/acts_as_tokenizable'

module ActiveRecord
class Base
def self.acts_as_tokenizable(field_name = :token)
include ActsAsTokenizable
self.token_field_name = field_name
before_save :tokenize
end
end
end
2 changes: 1 addition & 1 deletion lib/acts_as_tokenizable/string_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.numeric?(str)
# returns an array of strings containing the words on this string. Removes
# spaces, strange chars, etc
def self.words(str)
str.gsub(/[^\w|-]/, ' ').split
str.split(/[\s|\.|,]+/)
end

# removes certain words from a string.
Expand Down
31 changes: 29 additions & 2 deletions spec/acts_as_tokenizable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
require 'spec_helper'
require 'support_helper'

describe ActiveRecord do
it 'does stuff' do
pending # no code yet
describe 'classes with acts_as_tokenizable' do
it 'does include to_token and tokenize methods' do
expect(Friend.instance_methods)
.to include(:tokenize, :tokenize!, :to_token)
end

it 'updates the token field after creating an object' do
friend = Friend.new name: 'John', email: 'john@example.com'
expect(friend.token).to be_nil
friend.save
expect(friend.token).to eq('john')
end

it 'allows different token field name' do
malmo = City.create name: 'Malmö'
expect(malmo.tokenized_name).to eq('malmo')
end

it 'raises an error if `to_token` method is not defined' do
expect { Enemy.to_token }.to raise_error(NoMethodError)
end
end

describe 'classes without acts_as_tokenizable' do
it 'does not include to_token and tokenize methods' do
expect(Pet.instance_methods)
.to_not include(:tokenize, :tokenize!, :to_token)
end
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'coveralls'
Coveralls.wear!

$LOAD_PATH << File.dirname(__FILE__) + '/../lib'

require 'acts_as_tokenizable'
require 'byebug'
82 changes: 82 additions & 0 deletions spec/string_utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'spec_helper'

module ActsAsTokenizable
describe StringUtils do
describe '.numeric?' do
it 'returns true with a float' do
expect(described_class.numeric?('1.2')).to be_truthy
end

it 'returns true with an integer' do
expect(described_class.numeric?('1')).to be_truthy
end

it 'returns true with a negative number' do
expect(described_class.numeric?('-11')).to be_truthy
end

it 'returns false with a string' do
expect(described_class.numeric?('1a')).to be_falsy
end
end

describe '.words' do
it 'splits a string into by word separators' do
example = 'una mamá española aterrizó en Götemborg-int, Suecia.'

expect(described_class.words(example))
.to match_array %w(una mamá española aterrizó en Götemborg-int Suecia)
end
end

describe '.remove_words' do
it 'returns a string without the specified list of words' do
sentence = 'a b c d e f g'
words_to_remove = %w(c e f)
expect(described_class.remove_words(sentence, words_to_remove, ' '))
.to eq('a b d g')
end

it 'remove multiples occurences of a word' do
sentence = 'a b a d a f g'
words_to_remove = %w(a f)
expect(described_class.remove_words(sentence, words_to_remove, ' '))
.to eq('b d g')
end
end

describe '.to_token' do
it 'transforms tildes and letter modifications' do
examples = {
'mamá' => 'mama',
'éxtasis' => 'extasis',
'maría' => 'maria',
'camión' => 'camion',
'Úrsula' => 'ursula',
'Umeå' => 'umea',
'Gävle' => 'gavle',
'Malmö' => 'malmo',
'terraza' => 'teraza',
'España' => 'espana'
}

examples.each do |string, expected_token|
expect(described_class.to_token(string)).to eq(expected_token)
end
end

it 'removes duplicate characters' do
expect(described_class.to_token('terraza')).to eq('teraza')
end
end

describe '.words_to_token' do
it 'converts a string into something that can be used as an index key' do
example = 'una mamá española aterrizó en Götemborg-int, Suecia.'

expect(described_class.words_to_token(example))
.to eq('una mama espanola aterizo en gotemborg-int suecia')
end
end
end
end
55 changes: 55 additions & 0 deletions spec/support_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Pet < ActiveRecord::Base
end

class Enemy < ActiveRecord::Base
acts_as_tokenizable :token
end

class Friend < ActiveRecord::Base
acts_as_tokenizable :token

def to_token
ActsAsTokenizable::StringUtils.words_to_token(name)
end
end

class City < ActiveRecord::Base
acts_as_tokenizable :tokenized_name

def to_token
ActsAsTokenizable::StringUtils.words_to_token(name)
end
end

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

ActiveRecord::Base.logger
ActiveRecord::Schema.define do
self.verbose = false
end

ActiveRecord::Schema.define(version: 1) do
create_table :pets do |t|
t.column :name, :string
t.column :owner, :string
end
end

ActiveRecord::Schema.define(version: 2) do
create_table :friends do |t|
t.column :name, :string
t.column :email, :string
t.column :age, :integer
t.column :token, :text
end
end

ActiveRecord::Schema.define(version: 3) do
create_table :cities do |t|
t.column :name, :string
t.column :tokenized_name, :text
end
end

0 comments on commit e32ae0b

Please sign in to comment.