Skip to content

Commit 62de3d8

Browse files
committed
Setup dummy test server
1 parent 8981683 commit 62de3d8

File tree

6 files changed

+250
-0
lines changed

6 files changed

+250
-0
lines changed

active_model_serializers.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
1717
spec.files = `git ls-files -z`.split("\x0")
1818
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
1919
spec.require_paths = ['lib']
20+
spec.executables = []
2021

2122
spec.required_ruby_version = '>= 2.0.0'
2223

bin/serve_dummy

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
case "$1" in
5+
6+
start)
7+
config="${CONFIG_RU:-test/dummy/config.ru}"
8+
bundle exec ruby -Ilib -S rackup "$config" --daemonize --pid tmp/dummy_app.pid --server webrick
9+
until [ -f 'tmp/dummy_app.pid' ]; do
10+
sleep 0.1 # give it time to start.. I don't know a better way
11+
done
12+
cat tmp/dummy_app.pid
13+
true
14+
;;
15+
16+
stop)
17+
if [ -f 'tmp/dummy_app.pid' ]; then
18+
kill -TERM $(cat tmp/dummy_app.pid)
19+
else
20+
# echo 'No pidfile'
21+
false
22+
fi
23+
;;
24+
25+
status)
26+
if [ -f 'tmp/dummy_app.pid' ]; then
27+
kill -0 $(cat tmp/dummy_app.pid)
28+
[ "$?" -eq 0 ]
29+
else
30+
# echo 'No pidfile'
31+
false
32+
fi
33+
;;
34+
35+
*)
36+
echo "Usage: $0 [start|stop|status]"
37+
;;
38+
39+
esac

test/dummy/app.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# https://github.com/rails-api/active_model_serializers/pull/872
2+
# approx ref 792fb8a9053f8db3c562dae4f40907a582dd1720 to test against
3+
require 'bundler/setup'
4+
5+
require 'rails'
6+
require 'active_model'
7+
require 'active_support'
8+
require 'active_support/json'
9+
require 'action_controller'
10+
require 'action_controller/test_case'
11+
require 'action_controller/railtie'
12+
abort "Rails application already defined: #{Rails.application.class}" if Rails.application
13+
14+
# ref: https://gist.github.com/bf4/8744473
15+
class DummyApp < Rails::Application
16+
config.action_controller.perform_caching = ENV['CACHE_ON'] != 'off'
17+
config.action_controller.cache_store = :memory_store
18+
19+
# Set up production configuration
20+
config.eager_load = true
21+
config.cache_classes = true
22+
23+
config.active_support.test_order = :random
24+
config.secret_token = '1234'
25+
config.secret_key_base = 'abc123'
26+
config.logger = Logger.new(IO::NULL)
27+
28+
class DummyLogger < ActiveSupport::Logger
29+
def initialize
30+
@file = StringIO.new
31+
super(@file)
32+
end
33+
34+
def messages
35+
@file.rewind
36+
@file.read
37+
end
38+
end
39+
end
40+
41+
require 'active_model_serializers'
42+
43+
# Initialize app before any serializers are defined.
44+
# See https://github.com/rails-api/active_model_serializers/pull/1352.
45+
Rails.application.initialize!
46+
47+
require_relative 'fixtures'
48+
require_relative 'controllers'
49+
50+
# Clear cache
51+
ActionController::Base.cache_store.clear
52+
53+
# Test caching is on
54+
ActiveSupport::Cache::Store.logger = Logger.new(IO::NULL) # seems to be the best way
55+
# the below is used in some Rails tests but isn't available/working in all versions, so far as I can tell.
56+
# https://github.com/rails/rails/pull/15943
57+
# ActiveSupport::Notifications.subscribe(/^cache_(.*)\.active_support$/) do |*args|
58+
# p ActiveSupport::Notifications::Event.new(*args)
59+
# end

test/dummy/config.ru

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require File.join(File.dirname(__FILE__), 'app')
2+
3+
run Rails.application

test/dummy/controllers.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class PostController < ActionController::Base
2+
POST =
3+
begin
4+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
5+
author = Author.new(id: 1, name: 'Joao Moura.')
6+
Post.new(id: 1, title: 'New Post', blog: nil, body: 'Body', comments: [comment], author: author)
7+
end
8+
9+
def render_with_caching_serializer
10+
toggle_cache_status('on'.freeze)
11+
render json: POST, serializer: CachingPostSerializer, adapter: :json
12+
end
13+
14+
def render_with_non_caching_serializer
15+
toggle_cache_status('off'.freeze)
16+
render json: POST, serializer: PostSerializer, adapter: :json
17+
end
18+
19+
def render_cache_status
20+
toggle_cache_status
21+
# Uncomment to debug
22+
# STDERR.puts cache_store.class
23+
# STDERR.puts cache_dependencies if defined?(cache_dependencies)
24+
render json: { caching: perform_caching }.to_json
25+
end
26+
27+
private
28+
29+
def toggle_cache_status(cache_status = params[:on])
30+
case cache_status
31+
when 'on'.freeze then self.perform_caching = true
32+
when 'off'.freeze then self.perform_caching = false
33+
else nil # no-op
34+
end
35+
end
36+
end
37+
38+
Rails.application.routes.draw do
39+
get '/status(/:on)' => 'post#render_cache_status'
40+
get '/caching(/:on)' => 'post#render_with_caching_serializer'
41+
get '/non_caching(/:on)' => 'post#render_with_non_caching_serializer'
42+
end

test/dummy/fixtures.rb

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
class AuthorSerializer < ActiveModel::Serializer
2+
attributes :id, :name
3+
4+
has_many :posts, embed: :ids
5+
has_one :bio
6+
end
7+
8+
class BlogSerializer < ActiveModel::Serializer
9+
attributes :id, :name
10+
end
11+
12+
class CommentSerializer < ActiveModel::Serializer
13+
attributes :id, :body
14+
15+
belongs_to :post
16+
belongs_to :author
17+
end
18+
19+
class PostSerializer < ActiveModel::Serializer
20+
attributes :id, :title, :body
21+
22+
has_many :comments, serializer: CommentSerializer
23+
belongs_to :blog, serializer: BlogSerializer
24+
belongs_to :author, serializer: AuthorSerializer
25+
26+
def blog
27+
Blog.new(id: 999, name: 'Custom blog')
28+
end
29+
end
30+
31+
class CachingAuthorSerializer < AuthorSerializer
32+
cache key: 'writer', skip_digest: true
33+
end
34+
35+
class CachingCommentSerializer < CommentSerializer
36+
cache expires_in: 1.day, skip_digest: true
37+
end
38+
39+
class CachingPostSerializer < PostSerializer
40+
cache key: 'post', expires_in: 0.1, skip_digest: true
41+
belongs_to :blog, serializer: BlogSerializer
42+
belongs_to :author, serializer: CachingAuthorSerializer
43+
has_many :comments, serializer: CachingCommentSerializer
44+
end
45+
46+
# ActiveModelSerializers::Model is a convenient
47+
# serializable class to inherit from when making
48+
# serializable non-activerecord objects.
49+
class DummyModel
50+
include ActiveModel::Model
51+
include ActiveModel::Serializers::JSON
52+
53+
attr_reader :attributes
54+
55+
def initialize(attributes = {})
56+
@attributes = attributes
57+
super
58+
end
59+
60+
# Defaults to the downcased model name.
61+
def id
62+
attributes.fetch(:id) { self.class.name.downcase }
63+
end
64+
65+
# Defaults to the downcased model name and updated_at
66+
def cache_key
67+
attributes.fetch(:cache_key) { "#{self.class.name.downcase}/#{id}-#{updated_at.strftime("%Y%m%d%H%M%S%9N")}" }
68+
end
69+
70+
# Defaults to the time the serializer file was modified.
71+
def updated_at
72+
attributes.fetch(:updated_at) { File.mtime(__FILE__) }
73+
end
74+
75+
def read_attribute_for_serialization(key)
76+
if key == :id || key == 'id'
77+
attributes.fetch(key) { id }
78+
else
79+
attributes[key]
80+
end
81+
end
82+
end
83+
84+
class Comment < DummyModel
85+
attr_accessor :id, :body
86+
87+
def cache_key
88+
"#{self.class.name.downcase}/#{id}"
89+
end
90+
end
91+
92+
class Author < DummyModel
93+
attr_accessor :id, :name, :posts
94+
end
95+
96+
class Post < DummyModel
97+
attr_accessor :id, :title, :body, :comments, :blog, :author
98+
99+
def cache_key
100+
'benchmarking::post/1-20151215212620000000000'
101+
end
102+
end
103+
104+
class Blog < DummyModel
105+
attr_accessor :id, :name
106+
end

0 commit comments

Comments
 (0)