Remove performance and skipped tests.
This commit is contained in:
parent
ab9db291d4
commit
2a791bd90c
@ -1,14 +0,0 @@
|
|||||||
require 'spec_helper'
|
|
||||||
|
|
||||||
describe FastJsonapi::ObjectSerializer do
|
|
||||||
|
|
||||||
context 'instrument' do
|
|
||||||
context 'skylight' do
|
|
||||||
# skip for normal runs because this could alter some
|
|
||||||
# other test by insterting the instrumentation
|
|
||||||
xit 'make sure requiring skylight normalizers works' do
|
|
||||||
require 'fast_jsonapi/instrumentation/skylight'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,217 +0,0 @@
|
|||||||
require 'spec_helper'
|
|
||||||
|
|
||||||
describe FastJsonapi::ObjectSerializer, performance: true do
|
|
||||||
include_context 'movie class'
|
|
||||||
include_context 'ams movie class'
|
|
||||||
include_context 'jsonapi movie class'
|
|
||||||
include_context 'jsonapi-serializers movie class'
|
|
||||||
|
|
||||||
include_context 'group class'
|
|
||||||
include_context 'ams group class'
|
|
||||||
include_context 'jsonapi group class'
|
|
||||||
include_context 'jsonapi-serializers group class'
|
|
||||||
|
|
||||||
before(:all) { GC.disable }
|
|
||||||
after(:all) { GC.enable }
|
|
||||||
|
|
||||||
SERIALIZERS = {
|
|
||||||
fast_jsonapi: {
|
|
||||||
name: 'Fast Serializer',
|
|
||||||
hash_method: :serializable_hash,
|
|
||||||
json_method: :serialized_json
|
|
||||||
},
|
|
||||||
ams: {
|
|
||||||
name: 'AMS serializer',
|
|
||||||
speed_factor: 25,
|
|
||||||
hash_method: :as_json
|
|
||||||
},
|
|
||||||
jsonapi: {
|
|
||||||
name: 'jsonapi-rb serializer'
|
|
||||||
},
|
|
||||||
jsonapis: {
|
|
||||||
name: 'jsonapi-serializers'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
context 'when testing performance of serialization' do
|
|
||||||
it 'should create a hash of 1000 records in less than 50 ms' do
|
|
||||||
movies = 1000.times.map { |_i| movie }
|
|
||||||
expect { MovieSerializer.new(movies).serializable_hash }.to perform_under(50).ms
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should serialize 1000 records to jsonapi in less than 60 ms' do
|
|
||||||
movies = 1000.times.map { |_i| movie }
|
|
||||||
expect { MovieSerializer.new(movies).serialized_json }.to perform_under(60).ms
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should create a hash of 1000 records with includes and meta in less than 75 ms' do
|
|
||||||
count = 1000
|
|
||||||
movies = count.times.map { |_i| movie }
|
|
||||||
options = {}
|
|
||||||
options[:meta] = { total: count }
|
|
||||||
options[:include] = [:actors]
|
|
||||||
expect { MovieSerializer.new(movies, options).serializable_hash }.to perform_under(75).ms
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should serialize 1000 records to jsonapi with includes and meta in less than 75 ms' do
|
|
||||||
count = 1000
|
|
||||||
movies = count.times.map { |_i| movie }
|
|
||||||
options = {}
|
|
||||||
options[:meta] = { total: count }
|
|
||||||
options[:include] = [:actors]
|
|
||||||
expect { MovieSerializer.new(movies, options).serialized_json }.to perform_under(75).ms
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def print_stats(message, count, data)
|
|
||||||
puts
|
|
||||||
puts message
|
|
||||||
|
|
||||||
name_length = SERIALIZERS.collect { |s| s[1].fetch(:name, s[0]).length }.max
|
|
||||||
|
|
||||||
puts format("%-#{name_length+1}s %-10s %-10s %s", 'Serializer', 'Records', 'Time', 'Speed Up')
|
|
||||||
|
|
||||||
report_format = "%-#{name_length+1}s %-10s %-10s"
|
|
||||||
fast_jsonapi_time = data[:fast_jsonapi][:time]
|
|
||||||
puts format(report_format, 'Fast serializer', count, fast_jsonapi_time.round(2).to_s + ' ms')
|
|
||||||
|
|
||||||
data.reject { |k,v| k == :fast_jsonapi }.each_pair do |k,v|
|
|
||||||
t = v[:time]
|
|
||||||
factor = t / fast_jsonapi_time
|
|
||||||
|
|
||||||
speed_factor = SERIALIZERS[k].fetch(:speed_factor, 1)
|
|
||||||
result = factor >= speed_factor ? '✔' : '✘'
|
|
||||||
|
|
||||||
puts format("%-#{name_length+1}s %-10s %-10s %sx %s", SERIALIZERS[k][:name], count, t.round(2).to_s + ' ms', factor.round(2), result)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def run_hash_benchmark(message, movie_count, serializers)
|
|
||||||
data = Hash[serializers.keys.collect { |k| [ k, { hash: nil, time: nil, speed_factor: nil }] }]
|
|
||||||
|
|
||||||
serializers.each_pair do |k,v|
|
|
||||||
hash_method = SERIALIZERS[k].key?(:hash_method) ? SERIALIZERS[k][:hash_method] : :to_hash
|
|
||||||
data[k][:time] = Benchmark.measure { data[k][:hash] = v.send(hash_method) }.real * 1000
|
|
||||||
end
|
|
||||||
|
|
||||||
print_stats(message, movie_count, data)
|
|
||||||
|
|
||||||
data
|
|
||||||
end
|
|
||||||
|
|
||||||
def run_json_benchmark(message, movie_count, serializers)
|
|
||||||
data = Hash[serializers.keys.collect { |k| [ k, { json: nil, time: nil, speed_factor: nil }] }]
|
|
||||||
|
|
||||||
serializers.each_pair do |k,v|
|
|
||||||
ams_json = nil
|
|
||||||
json_method = SERIALIZERS[k].key?(:json_method) ? SERIALIZERS[k][:json_method] : :to_json
|
|
||||||
data[k][:time] = Benchmark.measure { data[k][:json] = v.send(json_method) }.real * 1000
|
|
||||||
end
|
|
||||||
|
|
||||||
print_stats(message, movie_count, data)
|
|
||||||
|
|
||||||
data
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when comparing with AMS 0.10.x' do
|
|
||||||
[1, 25, 250, 1000].each do |movie_count|
|
|
||||||
it "should serialize #{movie_count} records atleast #{SERIALIZERS[:ams][:speed_factor]} times faster than AMS" do
|
|
||||||
ams_movies = build_ams_movies(movie_count)
|
|
||||||
movies = build_movies(movie_count)
|
|
||||||
jsonapi_movies = build_jsonapi_movies(movie_count)
|
|
||||||
jsonapis_movies = build_js_movies(movie_count)
|
|
||||||
|
|
||||||
serializers = {
|
|
||||||
fast_jsonapi: MovieSerializer.new(movies),
|
|
||||||
ams: ActiveModelSerializers::SerializableResource.new(ams_movies),
|
|
||||||
jsonapi: JSONAPISerializer.new(jsonapi_movies),
|
|
||||||
jsonapis: JSONAPISSerializer.new(jsonapis_movies)
|
|
||||||
}
|
|
||||||
|
|
||||||
message = "Serialize to JSON string #{movie_count} records"
|
|
||||||
json_benchmarks = run_json_benchmark(message, movie_count, serializers)
|
|
||||||
|
|
||||||
message = "Serialize to Ruby Hash #{movie_count} records"
|
|
||||||
hash_benchmarks = run_hash_benchmark(message, movie_count, serializers)
|
|
||||||
|
|
||||||
# json
|
|
||||||
expect(json_benchmarks[:fast_jsonapi][:json].length).to eq json_benchmarks[:ams][:json].length
|
|
||||||
json_speed_up = json_benchmarks[:ams][:time] / json_benchmarks[:fast_jsonapi][:time]
|
|
||||||
|
|
||||||
# hash
|
|
||||||
hash_speed_up = hash_benchmarks[:ams][:time] / hash_benchmarks[:fast_jsonapi][:time]
|
|
||||||
expect(hash_speed_up).to be >= SERIALIZERS[:ams][:speed_factor]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when comparing with AMS 0.10.x and with includes and meta' do
|
|
||||||
[1, 25, 250, 1000].each do |movie_count|
|
|
||||||
it "should serialize #{movie_count} records atleast #{SERIALIZERS[:ams][:speed_factor]} times faster than AMS" do
|
|
||||||
ams_movies = build_ams_movies(movie_count)
|
|
||||||
movies = build_movies(movie_count)
|
|
||||||
jsonapi_movies = build_jsonapi_movies(movie_count)
|
|
||||||
jsonapis_movies = build_js_movies(movie_count)
|
|
||||||
|
|
||||||
options = {}
|
|
||||||
options[:meta] = { total: movie_count }
|
|
||||||
options[:include] = [:actors, :movie_type]
|
|
||||||
|
|
||||||
serializers = {
|
|
||||||
fast_jsonapi: MovieSerializer.new(movies, options),
|
|
||||||
ams: ActiveModelSerializers::SerializableResource.new(ams_movies, include: options[:include], meta: options[:meta]),
|
|
||||||
jsonapi: JSONAPISerializer.new(jsonapi_movies, include: options[:include], meta: options[:meta]),
|
|
||||||
jsonapis: JSONAPISSerializer.new(jsonapis_movies, include: options[:include].map { |i| i.to_s.dasherize }, meta: options[:meta])
|
|
||||||
}
|
|
||||||
|
|
||||||
message = "Serialize to JSON string #{movie_count} with includes and meta"
|
|
||||||
json_benchmarks = run_json_benchmark(message, movie_count, serializers)
|
|
||||||
|
|
||||||
message = "Serialize to Ruby Hash #{movie_count} with includes and meta"
|
|
||||||
hash_benchmarks = run_hash_benchmark(message, movie_count, serializers)
|
|
||||||
|
|
||||||
# json
|
|
||||||
expect(json_benchmarks[:fast_jsonapi][:json].length).to eq json_benchmarks[:ams][:json].length
|
|
||||||
json_speed_up = json_benchmarks[:ams][:time] / json_benchmarks[:fast_jsonapi][:time]
|
|
||||||
|
|
||||||
# hash
|
|
||||||
hash_speed_up = hash_benchmarks[:ams][:time] / hash_benchmarks[:fast_jsonapi][:time]
|
|
||||||
expect(hash_speed_up).to be >= SERIALIZERS[:ams][:speed_factor]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when comparing with AMS 0.10.x and with polymorphic has_many' do
|
|
||||||
[1, 25, 250, 1000].each do |group_count|
|
|
||||||
it "should serialize #{group_count} records at least #{SERIALIZERS[:ams][:speed_factor]} times faster than AMS" do
|
|
||||||
ams_groups = build_ams_groups(group_count)
|
|
||||||
groups = build_groups(group_count)
|
|
||||||
jsonapi_groups = build_jsonapi_groups(group_count)
|
|
||||||
jsonapis_groups = build_jsonapis_groups(group_count)
|
|
||||||
|
|
||||||
options = {}
|
|
||||||
|
|
||||||
serializers = {
|
|
||||||
fast_jsonapi: GroupSerializer.new(groups, options),
|
|
||||||
ams: ActiveModelSerializers::SerializableResource.new(ams_groups),
|
|
||||||
jsonapi: JSONAPISerializerB.new(jsonapi_groups),
|
|
||||||
jsonapis: JSONAPISSerializerB.new(jsonapis_groups)
|
|
||||||
}
|
|
||||||
|
|
||||||
message = "Serialize to JSON string #{group_count} with polymorphic has_many"
|
|
||||||
json_benchmarks = run_json_benchmark(message, group_count, serializers)
|
|
||||||
|
|
||||||
message = "Serialize to Ruby Hash #{group_count} with polymorphic has_many"
|
|
||||||
hash_benchmarks = run_hash_benchmark(message, group_count, serializers)
|
|
||||||
|
|
||||||
# json
|
|
||||||
expect(json_benchmarks[:fast_jsonapi][:json].length).to eq json_benchmarks[:ams][:json].length
|
|
||||||
json_speed_up = json_benchmarks[:ams][:time] / json_benchmarks[:fast_jsonapi][:time]
|
|
||||||
|
|
||||||
# hash
|
|
||||||
hash_speed_up = hash_benchmarks[:ams][:time] / hash_benchmarks[:fast_jsonapi][:time]
|
|
||||||
expect(hash_speed_up).to be >= SERIALIZERS[:ams][:speed_factor]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,155 +0,0 @@
|
|||||||
RSpec.shared_context 'ams movie class' do
|
|
||||||
before(:context) do
|
|
||||||
# models
|
|
||||||
class AMSModel < ActiveModelSerializers::Model
|
|
||||||
derive_attributes_from_names_and_fix_accessors
|
|
||||||
end
|
|
||||||
|
|
||||||
class AMSMovieType < AMSModel
|
|
||||||
attributes :id, :name, :movies
|
|
||||||
end
|
|
||||||
class AMSMovie < AMSModel
|
|
||||||
attributes :id, :name, :release_year, :actors, :owner, :movie_type, :advertising_campaign
|
|
||||||
|
|
||||||
def movie_type
|
|
||||||
mt = AMSMovieType.new
|
|
||||||
mt.id = 1
|
|
||||||
mt.name = 'Episode'
|
|
||||||
mt.movies = [self]
|
|
||||||
mt
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class AMSAdvertisingCampaign < AMSModel
|
|
||||||
attributes :id, :name, :movie
|
|
||||||
end
|
|
||||||
|
|
||||||
class AMSAward < AMSModel
|
|
||||||
attributes :id, :title, :actor
|
|
||||||
end
|
|
||||||
|
|
||||||
class AMSAgency < AMSModel
|
|
||||||
attributes :id, :name, :actors
|
|
||||||
end
|
|
||||||
|
|
||||||
class AMSActor < AMSModel
|
|
||||||
attributes :id, :name, :email, :agency, :awards, :agency_id
|
|
||||||
def agency
|
|
||||||
AMSAgency.new.tap do |a|
|
|
||||||
a.id = agency_id
|
|
||||||
a.name = "Test Agency #{agency_id}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def award_ids
|
|
||||||
[id * 9, id * 9 + 1]
|
|
||||||
end
|
|
||||||
|
|
||||||
def awards
|
|
||||||
award_ids.map do |i|
|
|
||||||
AMSAward.new.tap do |a|
|
|
||||||
a.id = i
|
|
||||||
a.title = "Test Award #{i}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class AMSUser < AMSModel
|
|
||||||
attributes :id, :name
|
|
||||||
end
|
|
||||||
class AMSMovieType < AMSModel
|
|
||||||
attributes :id, :name
|
|
||||||
end
|
|
||||||
# serializers
|
|
||||||
class AMSAwardSerializer < ActiveModel::Serializer
|
|
||||||
type 'award'
|
|
||||||
attributes :id, :title
|
|
||||||
belongs_to :actor
|
|
||||||
end
|
|
||||||
class AMSAgencySerializer < ActiveModel::Serializer
|
|
||||||
type 'agency'
|
|
||||||
attributes :id, :name
|
|
||||||
belongs_to :state
|
|
||||||
has_many :actors
|
|
||||||
end
|
|
||||||
class AMSActorSerializer < ActiveModel::Serializer
|
|
||||||
type 'actor'
|
|
||||||
attributes :name, :email
|
|
||||||
belongs_to :agency, serializer: ::AMSAgencySerializer
|
|
||||||
has_many :awards, serializer: ::AMSAwardSerializer
|
|
||||||
end
|
|
||||||
class AMSUserSerializer < ActiveModel::Serializer
|
|
||||||
type 'user'
|
|
||||||
attributes :name
|
|
||||||
end
|
|
||||||
class AMSMovieTypeSerializer < ActiveModel::Serializer
|
|
||||||
type 'movie_type'
|
|
||||||
attributes :name
|
|
||||||
has_many :movies
|
|
||||||
end
|
|
||||||
class AMSAdvertisingCampaignSerializer < ActiveModel::Serializer
|
|
||||||
type 'advertising_campaign'
|
|
||||||
attributes :name
|
|
||||||
end
|
|
||||||
class AMSMovieSerializer < ActiveModel::Serializer
|
|
||||||
type 'movie'
|
|
||||||
attributes :name, :release_year
|
|
||||||
has_many :actors
|
|
||||||
has_one :owner
|
|
||||||
belongs_to :movie_type
|
|
||||||
has_one :advertising_campaign
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
after(:context) do
|
|
||||||
classes_to_remove = %i[AMSMovie AMSMovieSerializer]
|
|
||||||
classes_to_remove.each do |klass_name|
|
|
||||||
Object.send(:remove_const, klass_name) if Object.constants.include?(klass_name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:ams_actors) do
|
|
||||||
3.times.map do |i|
|
|
||||||
a = AMSActor.new
|
|
||||||
a.id = i + 1
|
|
||||||
a.name = "Test #{a.id}"
|
|
||||||
a.email = "test#{a.id}@test.com"
|
|
||||||
a.agency_id = i
|
|
||||||
a
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:ams_user) do
|
|
||||||
ams_user = AMSUser.new
|
|
||||||
ams_user.id = 3
|
|
||||||
ams_user
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:ams_movie_type) do
|
|
||||||
ams_movie_type = AMSMovieType.new
|
|
||||||
ams_movie_type.id = 1
|
|
||||||
ams_movie_type.name = 'episode'
|
|
||||||
ams_movie_type
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:ams_advertising_campaign) do
|
|
||||||
campaign = AMSAdvertisingCampaign.new
|
|
||||||
campaign.id = 1
|
|
||||||
campaign.name = "Movie is incredible!!"
|
|
||||||
campaign
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_ams_movies(count)
|
|
||||||
count.times.map do |i|
|
|
||||||
m = AMSMovie.new
|
|
||||||
m.id = i + 1
|
|
||||||
m.name = 'test movie'
|
|
||||||
m.actors = ams_actors
|
|
||||||
m.owner = ams_user
|
|
||||||
m.movie_type = ams_movie_type
|
|
||||||
m.advertising_campaign = ams_advertising_campaign
|
|
||||||
m
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,87 +0,0 @@
|
|||||||
RSpec.shared_context 'ams group class' do
|
|
||||||
before(:context) do
|
|
||||||
# models
|
|
||||||
class AMSPerson < ActiveModelSerializers::Model
|
|
||||||
attr_accessor :id, :first_name, :last_name
|
|
||||||
end
|
|
||||||
|
|
||||||
class AMSGroup < ActiveModelSerializers::Model
|
|
||||||
attr_accessor :id, :name, :groupees
|
|
||||||
end
|
|
||||||
|
|
||||||
# serializers
|
|
||||||
class AMSPersonSerializer < ActiveModel::Serializer
|
|
||||||
type 'person'
|
|
||||||
attributes :first_name, :last_name
|
|
||||||
end
|
|
||||||
|
|
||||||
class AMSGroupSerializer < ActiveModel::Serializer
|
|
||||||
type 'group'
|
|
||||||
attributes :name
|
|
||||||
has_many :groupees
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
after(:context) do
|
|
||||||
classes_to_remove = %i[AMSPerson AMSGroup AMSPersonSerializer AMSGroupSerializer]
|
|
||||||
classes_to_remove.each do |klass_name|
|
|
||||||
Object.send(:remove_const, klass_name) if Object.constants.include?(klass_name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:ams_groups) do
|
|
||||||
group_count = 0
|
|
||||||
person_count = 0
|
|
||||||
3.times.map do |i|
|
|
||||||
group = AMSGroup.new
|
|
||||||
group.id = group_count + 1
|
|
||||||
group.name = "Test Group #{group.id}"
|
|
||||||
group_count = group.id
|
|
||||||
|
|
||||||
person = AMSPerson.new
|
|
||||||
person.id = person_count + 1
|
|
||||||
person.last_name = "Last Name #{person.id}"
|
|
||||||
person.first_name = "First Name #{person.id}"
|
|
||||||
person_count = person.id
|
|
||||||
|
|
||||||
child_group = AMSGroup.new
|
|
||||||
child_group.id = group_count + 1
|
|
||||||
child_group.name = "Test Group #{child_group.id}"
|
|
||||||
group_count = child_group.id
|
|
||||||
|
|
||||||
group.groupees = [person, child_group]
|
|
||||||
group
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:ams_person) do
|
|
||||||
ams_person = AMSPerson.new
|
|
||||||
ams_person.id = 3
|
|
||||||
ams_person
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_ams_groups(count)
|
|
||||||
group_count = 0
|
|
||||||
person_count = 0
|
|
||||||
count.times.map do |i|
|
|
||||||
group = AMSGroup.new
|
|
||||||
group.id = group_count + 1
|
|
||||||
group.name = "Test Group #{group.id}"
|
|
||||||
group_count = group.id
|
|
||||||
|
|
||||||
person = AMSPerson.new
|
|
||||||
person.id = person_count + 1
|
|
||||||
person.last_name = "Last Name #{person.id}"
|
|
||||||
person.first_name = "First Name #{person.id}"
|
|
||||||
person_count = person.id
|
|
||||||
|
|
||||||
child_group = AMSGroup.new
|
|
||||||
child_group.id = group_count + 1
|
|
||||||
child_group.name = "Test Group #{child_group.id}"
|
|
||||||
group_count = child_group.id
|
|
||||||
|
|
||||||
group.groupees = [person, child_group]
|
|
||||||
group
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,123 +0,0 @@
|
|||||||
RSpec.shared_context 'jsonapi-serializers movie class' do
|
|
||||||
before(:context) do
|
|
||||||
# models
|
|
||||||
class JSMovie
|
|
||||||
attr_accessor :id, :name, :release_year, :actors, :owner, :movie_type
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSActor
|
|
||||||
attr_accessor :id, :name, :email
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSUser
|
|
||||||
attr_accessor :id, :name
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSMovieType
|
|
||||||
attr_accessor :id, :name
|
|
||||||
end
|
|
||||||
|
|
||||||
# serializers
|
|
||||||
class JSActorSerializer
|
|
||||||
include JSONAPI::Serializer
|
|
||||||
attributes :name, :email
|
|
||||||
|
|
||||||
def type
|
|
||||||
'actor'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
class JSUserSerializer
|
|
||||||
include JSONAPI::Serializer
|
|
||||||
attributes :name
|
|
||||||
|
|
||||||
def type
|
|
||||||
'user'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
class JSMovieTypeSerializer
|
|
||||||
include JSONAPI::Serializer
|
|
||||||
attributes :name
|
|
||||||
|
|
||||||
def type
|
|
||||||
'movie_type'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
class JSMovieSerializer
|
|
||||||
include JSONAPI::Serializer
|
|
||||||
attributes :name, :release_year
|
|
||||||
has_many :actors
|
|
||||||
has_one :owner
|
|
||||||
has_one :movie_type
|
|
||||||
|
|
||||||
def type
|
|
||||||
'movie'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPISSerializer
|
|
||||||
def initialize(data, options = {})
|
|
||||||
@options = options.merge(is_collection: true)
|
|
||||||
@data = data
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_json
|
|
||||||
JSONAPI::Serializer.serialize(@data, @options).to_json
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_hash
|
|
||||||
JSONAPI::Serializer.serialize(@data, @options)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
after(:context) do
|
|
||||||
classes_to_remove = %i[
|
|
||||||
JSMovie
|
|
||||||
JSActor
|
|
||||||
JSUser
|
|
||||||
JSMovieType
|
|
||||||
JSONAPISSerializer
|
|
||||||
JSActorSerializer
|
|
||||||
JSUserSerializer
|
|
||||||
JSMovieTypeSerializer
|
|
||||||
JSMovieSerializer]
|
|
||||||
classes_to_remove.each do |klass_name|
|
|
||||||
Object.send(:remove_const, klass_name) if Object.constants.include?(klass_name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:js_actors) do
|
|
||||||
3.times.map do |i|
|
|
||||||
a = JSActor.new
|
|
||||||
a.id = i + 1
|
|
||||||
a.name = "Test #{a.id}"
|
|
||||||
a.email = "test#{a.id}@test.com"
|
|
||||||
a
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:js_user) do
|
|
||||||
ams_user = JSUser.new
|
|
||||||
ams_user.id = 3
|
|
||||||
ams_user
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:js_movie_type) do
|
|
||||||
ams_movie_type = JSMovieType.new
|
|
||||||
ams_movie_type.id = 1
|
|
||||||
ams_movie_type.name = 'episode'
|
|
||||||
ams_movie_type
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_js_movies(count)
|
|
||||||
count.times.map do |i|
|
|
||||||
m = JSMovie.new
|
|
||||||
m.id = i + 1
|
|
||||||
m.name = 'test movie'
|
|
||||||
m.actors = js_actors
|
|
||||||
m.owner = js_user
|
|
||||||
m.movie_type = js_movie_type
|
|
||||||
m
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,116 +0,0 @@
|
|||||||
RSpec.shared_context 'jsonapi-serializers group class' do
|
|
||||||
|
|
||||||
# Person, Group Classes and serializers
|
|
||||||
before(:context) do
|
|
||||||
# models
|
|
||||||
class JSPerson
|
|
||||||
attr_accessor :id, :first_name, :last_name
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSGroup
|
|
||||||
attr_accessor :id, :name, :groupees # Let's assume groupees can be Person or Group objects
|
|
||||||
end
|
|
||||||
|
|
||||||
# serializers
|
|
||||||
class JSPersonSerializer
|
|
||||||
include JSONAPI::Serializer
|
|
||||||
attributes :first_name, :last_name
|
|
||||||
|
|
||||||
def type
|
|
||||||
'person'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSGroupSerializer
|
|
||||||
include JSONAPI::Serializer
|
|
||||||
attributes :name
|
|
||||||
has_many :groupees
|
|
||||||
|
|
||||||
def type
|
|
||||||
'group'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPISSerializerB
|
|
||||||
def initialize(data, options = {})
|
|
||||||
@options = options.merge(is_collection: true)
|
|
||||||
@data = data
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_json
|
|
||||||
JSON.fast_generate(to_hash)
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_hash
|
|
||||||
JSONAPI::Serializer.serialize(@data, @options)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
after :context do
|
|
||||||
classes_to_remove = %i[
|
|
||||||
JSPerson
|
|
||||||
JSGroup
|
|
||||||
JSPersonSerializer
|
|
||||||
JSGroupSerializer]
|
|
||||||
classes_to_remove.each do |klass_name|
|
|
||||||
Object.send(:remove_const, klass_name) if Object.constants.include?(klass_name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:jsonapi_groups) do
|
|
||||||
group_count = 0
|
|
||||||
person_count = 0
|
|
||||||
3.times.map do |i|
|
|
||||||
group = JSGroup.new
|
|
||||||
group.id = group_count + 1
|
|
||||||
group.name = "Test Group #{group.id}"
|
|
||||||
group_count = group.id
|
|
||||||
|
|
||||||
person = JSPerson.new
|
|
||||||
person.id = person_count + 1
|
|
||||||
person.last_name = "Last Name #{person.id}"
|
|
||||||
person.first_name = "First Name #{person.id}"
|
|
||||||
person_count = person.id
|
|
||||||
|
|
||||||
child_group = JSGroup.new
|
|
||||||
child_group.id = group_count + 1
|
|
||||||
child_group.name = "Test Group #{child_group.id}"
|
|
||||||
group_count = child_group.id
|
|
||||||
|
|
||||||
group.groupees = [person, child_group]
|
|
||||||
group
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:jsonapis_person) do
|
|
||||||
person = JSPerson.new
|
|
||||||
person.id = 3
|
|
||||||
person
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_jsonapis_groups(count)
|
|
||||||
group_count = 0
|
|
||||||
person_count = 0
|
|
||||||
count.times.map do |i|
|
|
||||||
group = JSGroup.new
|
|
||||||
group.id = group_count + 1
|
|
||||||
group.name = "Test Group #{group.id}"
|
|
||||||
group_count = group.id
|
|
||||||
|
|
||||||
person = JSPerson.new
|
|
||||||
person.id = person_count + 1
|
|
||||||
person.last_name = "Last Name #{person.id}"
|
|
||||||
person.first_name = "First Name #{person.id}"
|
|
||||||
person_count = person.id
|
|
||||||
|
|
||||||
child_group = JSGroup.new
|
|
||||||
child_group.id = group_count + 1
|
|
||||||
child_group.name = "Test Group #{child_group.id}"
|
|
||||||
group_count = child_group.id
|
|
||||||
|
|
||||||
group.groupees = [person, child_group]
|
|
||||||
group
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,116 +0,0 @@
|
|||||||
RSpec.shared_context 'jsonapi movie class' do
|
|
||||||
before(:context) do
|
|
||||||
# models
|
|
||||||
class JSONAPIMovie
|
|
||||||
attr_accessor :id, :name, :release_year, :actors, :owner, :movie_type
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPIActor
|
|
||||||
attr_accessor :id, :name, :email
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPIUser
|
|
||||||
attr_accessor :id, :name
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPIMovieType
|
|
||||||
attr_accessor :id, :name
|
|
||||||
end
|
|
||||||
|
|
||||||
# serializers
|
|
||||||
class JSONAPIMovieSerializer < JSONAPI::Serializable::Resource
|
|
||||||
type 'movie'
|
|
||||||
attributes :name, :release_year
|
|
||||||
|
|
||||||
has_many :actors
|
|
||||||
has_one :owner
|
|
||||||
belongs_to :movie_type
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPIActorSerializer < JSONAPI::Serializable::Resource
|
|
||||||
type 'actor'
|
|
||||||
attributes :name, :email
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPIUserSerializer < JSONAPI::Serializable::Resource
|
|
||||||
type 'user'
|
|
||||||
attributes :name
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPIMovieTypeSerializer < JSONAPI::Serializable::Resource
|
|
||||||
type 'movie_type'
|
|
||||||
attributes :name
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPISerializer
|
|
||||||
def initialize(data, options = {})
|
|
||||||
@serializer = JSONAPI::Serializable::Renderer.new
|
|
||||||
@options = options.merge(class: {
|
|
||||||
JSONAPIMovie: JSONAPIMovieSerializer,
|
|
||||||
JSONAPIActor: JSONAPIActorSerializer,
|
|
||||||
JSONAPIUser: JSONAPIUserSerializer,
|
|
||||||
JSONAPIMovieType: JSONAPIMovieTypeSerializer
|
|
||||||
})
|
|
||||||
@data = data
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_json
|
|
||||||
@serializer.render(@data, @options).to_json
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_hash
|
|
||||||
@serializer.render(@data, @options)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
after :context do
|
|
||||||
classes_to_remove = %i[
|
|
||||||
JSONAPIMovie
|
|
||||||
JSONAPIActor
|
|
||||||
JSONAPIUser
|
|
||||||
JSONAPIMovieType
|
|
||||||
JSONAPIMovieSerializer
|
|
||||||
JSONAPIActorSerializer
|
|
||||||
JSONAPIUserSerializer
|
|
||||||
JSONAPIMovieTypeSerializer]
|
|
||||||
classes_to_remove.each do |klass_name|
|
|
||||||
Object.send(:remove_const, klass_name) if Object.constants.include?(klass_name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:jsonapi_actors) do
|
|
||||||
3.times.map do |i|
|
|
||||||
j = JSONAPIActor.new
|
|
||||||
j.id = i + 1
|
|
||||||
j.name = "Test #{j.id}"
|
|
||||||
j.email = "test#{j.id}@test.com"
|
|
||||||
j
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:jsonapi_user) do
|
|
||||||
jsonapi_user = JSONAPIUser.new
|
|
||||||
jsonapi_user.id = 3
|
|
||||||
jsonapi_user
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:jsonapi_movie_type) do
|
|
||||||
jsonapi_movie_type = JSONAPIMovieType.new
|
|
||||||
jsonapi_movie_type.id = 1
|
|
||||||
jsonapi_movie_type.name = 'episode'
|
|
||||||
jsonapi_movie_type
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_jsonapi_movies(count)
|
|
||||||
count.times.map do |i|
|
|
||||||
m = JSONAPIMovie.new
|
|
||||||
m.id = i + 1
|
|
||||||
m.name = 'test movie'
|
|
||||||
m.actors = jsonapi_actors
|
|
||||||
m.owner = jsonapi_user
|
|
||||||
m.movie_type = jsonapi_movie_type
|
|
||||||
m
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,112 +0,0 @@
|
|||||||
RSpec.shared_context 'jsonapi group class' do
|
|
||||||
|
|
||||||
# Person, Group Classes and serializers
|
|
||||||
before(:context) do
|
|
||||||
# models
|
|
||||||
class JSONAPIPerson
|
|
||||||
attr_accessor :id, :first_name, :last_name
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPIGroup
|
|
||||||
attr_accessor :id, :name, :groupees # Let's assume groupees can be Person or Group objects
|
|
||||||
end
|
|
||||||
|
|
||||||
# serializers
|
|
||||||
class JSONAPIPersonSerializer < JSONAPI::Serializable::Resource
|
|
||||||
type 'person'
|
|
||||||
attributes :first_name, :last_name
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPIGroupSerializer < JSONAPI::Serializable::Resource
|
|
||||||
type 'group'
|
|
||||||
attributes :name
|
|
||||||
has_many :groupees
|
|
||||||
end
|
|
||||||
|
|
||||||
class JSONAPISerializerB
|
|
||||||
def initialize(data, options = {})
|
|
||||||
@serializer = JSONAPI::Serializable::Renderer.new
|
|
||||||
@options = options.merge(class: {
|
|
||||||
JSONAPIPerson: JSONAPIPersonSerializer,
|
|
||||||
JSONAPIGroup: JSONAPIGroupSerializer
|
|
||||||
})
|
|
||||||
@data = data
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_json
|
|
||||||
@serializer.render(@data, @options).to_json
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_hash
|
|
||||||
@serializer.render(@data, @options)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
after :context do
|
|
||||||
classes_to_remove = %i[
|
|
||||||
JSONAPIPerson
|
|
||||||
JSONAPIGroup
|
|
||||||
JSONAPIPersonSerializer
|
|
||||||
JSONAPIGroupSerializer]
|
|
||||||
classes_to_remove.each do |klass_name|
|
|
||||||
Object.send(:remove_const, klass_name) if Object.constants.include?(klass_name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:jsonapi_groups) do
|
|
||||||
group_count = 0
|
|
||||||
person_count = 0
|
|
||||||
3.times.map do |i|
|
|
||||||
group = JSONAPIGroup.new
|
|
||||||
group.id = group_count + 1
|
|
||||||
group.name = "Test Group #{group.id}"
|
|
||||||
group_count = group.id
|
|
||||||
|
|
||||||
person = JSONAPIPerson.new
|
|
||||||
person.id = person_count + 1
|
|
||||||
person.last_name = "Last Name #{person.id}"
|
|
||||||
person.first_name = "First Name #{person.id}"
|
|
||||||
person_count = person.id
|
|
||||||
|
|
||||||
child_group = JSONAPIGroup.new
|
|
||||||
child_group.id = group_count + 1
|
|
||||||
child_group.name = "Test Group #{child_group.id}"
|
|
||||||
group_count = child_group.id
|
|
||||||
|
|
||||||
group.groupees = [person, child_group]
|
|
||||||
group
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:jsonapi_person) do
|
|
||||||
person = JSONAPIPerson.new
|
|
||||||
person.id = 3
|
|
||||||
person
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_jsonapi_groups(count)
|
|
||||||
group_count = 0
|
|
||||||
person_count = 0
|
|
||||||
count.times.map do |i|
|
|
||||||
group = JSONAPIGroup.new
|
|
||||||
group.id = group_count + 1
|
|
||||||
group.name = "Test Group #{group.id}"
|
|
||||||
group_count = group.id
|
|
||||||
|
|
||||||
person = JSONAPIPerson.new
|
|
||||||
person.id = person_count + 1
|
|
||||||
person.last_name = "Last Name #{person.id}"
|
|
||||||
person.first_name = "First Name #{person.id}"
|
|
||||||
person_count = person.id
|
|
||||||
|
|
||||||
child_group = JSONAPIGroup.new
|
|
||||||
child_group.id = group_count + 1
|
|
||||||
child_group.name = "Test Group #{child_group.id}"
|
|
||||||
group_count = child_group.id
|
|
||||||
|
|
||||||
group.groupees = [person, child_group]
|
|
||||||
group
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,21 +1,6 @@
|
|||||||
require 'active_record'
|
require 'active_record'
|
||||||
require 'fast_jsonapi'
|
require 'fast_jsonapi'
|
||||||
require 'rspec-benchmark'
|
|
||||||
require 'byebug'
|
require 'byebug'
|
||||||
require 'active_model_serializers'
|
|
||||||
require 'oj'
|
|
||||||
require 'jsonapi/serializable'
|
|
||||||
require 'jsonapi-serializers'
|
|
||||||
|
|
||||||
Dir[File.dirname(__FILE__) + '/shared/contexts/*.rb'].each {|file| require file }
|
Dir[File.dirname(__FILE__) + '/shared/contexts/*.rb'].each {|file| require file }
|
||||||
Dir[File.dirname(__FILE__) + '/shared/examples/*.rb'].each {|file| require file }
|
Dir[File.dirname(__FILE__) + '/shared/examples/*.rb'].each {|file| require file }
|
||||||
|
|
||||||
RSpec.configure do |config|
|
|
||||||
config.include RSpec::Benchmark::Matchers
|
|
||||||
config.filter_run_excluding performance: ENV['BENCHMARK'].blank?
|
|
||||||
end
|
|
||||||
|
|
||||||
Oj.optimize_rails
|
|
||||||
ActiveModel::Serializer.config.adapter = :json_api
|
|
||||||
ActiveModel::Serializer.config.key_transform = :underscore
|
|
||||||
ActiveModelSerializers.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new('/dev/null'))
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user