Skip to content

Instantly share code, notes, and snippets.

@groyoh
Last active December 21, 2017 05:12
Show Gist options
  • Save groyoh/aecf04fb34d42e8756bec536f5013c1b to your computer and use it in GitHub Desktop.
Save groyoh/aecf04fb34d42e8756bec536f5013c1b to your computer and use it in GitHub Desktop.
AMS #1950
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'active_model_serializers', '0.10.2'
gem 'sequel'
gem 'sqlite3'
gem 'minitest'
end
DB = Sequel.sqlite
DB.create_table(:zones) do
uuid :id
primary_key [:id]
end
DB.create_table(:devices) do
uuid :id
primary_key [:id]
foreign_key :zone_id, :zones
end
class Zone < Sequel::Model
extend ActiveModel::Naming
alias_method :read_attribute_for_serialization, :public_send
one_to_many :devices
end
class Device < Sequel::Model
extend ActiveModel::Naming
alias_method :read_attribute_for_serialization, :public_send
many_to_one :zone
end
class ZoneSerializer < ActiveModel::Serializer
attributes :id
has_many :devices
end
class DeviceSerializer < ActiveModel::Serializer
attributes :id
end
require 'minitest/autorun'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
class BugTest < Minitest::Test
def test_ams
z = Zone.new.tap { |z| z.id = "2d931510-d99f-494a-8c67-87feb05e1594" }.save
Device.new(zone: z).tap { |d| d.id = "2d931510-d99f-494a-8c67-87feb05e1595" }.save
Device.new(zone: z).tap { |d| d.id = "2d931510-d99f-494a-8c67-87feb05e1596" }.save
hash = ActiveModelSerializers::SerializableResource.new(z, adapter: :json_api).as_json
expected = {
:data => {
:id => "2d931510-d99f-494a-8c67-87feb05e1594",
:type => "zones",
:relationships => {
:devices => {
:data => [
{
:id => "2d931510-d99f-494a-8c67-87feb05e1595",
:type => "devices"
},
{
:id => "2d931510-d99f-494a-8c67-87feb05e1596",
:type => "devices"
}
]
}
}
}
}
assert_equal(expected, hash)
end
end
__END__
Fetching gem metadata from https://rubygems.org/........
Fetching version metadata from https://rubygems.org/.
Resolving dependencies...
Using rake 11.3.0
Using concurrent-ruby 1.0.2
Using i18n 0.7.0
Using minitest 5.9.1
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.1.0
Using rack 2.0.1
Using jsonapi-parser 0.1.1.beta2
Using jsonapi-renderer 0.1.1.beta1
Using method_source 0.8.2
Using thor 0.19.1
Using sequel 4.39.0
Using sqlite3 1.3.12
Using bundler 1.13.6
Using tzinfo 1.2.2
Using nokogiri 1.6.8.1
Using rack-test 0.6.3
Using jsonapi 0.1.1.beta5
Using activesupport 5.0.0.1
Using loofah 2.0.3
Using rails-dom-testing 2.0.1
Using activemodel 5.0.0.1
Using rails-html-sanitizer 1.0.3
Using actionview 5.0.0.1
Using actionpack 5.0.0.1
Using railties 5.0.0.1
Using active_model_serializers 0.10.2
Run options: --seed 19224
# Running:
[active_model_serializers] Rendered ZoneSerializer with ActiveModelSerializers::Adapter::JsonApi (2.54ms)
.
Finished in 0.008931s, 111.9666 runs/s, 111.9666 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment