Skip to content

Commit

Permalink
Add specs for AR objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nesaulov committed Jan 10, 2018
1 parent 4d15eb0 commit 0c604c2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
17 changes: 17 additions & 0 deletions spec/orms/active_record/active_record_spec.rb
Expand Up @@ -208,6 +208,16 @@
'Can\'t serialize collection - must respond to :each')
end
end

describe 'with serializer defined in a separate class' do
subject(:json) { Surrealist.surrealize_collection(Tree.all) }
let(:expectation) do
[{ name: 'Oak', height: 200, color: 'green' },
{ name: 'Pine', height: 140, color: 'green' }].to_json
end

it { is_expected.to eq(expectation) }
end
end

describe 'ActiveRecord instance #surrealize' do
Expand Down Expand Up @@ -285,5 +295,12 @@
end
end
end

describe 'with serializer defined in a separate class' do
subject(:json) { Tree.find_by(name: 'Oak').surrealize }
let(:expectation) { { name: 'Oak', height: 200, color: 'green' }.to_json }

it { is_expected.to eq(expectation) }
end
end
end
24 changes: 21 additions & 3 deletions spec/orms/active_record/models.rb
Expand Up @@ -89,6 +89,11 @@
table.column :name, :string
table.integer :question_id
end

create_table :trees do |table|
table.column :name, :string
table.column :height, :int
end
end

def name_string
Expand Down Expand Up @@ -349,9 +354,19 @@ class Answer < ActiveRecord::Base
json_schema { { name: String, question: Question } }
end

# def name_string
# ('a'..'z').to_a.sample(8).join
# end
# Using a separate class

TreeSerializer = Class.new(Surrealist::Serializer) do
json_schema { { name: String, height: Integer, color: String } }

def color; 'green'; end
end

class Tree < ActiveRecord::Base
include Surrealist

surrealize_with TreeSerializer
end

2.times { Executive.create(name: name_string) }
3.times { Manager.create(name: name_string) }
Expand All @@ -363,3 +378,6 @@ class Answer < ActiveRecord::Base
PromKing.create(prom_king_name: name_string, prom_couple_id: PromCouple.first.id)
5.times { Question.create(name: name_string) }
10.times { Answer.create(name: name_string, question_id: Question.all.sample.id) }

Tree.create!(name: 'Oak', height: 200)
Tree.create!(name: 'Pine', height: 140)
4 changes: 1 addition & 3 deletions spec/serializer_spec.rb
@@ -1,9 +1,7 @@
# frozen_string_literal: true

class DogeSerializer < Surrealist::Serializer
json_schema do
{ name: String, name_length: Integer }
end
json_schema { { name: String, name_length: Integer } }

private def name_length
name.length
Expand Down

0 comments on commit 0c604c2

Please sign in to comment.