superdupe 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. data/README.markdown +10 -0
  2. data/lib/superdupe/active_resource_extensions.rb +161 -0
  3. data/lib/superdupe/attribute_template.rb +71 -0
  4. data/lib/superdupe/cucumber_hooks.rb +16 -0
  5. data/lib/superdupe/custom_mocks.rb +116 -0
  6. data/lib/superdupe/database.rb +69 -0
  7. data/lib/superdupe/dupe.rb +534 -0
  8. data/lib/superdupe/hash_pruner.rb +37 -0
  9. data/lib/superdupe/log.rb +38 -0
  10. data/lib/superdupe/mock.rb +127 -0
  11. data/lib/superdupe/model.rb +59 -0
  12. data/lib/superdupe/network.rb +56 -0
  13. data/lib/superdupe/record.rb +42 -0
  14. data/lib/superdupe/rest_validation.rb +16 -0
  15. data/lib/superdupe/schema.rb +52 -0
  16. data/lib/superdupe/sequence.rb +20 -0
  17. data/lib/superdupe/singular_plural_detection.rb +9 -0
  18. data/lib/superdupe/string.rb +7 -0
  19. data/lib/superdupe/symbol.rb +3 -0
  20. data/lib/superdupe.rb +20 -0
  21. data/rails_generators/dupe/dupe_generator.rb +20 -0
  22. data/rails_generators/dupe/templates/custom_mocks.rb +4 -0
  23. data/rails_generators/dupe/templates/definitions.rb +9 -0
  24. data/rails_generators/dupe/templates/load_dupe.rb +9 -0
  25. data/spec/lib_specs/active_resource_extensions_spec.rb +141 -0
  26. data/spec/lib_specs/attribute_template_spec.rb +173 -0
  27. data/spec/lib_specs/database_spec.rb +163 -0
  28. data/spec/lib_specs/dupe_spec.rb +522 -0
  29. data/spec/lib_specs/hash_pruner_spec.rb +73 -0
  30. data/spec/lib_specs/log_spec.rb +78 -0
  31. data/spec/lib_specs/logged_request_spec.rb +22 -0
  32. data/spec/lib_specs/mock_definitions_spec.rb +58 -0
  33. data/spec/lib_specs/mock_spec.rb +194 -0
  34. data/spec/lib_specs/model_spec.rb +95 -0
  35. data/spec/lib_specs/network_spec.rb +130 -0
  36. data/spec/lib_specs/record_spec.rb +70 -0
  37. data/spec/lib_specs/rest_validation_spec.rb +17 -0
  38. data/spec/lib_specs/schema_spec.rb +109 -0
  39. data/spec/lib_specs/sequence_spec.rb +39 -0
  40. data/spec/lib_specs/string_spec.rb +31 -0
  41. data/spec/lib_specs/symbol_spec.rb +17 -0
  42. data/spec/spec_helper.rb +8 -0
  43. metadata +142 -0
@@ -0,0 +1,173 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Dupe::Model::Schema::AttributeTemplate do
4
+ describe "new" do
5
+ it "should require a name" do
6
+ proc {
7
+ Dupe::Model::Schema::AttributeTemplate.new
8
+ }.should raise_error(ArgumentError)
9
+ end
10
+
11
+ it "should set the name if one is passed to it" do
12
+ attribute = Dupe::Model::Schema::AttributeTemplate.new(:title)
13
+ attribute.name.should == :title
14
+ end
15
+
16
+ it "should set a default value if one is passed to it" do
17
+ attribute =
18
+ Dupe::Model::Schema::AttributeTemplate.new(
19
+ :title,
20
+ :default => 'Untitled'
21
+ )
22
+ attribute.default.should == 'Untitled'
23
+ end
24
+
25
+ it "should verify that the transformer is a proc" do
26
+ proc {
27
+ attribute =
28
+ Dupe::Model::Schema::AttributeTemplate.new(
29
+ :title,
30
+ :default => nil,
31
+ :transformer => 'not a proc'
32
+ )
33
+ }.should raise_error(
34
+ ArgumentError,
35
+ "Your transformer must be a kind of proc."
36
+ )
37
+ end
38
+
39
+ it "should verify that the transformer requires a single parameter" do
40
+ proc {
41
+ attribute = Dupe::Model::Schema::AttributeTemplate.new(
42
+ :title,
43
+ :default => nil,
44
+ :transformer => proc {'test'}
45
+ )
46
+ }.should raise_error(
47
+ ArgumentError,
48
+ "Your transformer must accept a parameter."
49
+ )
50
+ end
51
+
52
+ it "should set the transformer if one is passed" do
53
+ transformer = proc {|dont_care| }
54
+ attribute = Dupe::Model::Schema::AttributeTemplate.new(
55
+ :title,
56
+ :default => nil,
57
+ :transformer => transformer
58
+ )
59
+ attribute.transformer.should == transformer
60
+ end
61
+ end
62
+
63
+ describe "generate" do
64
+ describe "on an attribute without a default value and without a transformer" do
65
+ before do
66
+ @attribute = Dupe::Model::Schema::AttributeTemplate.new(:title)
67
+ end
68
+
69
+ it "should generate a key with the name of the attribute" do
70
+ key, value = @attribute.generate
71
+ key.should == @attribute.name
72
+ end
73
+
74
+ describe "when passed nothing" do
75
+ it "should generate a nil value" do
76
+ key, value = @attribute.generate
77
+ value.should == nil
78
+ end
79
+ end
80
+
81
+ describe "when passed a value" do
82
+ it "should generate a value equal to the value passed in" do
83
+ key, value = @attribute.generate('test')
84
+ value.should == 'test'
85
+ end
86
+ end
87
+ end
88
+
89
+ describe "on an attribute with a default value but without a transformer" do
90
+ before do
91
+ @attribute = Dupe::Model::Schema::AttributeTemplate.new(:title, :default => 'Untitled')
92
+ end
93
+
94
+ it "should generate a key with the name of the attribute" do
95
+ key, value = @attribute.generate
96
+ key.should == @attribute.name
97
+ end
98
+
99
+ describe "when passed nothing" do
100
+ it "should generate a value equal to the default value" do
101
+ key, value = @attribute.generate
102
+ value.should == @attribute.default
103
+ end
104
+ end
105
+
106
+ describe "when passed a value" do
107
+ it "should generate a value equal to the value passed in" do
108
+ title = '2001: A Space Odyssey'
109
+ key, value = @attribute.generate title
110
+ value.should == title
111
+ end
112
+ end
113
+ end
114
+
115
+ describe "on an attribute with a default value that is a proc" do
116
+ before do
117
+ @default_value = proc { 'knock' * 3 }
118
+ @attribute = Dupe::Model::Schema::AttributeTemplate.new(:title, :default => @default_value)
119
+ end
120
+
121
+ it "should generate a key with the name of the attribute" do
122
+ key, value = @attribute.generate
123
+ key.should == @attribute.name
124
+ end
125
+
126
+ describe "when passed nothing" do
127
+ it "should return the value of the default_value proc" do
128
+ key, value = @attribute.generate
129
+ value.should == @default_value.call
130
+ end
131
+ end
132
+
133
+ describe "when passed a value" do
134
+ it "should generate a value equal to the value passed in" do
135
+ title = 'Rooby'
136
+ key, value = @attribute.generate title
137
+ value.should == title
138
+ end
139
+ end
140
+
141
+ end
142
+
143
+ describe "on an attribute with a transformer" do
144
+ before do
145
+ @transformer = proc {|dont_care| 'test'}
146
+ @attribute = Dupe::Model::Schema::AttributeTemplate.new(
147
+ :title,
148
+ :default => nil,
149
+ :transformer => @transformer
150
+ )
151
+ end
152
+
153
+ it "should generate a key with the name of the attribute" do
154
+ key, value = @attribute.generate
155
+ key.should == @attribute.name
156
+ end
157
+
158
+ describe "when passed nothing" do
159
+ it "should generate a value equal to the default" do
160
+ key, value = @attribute.generate
161
+ value.should == @attribute.default
162
+ end
163
+ end
164
+
165
+ describe "when passed a value" do
166
+ it "should generate a value equal to the value returned by the transformer" do
167
+ key, value = @attribute.generate 'blah'
168
+ value.should == @transformer.call('blah')
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,163 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Dupe::Database do
4
+ before do
5
+ Dupe.reset
6
+ end
7
+
8
+ describe "new" do
9
+ before do
10
+ @database = Dupe::Database.new
11
+ end
12
+
13
+ it "should initialize an empty table set" do
14
+ @database.tables.should be_kind_of(Hash)
15
+ @database.tables.should be_empty
16
+ end
17
+ end
18
+
19
+ describe "delete" do
20
+ before do
21
+ Dupe.stub 10, :books
22
+ end
23
+
24
+ it "should delete the first record found if the resource name is singular and there is no conditions proc" do
25
+ Dupe.find(:books).length.should == 10
26
+ Dupe.database.delete :book
27
+ Dupe.find(:books).length.should == 9
28
+ end
29
+
30
+ it "should delete all records if the resource name is plural and there is no conditions proc" do
31
+ Dupe.find(:books).length.should == 10
32
+ Dupe.database.delete :books
33
+ Dupe.find(:books).length.should == 0
34
+ end
35
+
36
+ it "should delete all matching records if there is a conditions proc and the resource name is singular" do
37
+ Dupe.find(:books).length.should == 10
38
+ Dupe.database.delete :book, proc {|b| b.id < 3}
39
+ Dupe.find(:books).length.should == 8
40
+ end
41
+
42
+ it "should delete all matching records if there is a conditions proc and the resource name is plural" do
43
+ Dupe.find(:books).length.should == 10
44
+ Dupe.database.delete :books, proc {|b| b.id < 3}
45
+ Dupe.find(:books).length.should == 8
46
+ end
47
+ end
48
+
49
+ describe "insert" do
50
+ before do
51
+ Dupe.define :book
52
+ @book = Dupe.models[:book].create :title => 'test'
53
+ @database = Dupe::Database.new
54
+ end
55
+
56
+ it "should require a record" do
57
+ proc {@database.insert}.should raise_error(ArgumentError)
58
+ proc {
59
+ @database.insert 'not a Dupe::Database::Record object'
60
+ }.should raise_error(
61
+ ArgumentError,
62
+ "You may only insert well-defined Dupe::Database::Record objects"
63
+ )
64
+ proc {
65
+ @database.insert Dupe::Database::Record.new
66
+ }.should raise_error(
67
+ ArgumentError,
68
+ "You may only insert well-defined Dupe::Database::Record objects"
69
+ )
70
+ proc {@database.insert @book}.should_not raise_error
71
+ end
72
+
73
+ it "should create a new table if one does not already exist upon insert" do
74
+ @database.tables.should be_empty
75
+ @database.insert @book
76
+ @database.tables[:book].should_not be_nil
77
+ @database.tables[:book].should be_kind_of(Array)
78
+ @database.tables[:book].should_not be_empty
79
+ @database.tables[:book].first.should_not be_nil
80
+ @database.tables[:book].first.should == @book
81
+ end
82
+ end
83
+
84
+ describe "select" do
85
+ before do
86
+ Dupe.define :book
87
+ @book = Dupe.models[:book].create :title => 'test'
88
+ @database = Dupe::Database.new
89
+ @database.insert @book
90
+ end
91
+
92
+ it "should require a valid model name" do
93
+ proc { @database.select }.should raise_error(ArgumentError)
94
+ proc { @database.select :undefined_model }.should raise_error(
95
+ Dupe::Database::TableDoesNotExistError,
96
+ "The table ':undefined_model' does not exist."
97
+ )
98
+ proc { @database.select :book }.should_not raise_error
99
+ end
100
+
101
+ it "should accept a conditions proc" do
102
+ proc { @database.select :book, proc {|c| true} }.should_not raise_error
103
+ end
104
+
105
+ it "should verify that the conditions proc accepts a single parameter" do
106
+ proc { @database.select :book, proc {true} }.should raise_error(
107
+ Dupe::Database::InvalidQueryError,
108
+ "There was a problem with your select conditions. Please consult the API."
109
+ )
110
+ end
111
+
112
+ it "should find the requested items and return an array" do
113
+ results = @database.select :book, proc {|b| b.title == 'test' }
114
+ results.should be_kind_of(Array)
115
+ results.should_not be_empty
116
+ results.first.__model__.should == Dupe.models[:book]
117
+ results.first.__model__.name.should == :book
118
+ results.first.title.should == 'test'
119
+ results.first.id.should == 1
120
+ end
121
+ end
122
+
123
+ describe "create_table" do
124
+ it "should create a database table if one doesn't already exist" do
125
+ @database = Dupe::Database.new
126
+ @database.tables.length.should == 0
127
+ @database.tables[:book].should be_nil
128
+ @database.create_table 'book'
129
+ @database.tables[:book].should_not be_nil
130
+ @database.tables.length.should == 1
131
+ @database.tables[:book].should == []
132
+
133
+ # make sure it doesn't overwrite a table that already exists
134
+ m = Dupe::Model.new :book
135
+ record = m.create
136
+ @database.insert record
137
+ @database.tables[:book].length.should == 1
138
+ @database.tables[:book].first.should == record
139
+ @database.create_table :book
140
+ @database.tables[:book].length.should == 1
141
+ @database.tables[:book].first.should == record
142
+ end
143
+ end
144
+
145
+ describe "truncate_tables" do
146
+ it "should remove all records from all tables" do
147
+ Dupe.create :book
148
+ Dupe.create :author
149
+ Dupe.create :publisher
150
+ Dupe.database.tables.length.should == 3
151
+ Dupe.database.tables[:book].length.should == 1
152
+ Dupe.database.tables[:author].length.should == 1
153
+ Dupe.database.tables[:publisher].length.should == 1
154
+ Dupe.database.truncate_tables
155
+ Dupe.database.tables.length.should == 3
156
+ Dupe.database.tables[:book].length.should == 0
157
+ Dupe.database.tables[:author].length.should == 0
158
+ Dupe.database.tables[:publisher].length.should == 0
159
+ end
160
+ end
161
+
162
+
163
+ end