From 29de1da76681c2f526b5709d14d795c767f91594 Mon Sep 17 00:00:00 2001 From: Luke Hill Date: Fri, 15 May 2026 14:22:03 +0100 Subject: [PATCH 1/3] Expose .find_by publically and add unit tests --- lib/testing_record/dsl/builder/filters.rb | 28 ++++++++-------- .../dsl/builder/filters_spec.rb | 32 +++++++++++++++++++ 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/lib/testing_record/dsl/builder/filters.rb b/lib/testing_record/dsl/builder/filters.rb index 9f57cfc..ebda919 100644 --- a/lib/testing_record/dsl/builder/filters.rb +++ b/lib/testing_record/dsl/builder/filters.rb @@ -13,6 +13,19 @@ def exists?(attributes) find_by(attributes).any? end + # Finds all entities that match specified attribute values + # + # @return [Array] + def find_by(attributes) + pool = all + attributes.each do |key, value| + TestingRecord.logger.debug("Current user pool size: #{pool.length}") + TestingRecord.logger.debug("Filtering User list by #{key}: #{value}") + pool = pool.select { |entity| entity.attributes[key] == value } + end + pool + end + # Finds an entity with the provided email address # If one is found, set it as the current entity # @@ -50,21 +63,6 @@ def with_primary_key?(primary_key) def with_primary_key(primary_key) find_by({ __primary_key => primary_key })&.first&.tap { |entity| entity.class.current = entity } end - - private - - # Finds all entities that match specified attribute values - # - # @return [Array] - def find_by(attributes) - pool = all - attributes.each do |key, value| - TestingRecord.logger.debug("Current user pool size: #{pool.length}") - TestingRecord.logger.debug("Filtering User list by #{key}: #{value}") - pool = pool.select { |entity| entity.attributes[key] == value } - end - pool - end end end end diff --git a/spec/testing_record/dsl/builder/filters_spec.rb b/spec/testing_record/dsl/builder/filters_spec.rb index 2ae97e8..3dbb200 100644 --- a/spec/testing_record/dsl/builder/filters_spec.rb +++ b/spec/testing_record/dsl/builder/filters_spec.rb @@ -48,6 +48,38 @@ end end + describe '.find_by' do + let(:foo_entity) { model_klazz.create({ email_address: 'foo@foo.com', foo: 3, other: :foo }) } + let(:bar_entity) { model_klazz.create({ email_address: 'bar@bar.com', foo: 3, other: :bar }) } + let(:baz_entity) { model_klazz.create({ email_address: 'baz@baz.com', foo: 3, other: :baz }) } + + before do + foo_entity + bar_entity + baz_entity + end + + context 'with a simple 1 attribute query' do + it 'returns a collection of entities that match the query' do + expect(model_klazz.find_by({ foo: 3 })).to eq([foo_entity, bar_entity, baz_entity]) + end + + it 'returns a blank collection when no entities match the query' do + expect(model_klazz.find_by({ foo: 4 })).to eq([]) + end + end + + context 'with a more complex set of attributes as a query' do + it 'returns a collection of entities that match all query attributes' do + expect(model_klazz.find_by({ foo: 3, other: :foo })).to eq([foo_entity]) + end + + it 'returns a blank collection when no entities match all query attributes' do + expect(model_klazz.find_by({ foo: 3, other: :jeff })).to eq([]) + end + end + end + describe '.with_id' do before { model_klazz.primary_key :id } after { model_klazz.primary_key :email_address } From bf2b302c9bae42a9e8f562c630c25670bf73ab9a Mon Sep 17 00:00:00 2001 From: Luke Hill Date: Fri, 15 May 2026 14:23:13 +0100 Subject: [PATCH 2/3] Match on more attributes in unit test --- spec/testing_record/dsl/builder/filters_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/testing_record/dsl/builder/filters_spec.rb b/spec/testing_record/dsl/builder/filters_spec.rb index 3dbb200..b239148 100644 --- a/spec/testing_record/dsl/builder/filters_spec.rb +++ b/spec/testing_record/dsl/builder/filters_spec.rb @@ -71,11 +71,11 @@ context 'with a more complex set of attributes as a query' do it 'returns a collection of entities that match all query attributes' do - expect(model_klazz.find_by({ foo: 3, other: :foo })).to eq([foo_entity]) + expect(model_klazz.find_by({ foo: 3, other: :foo, email_address: 'foo@foo.com' })).to eq([foo_entity]) end it 'returns a blank collection when no entities match all query attributes' do - expect(model_klazz.find_by({ foo: 3, other: :jeff })).to eq([]) + expect(model_klazz.find_by({ foo: 3, other: :jeff, email_address: 'foo@foo.com' })).to eq([]) end end end From 314eaa51c89c298248fc30cefc84bc56ef0706d1 Mon Sep 17 00:00:00 2001 From: Luke Hill Date: Fri, 15 May 2026 14:23:56 +0100 Subject: [PATCH 3/3] Add changelog; --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e4a5d4..7d4d74d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### Added ### Changed +- Exposed `.find_by` as a public method for more complex querying of models, and to allow people to write +their own custom filters on top of this method ### Fixed