spec/signore/database_spec.rb in signore-0.2.2 vs spec/signore/database_spec.rb in signore-0.2.3
- old
+ new
@@ -1,43 +1,56 @@
+require 'fileutils'
+require 'pathname'
+require 'tempfile'
+require 'tmpdir'
require_relative '../spec_helper'
+require_relative '../../lib/signore/database'
+require_relative '../../lib/signore/signature'
+require_relative '../../lib/signore/tags'
-module Signore describe Database do
- describe '#find' do
- let(:db) { Database.new path }
- let(:path) { 'spec/fixtures/signatures.yml' }
+module Signore
+ describe Database do
+ describe '#<<' do
+ let(:file) { Tempfile.new('') }
+ let(:sig) { Signature.new(text) }
+ let(:text) { 'Normaliser Unix c’est comme pasteuriser le camembert.' }
- it 'returns a random signature by default' do
- Database.new(path).find(random: Random.new(1981)).text
- .must_include 'Amateur fighter pilot ignores orders'
- Database.new(path).find(random: Random.new(2009)).text
- .must_include '// sometimes I believe compiler ignores all my comments'
- end
+ it 'saves the provided signature to disk' do
+ Database.new(path: Pathname.new(file.path)) << sig
+ file.read.must_include text
+ end
- it 'returns a random signature if the tags are empty' do
- Database.new(path).find(required_tags: [], random: Random.new(2013)).text
- .must_equal '// sometimes I believe compiler ignores all my comments'
+ it 'returns the saved signature' do
+ Database.new(path: Pathname.new(file.path)).<<(sig).must_equal sig
+ end
end
- it 'returns a random signature based on provided tags' do
- db.find(required_tags: ['programming']).text
- .must_equal '// sometimes I believe compiler ignores all my comments'
- db.find(required_tags: ['work']).text
- .must_equal 'You do have to be mad to work here, but it doesn’t help.'
- end
+ describe '#find' do
+ let(:database) { Database.new(path: path, sig_finder: sig_finder) }
+ let(:path) { Pathname.new('spec/fixtures/signatures.yml') }
+ let(:sig_finder) { fake(:sig_finder, as: :class) }
+ let(:sigs) { store.transaction(true) { store['signatures'] } }
+ let(:store) { YAML::Store.new(path) }
- it 'returns a random signature based on required and forbidden tags' do
- forbidden_tags = %w(programming security)
- db.find(required_tags: ['tech'], forbidden_tags: forbidden_tags).text
- .must_equal 'You do have to be mad to work here, but it doesn’t help.'
- end
- end
+ it 'returns a random signature by default' do
+ stub(sig_finder).find(sigs, tags: Tags.new) { sigs.last }
+ database.find.must_equal sigs.last
+ end
- describe '#<<' do
- it 'saves the provided signature to disk' do
- text = 'Normaliser Unix c’est comme pasteuriser le camembert.'
- file = Tempfile.new ''
- db = Database.new file.path
- db << Signature.new(text)
- file.read.must_include text
+ it 'returns a random signature based on required and forbidden tags' do
+ tags = Tags.new(forbidden: %w(tech), required: %w(programming security))
+ stub(sig_finder).find(sigs, tags: tags) { sigs.last }
+ database.find(tags: tags).must_equal sigs.last
+ end
+
+ it 'doesn’t blow up if the path is missing' do
+ begin
+ tempdir = Dir.mktmpdir
+ path = Pathname.new("#{tempdir}/some_intermediate_dir/sigs.yml")
+ Database.new(path: path).find(tags: Tags.new).must_equal Signature.new
+ ensure
+ FileUtils.rmtree tempdir
+ end
+ end
end
end
-end end
+end