Skip to content

Commit

Permalink
Add tests for railtie
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Nov 6, 2020
1 parent 40c8c06 commit 897ca5d
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Style/TrailingCommaInArrayLiteral:
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: comma

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma

Style/Lambda:
EnforcedStyle: literal

Expand Down
29 changes: 19 additions & 10 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in oj_serializers.gemspec
gemspec

gem 'pry-byebug'
gem 'rake', '~> 12.0'
gem 'rspec', '~> 3.0'
gem 'rubocop'

# Test library-specific optimizations.
gem 'active_model_serializers', '~> 0.8', require: false
gem 'activerecord', require: false
gem 'benchmark-ips', require: false
gem 'mongoid', require: false
gem 'memory_profiler', require: false
gem 'sqlite3', require: false
group :development do
gem 'pry-byebug'
gem 'rubocop'
end

group :test do
gem 'active_model_serializers', '~> 0.8'
gem 'activerecord'
gem 'mongoid'
gem 'rspec', '~> 3.0'
gem 'rspec-rails'
gem 'simplecov', '< 0.18'
gem 'sqlite3'
end

group :performance do
gem 'benchmark-ips'
gem 'memory_profiler'
end
17 changes: 17 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ GEM
concurrent-ruby (1.1.7)
crass (1.0.6)
diff-lcs (1.4.4)
docile (1.3.2)
erubi (1.9.0)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
json (2.3.1)
jsonapi-renderer (0.2.2)
loofah (2.7.0)
crass (~> 1.0.2)
Expand Down Expand Up @@ -105,6 +107,14 @@ GEM
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-rails (4.0.1)
actionpack (>= 4.2)
activesupport (>= 4.2)
railties (>= 4.2)
rspec-core (~> 3.9)
rspec-expectations (~> 3.9)
rspec-mocks (~> 3.9)
rspec-support (~> 3.9)
rspec-support (3.9.4)
rubocop (0.86.0)
parallel (~> 1.10)
Expand All @@ -118,6 +128,11 @@ GEM
rubocop-ast (0.1.0)
parser (>= 2.7.0.1)
ruby-progressbar (1.10.1)
simplecov (0.17.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
sqlite3 (1.4.2)
thor (1.0.1)
thread_safe (0.3.6)
Expand All @@ -141,7 +156,9 @@ DEPENDENCIES
railties (>= 4.0)
rake (~> 12.0)
rspec (~> 3.0)
rspec-rails
rubocop
simplecov (< 0.18)
sqlite3

BUNDLED WITH
Expand Down
11 changes: 8 additions & 3 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class AlbumSerializer < ActiveModel::Serializer

has_many :songs

def album
object
end

def release
album.release_date.strftime('%B %d, %Y')
end
Expand Down Expand Up @@ -96,12 +100,13 @@ class AlbumSerializer < Oj::Serializer
def release
album.release_date.strftime('%B %d, %Y')
end, if: -> { album.released? }

# NOTE: This shorthand syntax might not be very palatable at first, but having
# the entire definition in one place makes it a lot easier to follow.
end
```

The shorthand syntax for serializer attributes might not be very palatable at
first, but having the entire definition in one place makes it a lot easier to
follow, specially in large serializers.

## Migrate gradually, one at a time

You can use these serializers inside arrays, hashes, or even inside `ActiveModel::Serializer`.
Expand Down
4 changes: 2 additions & 2 deletions lib/oj_serializers/controller_serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ module OjSerializers::ControllerSerialization
define_method renderer_method do |resource, **options|
serializer_class = options[:serializer] || options[:each_serializer]
if serializer_class && serializer_class < OjSerializers::Serializer
super(JsonStringEncoder.encode_to_json(resource, options), options)
super(OjSerializers::JsonStringEncoder.encode_to_json(resource, options), options.except(:root, :serializer, :each_serializer))
else
super(json_string, options)
super(resource, **options)
end
end
end
Expand Down
Empty file added log/test.log
Empty file.
39 changes: 34 additions & 5 deletions spec/oj_serializers/sugar_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
# frozen_string_literal: true

require 'spec_helper'
require 'rails/all'
require 'rspec/rails'

require 'oj_serializers/sugar'
require 'support/controllers/albums_controller'

RSpec.describe AlbumsController, type: :controller do
it 'should work as expected' do
get :index
get :show, params: { id: 'example' }
binding.pry
before(:all) do
Rails.application.quick_setup
end

it 'should be able to use serializer options and legacy serializers' do
get :list
albums = parse_json[:albums]
expect(albums.size).to eq 3

get :show
album = parse_json
songs = album[:songs]
expect(album).to eq albums.first
expect(album).to include(
name: 'Abraxas',
release: 'September 23, 1970',
genres: ['Pyschodelic Rock', 'Blues Rock', 'Jazz Fusion', 'Latin Rock'],
)
expect(songs.size).to eq 9
expect(songs.second).to eq(
track: 2,
name: 'Black Magic Woman / Gypsy Queen',
composers: ['Peter Green', 'Gábor Szabó'],
)

get :legacy_list
legacy_albums = parse_json[:albums]
expect(legacy_albums).to eq albums

get :legacy_show
legacy_album = parse_json
expect(legacy_album).to eq album
end
end
16 changes: 16 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

ENV['RACK_ENV'] ||= 'test'

require 'simplecov'
SimpleCov.start { add_filter '/spec/' }

require 'bundler/setup'
require 'oj_serializers'
require 'pry-byebug'
Expand All @@ -15,6 +18,17 @@ def as_json(_options = nil)
end
end

module JsonHelpers
def parse_json(json = response.body)
item = JSON.parse(json)
item.is_a?(Array) ? item.map(&:deep_symbolize_keys) : item.deep_symbolize_keys!
end

def expect_parsed_json(json = response.body)
expect(parse_json(json))
end
end

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
Expand All @@ -25,4 +39,6 @@ def as_json(_options = nil)
config.expect_with :rspec do |c|
c.syntax = :expect
end

config.include JsonHelpers
end
32 changes: 27 additions & 5 deletions spec/support/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
# frozen_string_literal: true

require 'oj_serializers/sugar'
require 'actionpack'
require_relative 'application'

class AlbumsController < ActionController::Base
require 'support/models/album'
require 'support/serializers/album_serializer'
require 'support/serializers/legacy_serializers'

class AlbumsController < ApplicationController
def show
album = Album.abraxas
render json: album, serializer: AlbumSerializer
end

def index
albums = [Album.abraxas]
def list
albums = [Album.abraxas] * 3
render json: albums, each_serializer: AlbumSerializer, root: :albums
end

def legacy_show
album = Album.abraxas
render json: album, serializer: LegacyAlbumSerializer
end

def legacy_list
albums = [Album.abraxas] * 3
render json: { albums: albums.map { |album| LegacyAlbumSerializer.new(album) } }
end
end

Rails.application.routes.draw do
resource :albums, only: [] do
get :show, on: :collection
get :list, on: :collection
get :legacy_show, on: :collection
get :legacy_list, on: :collection
end
end
18 changes: 18 additions & 0 deletions spec/support/controllers/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'rails'
require 'action_pack'
require 'action_controller/railtie'
require 'support/models/sql'

class MusicApplication < Rails::Application
def quick_setup
initializers.find { |i| i.name == 'active_model_serializers.action_controller' }.run
initializers.find { |i| i.name == 'oj_serializers.action_controller' }.run
end
end

class ApplicationController < ActionController::Base
end

require 'oj_serializers/sugar'
7 changes: 2 additions & 5 deletions spec/support/serializers/album_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
class AlbumSerializer < Oj::Serializer
mongo_attributes(
:id,
:name
)

attributes(
:genres
:name,
:genres,
)

has_many :songs, serializer: SongSerializer
Expand Down
42 changes: 42 additions & 0 deletions spec/support/serializers/legacy_serializers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require 'active_model_serializers'

# https://github.com/rails-api/active_model_serializers/blob/0-8-stable/README.md#1-disable-root-globally-for-all-or-per-class
ActiveSupport.on_load(:active_model_serializers) do
# Disable for all serializers (except ArraySerializer)
ActiveModel::Serializer.root = false

# Disable for ArraySerializer
ActiveModel::ArraySerializer.root = false
end

class LegacySongSerializer < ActiveModel::Serializer
attributes(
:track,
:name,
:composers,
)

def composers
object.composer&.split(', ')
end
end

class LegacyAlbumSerializer < ActiveModel::Serializer
attributes(
:name,
:genres,
:release,
)

has_many :songs, serializer: LegacySongSerializer

def release
object.release_date.strftime('%B %d, %Y')
end

def include_release?
object.released?
end
end

0 comments on commit 897ca5d

Please sign in to comment.