1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
require 'spec_helper'
describe PuppetForge::LazyRelations do
class PuppetForge::V3::Parent < PuppetForge::V3::Base
lazy :relation, 'Thing'
lazy :chained, 'Parent'
lazy_collection :relations, 'Thing'
lazy_collection :parents, 'Parent'
def uri
"/parents/#{slug}"
end
def slug
"1"
end
end
class PuppetForge::V3::Thing < PuppetForge::V3::Base
# Needed for #inspect
def uri
"/things/1"
end
# Needed for fetch
def slug
"1"
end
def standalone_method
"a/b/c"
end
def satisified_dependent_method
"-#{local}-"
end
def unsatisfied_dependent_method
"-#{remote}-"
end
def shadow
"-#{super}-"
end
def remote_shadow
"-#{super}-"
end
end
let(:base_class) { PuppetForge::V3::Base }
let(:klass) do |*args|
PuppetForge::V3::Parent
end
let(:related_class) do
PuppetForge::V3::Thing
end
let(:local_data) { { :id => 1, :local => 'data', :shadow => 'x' } }
let(:remote_data) { local_data.merge(:remote => 'DATA', :remote_shadow => 'X') }
describe '.lazy' do
subject { klass.new(:relation => local_data).relation }
it do
should be_a(related_class)
end
it 'does not call methods to #inspect' do
expect(subject).not_to receive(:shadow)
subject.inspect
end
describe 'local attributes' do
before { expect(related_class).not_to receive(:request) }
example 'allow access to local attributes' do
expect(subject.local).to eql('data')
end
example 'provide local attributes predicates' do
expect(subject.local?).to be true
end
example 'provide local attributes setters' do
subject.local = 'foo'
expect(subject.local).to eql('foo')
end
example 'allow access to local standalone methods' do
expect(subject.standalone_method).to eql('a/b/c')
end
example 'allow access to locally satisfiable methods' do
expect(subject.satisified_dependent_method).to eql('-data-')
end
example 'allow `super` access to shadowed attributes' do
expect(subject.shadow).to eql('-x-')
end
end
describe 'remote attributes' do
before do
stub_api_for(base_class) do |stubs|
stubs.get('/v3/things/1') do
[ 200, { 'Content-Type' => 'json' }, remote_data ]
end
end
end
example 'allow access to remote attributes' do
expect(subject.remote).to eql('DATA')
end
example 'provide remote attributes predicates' do
expect(subject.remote?).to be true
end
example 'provide remote attributes setters' do
subject.remote = 'foo'
expect(subject.remote).to eql('foo')
end
example 'allow multiple instances to access remote attributes' do
expect(related_class).to receive(:request) \
.exactly(9).times \
.and_call_original
9.times do
subject = klass.new(:relation => local_data).relation
expect(subject.remote).to eql('DATA')
end
end
example 'allow access to locally unsatisfiable methods' do
expect(subject.unsatisfied_dependent_method).to eql('-DATA-')
end
example 'allow `super` access to shadowed remote attributes' do
expect(subject.remote_shadow).to eql('-X-')
end
end
describe 'remote relations' do
before do
stub_api_for(base_class) do |api|
api.get('/v3/parents/1') do
data = { :id => 1, :relation => local_data }
[ 200, { 'Content-Type' => 'json' }, data ]
end
api.get('/v3/things/1') do
[ 200, { 'Content-Type' => 'json' }, remote_data ]
end
end
end
subject { klass.new(:chained => { :id => 1 }) }
example 'allow chained lookups of lazy relations' do
expect(subject.chained.relation.remote).to eql('DATA')
end
end
describe 'null relations' do
subject { klass.new(:relation => nil) }
example 'do not return new instances' do
expect(subject.relation).to be nil
end
end
describe 'unsatisfiable attributes' do
before do
stub_api_for(base_class) do |api|
api.get('/v3/parents/1') do
[ 200, { 'Content-Type' => 'json' }, remote_data ]
end
end
end
subject { klass.new(local_data) }
example 'raise an exception when accessing an unknown attribute' do
expect { subject.unknown_attribute }.to raise_error(NoMethodError)
end
end
end
describe '.lazy_collection' do
subject { klass.new(:relations => [local_data]).relations.first }
it { should be_a(related_class) }
it 'does not call methods to #inspect' do
expect(subject).not_to receive(:shadow)
subject.inspect
end
describe 'local attributes' do
before { expect(related_class).not_to receive(:request) }
example 'allow access to local attributes' do
expect(subject.local).to eql('data')
end
example 'provide local attributes predicates' do
expect(subject.local?).to be true
end
example 'provide local attributes setters' do
subject.local = 'foo'
expect(subject.local).to eql('foo')
end
example 'allow access to local standalone methods' do
expect(subject.standalone_method).to eql('a/b/c')
end
example 'allow access to locally satisfiable methods' do
expect(subject.satisified_dependent_method).to eql('-data-')
end
example 'allow `super` access to shadowed attributes' do
expect(subject.shadow).to eql('-x-')
end
end
describe 'remote attributes' do
before do
stub_api_for(base_class) do |api|
api.get('/v3/things/1') do
[ 200, { 'Content-Type' => 'json' }, remote_data ]
end
end
end
example 'allow access to remote attributes' do
expect(subject.remote).to eql('DATA')
end
example 'provide remote attributes predicates' do
expect(subject.remote?).to be true
end
example 'provide remote attributes setters' do
subject.remote = 'foo'
expect(subject.remote).to eql('foo')
end
example 'allow multiple instances to access remote attributes' do
expect(related_class).to receive(:request) \
.exactly(9).times \
.and_call_original
9.times do
subject = klass.new(:relations => [local_data]).relations.first
expect(subject.remote).to eql('DATA')
end
end
example 'allow access to locally unsatisfiable methods' do
expect(subject.unsatisfied_dependent_method).to eql('-DATA-')
end
example 'allow `super` access to shadowed remote attributes' do
expect(subject.remote_shadow).to eql('-X-')
end
end
describe 'remote relations' do
before do
stub_api_for(base_class) do |api|
api.get('/v3/parents/1') do
data = { :id => 1, :parents => [{ :id => 1, :relation => local_data }] }
[ 200, { 'Content-Type' => 'json' }, data ]
end
api.get('/v3/things/1') do
[ 200, { 'Content-Type' => 'json' }, remote_data ]
end
end
end
subject { klass.new(:id => 1) }
example 'allow chained lookups of lazy relations' do
expect(subject.parents[0].relation.remote).to eql('DATA')
end
end
describe 'null relations' do
subject { klass.new(:relations => nil) }
example 'return an empty list' do
expect(subject.relations).to be_empty
end
end
describe 'unsatisfiable attributes' do
before do
stub_api_for(base_class) do |api|
api.get('/v3/parents/1') do
[ 200, { 'Content-Type' => 'json' }, remote_data ]
end
end
end
subject { klass.new(local_data) }
example 'raise an exception when accessing an unknown attribute' do
expect { subject.unknown_attribute }.to raise_error(NoMethodError)
end
end
end
end
|