# frozen_string_literal: true RSpec.describe PlanetExpress::Repository do let!(:repository_item_class) { Struct.new(:test_attribute_a, :test_attribute_b) } let(:repository_items) do 5.times.map do |i| repository_item_class.new(i * 100, "Testing #{i}") end end let!(:entries) { repository_items } subject { described_class.new(data: entries) } describe '#where' do context 'no data' do let(:entries) { [] } it 'returns an empty array' do expect(subject.where).to eq([]) end end context 'no arguments' do before(:each) do expect(subject.all).to_not be_empty end it 'returns all items' do expect(subject.where).to eq(subject.all) end end context 'with existing attribute' do let(:expected_item) do repository_item_class.new(150, 'Testing') end let(:entries) { repository_items + [expected_item] } it 'returns a list with the expected item' do expect(subject.where(test_attribute_a: 150, test_attribute_b: 'Testing')).to eq([expected_item]) end end context 'with unknown attribute' do it 'throws an exception' do expect do subject.where(unknown_attribute: nil) end.to raise_error(NoMethodError) end end end describe '#add' do let(:item) { repository_item_class.new(152, 'Testing') } it 'adds the item to the list' do subject.add(item) expect(subject.where(test_attribute_a: 152, test_attribute_b: 'Testing')).to eq([item]) end end describe '#remove' do let(:item) { repository_item_class.new(153, 'Test') } it 'removes the item from the list' do subject.add(item) expect { subject.remove(item) } .to(change { subject.where(test_attribute_a: 153, test_attribute_b: 'Test') }.to([])) end end end