-
Notifications
You must be signed in to change notification settings - Fork 12
Proper tests. #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| require 'spec_helper' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing frozen string literal comment. |
||
|
|
||
| require 'jsonapi/include_directive' | ||
|
|
||
| describe JSONAPI::IncludeDirective, '.key?' do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,26 @@ | ||
| require 'jsonapi/renderer' | ||
| require 'spec_helper' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing frozen string literal comment. |
||
|
|
||
| class Model | ||
| def initialize(params) | ||
| params.each { |k, v| instance_variable_set("@#{k}", v) } | ||
| end | ||
| end | ||
|
|
||
| class User < Model | ||
| class UserResource | ||
| attr_accessor :id, :name, :address, :posts | ||
| end | ||
|
|
||
| class Post < Model | ||
| attr_accessor :id, :title, :date, :author | ||
| end | ||
|
|
||
| class UserResource | ||
| def initialize(user) | ||
| @user = user | ||
| def initialize(id, name, address, posts) | ||
| @id = id | ||
| @name = name | ||
| @address = address | ||
| @posts = posts | ||
| end | ||
|
|
||
| def jsonapi_type | ||
| 'users' | ||
| end | ||
|
|
||
| def jsonapi_id | ||
| @user.id.to_s | ||
| @id.to_s | ||
| end | ||
|
|
||
| def jsonapi_related(included) | ||
| if included.include?(:posts) | ||
| { posts: @user.posts.map { |p| PostResource.new(p) } } | ||
| { posts: @posts.map { |p| p } } | ||
| else | ||
| {} | ||
| end | ||
|
|
@@ -40,15 +31,15 @@ def as_jsonapi(options = {}) | |
| included = options[:include] || [] | ||
|
|
||
| hash = { id: jsonapi_id, type: jsonapi_type } | ||
| hash[:attributes] = { name: @user.name, address: @user.address } | ||
| hash[:attributes] = { name: @name, address: @address } | ||
| .select { |k, _| fields.include?(k) } | ||
| if fields.include?(:posts) | ||
| hash[:relationships] = { posts: {} } | ||
| hash[:relationships][:posts] = { | ||
| links: { | ||
| self: "http://api.example.com/users/#{@user.id}/relationships/posts", | ||
| self: "http://api.example.com/users/#{@id}/relationships/posts", | ||
| related: { | ||
| href: "http://api.example.com/users/#{@user.id}/posts", | ||
| href: "http://api.example.com/users/#{@id}/posts", | ||
| meta: { | ||
| do_not_use: true | ||
| } | ||
|
|
@@ -59,14 +50,14 @@ def as_jsonapi(options = {}) | |
| } | ||
| } | ||
| if included.include?(:posts) | ||
| hash[:relationships][:posts][:data] = @user.posts.map do |p| | ||
| hash[:relationships][:posts][:data] = @posts.map do |p| | ||
| { type: 'posts', id: p.id.to_s } | ||
| end | ||
| end | ||
| end | ||
|
|
||
| hash[:links] = { | ||
| self: "http://api.example.com/users/#{@user.id}" | ||
| self: "http://api.example.com/users/#{@id}" | ||
| } | ||
| hash[:meta] = { user_meta: 'is_meta' } | ||
|
|
||
|
|
@@ -75,46 +66,51 @@ def as_jsonapi(options = {}) | |
| end | ||
|
|
||
| class PostResource | ||
| def initialize(post) | ||
| @post = post | ||
| attr_accessor :id, :title, :date, :author | ||
|
|
||
| def initialize(id, title, date, author) | ||
| @id = id | ||
| @title = title | ||
| @date = date | ||
| @author = author | ||
| end | ||
|
|
||
| def jsonapi_type | ||
| 'posts' | ||
| end | ||
|
|
||
| def jsonapi_id | ||
| @post.id.to_s | ||
| @id.to_s | ||
| end | ||
|
|
||
| def jsonapi_related(included) | ||
| included.include?(:author) ? { author: UserResource.new(@post.author) } : {} | ||
| included.include?(:author) ? { author: @author } : {} | ||
| end | ||
|
|
||
| def as_jsonapi(options = {}) | ||
| fields = options[:fields] || [:title, :date, :author] | ||
| included = options[:include] || [] | ||
| hash = { id: jsonapi_id, type: jsonapi_type } | ||
|
|
||
| hash[:attributes] = { title: @post.title, date: @post.date } | ||
| hash[:attributes] = { title: @title, date: @date } | ||
| .select { |k, _| fields.include?(k) } | ||
| if fields.include?(:author) | ||
| hash[:relationships] = { author: {} } | ||
| hash[:relationships][:author] = { | ||
| links: { | ||
| self: "http://api.example.com/posts/#{@post.id}/relationships/author", | ||
| related: "http://api.example.com/posts/#{@post.id}/author" | ||
| self: "http://api.example.com/posts/#{@id}/relationships/author", | ||
| related: "http://api.example.com/posts/#{@id}/author" | ||
| }, | ||
| meta: { | ||
| author_active: true | ||
| } | ||
| } | ||
| if included.include?(:author) | ||
| hash[:relationships][:author][:data] = | ||
| if @post.author.nil? | ||
| if @author.nil? | ||
| nil | ||
| else | ||
| { type: 'users', id: @post.author.id.to_s } | ||
| { type: 'users', id: @author.id.to_s } | ||
| end | ||
| end | ||
| end | ||
|
|
@@ -126,15 +122,15 @@ def as_jsonapi(options = {}) | |
| describe JSONAPI, '#render' do | ||
| before(:all) do | ||
| @users = [ | ||
| User.new(id: 1, name: 'User 1', address: '123 Example st.', posts: []), | ||
| User.new(id: 2, name: 'User 2', address: '234 Example st.', posts: []), | ||
| User.new(id: 3, name: 'User 3', address: '345 Example st.', posts: []), | ||
| User.new(id: 4, name: 'User 4', address: '456 Example st.', posts: []) | ||
| UserResource.new(1, 'User 1', '123 Example st.', []), | ||
| UserResource.new(2, 'User 2', '234 Example st.', []), | ||
| UserResource.new(3, 'User 3', '345 Example st.', []), | ||
| UserResource.new(4, 'User 4', '456 Example st.', []) | ||
| ] | ||
| @posts = [ | ||
| Post.new(id: 1, title: 'Post 1', date: 'yesterday', author: @users[1]), | ||
| Post.new(id: 2, title: 'Post 2', date: 'today', author: @users[0]), | ||
| Post.new(id: 3, title: 'Post 3', date: 'tomorrow', author: @users[1]) | ||
| PostResource.new(1, 'Post 1', 'yesterday', @users[1]), | ||
| PostResource.new(2, 'Post 2', 'today', @users[0]), | ||
| PostResource.new(3, 'Post 3', 'tomorrow', @users[1]) | ||
| ] | ||
| @users[0].posts = [@posts[1]] | ||
| @users[1].posts = [@posts[0], @posts[2]] | ||
|
|
@@ -159,7 +155,7 @@ def as_jsonapi(options = {}) | |
| end | ||
|
|
||
| it 'renders a single resource' do | ||
| actual = JSONAPI.render(data: UserResource.new(@users[0])) | ||
| actual = JSONAPI.render(data: @users[0]) | ||
| expected = { | ||
| data: { | ||
| type: 'users', | ||
|
|
@@ -197,8 +193,8 @@ def as_jsonapi(options = {}) | |
| end | ||
|
|
||
| it 'renders a collection of resources' do | ||
| actual = JSONAPI.render(data: [UserResource.new(@users[0]), | ||
| UserResource.new(@users[1])]) | ||
| actual = JSONAPI.render(data: [@users[0], | ||
| @users[1]]) | ||
| expected = { | ||
| data: [ | ||
| { | ||
|
|
@@ -268,7 +264,7 @@ def as_jsonapi(options = {}) | |
| end | ||
|
|
||
| it 'renders included relationships' do | ||
| actual = JSONAPI.render(data: UserResource.new(@users[0]), | ||
| actual = JSONAPI.render(data: @users[0], | ||
| include: 'posts') | ||
| expected = { | ||
| data: { | ||
|
|
@@ -329,7 +325,7 @@ def as_jsonapi(options = {}) | |
| end | ||
|
|
||
| it 'filters out fields' do | ||
| actual = JSONAPI.render(data: UserResource.new(@users[0]), | ||
| actual = JSONAPI.render(data: @users[0], | ||
| fields: { users: [:name] }) | ||
| expected = { | ||
| data: { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| require 'simplecov' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing frozen string literal comment. |
||
| SimpleCov.start | ||
|
|
||
| require 'codecov' | ||
| SimpleCov.formatter = SimpleCov::Formatter::Codecov | ||
|
|
||
| require 'jsonapi/renderer' | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing frozen string literal comment.