Module: Cuprum::Collections::RSpec::Contracts::QueryContracts::ShouldBeAQuery Deprecated
- Extended by:
- RSpec::SleepingKingStudios::Contract
- Defined in:
- lib/cuprum/collections/rspec/contracts/query_contracts.rb
Overview
Deprecated.
0.6.0
Contract validating the behavior of a Query implementation.
Instance Method Summary collapse
-
#apply(example_group, abstract: false) ⇒ Object
Adds the contract to the example group.
Instance Method Details
#apply(example_group, abstract: false) ⇒ Object
Adds the contract to the example group.
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 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 |
# File 'lib/cuprum/collections/rspec/contracts/query_contracts.rb', line 32 contract do |abstract: false| include Cuprum::Collections::RSpec::Contracts::QueryContracts SleepingKingStudios::Tools::CoreTools.deprecate( 'QueryContracts "should be a query"', message: 'Use Deferred::QueryExamples instead.' ) shared_context 'when initialized with a scope' do let(:initial_scope) do Cuprum::Collections::Scope.new do |scope| { 'published_at' => scope.less_than('1973-01-01') } end end let(:filtered_data) do super().select { |item| item['published_at'] < '1973-01-01' } end end shared_context 'when the query has composed filters' do let(:scoped_query) do super() .where { { author: 'Ursula K. LeGuin' } } .where { |scope| { series: scope.not_equal('Earthsea') } } end let(:filtered_data) do super() .select { |item| item['author'] == 'Ursula K. LeGuin' } .reject { |item| item['series'] == 'Earthsea' } end end let(:filter) { nil } let(:limit) { nil } let(:offset) { nil } let(:order) { nil } let(:scoped_query) do scoped = if filter.is_a?(Proc) subject.where(&filter) elsif filter subject.where(filter) else subject end scoped = scoped.limit(limit) if limit scoped = scoped.offset(offset) if offset scoped = scoped.order(order) if order scoped end it 'should be enumerable' do expect(described_class).to be < Enumerable end describe '#count' do it { expect(query).to respond_to(:count).with(0).arguments } next if abstract it { expect(query.count).to be 0 } context 'when the collection data changes' do let(:item) { BOOKS_FIXTURES.first } before(:example) do query.count # Cache query results. add_item_to_collection(item) end it { expect(query.count).to be 0 } end context 'when the collection has many items' do let(:data) { BOOKS_FIXTURES } include_deferred 'should query the collection', ignore_order: true \ do it { expect(scoped_query.count).to be == expected_data.count } wrap_context 'when the query has composed filters' do it { expect(scoped_query.count).to be == expected_data.count } end wrap_context 'when initialized with a scope' do it { expect(scoped_query.count).to be == expected_data.count } wrap_context 'when the query has composed filters' do it { expect(scoped_query.count).to be == expected_data.count } end end end context 'when the collection data changes' do let(:data) { BOOKS_FIXTURES[0...-1] } let(:item) { BOOKS_FIXTURES.last } before(:example) do query.count # Cache query results. add_item_to_collection(item) end it { expect(query.count).to be == expected_data.count } end end end describe '#each' do shared_examples 'should enumerate the matching data' do describe 'with no arguments' do it { expect(scoped_query.each).to be_a Enumerator } it { expect(scoped_query.each.count).to be == expected_data.size } it { expect(scoped_query.each.to_a).to deep_match expected_data } end describe 'with a block' do it 'should yield each matching item' do expect { |block| scoped_query.each(&block) } .to yield_successive_args(*expected_data) end end end next if abstract it { expect(query).to respond_to(:each).with(0).arguments } include_examples 'should enumerate the matching data' context 'when the collection data changes' do let(:item) { BOOKS_FIXTURES.first } before(:example) do query.each {} # Cache query results. add_item_to_collection(item) end include_examples 'should enumerate the matching data' end context 'when the collection has many items' do let(:data) { BOOKS_FIXTURES } include_deferred 'should query the collection' do include_examples 'should enumerate the matching data' wrap_context 'when the query has composed filters' do include_examples 'should enumerate the matching data' end wrap_context 'when initialized with a scope' do include_examples 'should enumerate the matching data' wrap_context 'when the query has composed filters' do include_examples 'should enumerate the matching data' end end end context 'when the collection data changes' do let(:data) { BOOKS_FIXTURES[0...-1] } let(:item) { BOOKS_FIXTURES.last } before(:example) do query.each {} # Cache query results. add_item_to_collection(item) end include_examples 'should enumerate the matching data' end end end describe '#exists?' do shared_examples 'should check the existence of matching data' do let(:data) { [] } let(:expected_data) { defined?(super()) ? super() : data } it { expect(query.exists?).to be == !expected_data.empty? } end next if abstract include_examples 'should define predicate', :exists? include_examples 'should check the existence of matching data' context 'when the collection has many items' do let(:data) { BOOKS_FIXTURES } include_deferred 'should query the collection', ignore_order: true \ do include_examples 'should check the existence of matching data' wrap_context 'when the query has composed filters' do include_examples 'should check the existence of matching data' end wrap_context 'when initialized with a scope' do include_examples 'should check the existence of matching data' wrap_context 'when the query has composed filters' do include_examples 'should check the existence of matching data' end end end end end describe '#limit' do it { expect(query).to respond_to(:limit).with(0..1).arguments } describe 'with no arguments' do it { expect(query.limit).to be nil } end describe 'with nil' do let(:error_message) { 'limit must be a non-negative integer' } it 'should raise an exception' do expect { query.limit nil } .to raise_error ArgumentError, end end describe 'with an object' do let(:error_message) { 'limit must be a non-negative integer' } it 'should raise an exception' do expect { query.limit Object.new.freeze } .to raise_error ArgumentError, end end describe 'with a negative integer' do let(:error_message) { 'limit must be a non-negative integer' } it 'should raise an exception' do expect { query.limit(-1) } .to raise_error ArgumentError, end end describe 'with zero' do it { expect(query.limit(0)).to be_a described_class } it { expect(query.limit(0)).not_to be query } it { expect(query.limit(0).limit).to be 0 } end describe 'with a positive integer' do it { expect(query.limit(3)).to be_a described_class } it { expect(query.limit(3)).not_to be query } it { expect(query.limit(3).limit).to be 3 } end end describe '#offset' do it { expect(query).to respond_to(:offset).with(0..1).argument } describe 'with no arguments' do it { expect(query.offset).to be nil } end describe 'with nil' do let(:error_message) { 'offset must be a non-negative integer' } it 'should raise an exception' do expect { query.offset nil } .to raise_error ArgumentError, end end describe 'with an object' do let(:error_message) { 'offset must be a non-negative integer' } it 'should raise an exception' do expect { query.offset Object.new.freeze } .to raise_error ArgumentError, end end describe 'with a negative integer' do let(:error_message) { 'offset must be a non-negative integer' } it 'should raise an exception' do expect { query.offset(-1) } .to raise_error ArgumentError, end end describe 'with zero' do it { expect(query.offset(0)).to be_a described_class } it { expect(query.offset(0)).not_to be query } it { expect(query.offset(0).offset).to be 0 } end describe 'with a positive integer' do it { expect(query.offset(3)).to be_a described_class } it { expect(query.offset(3)).not_to be query } it { expect(query.offset(3).offset).to be 3 } end end describe '#order' do let(:default_order) { defined?(super()) ? super() : {} } let(:error_message) do 'order must be a list of attribute names and/or a hash of ' \ 'attribute names with values :asc or :desc' end it 'should define the method' do expect(query) .to respond_to(:order) .with(0).arguments .and_unlimited_arguments end it { expect(query).to have_aliased_method(:order).as(:order_by) } describe 'with no arguments' do it { expect(query.order).to be == default_order } end describe 'with a hash with invalid keys' do it 'should raise an exception' do expect { query.order({ nil => :asc }) } .to raise_error ArgumentError, end end describe 'with a hash with empty string keys' do it 'should raise an exception' do expect { query.order({ '' => :asc }) } .to raise_error ArgumentError, end end describe 'with a hash with empty symbol keys' do it 'should raise an exception' do expect { query.order({ '': :asc }) } .to raise_error ArgumentError, end end describe 'with a hash with nil value' do it 'should raise an exception' do expect { query.order({ title: nil }) } .to raise_error ArgumentError, end end describe 'with a hash with object value' do it 'should raise an exception' do expect { query.order({ title: Object.new.freeze }) } .to raise_error ArgumentError, end end describe 'with a hash with empty value' do it 'should raise an exception' do expect { query.order({ title: '' }) } .to raise_error ArgumentError, end end describe 'with a hash with invalid value' do it 'should raise an exception' do expect { query.order({ title: 'wibbly' }) } .to raise_error ArgumentError, end end describe 'with a valid ordering' do let(:expected) do { title: :asc } end it { expect(query.order(:title)).to be_a described_class } it { expect(query.order(:title)).not_to be query } it { expect(query.order(:title).order).to be == expected } end end describe '#reset' do let(:data) { [] } let(:matching_data) { data } let(:expected_data) do defined?(super()) ? super() : matching_data end it { expect(query).to respond_to(:reset).with(0).arguments } it { expect(query.reset).to be_a query.class } it { expect(query.reset).not_to be query } next if abstract it { expect(query.reset.to_a).to be == query.to_a } context 'when the collection data changes' do let(:item) { BOOKS_FIXTURES.first } let(:matching_data) { [item] } before(:example) do query.to_a # Cache query results. add_item_to_collection(item) end it { expect(query.reset.count).to be expected_data.size } it { expect(query.reset.to_a).to deep_match expected_data } end context 'when the collection has many items' do let(:data) { BOOKS_FIXTURES } it { expect(query.reset).to be_a query.class } it { expect(query.reset).not_to be query } it { expect(query.reset.to_a).to be == query.to_a } context 'when the collection data changes' do let(:data) { BOOKS_FIXTURES[0...-1] } let(:item) { BOOKS_FIXTURES.last } let(:matching_data) { [*data, item] } before(:example) do query.to_a # Cache query results. add_item_to_collection(item) end it { expect(query.reset.count).to be expected_data.size } it { expect(query.reset.to_a).to deep_match expected_data } end end end describe '#scope' do include_examples 'should define reader', :scope it { expect(query.scope).to be_a Cuprum::Collections::Scopes::Base } it { expect(query.scope.type).to be :all } wrap_context 'when initialized with a scope' do let(:expected) do Cuprum::Collections::Scope.new do |scope| { 'published_at' => scope.less_than('1973-01-01') } end end it { expect(scoped_query.scope).to be == expected } wrap_context 'when the query has composed filters' do let(:expected) do Cuprum::Collections::Scope.new do |scope| { 'published_at' => scope.less_than('1973-01-01'), 'author' => 'Ursula K. LeGuin', 'series' => scope.not_equal('Earthsea') } end end it { expect(scoped_query.scope).to be == expected } end end wrap_context 'when the query has composed filters' do let(:expected) do Cuprum::Collections::Scope.new do |scope| { 'author' => 'Ursula K. LeGuin', 'series' => scope.not_equal('Earthsea') } end end it { expect(scoped_query.scope).to be == expected } end end describe '#to_a' do let(:data) { [] } let(:queried_data) { scoped_query.to_a } let(:expected_data) { defined?(super()) ? super() : data } it { expect(query).to respond_to(:to_a).with(0).arguments } next if abstract it { expect(queried_data).to be == [] } context 'when the collection data changes' do let(:item) { BOOKS_FIXTURES.first } before(:example) do scoped_query.to_a # Cache query results. add_item_to_collection(item) end it { expect(queried_data).to be == [] } end context 'when the collection has many items' do let(:data) { BOOKS_FIXTURES } include_deferred 'should query the collection' do it { expect(queried_data).to be == expected_data } wrap_context 'when the query has composed filters' do it { expect(queried_data).to be == expected_data } end wrap_context 'when initialized with a scope' do it { expect(queried_data).to be == expected_data } wrap_context 'when the query has composed filters' do it { expect(queried_data).to be == expected_data } end end end context 'when the collection data changes' do let(:data) { BOOKS_FIXTURES[0...-1] } let(:item) { BOOKS_FIXTURES.last } before(:example) do scoped_query.to_a # Cache query results. add_item_to_collection(item) end it { expect(queried_data).to deep_match expected_data } end end end describe '#where' do let(:block) { -> { { title: 'Gideon the Ninth' } } } it 'should define the method' do expect(subject) .to respond_to(:where) .with(0..1).arguments .and_a_block end it { expect(subject.where(&block)).to be_a described_class } it { expect(subject.where(&block)).not_to be subject } it 'should set the scope' do expect(subject.where(&block).scope) .to be_a Cuprum::Collections::Scopes::Base end it 'should not change the original query scope' do expect { subject.where(&block) } .not_to change(subject, :scope) end context 'when the query does not have a scope' do let(:expected) do Cuprum::Collections::Scope.new({ 'title' => 'Gideon the Ninth' }) end describe 'with a block' do let(:block) { -> { { 'title' => 'Gideon the Ninth' } } } it { expect(subject.where(&block).scope).to be == expected } end describe 'with a hash' do let(:value) { { 'title' => 'Gideon the Ninth' } } it { expect(subject.where(value).scope).to be == expected } end describe 'with a basic scope' do let(:value) do Cuprum::Collections::Scope .new({ 'title' => 'Gideon the Ninth' }) end it { expect(subject.where(value).scope).to be == value } end describe 'with a complex scope' do let(:value) do Cuprum::Collections::Scope .new({ 'title' => 'Gideon the Ninth' }) .or({ 'title' => 'Harrow the Ninth' }) end it { expect(subject.where(value).scope).to be == value } end end context 'when the query has a scope' do let(:initial_scope) do Cuprum::Collections::Scope.new({ 'author' => 'Tamsyn Muir' }) end let(:expected) do operators = Cuprum::Collections::Queries::Operators [ [ 'author', operators::EQUAL, 'Tamsyn Muir' ], [ 'title', operators::EQUAL, 'Gideon the Ninth' ] ] end describe 'with a block' do let(:block) { -> { { 'title' => 'Gideon the Ninth' } } } let(:scope) { subject.where(&block).scope } it { expect(scope).to be_a Cuprum::Collections::Scopes::Base } it { expect(scope.type).to be :criteria } it { expect(scope.criteria).to be == expected } end describe 'with a value' do let(:value) { { 'title' => 'Gideon the Ninth' } } let(:scope) { subject.where(value).scope } it { expect(scope).to be_a Cuprum::Collections::Scopes::Base } it { expect(scope.type).to be :criteria } it { expect(scope.criteria).to be == expected } end describe 'with a basic scope' do let(:value) do Cuprum::Collections::Scope .new({ 'title' => 'Gideon the Ninth' }) end let(:scope) { subject.where(value).scope } it { expect(scope).to be_a Cuprum::Collections::Scopes::Base } it { expect(scope.type).to be :criteria } it { expect(scope.criteria).to be == expected } end describe 'with a complex scope' do let(:value) do Cuprum::Collections::Scope .new({ 'title' => 'Gideon the Ninth' }) .or({ 'title' => 'Harrow the Ninth' }) end let(:scope) { subject.where(value).scope } let(:outer) { scope.scopes.last } let(:expected) do operators = Cuprum::Collections::Queries::Operators [ [ 'author', operators::EQUAL, 'Tamsyn Muir' ] ] end let(:expected_first) do operators = Cuprum::Collections::Queries::Operators [ [ 'title', operators::EQUAL, 'Gideon the Ninth' ] ] end let(:expected_second) do operators = Cuprum::Collections::Queries::Operators [ [ 'title', operators::EQUAL, 'Harrow the Ninth' ] ] end it { expect(scope).to be_a Cuprum::Collections::Scopes::Base } it { expect(scope.type).to be :conjunction } it { expect(scope.scopes.size).to be 2 } it { expect(scope.scopes.first.type).to be :criteria } it { expect(scope.scopes.first.criteria).to be == expected } it { expect(outer).to be_a Cuprum::Collections::Scopes::Base } it { expect(outer.type).to be :disjunction } it { expect(outer.scopes.size).to be 2 } it { expect(outer.scopes.first.criteria).to be == expected_first } it { expect(outer.scopes.last.criteria).to be == expected_second } end end end end |