From db52a3f4fb340c7d0fe4d2dba388029d8358e6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Courtois?= Date: Thu, 18 Apr 2013 22:52:01 -0400 Subject: [PATCH] Fix getting the first result and add specs --- .rspec | 2 + Gemfile | 3 + Gemfile.lock | 32 +++++++++ hiera-psql.gemspec | 8 ++- lib/hiera/backend/psql_backend.rb | 13 ++-- spec/lib/hiera/backend/psql_backend_spec.rb | 76 +++++++++++++++++++++ spec/spec_helper.rb | 22 ++++++ 7 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 .rspec create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 spec/lib/hiera/backend/psql_backend_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fa75df1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..f9edbcc --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,32 @@ +PATH + remote: . + specs: + hiera-psql (0.1.0) + hiera (~> 1.0) + json (~> 1.7) + pg (~> 0.15) + +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.2.3) + hiera (1.2.0) + json_pure + json (1.7.7) + json_pure (1.7.7) + pg (0.15.1) + rspec (2.13.0) + rspec-core (~> 2.13.0) + rspec-expectations (~> 2.13.0) + rspec-mocks (~> 2.13.0) + rspec-core (2.13.1) + rspec-expectations (2.13.0) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.13.1) + +PLATFORMS + ruby + +DEPENDENCIES + hiera-psql! + rspec (= 2.13) diff --git a/hiera-psql.gemspec b/hiera-psql.gemspec index 5125622..70bf438 100644 --- a/hiera-psql.gemspec +++ b/hiera-psql.gemspec @@ -10,7 +10,13 @@ Gem::Specification.new do |s| s.has_rdoc = false s.homepage = "http://github.com/dalen/hiera-psql" s.license = "Apache 2.0" - s.add_dependency 'hiera', '~> 1.0' s.files = Dir["lib/**/*.rb"] s.files += ["LICENSE"] + + s.add_dependency 'hiera', '~> 1.0' + s.add_dependency 'pg', '~> 0.15' + s.add_dependency 'json', '~> 1.7' + + s.add_development_dependency 'rspec', '2.13' + end diff --git a/lib/hiera/backend/psql_backend.rb b/lib/hiera/backend/psql_backend.rb index 41b8882..0b79eb0 100644 --- a/lib/hiera/backend/psql_backend.rb +++ b/lib/hiera/backend/psql_backend.rb @@ -1,10 +1,10 @@ +require 'pg' +require 'json' + class Hiera module Backend class Psql_backend def initialize - require 'pg' - require 'json' - Hiera.debug("Hiera PostgreSQL backend starting") end @@ -21,12 +21,17 @@ def lookup(key, scope, order_override, resolution_type) # places where the key is found. Hiera.debug("Found #{key} in #{source}") + + entry = result.first + return nil unless result.first + new_answer = JSON.load(entry.values_at('value')) + + # for array resolution we just append to the array whatever # we find, we then goes onto the next file and keep adding to # the array # # for priority searches we break after the first found data item - new_answer = JSON.load(result.values_at('value').first) case resolution_type when :array raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String diff --git a/spec/lib/hiera/backend/psql_backend_spec.rb b/spec/lib/hiera/backend/psql_backend_spec.rb new file mode 100644 index 0000000..4fa4291 --- /dev/null +++ b/spec/lib/hiera/backend/psql_backend_spec.rb @@ -0,0 +1,76 @@ +require 'spec_helper' +require 'hiera/backend/psql_backend' + +class Hiera + module Backend + describe Psql_backend do + before do + #Config.load({'psql'=>'asdfas'}) + Hiera.stub :debug + Hiera.stub :warn + @backend = Psql_backend.new + end + + describe '#initialize' do + it 'should print debug through Hiera' do + Hiera.should_receive(:debug).with('Hiera PostgreSQL backend starting') + Psql_backend.new + end + end + + describe '#lookup' do + it 'should look for data in all sources' do + Backend.should_receive(:datasources).and_yield(["one"]).and_yield(["two"]) + + connection_mock = double.as_null_object + Psql_backend.should_receive(:connection).twice.and_return(connection_mock) + connection_mock.should_receive(:exec).once.ordered.with(anything(), [["one"], anything()]) + connection_mock.should_receive(:exec).once.ordered.with(anything(), [["two"], anything()]) + + @backend.lookup('key', {}, nil, :priority) + end + + #it 'should pick data earliest source that has it for priority searches' do + # Backend.should_receive(:datasources).and_yield(["one"])#.and_yield(["two"]) + # + # connection_mock = double('connection').as_null_object + # #result_mock = double('result').as_null_object + # Psql_backend.should_receive(:connection).once.and_return(connection_mock) + # connection_mock.should_receive(:exec).once.ordered.with(anything(), [['one'], anything()]).and_yield({'value'=>'"patate"'}) + # + # #result_mock.should_receive(:first).once.ordered.and_return({'value'=>'"patate"'}) + # #connection_mock.should_receive(:exec).once.ordered.with(anything(), [["two"], anything()]).and_return result_mock + # #result_mock.should_receive(:first).once.ordered.and_return nil + # + # @backend.lookup('key', {}, nil, :priority).should == 'patate' + #end + + + #it 'should return nil for missing path/value' do + # mock_source = double.as_null_object + # Backend.should_receive(:datasources).with(:scope, :override).and_yield(mock_source) + # + # described_class.should_receive(:exec).with(:query, :params).and_return(double.as_null_object) + # + # + # @backend.lookup(:key, :scope, :override, :resolution_type) + #end + # + #it 'should build an array of all data sources for array searches' do + # # todo + # @backend.lookup("key", {}, nil, :array).should == ["answer", "answer"] + #end + # + #it 'should build an array of all data sources for array searches' do + # # todo + # @backend.lookup("key", {}, nil, :hash).should == {"a" => "answer"} + #end + # + #it 'should parse the answer for scope variables' do + # # todo + # @backend.lookup("key", {"rspec" => "test"}, nil, :priority).should == "test_test" + #end + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..6bfbcfb --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,22 @@ +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# Require this file using `require "spec_helper"` to ensure that it is only +# loaded once. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + config.treat_symbols_as_metadata_keys_with_true_values = true + config.run_all_when_everything_filtered = true + config.filter_run :focus + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = 'random' + + config.mock_with :rspec +end + +require 'lib/hiera/backend/psql_backend' +