Skip to content

Commit

Permalink
Cleanup specs #65
Browse files Browse the repository at this point in the history
  • Loading branch information
esmarkowski committed Mar 13, 2024
1 parent 3fc19db commit b2c6603
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 51 deletions.
14 changes: 14 additions & 0 deletions spec/models/with_validations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ModelWithValidations < StretchyModel
attribute :name, :text
attribute :age, :integer
attribute :tags, :array
attribute :data, :hash
attribute :published_at, :datetime
attribute :agreed, :boolean

validates :name, presence: true
validates :age, numericality: { only_integer: true, greater_than: 21}
validates :tags, length: { minimum: 1 }
validates :data, presence: true
validates :agreed, acceptance: true
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
)
end
else

# Configure for Elasticsearch
Stretchy.configure do |config|
config.client = Elasticsearch::Client.new url: 'http://localhost:9200'
end
end


Expand Down
7 changes: 2 additions & 5 deletions spec/stretchy/relations/aggregation_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
require 'models/with_validations'
describe Stretchy::Relations::AggregationMethods do
let(:model) do
class TestModel < Stretchy::Record
end
TestModel
end
let(:model) {ModelWithValidations}


let(:relation) { Stretchy::Relation.new(model, {}) } # create a real instance
Expand Down
20 changes: 2 additions & 18 deletions spec/stretchy/validation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
require 'spec_helper'
require 'models/with_validations'

describe "Validations" do

context 'in model' do
before do
Object.send(:remove_const, :TestModel) if Object.constants.include?(:TestModel)
TestModel = Class.new(Stretchy::Record) do
attribute :name, :text
attribute :age, :integer
attribute :tags, :array
attribute :data, :hash
attribute :published_at, :datetime
attribute :agreed, :boolean

validates :name, presence: true
validates :age, numericality: { only_integer: true, greater_than: 21}
validates :tags, length: { minimum: 1 }
validates :data, presence: true
validates :agreed, acceptance: true
end
end

let(:model) { TestModel }
let(:model) { ModelWithValidations }

it 'validates presence' do
record = model.new
Expand Down
21 changes: 3 additions & 18 deletions spec/stretchy_model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
require 'spec_helper'
require 'models/with_validations'
require 'support/behaves_like_stretchy_model'

describe StretchyModel do

TestModel = Class.new(StretchyModel) do
attribute :name, :text
attribute :age, :integer
attribute :tags, :array
attribute :data, :hash
attribute :published_at, :datetime
attribute :agreed, :boolean
it_behaves_like 'a stretchy model', ModelWithValidations
it_behaves_like 'CRUD', ModelWithValidations, {name: "hello", age: 30, tags: ["hello", "world"], data: {name: "hello", age: 30}, agreed: true}, {name: "goodbye"}

validates :name, presence: true
validates :age, numericality: { only_integer: true, greater_than: 21}
validates :tags, length: { minimum: 1 }
validates :data, presence: true
validates :agreed, acceptance: true
end

let(:model) { TestModel }

it_behaves_like 'a stretchy model', TestModel
it_behaves_like 'CRUD', TestModel, {name: "hello", age: 30, tags: ["hello", "world"], data: {name: "hello", age: 30}, agreed: true}, {name: "goodbye"}
end
7 changes: 5 additions & 2 deletions spec/support/behaves_like_stretchy_model.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require 'support/configurable'

shared_examples 'a stretchy model' do |model_class|
it_behaves_like 'configurable', model_class

it 'responds to attributes' do
expect(model_class.new).to respond_to(:attributes)
end

it 'has an index name' do
expect(model_class.index_name).to eq(model_class.model_name.collection)
expect(model_class.index_name).to eq(model_class.model_name.collection.parameterize.underscore)
end

it 'includes Stretchy::Associations' do
Expand Down Expand Up @@ -198,7 +201,7 @@
it 'has correct id and data keys' do
bulk = record.to_bulk(:update)
expect(bulk[:update][:_id]).to eq(record.id)
expect(bulk[:update][:data].keys).to eq(record.attributes.keys)
expect(bulk[:update][:data][:doc].keys).to eq(record.attributes.keys)
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
require 'spec_helper'

describe Stretchy::Configuration do
shared_examples 'configurable' do |model|
before(:all) do
@original_config = Stretchy.configuration.client.dup
end

after(:all) do
Stretchy.configure do |config|
config.client = @original_config
end
end

it 'has a default host' do
client = Stretchy.configuration.client.transport.transport.hosts.first
expect(client[:host]).to eq('localhost')
expect(client[:port]).to eq(9200)
end

let(:model) do
class TestModel < Stretchy::Record
end
TestModel
end

it 'can configure client' do
Stretchy.configure do |config|
config.client = Elasticsearch::Client.new url: 'http://localhost:92929'
Expand All @@ -24,5 +27,4 @@ class TestModel < Stretchy::Record
expect(model.gateway.client.transport.transport.hosts.first[:port]).to eq(92929)

end

end

0 comments on commit b2c6603

Please sign in to comment.