From 8c084e0faa971e6db265ea39aefb8cf07e39de43 Mon Sep 17 00:00:00 2001 From: mms Date: Thu, 14 Nov 2024 22:34:47 +0100 Subject: chore: add specs for all classes --- spec/chotto_spec.rb | 52 ++++++++++++++++++++++++++++++++++ spec/lib/database_spec.rb | 31 +++++++++++++++++++++ spec/lib/helpers_spec.rb | 15 ++++++++++ spec/lib/message_spec.rb | 46 ++++++++++++++++++++++++++++++ spec/lib/messages_spec.rb | 71 +++++++++++++++++++++++++++++++++++++++++++++++ spec/lib/ruleset_spec.rb | 27 ++++++++++++++++++ spec/message_spec.rb | 46 ------------------------------ spec/messages_spec.rb | 71 ----------------------------------------------- 8 files changed, 242 insertions(+), 117 deletions(-) create mode 100644 spec/chotto_spec.rb create mode 100644 spec/lib/database_spec.rb create mode 100644 spec/lib/helpers_spec.rb create mode 100644 spec/lib/message_spec.rb create mode 100644 spec/lib/messages_spec.rb create mode 100644 spec/lib/ruleset_spec.rb delete mode 100644 spec/message_spec.rb delete mode 100644 spec/messages_spec.rb (limited to 'spec') diff --git a/spec/chotto_spec.rb b/spec/chotto_spec.rb new file mode 100644 index 0000000..9e8650b --- /dev/null +++ b/spec/chotto_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../lib/chotto' + +describe Chotto do + let(:db_double) { double('::Notmuch::Database') } + let(:db_instance_double) { double('::Notmuch::Database') } + let(:db_query_double) { double('::Notmuch::Query') } + let(:message_double) { double('::Notmuch::Message') } + + before do + $db_double = db_double # we need to access this double within block evaled elsewhere + + allow(db_double).to receive(:new).with('/path', mode: Notmuch::MODE_READ_WRITE).and_return(db_instance_double) + + allow(db_instance_double).to receive(:query).with(' (from:baltar@galactica.com)').and_return(db_query_double) + allow(db_query_double).to receive(:search_messages).and_return([message_double]) + + Chotto.configure do + config.database_path = '/path' + config.db_class = $db_double + end + + Chotto.rule_set 'sample' do + messages.or.filter(from: 'baltar@galactica.com').each do |msg| + msg.tags = msg.subject == 'Number Six' ? [:todo] : [:spam] + msg.save! + end + end + end + + it 'sends the rules to Notmuch - scenario 1' do + expect(message_double).to receive(:tags).and_return([]) + expect(message_double).to receive(:header).with('Subject').and_return('Number Six') + expect(message_double).to receive(:remove_all_tags) + expect(message_double).to receive(:add_tag).with('todo') + expect(db_instance_double).to receive(:close) + + eval_rules + end + + it 'sends the rules to Notmuch - scenario 2' do + expect(message_double).to receive(:tags).and_return([]) + expect(message_double).to receive(:header).with('Subject').and_return('Boomer') + expect(message_double).to receive(:remove_all_tags) + expect(message_double).to receive(:add_tag).with('spam') + expect(db_instance_double).to receive(:close) + + eval_rules + end +end diff --git a/spec/lib/database_spec.rb b/spec/lib/database_spec.rb new file mode 100644 index 0000000..8da04e4 --- /dev/null +++ b/spec/lib/database_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../../lib/chotto/database' + +RSpec.describe Chotto::Database do + let(:db_double) { double('Notmuch::Database') } + let(:db_instance_double) { double('Notmuch::Database') } + let(:query_double) { double('Notmuch::Query') } + let(:query) { 'from:baltar@battlestar.com' } + let(:path) { '/var/db/mail' } + let(:subject) { Chotto::Database } + + before do + expect(db_double).to receive(:new).with(path, mode: Notmuch::MODE_READ_WRITE).and_return(db_instance_double) + end + + it 'initializes the database instance' do + subject.new(path: path, db_class: db_double) + end + + describe '#search_messages' do + it 'uses the correct method chain' do + expect(db_instance_double).to receive(:query).with(query).and_return(query_double) + expect(query_double).to receive(:search_messages) + + db = subject.new(path: path, db_class: db_double) + db.search_messages(query) + end + end +end diff --git a/spec/lib/helpers_spec.rb b/spec/lib/helpers_spec.rb new file mode 100644 index 0000000..5b841e4 --- /dev/null +++ b/spec/lib/helpers_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../../lib/chotto/helpers' + +RSpec.describe Chotto::Helpers do + let(:subject) { Chotto::Helpers } + describe '#header_name_from_dsl' do + it { expect(subject.header_name_from_dsl('word')).to eq('Word') } + it { expect(subject.header_name_from_dsl('wOrd')).to eq('Word') } + it { expect(subject.header_name_from_dsl('Word')).to eq('Word') } + + it { expect(subject.header_name_from_dsl('many_words')).to eq('Many-Words') } + end +end diff --git a/spec/lib/message_spec.rb b/spec/lib/message_spec.rb new file mode 100644 index 0000000..2605117 --- /dev/null +++ b/spec/lib/message_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../../lib/chotto/message' +require_relative '../../lib/chotto/helpers' + +RSpec.describe Chotto::Message do + let(:tags) { [] } + let(:msg) { double('Notmuch::Message', tags: tags) } + + let(:subject) { Chotto::Message.new(msg: msg) } + + it 'reads headers' do + expect(msg).to receive(:header).with('A-Header') + + subject.a_header + end + + describe 'tags' do + it 'keeps tags state in memory' do + subject.tags << 'tag' + expect(subject.tags).to match ['tag'] + + subject.tags << 'tag2' + expect(subject.tags).to match %w[tag tag2] + end + + it 'allows to overwritte entire tags array' do + subject.tags = [1, 2, 3, 4] + expect(subject.tags).to match [1, 2, 3, 4] + end + end + + describe 'save!' do + let(:tags) { [1, 2, 3] } + it 'saves current tag array to db' do + expect(msg).to receive(:remove_all_tags) + tags.each do |tag| + expect(msg).to receive(:add_tag).with(tag.to_s) + end + + subject.tags = tags + subject.save! + end + end +end diff --git a/spec/lib/messages_spec.rb b/spec/lib/messages_spec.rb new file mode 100644 index 0000000..7550991 --- /dev/null +++ b/spec/lib/messages_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../../lib/chotto/messages' + +RSpec.describe Chotto::Messages do + let(:subject) { Chotto::Messages.new(db: double('Notmuch')) } + + describe 'direct filters' do + it { expect(subject.filter('from:baltar@battlestar.com').query_string).to eq(' (from:baltar@battlestar.com)') } + + it { + expect(subject.filter('from:baltar@battlestar.com').filter('ship:galactica').query_string) + .to eq(' (from:baltar@battlestar.com) and (ship:galactica)') + } + end + + describe 'conjuction change' do + it { + expect(subject.filter('from:baltar@battlestar.com') + .or.filter('ship:galactica').query_string).to eq(' (from:baltar@battlestar.com) or (ship:galactica)') + } + + it { + expect(subject.filter('from:baltar@battlestar.com') + .or.filter('ship:galactica') + .filter('hair:long').query_string) + .to eq(' (from:baltar@battlestar.com) or (ship:galactica) or (hair:long)') + } + + it { + expect(subject.filter('from:baltar@battlestar.com') + .or.filter('ship:galactica') + .and.filter('hair:long').query_string) + .to eq(' (from:baltar@battlestar.com) or (ship:galactica) and (hair:long)') + } + end + + describe 'hash filters' do + context 'with singular values' do + it { expect(subject.filter(from: 'baltar@battlestar.com').query_string).to eq(' (from:baltar@battlestar.com)') } + + it { + expect(subject.filter(from: 'baltar@battlestar.com', ship: 'galactica').query_string) + .to eq(' (from:baltar@battlestar.com) and (ship:galactica)') + } + + it { + expect(subject.or.filter(from: 'baltar@battlestar.com', ship: 'galactica').query_string) + .to eq(' (from:baltar@battlestar.com) or (ship:galactica)') + } + end + + context 'with set of values' do + it { + expect(subject.filter(from: ['baltar@battlestar.com', + 'adama@battestar.com']).query_string) + .to eq(' (from:baltar@battlestar.com) or (from:adama@battestar.com)') + } + end + end + + context 'advanced filters' do + it { + expect(subject.filter(from: ['baltar@battlestar.com', 'adama@battestar.com']) + .and.filter(ship: 'battlestar') + .or.filter(ship: 'pegasus').query_string) + .to eq(' (from:baltar@battlestar.com) or (from:adama@battestar.com) and (ship:battlestar) or (ship:pegasus)') + } + end +end diff --git a/spec/lib/ruleset_spec.rb b/spec/lib/ruleset_spec.rb new file mode 100644 index 0000000..e7a6904 --- /dev/null +++ b/spec/lib/ruleset_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'rspec' +require_relative '../../lib/chotto/ruleset' +require_relative '../../lib/chotto/messages' + +RSpec.describe Chotto::RuleSet do + let(:db) { instance_double('Chotto::Database') } + let(:messages) { double('Chotto::Messages', mth: true) } + let(:rule) do + proc do + messages.mth + end + end + let(:subject) do + Chotto::RuleSet.new('a name', db, rule) + end + + describe '#run' do + it 'evaluates the run block' do + expect(Chotto::Messages).to receive(:new).with(db: db).and_return(messages) + expect(messages).to receive(:mth) + + subject.run + end + end +end diff --git a/spec/message_spec.rb b/spec/message_spec.rb deleted file mode 100644 index 5564d34..0000000 --- a/spec/message_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -require 'rspec' -require_relative '../lib/chotto/message' -require_relative '../lib/chotto/helpers' - -RSpec.describe Chotto::Message do - let(:tags) { [] } - let(:msg) { double('Notmuch::Message', tags: tags) } - - let(:subject) { Chotto::Message.new(msg: msg) } - - it 'reads headers' do - expect(msg).to receive(:header).with('A-Header') - - subject.a_header - end - - describe 'tags' do - it 'keeps tags state in memory' do - subject.tags << 'tag' - expect(subject.tags).to match ['tag'] - - subject.tags << 'tag2' - expect(subject.tags).to match %w[tag tag2] - end - - it 'allows to overwritte entire tags array' do - subject.tags = [1, 2, 3, 4] - expect(subject.tags).to match [1, 2, 3, 4] - end - end - - describe 'save!' do - let(:tags) { [1, 2, 3] } - it 'saves current tag array to db' do - expect(msg).to receive(:remove_all_tags) - tags.each do |tag| - expect(msg).to receive(:add_tag).with(tag) - end - - subject.tags = tags - subject.save! - end - end -end diff --git a/spec/messages_spec.rb b/spec/messages_spec.rb deleted file mode 100644 index ec43379..0000000 --- a/spec/messages_spec.rb +++ /dev/null @@ -1,71 +0,0 @@ -# frozen_string_literal: true - -require 'rspec' -require_relative '../lib/chotto/messages' - -RSpec.describe Chotto::Messages do - let(:subject) { Chotto::Messages.new(db: double('Notmuch')) } - - describe 'direct filters' do - it { expect(subject.filter('from:baltar@battlestar.com').query_string).to eq(' (from:baltar@battlestar.com)') } - - it { - expect(subject.filter('from:baltar@battlestar.com').filter('ship:galactica').query_string) - .to eq(' (from:baltar@battlestar.com) and (ship:galactica)') - } - end - - describe 'conjuction change' do - it { - expect(subject.filter('from:baltar@battlestar.com') - .or.filter('ship:galactica').query_string).to eq(' (from:baltar@battlestar.com) or (ship:galactica)') - } - - it { - expect(subject.filter('from:baltar@battlestar.com') - .or.filter('ship:galactica') - .filter('hair:long').query_string) - .to eq(' (from:baltar@battlestar.com) or (ship:galactica) or (hair:long)') - } - - it { - expect(subject.filter('from:baltar@battlestar.com') - .or.filter('ship:galactica') - .and.filter('hair:long').query_string) - .to eq(' (from:baltar@battlestar.com) or (ship:galactica) and (hair:long)') - } - end - - describe 'hash filters' do - context 'with singular values' do - it { expect(subject.filter(from: 'baltar@battlestar.com').query_string).to eq(' (from:baltar@battlestar.com)') } - - it { - expect(subject.filter(from: 'baltar@battlestar.com', ship: 'galactica').query_string) - .to eq(' (from:baltar@battlestar.com) and (ship:galactica)') - } - - it { - expect(subject.or.filter(from: 'baltar@battlestar.com', ship: 'galactica').query_string) - .to eq(' (from:baltar@battlestar.com) or (ship:galactica)') - } - end - - context 'with set of values' do - it { - expect(subject.filter(from: ['baltar@battlestar.com', - 'adama@battestar.com']).query_string) - .to eq(' (from:baltar@battlestar.com) or (from:adama@battestar.com)') - } - end - end - - context 'advanced filters' do - it { - expect(subject.filter(from: ['baltar@battlestar.com', 'adama@battestar.com']) - .and.filter(ship: 'battlestar') - .or.filter(ship: 'pegasus').query_string) - .to eq(' (from:baltar@battlestar.com) or (from:adama@battestar.com) and (ship:battlestar) or (ship:pegasus)') - } - end -end -- cgit v1.2.3