datasource 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,48 +0,0 @@
1
- require 'test_helper'
2
- require 'active_record_helper'
3
- require 'pry'
4
-
5
- class PostsDatasource < Datasource::Base
6
- attributes :id, :title, :blog_id
7
- end
8
-
9
- class BlogsDatasource < Datasource::Base
10
- attributes :id, :title
11
- includes_many :posts, PostsDatasource, :blog_id
12
- end
13
-
14
- class BlogsAndPostsSerializer < Datasource::Serializer::Composite
15
- hash do
16
- key :blogs do
17
- datasource BlogsDatasource
18
- attributes :id, :title,
19
- posts: { select: [ :id ], scope: Post.all }
20
- end
21
-
22
- key :posts do
23
- datasource PostsDatasource
24
- attributes :id, :title
25
- end
26
- end
27
- end
28
-
29
- class SerializerCompositeTest < ActiveSupport::TestCase
30
- # SELECT blogs.id, blogs.title FROM "blogs"
31
- # SELECT posts.id, posts.blog_id FROM "posts" WHERE (posts.blog_id IN (1,2))
32
- # SELECT posts.id, posts.title FROM "posts"
33
- def test_blogs_and_posts_serializer
34
- blog = Blog.create! title: "Blog 1"
35
- blog.posts.create! title: "Post 1", author_first_name: "John", author_last_name: "Doe"
36
- blog.posts.create! title: "Post 2", author_first_name: "Maria", author_last_name: "Doe"
37
- blog = Blog.create! title: "Blog 2"
38
-
39
- serializer = BlogsAndPostsSerializer.new(Blog.all, Post.all)
40
-
41
- assert_equal({"blogs"=>[{"id"=>1, "title"=>"Blog 1", "posts"=>[{"id"=>1}, {"id"=>2}]}, {"id"=>2, "title"=>"Blog 2", "posts"=>[]}], "posts"=>[{"id"=>1, "title"=>"Post 1"}, {"id"=>2, "title"=>"Post 2"}]},
42
- serializer.as_json)
43
- end
44
-
45
- def teardown
46
- clean_db
47
- end
48
- end