Skip to content

Commit

Permalink
Fix getting the first result and add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-André Courtois committed Apr 19, 2013
1 parent a1e0955 commit db52a3f
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--color
--format documentation
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
32 changes: 32 additions & 0 deletions 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)
8 changes: 7 additions & 1 deletion hiera-psql.gemspec
Expand Up @@ -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
13 changes: 9 additions & 4 deletions 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

Expand All @@ -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
Expand Down
76 changes: 76 additions & 0 deletions 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
22 changes: 22 additions & 0 deletions 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'

0 comments on commit db52a3f

Please sign in to comment.