Module: Cuprum::Collections::RSpec::Contracts::RepositoryContracts::ShouldBeARepositoryContract
- Extended by:
- RSpec::SleepingKingStudios::Contract
- Defined in:
- lib/cuprum/collections/rspec/contracts/repository_contracts.rb
Overview
Contract validating the behavior of a Repository.
Instance Method Summary collapse
-
#apply(example_group, abstract: , **options) ⇒ Object
Adds the contract to the example group.
Instance Method Details
#apply(example_group, abstract: , **options) ⇒ Object
Adds the contract to the example group.
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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/cuprum/collections/rspec/contracts/repository_contracts.rb', line 25 contract do |abstract: false, **| shared_examples 'should create the collection' do let(:configured_collection_class) do return super() if defined?(super()) configured = [:collection_class] # :nocov: if configured.is_a?(String) configured = Object.const_get(configured) end # :nocov: configured end let(:configured_entity_class) do return super() if defined?(super()) # :nocov: expected = if .key?(:entity_class) [:entity_class] elsif .key?(:entity_class) [:entity_class] else qualified_name .split('/') .then { |ary| [*ary[0...-1], tools.str.singularize(ary[-1])] } .map { |str| tools.str.camelize(str) } .join('::') end # :nocov: expected = Object.const_get(expected) if expected.is_a?(String) expected end let(:configured_member_name) do return super() if defined?(super()) tools.str.singularize(collection_name.to_s.split('/').last) end def tools SleepingKingStudios::Tools::Toolbelt.instance end it 'should create the collection' do create_collection(safe: false) expect(repository.key?(qualified_name)).to be true end it 'should return the collection' do collection = create_collection(safe: false) expect(collection).to be repository[qualified_name] end it { expect(collection).to be_a configured_collection_class } it 'should set the entity class' do expect(collection.entity_class).to be == configured_entity_class end it 'should set the collection name' do expect(collection.name).to be == collection_name.to_s end it 'should set the member name' do expect(collection.singular_name).to be == configured_member_name end it 'should set the qualified name' do expect(collection.qualified_name).to be == qualified_name end it 'should set the collection options' do expect(collection).to have_attributes( primary_key_name: primary_key_name, primary_key_type: primary_key_type ) end end describe '#[]' do let(:error_class) do described_class::UndefinedCollectionError end let(:error_message) do "repository does not define collection #{collection_name.inspect}" end it { expect(repository).to respond_to(:[]).with(1).argument } describe 'with nil' do let(:collection_name) { nil } it 'should raise an exception' do expect { repository[collection_name] } .to raise_error(error_class, ) end end describe 'with an object' do let(:collection_name) { Object.new.freeze } it 'should raise an exception' do expect { repository[collection_name] } .to raise_error(error_class, ) end end describe 'with an invalid string' do let(:collection_name) { 'invalid_name' } it 'should raise an exception' do expect { repository[collection_name] } .to raise_error(error_class, ) end end describe 'with an invalid symbol' do let(:collection_name) { :invalid_name } it 'should raise an exception' do expect { repository[collection_name] } .to raise_error(error_class, ) end end wrap_context 'when the repository has many collections' do describe 'with an invalid string' do let(:collection_name) { 'invalid_name' } it 'should raise an exception' do expect { repository[collection_name] } .to raise_error(error_class, ) end end describe 'with an invalid symbol' do let(:collection_name) { :invalid_name } it 'should raise an exception' do expect { repository[collection_name] } .to raise_error(error_class, ) end end describe 'with a valid string' do let(:collection) { collections.values.first } let(:collection_name) { collections.keys.first } it { expect(repository[collection_name]).to be collection } end describe 'with a valid symbol' do let(:collection) { collections.values.first } let(:collection_name) { collections.keys.first.intern } it { expect(repository[collection_name]).to be collection } end end end describe '#add' do let(:error_class) do described_class::InvalidCollectionError end let(:error_message) do "#{collection.inspect} is not a valid collection" end it 'should define the method' do expect(repository) .to respond_to(:add) .with(1).argument .and_keywords(:force) end it 'should alias #add as #<<' do expect(repository.method(:<<)).to be == repository.method(:add) end describe 'with nil' do let(:collection) { nil } it 'should raise an exception' do expect { repository.add(collection) } .to raise_error(error_class, ) end end describe 'with an object' do let(:collection) { Object.new.freeze } it 'should raise an exception' do expect { repository.add(collection) } .to raise_error(error_class, ) end end describe 'with a collection' do it { expect(repository.add(example_collection)).to be repository } it 'should add the collection to the repository' do repository.add(example_collection) expect(repository[example_collection.qualified_name]) .to be example_collection end describe 'with force: true' do it 'should add the collection to the repository' do repository.add(example_collection, force: true) expect(repository[example_collection.qualified_name]) .to be example_collection end end context 'when the collection already exists' do let(:error_message) do "collection #{example_collection.qualified_name} already exists" end before(:example) do allow(repository) .to receive(:key?) .with(example_collection.qualified_name) .and_return(true) end it 'should raise an exception' do expect { repository.add(example_collection) } .to raise_error( described_class::DuplicateCollectionError, ) end it 'should not update the repository' do begin repository.add(example_collection) rescue described_class::DuplicateCollectionError # Do nothing. end expect { repository[example_collection.qualified_name] } .to raise_error( described_class::UndefinedCollectionError, 'repository does not define collection ' \ "#{example_collection.qualified_name.inspect}" ) end describe 'with force: true' do it 'should add the collection to the repository' do repository.add(example_collection, force: true) expect(repository[example_collection.qualified_name]) .to be example_collection end end end end end describe '#create' do let(:collection_name) { 'books' } let(:qualified_name) { collection_name.to_s } let(:primary_key_name) { 'id' } let(:primary_key_type) { Integer } let(:collection_options) { {} } let(:collection) do create_collection repository[qualified_name] end let(:error_message) do "#{described_class.name} is an abstract class. Define a " \ 'repository subclass and implement the #build_collection method.' end def create_collection(force: false, safe: true, **) if safe begin repository.create(force: force, **, **) rescue StandardError # Do nothing. end else repository.create(force: force, **, **) end end it 'should define the method' do expect(repository) .to respond_to(:create) .with(0).arguments .and_keywords(:collection_name, :entity_class, :force) .and_any_keywords end if abstract it 'should raise an exception' do expect { create_collection(safe: false) } .to raise_error( described_class::AbstractRepositoryError, ) end next end describe 'with entity_class: a Class' do let(:entity_class) { Book } let(:collection_options) do super().merge(entity_class: entity_class) end include_examples 'should create the collection' end describe 'with entity_class: a String' do let(:entity_class) { 'Book' } let(:collection_options) do super().merge(entity_class: entity_class) end include_examples 'should create the collection' end describe 'with name: a String' do let(:collection_name) { 'books' } let(:collection_options) do super().merge(name: collection_name) end include_examples 'should create the collection' end describe 'with name: a Symbol' do let(:collection_name) { :books } let(:collection_options) do super().merge(name: collection_name) end include_examples 'should create the collection' end describe 'with collection options' do let(:primary_key_name) { 'uuid' } let(:primary_key_type) { String } let(:collection_options) do super().merge( name: collection_name, primary_key_name: primary_key_name, primary_key_type: primary_key_type ) end include_examples 'should create the collection' end context 'when the collection already exists' do let(:collection_name) { 'books' } let(:collection_options) do super().merge(name: collection_name) end let(:error_message) do "collection #{qualified_name} already exists" end before { create_collection(old: true) } it 'should raise an exception' do expect { create_collection(safe: false) } .to raise_error( described_class::DuplicateCollectionError, ) end it 'should not update the repository' do create_collection(old: false) collection = repository[qualified_name] expect(collection.[:old]).to be true end describe 'with force: true' do it 'should update the repository' do create_collection(force: true, old: false) collection = repository[qualified_name] expect(collection.[:old]).to be false end end end end describe '#find_or_create' do let(:collection_name) { 'books' } let(:qualified_name) { collection_name.to_s } let(:primary_key_name) { 'id' } let(:primary_key_type) { Integer } let(:collection_options) { {} } let(:collection) do create_collection repository[qualified_name] end let(:error_message) do "#{described_class.name} is an abstract class. Define a " \ 'repository subclass and implement the #build_collection method.' end def create_collection(safe: true, **) if safe begin repository.find_or_create(**, **) rescue StandardError # Do nothing. end else repository.find_or_create(**, **) end end it 'should define the method' do expect(repository) .to respond_to(:find_or_create) .with(0).arguments .and_keywords(:entity_class) .and_any_keywords end if abstract let(:collection_options) { { name: collection_name } } it 'should raise an exception' do expect { create_collection(safe: false) } .to raise_error( described_class::AbstractRepositoryError, ) end next end describe 'with entity_class: a Class' do let(:entity_class) { Book } let(:collection_options) do super().merge(entity_class: entity_class) end include_examples 'should create the collection' end describe 'with entity_class: a String' do let(:entity_class) { Book } let(:collection_options) do super().merge(entity_class: entity_class) end include_examples 'should create the collection' end describe 'with name: a String' do let(:collection_name) { 'books' } let(:collection_options) do super().merge(name: collection_name) end include_examples 'should create the collection' end describe 'with name: a Symbol' do let(:collection_name) { :books } let(:collection_options) do super().merge(name: collection_name) end include_examples 'should create the collection' end describe 'with collection options' do let(:primary_key_name) { 'uuid' } let(:primary_key_type) { String } let(:qualified_name) { 'spec/scoped_books' } let(:collection_options) do super().merge( name: collection_name, primary_key_name: primary_key_name, primary_key_type: primary_key_type, qualified_name: qualified_name ) end include_examples 'should create the collection' end context 'when the collection already exists' do let(:collection_name) { 'books' } let(:collection_options) do super().merge(name: collection_name) end let(:error_message) do "collection #{qualified_name} already exists" end before { create_collection(old: true) } describe 'with non-matching options' do it 'should raise an exception' do expect { create_collection(old: false, safe: false) } .to raise_error( described_class::DuplicateCollectionError, ) end it 'should not update the repository' do create_collection(old: false) collection = repository[qualified_name] expect(collection.[:old]).to be true end end describe 'with matching options' do it 'should return the collection' do collection = create_collection(old: true) expect(collection.[:old]).to be true end end end end describe '#key?' do it { expect(repository).to respond_to(:key?).with(1).argument } it { expect(repository.key?(nil)).to be false } it { expect(repository.key?(Object.new.freeze)).to be false } it { expect(repository.key?('invalid_name')).to be false } it { expect(repository.key?(:invalid_name)).to be false } wrap_context 'when the repository has many collections' do it { expect(repository.key?('invalid_name')).to be false } it { expect(repository.key?(:invalid_name)).to be false } it { expect(repository.key?(collections.keys.first)).to be true } it 'should include the key' do expect(repository.key?(collections.keys.first.intern)).to be true end end end describe '#keys' do include_examples 'should define reader', :keys, [] wrap_context 'when the repository has many collections' do it { expect(repository.keys).to be == collections.keys } end end end |