Commit 1afe60c

mo <mo.khan@gmail.com>
2018-05-11 22:03:57
add specs for repository.
1 parent 7afa254
Changed files (2)
lib/del/repository.rb
@@ -1,17 +1,18 @@
 module Del
   class Repository
-    def initialize(storage = {})
+    def initialize(storage: {}, mapper:)
       @storage = storage
+      @mapper = mapper
       @lock = Mutex.new
     end
 
     def [](id)
-      find_by(id)
+      find(id)
     end
 
-    def find_by(id)
+    def find(id)
       @lock.synchronize do
-        Del::User.new(id, @storage[id.to_s])
+        @mapper.map_from(@storage[id.to_s])
       end
     end
 
@@ -20,7 +21,6 @@ module Del
     end
 
     def upsert(id, attributes = {})
-      Del.logger.debug([id, attributes].inspect)
       @lock.synchronize do
         @storage[id.to_s] = attributes
       end
spec/repository_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+RSpec.describe Del::Repository do
+  subject { described_class.new(storage: storage, mapper: mapper) }
+  let(:storage) { Hash.new }
+  let(:mapper) { double(:mapper, map_from: nil) }
+
+  describe "#[]" do
+    let(:id) { SecureRandom.uuid }
+    let(:attributes) { { id: id, name: 'Teren Delvon Jones' } }
+    let(:user) { instance_double(Del::User) }
+
+    before do
+      subject.upsert(id, attributes)
+      allow(mapper).to receive(:map_from).with(attributes).and_return(user)
+    end
+
+    specify { expect(subject[id]).to eql(user) }
+    specify { expect(subject.find(id)).to eql(user) }
+  end
+end