Skip to content
Browse files

Now able to specify AR model + key on DataPatcher instanciation. Test…

…s cleanup. We're mostly done.
  • Loading branch information...
1 parent 04916f8 commit 645fb60981cc5bdab1bbd44579bcfb96ed24d618 @thbar committed Mar 11, 2009
Showing with 39 additions and 42 deletions.
  1. +13 −12 lib/data_patcher.rb
  2. +12 −23 spec/data_patcher_spec.rb
  3. +0 −7 spec/migrations/create_items.rb
  4. +14 −0 spec/spec_helper.rb
View
25 lib/data_patcher.rb
@@ -1,9 +1,19 @@
-require 'activesupport'
+require 'active_record'
+require 'active_support'
class DataPatcher
+ attr_reader :model, :key, :patch
- def analyze_record(key, attributes)
- existing = Item.find_by_asin(attributes[key])
+ # model: activerecord class
+ # key: unique key accross all model instances
+ def initialize(model, key)
+ @model = model
+ @key = key
+ @patch = {}
+ end
+
+ def analyze_record(attributes)
+ existing = model.find(:first, :conditions => { key => attributes[key] })
if existing
existing_attributes = existing.attributes.dup.symbolize_keys.except(:id)
diff = attributes.diff(existing_attributes)
@@ -19,14 +29,5 @@ def analyze_record(key, attributes)
end
end
- # test commodity - remove ?
- def analyze_records(key, records)
- records.each { |record| analyze_record(key, record) }
- end
-
- def patch
- @patch ||= {}
- end
-
end
View
35 spec/data_patcher_spec.rb
@@ -3,40 +3,29 @@
describe DataPatcher do
before(:each) do
- @existing_item_1 = {
- :title => "Mastering Data Warehouse Aggregates",
- :summary => "The first book to provide in-depth coverage of star schema aggregates...",
- :asin => "0471777099" }
- @existing_item_2 = {
- :title => "The Data Warehouse ETL Toolkit",
- :summary => "The single most authoritative guide on the most difficult phase of building a data warehouse",
- :asin => "0764567578" }
- @existing_items = [@existing_item_1, @existing_item_2]
- Item.delete_all
- Item.create!(@existing_item_1)
- Item.create!(@existing_item_2)
- @patcher = DataPatcher.new
- end
-
- def patcher
- @patcher
+ load_fixture
+
+ @patcher = DataPatcher.new(Item, :asin)
+ # pre-load the patcher with all the existing data, so that we can simulate an addition or modification
+ @patcher.analyze_record(@existing_item_1)
+ @patcher.analyze_record(@existing_item_2)
end
it "provides an addition patch including the key and the attributes" do
- patcher.analyze_records(:asin, @existing_items)
- patcher.analyze_record(:asin, { :asin => "123", :title => "Hello", :summary => "The best of hello world" } )
- patcher.patch.should == {
+ @patcher.analyze_record({ :asin => "123", :title => "Hello", :summary => "The best of hello world" } )
+
+ @patcher.patch.should == {
:added => [{ :asin => "123", :title => "Hello", :summary => "The best of hello world" }]
}
end
it "provides a modification patch including the key and updated attributes" do
- patcher.analyze_records(:asin, @existing_items)
- patcher.analyze_record(:asin, {
+ @patcher.analyze_record( {
:asin => "0471777099", # key
:title => "Mastering Data Warehouse Aggregates (Hard cover)", # changed data
:summary => "The first book to provide in-depth coverage of star schema aggregates..."}) # unchanged data
- patcher.patch.should == {
+
+ @patcher.patch.should == {
:modified => [{ :asin => "0471777099", :title => "Mastering Data Warehouse Aggregates (Hard cover)"}]
}
end
View
7 spec/migrations/create_items.rb
@@ -5,10 +5,3 @@
t.column :asin, :string, :null => false
# TODO - add timestamps here ?
end
-
-Item.create :title => "Mastering Data Warehouse Aggregates",
- :summary => "tThe first book to provide in-depth coverage of star schema aggregates...",
- :asin => "0471777099"
-Item.create :title => "The Data Warehouse ETL Toolkit",
- :summary => "The single most authoritative guide on the most difficult phase of building a data warehouse",
- :asin => "0764567578"
View
14 spec/spec_helper.rb
@@ -2,6 +2,7 @@
require 'spec'
$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.dirname(__FILE__) + "/../lib")
require 'data_patcher'
@@ -28,3 +29,16 @@
require file.gsub(/\.rb$/, '')
end
+def load_fixture
+ @existing_item_1 = {
+ :title => "Mastering Data Warehouse Aggregates",
+ :summary => "The first book to provide in-depth coverage of star schema aggregates...",
+ :asin => "0471777099" }
+ @existing_item_2 = {
+ :title => "The Data Warehouse ETL Toolkit",
+ :summary => "The single most authoritative guide on the most difficult phase of building a data warehouse",
+ :asin => "0764567578" }
+ Item.delete_all
+ Item.create!(@existing_item_1)
+ Item.create!(@existing_item_2)
+end

0 comments on commit 645fb60

Please sign in to comment.
Something went wrong with that request. Please try again.