importer 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +70 -0
  5. data/Rakefile +60 -0
  6. data/VERSION +1 -0
  7. data/importer.gemspec +114 -0
  8. data/lib/importer.rb +102 -0
  9. data/lib/importer/import.rb +5 -0
  10. data/lib/importer/import/active_record.rb +37 -0
  11. data/lib/importer/import/simple.rb +57 -0
  12. data/lib/importer/imported_object.rb +7 -0
  13. data/lib/importer/imported_object/active_record.rb +41 -0
  14. data/lib/importer/imported_object/simple.rb +25 -0
  15. data/lib/importer/parser.rb +22 -0
  16. data/lib/importer/parser/base.rb +31 -0
  17. data/lib/importer/parser/csv.rb +24 -0
  18. data/lib/importer/parser/xml.rb +25 -0
  19. data/rails/init.rb +1 -0
  20. data/rails_generators/importer/importer_generator.rb +18 -0
  21. data/rails_generators/importer/templates/imported_objects_migration.rb +19 -0
  22. data/rails_generators/importer/templates/imports_migration.rb +17 -0
  23. data/test/database.yml +3 -0
  24. data/test/factories.rb +11 -0
  25. data/test/fixtures/empty.csv +0 -0
  26. data/test/fixtures/empty.xml +3 -0
  27. data/test/fixtures/product.csv +2 -0
  28. data/test/fixtures/product.xml +9 -0
  29. data/test/fixtures/products.csv +4 -0
  30. data/test/fixtures/products.xml +21 -0
  31. data/test/helper.rb +71 -0
  32. data/test/importer/import/active_record_test.rb +27 -0
  33. data/test/importer/import/simple_test.rb +27 -0
  34. data/test/importer/imported_object/active_record_test.rb +37 -0
  35. data/test/importer/imported_object/simple_test.rb +35 -0
  36. data/test/importer/imported_object_test.rb +13 -0
  37. data/test/importer/parser/csv_test.rb +59 -0
  38. data/test/importer/parser/xml_test.rb +58 -0
  39. data/test/importer/parser_test.rb +13 -0
  40. data/test/importer_test.rb +50 -0
  41. metadata +184 -0
@@ -0,0 +1,27 @@
1
+ require 'helper'
2
+
3
+ class Importer::Import::ActiveRecordTest < Test::Unit::TestCase
4
+ context "" do
5
+ setup do
6
+ @product = Factory(:product, :customid => "1", :name => "A pink ball", :description => "Round glass ball.", :price => 86)
7
+ end
8
+
9
+ context "importing from an XML file" do
10
+ setup do
11
+ @import = Product.import(fixture_file("products.xml"))
12
+ end
13
+
14
+ should "create one new product" do
15
+ assert_equal 1, @import.new_objects_count
16
+ end
17
+
18
+ should "modify one existing product" do
19
+ assert_equal 1, @import.existing_objects_count
20
+ end
21
+
22
+ should "not save one product because of errors" do
23
+ assert_equal 1, @import.invalid_objects_count
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ require 'helper'
2
+
3
+ class Importer::Import::SimpleTest < Test::Unit::TestCase
4
+ context "" do
5
+ setup do
6
+ @product = Factory(:product, :customid => "1", :name => "A pink ball", :description => "Round glass ball.", :price => 86)
7
+ end
8
+
9
+ context "importing from an XML file" do
10
+ setup do
11
+ @import = Product.import(fixture_file("products.xml"), :import => Factory(:simple_import))
12
+ end
13
+
14
+ should "create one new product" do
15
+ assert_equal 1, @import.new_objects_count
16
+ end
17
+
18
+ should "modify one existing product" do
19
+ assert_equal 1, @import.existing_objects_count
20
+ end
21
+
22
+ should "not save one product because of errors" do
23
+ assert_equal 1, @import.invalid_objects_count
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,37 @@
1
+ require 'helper'
2
+
3
+ class Importer::ImportedObject::ActiveRecordTest < Test::Unit::TestCase
4
+ context "given an import" do
5
+ setup { @import = Factory(:active_record_import) }
6
+
7
+ context "creating a new imported object" do
8
+ setup { Factory(:active_record_imported_object, :import => @import, :state => "new_object") }
9
+
10
+ should_change("import's new_objects_count", :by => 1) { @import.reload.new_objects_count }
11
+ end
12
+
13
+ context "with an imported object" do
14
+ setup { @imported_object = Factory(:active_record_imported_object, :import => @import, :state => "new_object") }
15
+
16
+ context "destroying the imported object" do
17
+ setup { @imported_object.destroy }
18
+
19
+ should_change("import's new_objects_count", :by => -1) { @import.reload.new_objects_count }
20
+ end
21
+ end
22
+ end
23
+
24
+ context "creating new imported object" do
25
+ setup { @imported_object = Factory(:active_record_imported_object, :data => { "a" => "b" }, :validation_errors => { "c" => "d" }) }
26
+
27
+ should "serialize it's data" do
28
+ data = { "a" => "b" }
29
+ assert_equal data, @imported_object.data
30
+ end
31
+
32
+ should "serialize it's validation errors" do
33
+ validation_errors = { "c" => "d" }
34
+ assert_equal validation_errors, @imported_object.validation_errors
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+
3
+ class Importer::ImportedObject::SimpleTest < Test::Unit::TestCase
4
+ context "given an import" do
5
+ setup { @import = Factory(:simple_import) }
6
+
7
+ context "creating a new imported object" do
8
+ setup do
9
+ imported_object = Importer::ImportedObject::Simple.new(:import => @import)
10
+ imported_object.state = "new_object"
11
+ imported_object.save
12
+ end
13
+
14
+ should_change("import's new_objects_count", :by => 1) { @import.new_objects_count }
15
+ end
16
+
17
+ context "creating new imported object" do
18
+ setup do
19
+ @imported_object = Importer::ImportedObject::Simple.new(:import => @import)
20
+ @imported_object.data = { "a" => "b" }
21
+ @imported_object.validation_errors = { "c" => "d" }
22
+ end
23
+
24
+ should "correctly set/get data" do
25
+ data = { "a" => "b" }
26
+ assert_equal data, @imported_object.data
27
+ end
28
+
29
+ should "correctly set/get validation errors" do
30
+ validation_errors = { "c" => "d" }
31
+ assert_equal validation_errors, @imported_object.validation_errors
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ require 'helper'
2
+
3
+ class Importer::ImportedObjectTest < Test::Unit::TestCase
4
+ should "return ActiveRecord imported object for ActiveRecord import" do
5
+ import = Factory(:active_record_import)
6
+ assert_equal Importer::ImportedObject::ActiveRecord, Importer::ImportedObject.get_klass(import)
7
+ end
8
+
9
+ should "return Simple imported object for Simple import" do
10
+ import = Factory(:simple_import)
11
+ assert_equal Importer::ImportedObject::Simple, Importer::ImportedObject.get_klass(import)
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ require 'helper'
2
+
3
+ class Importer::Parser::CsvTest < Test::Unit::TestCase
4
+ context "parsing an CSV file with many objects defined" do
5
+ setup { @data = Importer::Parser::Csv.run(fixture_file("products.csv")) }
6
+
7
+ should "detect all objects" do
8
+ product1 = {
9
+ "customid" => "1",
10
+ "name" => "A black ball",
11
+ "description" => "Round glass ball.",
12
+ "price" => "86.00"
13
+ }
14
+
15
+ product2 = {
16
+ "customid" => "2",
17
+ "name" => "A red hat",
18
+ "description" => "Party hat.",
19
+ "price" => "114.00"
20
+ }
21
+
22
+ product3 = {
23
+ "customid" => "3",
24
+ "name" => "A white ribbon",
25
+ "description" => "A really long one.",
26
+ "price" => "oops"
27
+ }
28
+
29
+ assert @data.include?(product1)
30
+ assert @data.include?(product2)
31
+ assert @data.include?(product3)
32
+ assert_equal 3, @data.size
33
+ end
34
+ end
35
+
36
+ context "parsing an CSV file with one object defined" do
37
+ setup { @data = Importer::Parser::Csv.run(fixture_file("product.csv")) }
38
+
39
+ should "detect one object" do
40
+ product1 = {
41
+ "customid" => "1",
42
+ "name" => "A black ball",
43
+ "description" => "Round glass ball.",
44
+ "price" => "86.00"
45
+ }
46
+
47
+ assert_equal [product1], @data
48
+ end
49
+ end
50
+
51
+ context "parsing an CSV file with no objects defined" do
52
+ setup { @data = Importer::Parser::Csv.run(fixture_file("empty.csv")) }
53
+
54
+ should "detect no objects" do
55
+ assert_equal [], @data
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,58 @@
1
+ require 'helper'
2
+
3
+ class Importer::Parser::XmlTest < Test::Unit::TestCase
4
+ context "parsing an XML file with many objects defined" do
5
+ setup { @data = Importer::Parser::Xml.run(fixture_file("products.xml")) }
6
+
7
+ should "detect all objects" do
8
+ product1 = {
9
+ "customid" => "1",
10
+ "name" => "A black ball",
11
+ "description" => "Round glass ball.",
12
+ "price" => "86.00"
13
+ }
14
+
15
+ product2 = {
16
+ "customid" => "2",
17
+ "name" => "A red hat",
18
+ "description" => "Party hat.",
19
+ "price" => "114.00"
20
+ }
21
+
22
+ product3 = {
23
+ "customid" => "3",
24
+ "name" => "A white ribbon",
25
+ "description" => "A really long one.",
26
+ "price" => "oops"
27
+ }
28
+
29
+ assert @data.include?(product1)
30
+ assert @data.include?(product2)
31
+ assert @data.include?(product3)
32
+ assert_equal 3, @data.size
33
+ end
34
+ end
35
+
36
+ context "parsing an XML file with one object defined" do
37
+ setup { @data = Importer::Parser::Xml.run(fixture_file("product.xml")) }
38
+
39
+ should "detect one object" do
40
+ product1 = {
41
+ "customid" => "1",
42
+ "name" => "A black ball",
43
+ "description" => "Round glass ball.",
44
+ "price" => "86.00"
45
+ }
46
+
47
+ assert_equal [product1], @data
48
+ end
49
+ end
50
+
51
+ context "parsing an XML file with no objects defined" do
52
+ setup { @data = Importer::Parser::Xml.run(fixture_file("empty.xml")) }
53
+
54
+ should "detect no objects" do
55
+ assert_equal [], @data
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,13 @@
1
+ require 'helper'
2
+
3
+ class Importer::ParserTest < Test::Unit::TestCase
4
+ should "return Xml parser for file with .xml extension" do
5
+ assert_equal Importer::Parser::Xml, Importer::Parser.get_klass("file.xml")
6
+ end
7
+
8
+ should "raise exception if no parser can be found for file" do
9
+ assert_raises Importer::ParserNotFoundError do
10
+ Importer::Parser.get_klass("file.unknown")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,50 @@
1
+ require 'helper'
2
+
3
+ class ImporterTest < Test::Unit::TestCase
4
+ context "" do
5
+ setup do
6
+ @product = Factory(:product, :customid => "1", :name => "A pink ball", :description => "Round glass ball.", :price => 86)
7
+ @import = Importer::Import::ActiveRecord.create
8
+ end
9
+
10
+ context "importing from an XML file" do
11
+ setup do
12
+ Product.import(fixture_file("products.xml"), :import => @import)
13
+ end
14
+
15
+ should_change("product's name", :from => "A pink ball", :to => "A black ball") { @product.reload.name }
16
+
17
+ should_change("products count", :by => 1) { Product.count }
18
+ should "correctly create new product" do
19
+ product = Product.last
20
+
21
+ assert_equal "A red hat", product.name
22
+ assert_equal "Party hat.", product.description
23
+ assert_equal 114, product.price
24
+ assert_equal "2", product.customid
25
+ end
26
+
27
+ should_change("imported objects counts", :by => 3) { Importer::ImportedObject::ActiveRecord.count }
28
+
29
+ should_change("import's workflow state", :to => "finished") { @import.reload.workflow_state }
30
+ end
31
+
32
+ context "when there is exception raised while importing from an XML file" do
33
+ setup do
34
+ begin
35
+ InvalidProduct.import(fixture_file("products.xml"), :import => @import)
36
+ rescue ::Exception => e
37
+ @exception = e
38
+ end
39
+ end
40
+
41
+ should_not_change("product's name") { @product.reload.name }
42
+ should_not_change("products count") { InvalidProduct.count }
43
+ should_not_change("imported objects count") { Importer::ImportedObject::ActiveRecord.count }
44
+ should_not_change("import's workflow state") { @import.reload.workflow_state }
45
+ should "propagate exception" do
46
+ assert_equal "An error occured.", @exception.message
47
+ end
48
+ end
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: importer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - "Micha\xC5\x82 Szajbe"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-20 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: crack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: fastercsv
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: activerecord
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: activesupport
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: workflow
57
+ type: :runtime
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ - !ruby/object:Gem::Dependency
66
+ name: thoughtbot-shoulda
67
+ type: :development
68
+ version_requirement:
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ - !ruby/object:Gem::Dependency
76
+ name: factory_girl
77
+ type: :development
78
+ version_requirement:
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ - !ruby/object:Gem::Dependency
86
+ name: sqlite3-ruby
87
+ type: :development
88
+ version_requirement:
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
95
+ description: Define new objects or modifications of existing ones in XML file and import them to your application.
96
+ email: michal.szajbe@gmail.com
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files:
102
+ - LICENSE
103
+ - README.rdoc
104
+ files:
105
+ - .document
106
+ - .gitignore
107
+ - LICENSE
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - importer.gemspec
112
+ - lib/importer.rb
113
+ - lib/importer/import.rb
114
+ - lib/importer/import/active_record.rb
115
+ - lib/importer/import/simple.rb
116
+ - lib/importer/imported_object.rb
117
+ - lib/importer/imported_object/active_record.rb
118
+ - lib/importer/imported_object/simple.rb
119
+ - lib/importer/parser.rb
120
+ - lib/importer/parser/base.rb
121
+ - lib/importer/parser/csv.rb
122
+ - lib/importer/parser/xml.rb
123
+ - rails/init.rb
124
+ - rails_generators/importer/importer_generator.rb
125
+ - rails_generators/importer/templates/imported_objects_migration.rb
126
+ - rails_generators/importer/templates/imports_migration.rb
127
+ - test/database.yml
128
+ - test/factories.rb
129
+ - test/fixtures/empty.csv
130
+ - test/fixtures/empty.xml
131
+ - test/fixtures/product.csv
132
+ - test/fixtures/product.xml
133
+ - test/fixtures/products.csv
134
+ - test/fixtures/products.xml
135
+ - test/helper.rb
136
+ - test/importer/import/active_record_test.rb
137
+ - test/importer/import/simple_test.rb
138
+ - test/importer/imported_object/active_record_test.rb
139
+ - test/importer/imported_object/simple_test.rb
140
+ - test/importer/imported_object_test.rb
141
+ - test/importer/parser/csv_test.rb
142
+ - test/importer/parser/xml_test.rb
143
+ - test/importer/parser_test.rb
144
+ - test/importer_test.rb
145
+ has_rdoc: true
146
+ homepage: http://github.com/szajbus/importer
147
+ licenses: []
148
+
149
+ post_install_message:
150
+ rdoc_options:
151
+ - --charset=UTF-8
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: "0"
159
+ version:
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: "0"
165
+ version:
166
+ requirements: []
167
+
168
+ rubyforge_project:
169
+ rubygems_version: 1.3.5
170
+ signing_key:
171
+ specification_version: 3
172
+ summary: Import objects from XML files
173
+ test_files:
174
+ - test/factories.rb
175
+ - test/helper.rb
176
+ - test/importer/import/active_record_test.rb
177
+ - test/importer/import/simple_test.rb
178
+ - test/importer/imported_object/active_record_test.rb
179
+ - test/importer/imported_object/simple_test.rb
180
+ - test/importer/imported_object_test.rb
181
+ - test/importer/parser/csv_test.rb
182
+ - test/importer/parser/xml_test.rb
183
+ - test/importer/parser_test.rb
184
+ - test/importer_test.rb