summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2013-07-20 19:24:48 -0600
committermo khan <mo@mokhan.ca>2013-07-20 19:24:48 -0600
commit109ba143023eb971d0cfac3e8e9552a0888fbfd8 (patch)
tree6a6715fdd62f17ef6072bf59c7d117ccb22ae9d5
parentea77657949bdcc0849a49b048aba8579cf49ffd9 (diff)
create humble repository and user mapping
-rw-r--r--Gemfile.lock2
-rw-r--r--lib/orm/humble_repository.rb18
-rw-r--r--lib/orm/mappings/user_mapping.rb2
-rw-r--r--spec/integration/orm/humble_repository_spec.rb96
-rw-r--r--spec/integration/orm/mappings/user_mapping_spec.rb7
-rw-r--r--spec/integration_helper.rb1
6 files changed, 123 insertions, 3 deletions
diff --git a/Gemfile.lock b/Gemfile.lock
index 7c32f0a..ee7aa37 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -16,7 +16,7 @@ GEM
gem_plugin (0.2.3)
headless (1.0.1)
httpauth (0.2.0)
- humble (0.0.1374296843)
+ humble (0.0.1374368890)
sequel
jwt (0.1.8)
multi_json (>= 1.5)
diff --git a/lib/orm/humble_repository.rb b/lib/orm/humble_repository.rb
new file mode 100644
index 0000000..19940ef
--- /dev/null
+++ b/lib/orm/humble_repository.rb
@@ -0,0 +1,18 @@
+class HumbleRepository
+ def initialize(session, target_type)
+ @session = session
+ @target_type = target_type
+ end
+
+ def find_by(id)
+ find_all.find { |x| x.id == id }
+ end
+
+ def find_all
+ @session.find_all(@target_type)
+ end
+
+ def save(item)
+ @session.save(item)
+ end
+end
diff --git a/lib/orm/mappings/user_mapping.rb b/lib/orm/mappings/user_mapping.rb
index accbfd0..b5c95a3 100644
--- a/lib/orm/mappings/user_mapping.rb
+++ b/lib/orm/mappings/user_mapping.rb
@@ -6,6 +6,6 @@ class UserMapping < Humble::DatabaseMapping
map.type User
map.primary_key(:id, :default => -1)
map.column :username
- #map.column :password
+ map.column :password_hash
end
end
diff --git a/spec/integration/orm/humble_repository_spec.rb b/spec/integration/orm/humble_repository_spec.rb
new file mode 100644
index 0000000..2f3af15
--- /dev/null
+++ b/spec/integration/orm/humble_repository_spec.rb
@@ -0,0 +1,96 @@
+require "integration_helper"
+
+describe HumbleRepository do
+ let(:sut) { HumbleRepository.new(session, Product) }
+ let(:session) { @session_factory.create_session }
+
+ before :each do
+ @configuration.add(ProductMapping.new)
+ end
+
+ context "when fetching all products from the database" do
+ let(:product) { Product.new(:id => 1, :name => "putty") }
+
+ before :each do
+ TestDatabaseGateway.connection.from(:products).insert(:name => product.name)
+ @results = sut.find_all
+ end
+
+ after :each do
+ TestDatabaseGateway.connection.from(:products).delete
+ end
+
+ it "should return each product" do
+ @results.count.should == 1
+ end
+
+ it "should return a product" do
+ @results.first.should be_an_instance_of(Product)
+ end
+
+ it "should map the name properly" do
+ @results.first.name.should == product.name
+ end
+
+ it "should map the id properly" do
+ @results.first.id.should_not be_nil
+ end
+ end
+
+ context "when saving a new item" do
+ let(:product) { Product.new(:name => 'blah') }
+
+ before :each do
+ sut.save(product)
+ end
+
+ after :each do
+ TestDatabaseGateway.delete_all
+ end
+
+ it "should insert a new record into the database" do
+ found = TestDatabaseGateway.connection.from(:products)
+ found.count.should == 1
+ end
+
+ it "should update the product with its new id" do
+ product.id.should be > 0
+ end
+ end
+
+ context "when saving an existing item" do
+ before :each do
+ TestDatabaseGateway.connection.from(:products).insert(:name => 'foo')
+ result = sut.find_all.first
+ result.change_name('bar')
+ sut.save(result)
+ end
+
+ after :each do
+ TestDatabaseGateway.connection.from(:products).delete
+ end
+
+ it "should update the product with the latest changes" do
+ found = TestDatabaseGateway.connection.from(:products).first
+ found[:name].should == 'bar'
+ end
+ end
+
+ context "when fetching an item by its id" do
+ context "when the id exists" do
+ before :each do
+ @id = TestDatabaseGateway.connection.from(:products).insert(:name => 'blah')
+ end
+
+ let(:result) { sut.find_by(@id) }
+
+ it "should return the item" do
+ result.should == Product.new(:id => @id, :name => 'blah')
+ end
+
+ after :each do
+ TestDatabaseGateway.connection.from(:products).delete
+ end
+ end
+ end
+end
diff --git a/spec/integration/orm/mappings/user_mapping_spec.rb b/spec/integration/orm/mappings/user_mapping_spec.rb
index 5525e34..cd9833c 100644
--- a/spec/integration/orm/mappings/user_mapping_spec.rb
+++ b/spec/integration/orm/mappings/user_mapping_spec.rb
@@ -2,9 +2,10 @@ require "integration_helper"
describe User do
context "when fetching all" do
- let(:user) { User.new(:id => 1, :username => "putty") }
+ let(:user) { User.new(:username => "putty") }
before :each do
+ user.change_password("password")
@configuration.add(UserMapping.new)
@session = @session_factory.create_session
@session.save(user)
@@ -27,5 +28,9 @@ describe User do
it "should map the id properly" do
results.first.id.should == user.id
end
+
+ it "should map the password hash" do
+ results.first.password_matches("password").should be_true
+ end
end
end
diff --git a/spec/integration_helper.rb b/spec/integration_helper.rb
index 0a4247d..e29d3f0 100644
--- a/spec/integration_helper.rb
+++ b/spec/integration_helper.rb
@@ -3,6 +3,7 @@ require "spec_helper"
RSpec.configure do |config|
config.before :all do
connection_string = DatabaseConfiguration.new.connection_string
+ @raw_connection = Sequel.connect(connection_string)
@configuration = Humble::Configuration.new(connection_string)
@session_factory = @configuration.build_session_factory
end