roaster 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,103 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'roaster/query'
4
+
5
+ require_relative 'test_helper'
6
+
7
+ class QueryTest < MiniTest::Test
8
+
9
+ def setup
10
+ super
11
+ @target = Roaster::Query::Target.new(:albums)
12
+ @mapping = AlbumMapping
13
+ end
14
+
15
+ def test_page_defaults
16
+ q = build_query()
17
+ assert_equal 1, q.page
18
+ assert_equal Roaster::Query::DEFAULT_PAGE_SIZE, q.page_size
19
+ assert_equal [], q.includes
20
+ assert_equal Hash.new, q.filters
21
+ assert_equal Hash.new, q.sorting
22
+ end
23
+
24
+ def test_includes
25
+ q = build_query({ include: 'band,tracks' })
26
+ assert_equal [:band, :tracks], q.includes
27
+ end
28
+
29
+ def test_filters
30
+ q = build_query({ band: 1 })
31
+ assert_equal({band: 1}, q.filters)
32
+ end
33
+
34
+ # TODO: Make this one pass !
35
+ # def test_nested_filters
36
+ # q = build_query({
37
+ # fields: {
38
+ # band: {
39
+ # name: 'Pink Floyd'
40
+ # },
41
+ # albums: {
42
+ # title: 'Animals'
43
+ # }
44
+ # }
45
+ # })
46
+ # assert_equal({
47
+ # band: {
48
+ # name: 'Pink Floyd'
49
+ # },
50
+ # albums: {
51
+ # title: 'Animals'
52
+ # }
53
+ # }, q.filters)
54
+ # end
55
+
56
+ def test_simple_sorting
57
+ q = build_query({ sort: '-title,created_at' })
58
+ assert_equal({
59
+ albums: {
60
+ title: :desc,
61
+ created_at: :asc}
62
+ }, q.sorting)
63
+ end
64
+
65
+ def test_typed_sorting
66
+ q = build_query({ sort: {
67
+ band: 'name',
68
+ albums: '-created_at,title'
69
+ }})
70
+ assert_equal({
71
+ band: {
72
+ name: :asc
73
+ },
74
+ albums: {
75
+ created_at: :desc,
76
+ title: :asc
77
+ }
78
+ }, q.sorting)
79
+ end
80
+
81
+ def test_sparse_fieldsets
82
+ q = build_query({ fields: 'title' })
83
+ assert_equal({albums: [:title]}, q.fields)
84
+ end
85
+
86
+ def test_typed_sparse_fieldsets
87
+ q = build_query({ fields: {
88
+ 'bands' => 'name,created_at',
89
+ 'albums' => 'title'}
90
+ })
91
+ assert_equal({
92
+ bands: [:name, :created_at],
93
+ albums: [:title]
94
+ }, q.fields)
95
+ end
96
+
97
+ private
98
+
99
+ def build_query(params = {})
100
+ Roaster::Query.new(:read, @target, @mapping, params)
101
+ end
102
+
103
+ end
@@ -0,0 +1,16 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'roaster/request'
4
+
5
+ require_relative 'test_helper'
6
+ require_relative 'models/album'
7
+
8
+ class RequestTest < MiniTest::Test
9
+
10
+ def test_mapping_class_from_target
11
+ target = Roaster::Query::Target.new(:albums)
12
+ mc = Roaster::Request.mapping_class_from_target(target)
13
+ assert_equal AlbumMapping, mc
14
+ end
15
+
16
+ end
@@ -0,0 +1,11 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'roaster/request'
4
+
5
+ require_relative 'test_helper'
6
+ require_relative 'models/album'
7
+
8
+ class ResourceTest < MiniTest::Test
9
+
10
+
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'logger'
2
+ require 'active_model'
3
+ require 'active_record'
4
+
5
+ ActiveRecord::Base.establish_connection(
6
+ :adapter => "sqlite3", :database => ':memory:'
7
+ )
8
+
9
+ if ENV['DEBUG']
10
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
11
+ end
@@ -0,0 +1,27 @@
1
+ require 'roaster'
2
+ require 'database_cleaner'
3
+ require 'awesome_print'
4
+ require 'factory_girl'
5
+ require 'byebug'
6
+
7
+ require_relative 'support/active_record'
8
+ require_relative 'models/album'
9
+ require_relative 'models/band'
10
+ require_relative 'models/track'
11
+ require_relative 'mappings/album'
12
+ require_relative 'mappings/band'
13
+ require_relative 'mappings/track'
14
+
15
+
16
+ #DatabaseCleaner.strategy = :truncation
17
+ DatabaseCleaner.strategy = :transaction
18
+
19
+ class MiniTest::Test
20
+ def setup
21
+ DatabaseCleaner.start
22
+ end
23
+
24
+ def teardown
25
+ DatabaseCleaner.clean
26
+ end
27
+ end
data/test/wut_test.rb ADDED
@@ -0,0 +1,205 @@
1
+ require 'set'
2
+ require 'minitest/autorun'
3
+
4
+ require 'roaster/query'
5
+ require 'roaster/resource'
6
+ require 'roaster/request'
7
+
8
+ require_relative 'test_helper'
9
+ require_relative 'factories/album'
10
+ require_relative 'factories/track'
11
+ require_relative 'factories/band'
12
+
13
+ class PoniesTest < MiniTest::Test
14
+
15
+ def setup
16
+ super
17
+ #TODO: Replace this by fixtures
18
+ FactoryGirl.create(:animals_album)
19
+ FactoryGirl.create(:the_wall_album)
20
+ FactoryGirl.create(:meddle_album)
21
+ =begin
22
+ 1 - Enemy Within
23
+ 2 - Burning Angel
24
+ 3 - Heart Of Darkness
25
+ =end
26
+ @arch_enemy_band = FactoryGirl.create :band, name: 'Arch Enemy'
27
+ @wages_of_sin_album = FactoryGirl.create :album, title: 'Wages of Sin', band: @arch_enemy_band, tracks: [
28
+ FactoryGirl.create(:track, title: 'Enemy Within'),
29
+ FactoryGirl.create(:track, title: 'Burning Angel'),
30
+ FactoryGirl.create(:track, title: 'Heart Of Darkness')
31
+ ]
32
+ @ar_resource = Roaster::Resource.new(Roaster::Adapters::ActiveRecord)
33
+ end
34
+
35
+ def build_target(resource_name = :albums, resource_ids = nil, relationship_name = nil, relationship_ids = nil)
36
+ Roaster::Query::Target.new(resource_name, resource_ids, relationship_name, relationship_ids)
37
+ end
38
+
39
+ def build_request(operation, target: build_target, resource: @ar_resource, params: {}, document: nil)
40
+ Roaster::Request.new(operation,
41
+ target,
42
+ resource,
43
+ params,
44
+ document: document)
45
+ end
46
+
47
+ def test_ponies
48
+ rq = build_request(:read)
49
+ res = rq.execute
50
+ assert_equal([{'title' => 'Animals'}, {'title' => 'The Wall'}, {'title' => 'Meddle'}, {'title' => 'Wages of Sin'}], res)
51
+ end
52
+
53
+ def test_sorted_ponies
54
+ params = {sort: :title}
55
+ rq = build_request(:read, params: params)
56
+ res = rq.execute
57
+ assert_equal([{'title' => 'Animals'}, {'title' => 'Meddle'}, {'title' => 'The Wall'}, {'title' => 'Wages of Sin'}], res)
58
+ end
59
+
60
+ def test_simple_filtered_ponies
61
+ params = {title: 'Animals'}
62
+ rq = build_request(:read, params: params)
63
+ res = rq.execute
64
+ assert_equal([{'title' => 'Animals'}], res)
65
+ end
66
+
67
+ #TODO: Make this one pass !
68
+ #TODO: This is an extension, document it according to JSONAPI spec !
69
+ def test_association_filtered_ponies
70
+ return
71
+ params = {
72
+ band: {
73
+ name: @arch_enemy_band.name
74
+ }
75
+ }
76
+ rq = build_request(:read, params: params)
77
+ res = rq.execute
78
+ assert_equal 1, res.count
79
+ assert_equal @arch_enemy_band.name, res.first.band.name
80
+ end
81
+
82
+ def test_read_to_one_relationship
83
+ target = build_target(:albums, @wages_of_sin_album, :band)
84
+ rq = build_request(:read, target: target)
85
+ res = rq.execute
86
+ assert_equal({'name' => 'Arch Enemy'}, res)
87
+ end
88
+
89
+ def test_read_to_many_relationship
90
+ target = build_target(:albums, @wages_of_sin_album, :tracks)
91
+ rq = build_request(:read, target: target)
92
+ res = rq.execute
93
+ assert_equal([{'title' => 'Enemy Within'}, {'title' => 'Burning Angel'}, {'title' => 'Heart Of Darkness'}], res)
94
+ end
95
+
96
+ def test_create_pony
97
+ album_hash = {
98
+ 'title' => 'The Downward Spiral'
99
+ }
100
+ rq = build_request(:create, document: album_hash)
101
+
102
+ res = rq.execute
103
+ refute_nil res.id
104
+ assert_equal 'The Downward Spiral', res.title
105
+ end
106
+
107
+ def test_add_to_one_relationship
108
+ album = FactoryGirl.create :album, title: 'Ride the Lightning'
109
+ band = FactoryGirl.create :band, name: 'Metallica'
110
+
111
+ document = {
112
+ "bands" => band.id
113
+ }
114
+ target = build_target(:albums, album.id, :band)
115
+ rq = build_request(:create, target: target, document: document)
116
+
117
+ res = rq.execute
118
+ album.reload
119
+ assert_equal album.band.name, band.name
120
+ end
121
+
122
+ def test_add_to_many_relationship
123
+ track_1 = FactoryGirl.create :track, title: 'Fight Fire With Fire'
124
+ # Track 2 omitted because it has the same title as the album
125
+ track_3 = FactoryGirl.create :track, title: 'For Whom The Bell Tolls'
126
+ track_4 = FactoryGirl.create :track, title: 'Fade to Black'
127
+ album = FactoryGirl.create :album, title: 'Ride the Lightning', tracks: [track_1]
128
+
129
+ document = {
130
+ "tracks" => [track_3.id.to_s, track_4.id.to_s]
131
+ }
132
+ target = build_target(:albums, album.id, :tracks)
133
+ rq = build_request(:create, target: target, document: document)
134
+
135
+ res = rq.execute
136
+ album.reload
137
+ assert_equal 3, album.tracks.count
138
+ assert_equal Set.new(album.tracks.map(&:id)), Set.new([track_1, track_3, track_4].map(&:id))
139
+ end
140
+
141
+ def test_update_pony
142
+ album = FactoryGirl.create(:album)
143
+ album_update_hash = {
144
+ 'title' => 'Antichrist Superstar'
145
+ }
146
+ target = build_target(:albums, album.id)
147
+ rq = build_request(:update, target: target, document: album_update_hash)
148
+
149
+ res = rq.execute
150
+ assert_equal album.id, res.id
151
+ assert_equal 'Antichrist Superstar', res.title
152
+ end
153
+
154
+ def test_update_to_one_relationship
155
+ album = FactoryGirl.create(:album, title: 'Killing Is My Business... and Business Is Good!')
156
+ band = FactoryGirl.create(:band, name: 'Megadeth')
157
+ album_update_hash = {
158
+ 'links' => {
159
+ 'band' => band.id.to_s
160
+ }
161
+ }
162
+ target = build_target(:albums, album.id)
163
+ rq = build_request(:update, target: target, document: album_update_hash)
164
+
165
+ res = rq.execute
166
+ album.reload
167
+ assert_equal 'Megadeth', album.band.name
168
+ end
169
+
170
+ def test_update_to_many_relationship
171
+ track_1 = FactoryGirl.create :track, title: 'Fight Fire With Fire'
172
+ # Track 2 omitted because it has the same title as the album
173
+ track_3 = FactoryGirl.create :track, title: 'For Whom The Bell Tolls'
174
+ track_4 = FactoryGirl.create :track, title: 'Fade to Black'
175
+ album = FactoryGirl.create :album, title: 'Ride the Lightning', tracks: [track_1]
176
+
177
+ document = {
178
+ 'links' => {
179
+ 'tracks' => [track_3.id.to_s, track_4.id.to_s]
180
+ }
181
+ }
182
+ target = build_target(:albums, album.id, :tracks)
183
+ rq = build_request(:update, target: target, document: document)
184
+
185
+ res = rq.execute
186
+ album.reload
187
+ assert_equal 2, album.tracks.count
188
+ assert_equal Set.new(album.tracks.map(&:id)), Set.new([track_3, track_4].map(&:id))
189
+ end
190
+
191
+ def test_delete_pony
192
+ album = FactoryGirl.create(:album)
193
+ album_id = album.id
194
+ target = build_target(:albums, album_id)
195
+ rq = build_request(:delete)
196
+
197
+ res = rq.execute
198
+ refute Album.exists?(album_id)
199
+ end
200
+
201
+ def test
202
+
203
+ end
204
+
205
+ end
metadata CHANGED
@@ -1,25 +1,185 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Albeza
8
+ - Jérémy Lecerf
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-09-09 00:00:00.000000000 Z
12
- dependencies: []
12
+ date: 2014-09-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: representable
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '4.1'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '4.1'
42
+ - !ruby/object:Gem::Dependency
43
+ name: activesupport
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '4.1'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '4.1'
56
+ - !ruby/object:Gem::Dependency
57
+ name: minitest
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '5.1'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '5.1'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '10.3'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '10.3'
84
+ - !ruby/object:Gem::Dependency
85
+ name: factory_girl
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '4.4'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '4.4'
98
+ - !ruby/object:Gem::Dependency
99
+ name: sqlite3
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '1.3'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '1.3'
112
+ - !ruby/object:Gem::Dependency
113
+ name: database_cleaner
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '1.3'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '1.3'
126
+ - !ruby/object:Gem::Dependency
127
+ name: byebug
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: '3.4'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '3.4'
13
140
  description: Model/JSONAPI mapping
14
141
  email:
15
142
  - n.albeza@gmail.com
143
+ - redpist.com@gmail.com
16
144
  executables: []
17
145
  extensions: []
18
146
  extra_rdoc_files: []
19
147
  files:
20
148
  - ".gitignore"
149
+ - Gemfile
150
+ - Gemfile.lock
21
151
  - LICENSE
22
152
  - README.md
153
+ - Rakefile
154
+ - STUFF
155
+ - lib/roaster.rb
156
+ - lib/roaster/adapters/active_record.rb
157
+ - lib/roaster/decorator.rb
158
+ - lib/roaster/json_api.rb
159
+ - lib/roaster/query.rb
160
+ - lib/roaster/request.rb
161
+ - lib/roaster/resource.rb
162
+ - lib/roaster/version.rb
163
+ - roaster.gemspec
164
+ - test/activerecord_adapter_test.rb
165
+ - test/decorator_test.rb
166
+ - test/factories/album.rb
167
+ - test/factories/band.rb
168
+ - test/factories/track.rb
169
+ - test/mappings/album.rb
170
+ - test/mappings/band.rb
171
+ - test/mappings/track.rb
172
+ - test/models/album.rb
173
+ - test/models/band.rb
174
+ - test/models/namespaced/model.rb
175
+ - test/models/track.rb
176
+ - test/query_target_test.rb
177
+ - test/query_test.rb
178
+ - test/request_test.rb
179
+ - test/resource_test.rb
180
+ - test/support/active_record.rb
181
+ - test/test_helper.rb
182
+ - test/wut_test.rb
23
183
  homepage: https://github.com/pause/roaster
24
184
  licenses:
25
185
  - MIT
@@ -44,4 +204,23 @@ rubygems_version: 2.2.2
44
204
  signing_key:
45
205
  specification_version: 4
46
206
  summary: Expose your models through a JSONAPI API with a simple mapping
47
- test_files: []
207
+ test_files:
208
+ - test/activerecord_adapter_test.rb
209
+ - test/decorator_test.rb
210
+ - test/factories/album.rb
211
+ - test/factories/band.rb
212
+ - test/factories/track.rb
213
+ - test/mappings/album.rb
214
+ - test/mappings/band.rb
215
+ - test/mappings/track.rb
216
+ - test/models/album.rb
217
+ - test/models/band.rb
218
+ - test/models/namespaced/model.rb
219
+ - test/models/track.rb
220
+ - test/query_target_test.rb
221
+ - test/query_test.rb
222
+ - test/request_test.rb
223
+ - test/resource_test.rb
224
+ - test/support/active_record.rb
225
+ - test/test_helper.rb
226
+ - test/wut_test.rb