tinkit 0.0.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 (51) hide show
  1. data/LICENSE +176 -0
  2. data/README +11 -0
  3. data/Rakefile +75 -0
  4. data/lib/glue_envs/couchrest/couchrest_attachment_handler.rb +260 -0
  5. data/lib/glue_envs/couchrest/couchrest_files_mgr.rb +198 -0
  6. data/lib/glue_envs/couchrest_glue_env.rb +536 -0
  7. data/lib/glue_envs/files_mgr_base.rb +51 -0
  8. data/lib/glue_envs/filesystem/filesystem_files_mgr.rb +187 -0
  9. data/lib/glue_envs/filesystem_glue_env.rb +395 -0
  10. data/lib/glue_envs/mysql/mysql_files_mgr.rb +175 -0
  11. data/lib/glue_envs/mysql_glue_env.rb +428 -0
  12. data/lib/glue_envs/sdb_s3/sdb_s3_files_mgr.rb +314 -0
  13. data/lib/glue_envs/sdb_s3_glue_env.rb +248 -0
  14. data/lib/helpers/camel.rb +21 -0
  15. data/lib/helpers/filesystem_helpers.rb +27 -0
  16. data/lib/helpers/hash_helpers.rb +74 -0
  17. data/lib/helpers/log_helper.rb +34 -0
  18. data/lib/helpers/mime_types_new.rb +126 -0
  19. data/lib/helpers/old_more_open_struct.rb +28 -0
  20. data/lib/helpers/require_helper.rb +45 -0
  21. data/lib/helpers/tk_escape.rb +17 -0
  22. data/lib/midas/bufs_data_structure.rb +84 -0
  23. data/lib/midas/node_element_operations.rb +264 -0
  24. data/lib/tinkit.rb +38 -0
  25. data/lib/tinkit_base_node.rb +733 -0
  26. data/lib/tinkit_node_factory.rb +47 -0
  27. data/spec/couchrest_files_mgr_spec.rb +551 -0
  28. data/spec/couchrest_glue_spec.rb +246 -0
  29. data/spec/filesystem_files_mgr_spec.rb +236 -0
  30. data/spec/filesystem_glue_spec.rb +243 -0
  31. data/spec/filesystem_helpers_spec.rb +42 -0
  32. data/spec/helpers/bufs_node_builder.rb +17 -0
  33. data/spec/helpers/bufs_sample_dataset.rb +160 -0
  34. data/spec/helpers/bufs_test_environments.rb +81 -0
  35. data/spec/helpers/tmp_view_cleaner.rb +15 -0
  36. data/spec/lib_helpers/tk_escape_spec.rb +45 -0
  37. data/spec/mysql_files_mgr_spec.rb +250 -0
  38. data/spec/mysql_glue_spec.rb +214 -0
  39. data/spec/node_element_operations_spec.rb +392 -0
  40. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec1.rb +82 -0
  41. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec2.rb +68 -0
  42. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec3.rb +80 -0
  43. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec4.rb +110 -0
  44. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec5.rb +84 -0
  45. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec6.rb +83 -0
  46. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec7.rb +101 -0
  47. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec8.rb +92 -0
  48. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec_all.rb +266 -0
  49. data/spec/sdb_s3_glue_spec.rb +230 -0
  50. data/spec/tinkit_node_factory_spec.rb +1108 -0
  51. metadata +114 -0
@@ -0,0 +1,392 @@
1
+ #require helper for cleaner require statements
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), '/../lib/helpers/require_helper')
3
+
4
+ require Tinkit.midas 'node_element_operations'
5
+
6
+ module NodeElementOperationsSpecHelpers
7
+ def execute_operations(neo_obj, operand_values={}, initial_field_values={}, op_names=:all)
8
+ field_ops = neo_obj.field_op_defs
9
+ final_field_values = {}
10
+ record_fields = field_ops.keys
11
+
12
+ record_fields.each do |field|
13
+ operations = field_ops[field].keys
14
+ operations.each do |op_label|
15
+ if (op_names == :all || op_names.include?(op_label))
16
+ init_val = initial_field_values[field]
17
+ op_val = operand_values[field]
18
+
19
+ result = execute_single_operation(neo_obj, field, op_label, init_val, op_val)
20
+
21
+ final_field_values[field] = final_field_values[field]||{}
22
+ final_field_values[field][op_label] = result
23
+ else
24
+ next
25
+ end
26
+ end
27
+ end
28
+ final_field_values
29
+ end
30
+
31
+ def execute_single_operation(neo_obj, field, op_label, init_val, op_val)
32
+ field_ops = neo_obj.field_op_defs
33
+ ops = field_ops[field]
34
+ op_proc = ops[op_label] if ops
35
+ result = op_proc.call(init_val, op_val) if op_proc
36
+ end
37
+
38
+ def check_field_results(results, field, operation)
39
+ #puts "Results for Field: #{field} / #{operation}"
40
+ results[field][operation][:update_this]
41
+ end
42
+ end
43
+
44
+ describe NodeElementOperations, "Defaults" do
45
+ include NodeElementOperationsSpecHelpers
46
+
47
+ before(:each) do
48
+ @default_fields = [:id, :data, :name, :tags]#, :kvlist]
49
+ @default_operations = [:add, :subtract]
50
+ #@key_fields = {:required_keys => [:id], :primary_key => :id}}
51
+ end
52
+
53
+ it "should initialize to default when no parameters are used" do
54
+ neo_set = NodeElementOperations.new
55
+ neo_set.field_op_defs.keys.should == @default_fields
56
+ end
57
+
58
+ it "should have a default set of fields and operations" do
59
+ #initial conditions
60
+ neo_set = NodeElementOperations.new
61
+ #the default field to operation assignmen: {:id => :static_ops, :data => :replace_ops}
62
+
63
+ #change the existing values of the fields to these with the default operations
64
+ operand_values = {:id => "should not be able to change default",
65
+ :data => ['new data', 'more new data']}
66
+
67
+ #test
68
+ results = execute_operations(neo_set, operand_values)
69
+ #verify results
70
+ init_vals = {} #default_init_field_values
71
+ neo_set.field_op_set_sym.each do |field, op_label|
72
+ #puts "Checking Field #{field}, Op Label: #{op_label}"
73
+ case op_label
74
+ when :static_ops
75
+ #check results set
76
+ @default_operations.each do |op|
77
+ results[field].keys.should include op
78
+ results[field][op].keys.should include :update_this
79
+ end
80
+ #check results value
81
+ check_field_results(results, field, :add).should == init_vals[field]
82
+ check_field_results(results, field, :subtract).should == init_vals[field]
83
+
84
+ when :replace_ops
85
+ #check results set
86
+ @default_operations.each do |op|
87
+ results[field].keys.should include op
88
+ results[field][op].keys.should include :update_this
89
+ end
90
+ #check results value
91
+ check_field_results(results, field, :add).should == operand_values[field]
92
+ check_field_results(results, field, :subtract).should == nil #nil - anything = nil
93
+
94
+ when :list_ops
95
+ #check results set
96
+ @default_operations.each do |op|
97
+ results[field].keys.should include op
98
+ results[field][op].keys.should include :update_this
99
+ end
100
+ #more complete tests are later
101
+ check_field_results(results, field, :add).should == []
102
+ check_field_results(results, field, :add).should == []
103
+
104
+ when :key_list_ops
105
+ #check results set
106
+ @default_operations.each do |op|
107
+ results[field].keys.should include op
108
+ results[field][op].keys.should include :update_this
109
+ end
110
+ #more complete tests are later
111
+ check_field_results(results, field, :add).should == {}
112
+ check_field_results(results, field, :add).should == {}
113
+
114
+ else
115
+ raise "Invalid Operation Label #{op_label} for field: #{field}"
116
+ end
117
+ end
118
+ end
119
+
120
+ it "should work as expected with populated initial values" do
121
+ end
122
+ end
123
+
124
+ describe NodeElementOperations, "Validate Default Operation Behaviors" do
125
+ include NodeElementOperationsSpecHelpers
126
+
127
+ before(:each) do
128
+ @default_behaviors = [:static_ops, :replace_ops, :list_ops, :key_list_ops]
129
+ @field_op_set = {:test_id => :static_ops,
130
+ :test_label1 => :replace_ops,
131
+ :test_label2 => :replace_ops,
132
+ :test_list => :list_ops,
133
+ :test_kvlist => :key_list_ops }
134
+
135
+ @neo_set = NodeElementOperations.new(:field_op_set => @field_op_set)
136
+ end
137
+
138
+ it "should have field operations for all default operation behaviors" do
139
+ all_behaviors = @default_behaviors.dup
140
+ @field_op_set.each { |op_behav, op_set_sym| all_behaviors.delete(op_set_sym) }
141
+ all_behaviors.size.should == 0 #empty
142
+ end
143
+
144
+ it "should return appropriate - add - operation results for default operation behaviors" do
145
+
146
+ op_name = :add
147
+
148
+ #initial conditions
149
+ initial_operand_values = {:test_id => "Original ID",
150
+ :test_label1 => "Original Label",
151
+ :test_label2 => "Not Used",
152
+ :test_list => ['item1', 'item2'],
153
+ :test_kvlist => { :list1 => ['klist1-a', 'klist1-b'],
154
+ :list2 => ['klist2-a', 'klist2-b']} }
155
+
156
+ new_operand_values = {:test_id => "New ID",
157
+ :test_label1 => "New Label",
158
+ :test_label2 => "Not Used",
159
+ :test_list => ['item3', 'item4'],
160
+ :test_kvlist => { :list1 => ['klist1-c', 'klist1-d'],
161
+ :list2 => 'klist2-c',
162
+ :list3 => 'klist3-a',
163
+ :list4 => ['klist4-a', 'klist4-b']} }
164
+
165
+ #test
166
+ results = execute_operations(@neo_set, new_operand_values, initial_operand_values, [op_name])
167
+
168
+ #verify
169
+ verified_list = {}
170
+ initial_operand_values.keys.should == new_operand_values.keys
171
+
172
+ static_field_name = :test_id
173
+ verified_list[static_field_name] = @field_op_set[static_field_name]
174
+ results[static_field_name][op_name][:update_this].should == initial_operand_values[static_field_name]
175
+
176
+ replace_field_name = :test_label1
177
+ verified_list[replace_field_name] = @field_op_set[replace_field_name]
178
+ results[replace_field_name][op_name][:update_this].should == new_operand_values[replace_field_name]
179
+
180
+ list_field_name = :test_list
181
+ verified_list[list_field_name] = @field_op_set[list_field_name]
182
+ combined_values = initial_operand_values[list_field_name] + new_operand_values[list_field_name]
183
+ results[list_field_name][op_name][:update_this].should == combined_values
184
+
185
+ kvlist_field_name = :test_kvlist
186
+ verified_list[kvlist_field_name] = @field_op_set[kvlist_field_name]
187
+ init_v = initial_operand_values[kvlist_field_name]
188
+ new_v = new_operand_values[kvlist_field_name]
189
+ combined_v = {}
190
+ combined_labels = (init_v.keys + new_v.keys).flatten
191
+ combined_labels.uniq!
192
+ combined_labels.each do |label|
193
+ init_list = [init_v[label]].flatten
194
+ new_list = [new_v[label]].flatten
195
+ #puts "Init List: #{init_list.inspect}"
196
+ #puts "New List: #{new_list.inspect}"
197
+ #combined_list
198
+ combined_v[label] = (init_list + new_list).flatten
199
+ combined_v[label].compact!
200
+ combined_v[label].uniq!
201
+ end
202
+ #what to do with verified list?
203
+ end
204
+
205
+ it "should return appropriate - subtract - operations results for default operation behaviors" do
206
+
207
+ op_name = :subtract
208
+
209
+ #initial conditions
210
+ initial_operand_values = {:test_id => "Original ID",
211
+ :test_label1 => "Original Label",
212
+ :test_label2 => "Remove Me",
213
+ :test_list => ['item1', 'item2'],
214
+ :test_kvlist => { :list1 => ['klist1-a', 'klist1-d'],
215
+ :list2 => ['klist2-a', 'klist2-b'],
216
+ :list3 => 'klist3-a' } }
217
+
218
+ new_operand_values = {:test_id => "New ID",
219
+ :test_label1 => "New Label",
220
+ :test_label2 => "Remove Me",
221
+ :test_list => ['item2', 'item4'],
222
+ :test_kvlist => { :list1 => ['klist1-a', 'klist1-b'],
223
+ :list2 => 'klist2-b',
224
+ :list3 => 'klist3-a',
225
+ :list4 => ['klist4-a', 'klist4-b']} }
226
+
227
+ #test
228
+ results = execute_operations(@neo_set, new_operand_values, initial_operand_values, [op_name])
229
+
230
+ #verify
231
+ verified_list = {}
232
+ initial_operand_values.keys.should == new_operand_values.keys
233
+
234
+ static_field_name = :test_id
235
+ verified_list[static_field_name] = @field_op_set[static_field_name]
236
+ results[static_field_name][op_name][:update_this].should == initial_operand_values[static_field_name]
237
+
238
+ replace_field_name = :test_label1
239
+ verified_list[replace_field_name] = @field_op_set[replace_field_name]
240
+ results[replace_field_name][op_name][:update_this].should == initial_operand_values[replace_field_name]
241
+
242
+ replace_field_name = :test_label2
243
+ verified_list[replace_field_name] = @field_op_set[replace_field_name]
244
+ results[replace_field_name][op_name][:update_this].should == nil
245
+
246
+ list_field_name = :test_list
247
+ verified_list[list_field_name] = @field_op_set[list_field_name]
248
+ subtracted_values = initial_operand_values[list_field_name] - new_operand_values[list_field_name]
249
+ results[list_field_name][op_name][:update_this].should == subtracted_values
250
+ end
251
+ end
252
+
253
+ describe NodeElementOperations, "NodeElementOperation defined but not default key-value lists" do
254
+ include NodeElementOperationsSpecHelpers
255
+
256
+ before(:each) do
257
+ @field_op = :kvlist
258
+ @op_def = :key_list_ops
259
+ @built_in_op_names = [:add, :subtract, :getkeys]
260
+ @neo_set = NodeElementOperations.new(:field_op_set => {@field_op => @op_def})
261
+ end
262
+
263
+ it "should merge in custom field operatoins with default field operations" do
264
+ @neo_set.field_op_set_sym.keys.should include :id
265
+ @neo_set.field_op_set_sym.keys.should include :tags
266
+ @neo_set.field_op_set_sym.keys.should include @field_op
267
+ end
268
+
269
+ it "should provide built-in operations to act on lists of lists" do
270
+ @built_in_op_names.each do |op_name|
271
+ @neo_set.lookup_op_proc(@op_def)[op_name].class.should == Proc
272
+ end
273
+ end
274
+
275
+ it "should return appropriate - add - operation results for list of lists" do
276
+ op_name = :add
277
+
278
+ #initial conditions
279
+ initial_operand_values = {:id => 'InitList',
280
+ :kvlist => { :list1 => ['klist1-a', 'klist1-d'],
281
+ :list2 => ['klist2-a', 'klist2-b'],
282
+ :list3 => 'klist3-a' } }
283
+
284
+ new_operand_values = {:kvlist => { :list1 => ['klist1-c', 'klist1-d'],
285
+ :list2 => 'klist2-c',
286
+ :list3 => 'klist3-a',
287
+ :list4 => ['klist4-a', 'klist4-b']} }
288
+
289
+ kvlist_field_name = :kvlist
290
+ #verified_list[kvlist_field_name] = @field_op_set[kvlist_field_name]
291
+ init_v = initial_operand_values[kvlist_field_name]
292
+ new_v = new_operand_values[kvlist_field_name]
293
+ combined_v = {}
294
+ combined_labels = (init_v.keys + new_v.keys).flatten
295
+ combined_labels.uniq!
296
+ combined_labels.each do |label|
297
+ init_list = [init_v[label]].flatten
298
+ new_list = [new_v[label]].flatten
299
+ #puts "Init List: #{init_list.inspect}"
300
+ #puts "New List: #{new_list.inspect}"
301
+ #combined_list
302
+ combined_v[label] = (init_list + new_list).flatten
303
+ combined_v[label].compact!
304
+ combined_v[label].uniq!
305
+ end
306
+ #combined_v is what we should get back from the operation
307
+
308
+ result_to_update = execute_single_operation(@neo_set, kvlist_field_name, op_name, init_v, new_v)
309
+ result = result_to_update[:update_this]
310
+ result.should == combined_v
311
+ end
312
+
313
+ it "should return appropriate - subtract - operation results for list of lists" do
314
+ op_name = :subtract
315
+
316
+ #initial conditions
317
+ initial_operand_values = {:id => 'InitList',
318
+ :kvlist => { :list1 => ['klist1-a', 'klist1-d'],
319
+ :list2 => ['klist2-a', 'klist2-b'],
320
+ :list3 => 'klist3-a' } }
321
+
322
+ new_operand_values = {:kvlist => { :list1 => ['klist1-c', 'klist1-d'],
323
+ :list2 => 'klist2-b',
324
+ :list3 => 'klist3-a',
325
+ :list4 => ['klist4-a', 'klist4-b']} }
326
+
327
+ kvlist_field_name = :kvlist
328
+ #verified_list[kvlist_field_name] = @field_op_set[kvlist_field_name]
329
+ init_v = initial_operand_values[kvlist_field_name]
330
+ new_v = new_operand_values[kvlist_field_name]
331
+ combined_v = {}
332
+ combined_labels = (init_v.keys).flatten
333
+ combined_labels.uniq!
334
+ combined_labels.each do |label|
335
+ init_list = [init_v[label]].flatten
336
+ new_list = [new_v[label]].flatten
337
+ #puts "Init List: #{init_list.inspect}"
338
+ #puts "New List: #{new_list.inspect}"
339
+ #combined_list
340
+ combined_v[label] = (init_list - new_list).flatten
341
+ combined_v[label].compact!
342
+ combined_v[label].uniq!
343
+ end
344
+ #combined_v is what we should get back from the operation
345
+
346
+ result_to_update = execute_single_operation(@neo_set, kvlist_field_name, op_name, init_v, new_v)
347
+ result = result_to_update[:update_this]
348
+ result.should == combined_v
349
+ end
350
+
351
+ it "should return appropriate - getkeys - operations results for default operation behaviors" do
352
+
353
+ op_name = :getkeys
354
+
355
+ #initial conditions
356
+ initial_operand_values = {:id => "Original ID",
357
+ :kvlist => { :list1 => ['klist1-a', 'klist1-d'],
358
+ :list2 => ['klist2-a', 'klist2-b'],
359
+ :list3 => 'klist3-a' } }
360
+
361
+ lookup_values1 = {:kvlist => 'klist2-b'}
362
+ lookup_values2 = {:kvlist => ['klist2-a', 'klist3-a']}
363
+
364
+ kvlist_field_name = :kvlist
365
+ #verified_list[kvlist_field_name] = @field_op_set[kvlist_field_name]
366
+ init_v = initial_operand_values[kvlist_field_name]
367
+ lookup_v1 = lookup_values1[kvlist_field_name]
368
+ lookup_v2 = lookup_values2[kvlist_field_name]
369
+ expected_return_value1 = []
370
+ expected_return_value2 = []
371
+ init_v.each do |label, list_items|
372
+ expected_return_value1 << label if list_items.include? lookup_v1
373
+ end
374
+ init_v.each do |label, list_items|
375
+ lookup_v2.each do |lookup|
376
+ expected_return_value2 << label if list_items.include? lookup
377
+ end
378
+ end
379
+
380
+ results1 = execute_single_operation(@neo_set, kvlist_field_name, op_name, init_v, lookup_v1)
381
+ results2 = execute_single_operation(@neo_set, kvlist_field_name, op_name, init_v, lookup_v2)
382
+ expected_return_value1.should == results1[:return_value]
383
+ expected_return_value2.should == results2[:return_value]
384
+ initial_operand_values[kvlist_field_name].should == results1[:update_this]
385
+ initial_operand_values[kvlist_field_name].should == results2[:update_this]
386
+
387
+ end
388
+
389
+
390
+ end
391
+
392
+ #complete non-default with proc from scratch
@@ -0,0 +1,82 @@
1
+ #require helper for cleaner require statements
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), '../../lib/helpers/require_helper')
3
+ require Tinkit.glue 'sdb_s3/sdb_s3_files_mgr'
4
+
5
+ include SdbS3Interface
6
+ GlueEnvMock = Struct.new(:user_datastore_location)
7
+
8
+ describe FilesMgr, "Setup and intializatoin" do
9
+ before(:all) do
10
+
11
+ @glue_env_mock = GlueEnvMock.new("attachment_spec__node_loc")
12
+ @node_key = :_id
13
+
14
+ file1_data = "Example File1\nJust some text"
15
+ file2_data = "Example File2\nJust some more text"
16
+ file1_fname = "/tmp/example_file1.txt"
17
+ file2_fname = "/tmp/example_file2.txt"
18
+ files = {file1_fname => file1_data, file2_fname => file2_data}
19
+ files.each do |fname, data|
20
+ File.open(fname, 'w'){|f| f.write(data)}
21
+ end
22
+ @file_datas = [{:src_filename => file1_fname}, {:src_filename => file2_fname}]
23
+ @node1_data = {:_id => 'spec_test1', :data => 'stuff1'}
24
+ end
25
+
26
+ it "should initialize" do
27
+ node_key_value = @node1_data[@node_key]
28
+ attach_handler = FilesMgr.new(@glue_env_mock, node_key_value)
29
+ buck_prefix = FilesMgr::BucketNamespacePrefix
30
+ node_loc = @glue_env_mock.user_datastore_location
31
+ check_bucket_location = "#{buck_prefix}_#{node_loc}"
32
+ #the below will sometimes fail due to AWS
33
+ attach_handler.bucket_name.should == check_bucket_location
34
+ end
35
+ end
36
+
37
+ describe FilesMgr, "Basic Operations" do
38
+ before(:all) do
39
+ @file1_fname = "/tmp/example_file1.txt"
40
+ @file2_fname = "/tmp/example_file2.txt"
41
+ f1_bname = File.basename(@file1_fname)
42
+ f2_bname = File.basename(@file2_fname)
43
+ @file_stored_data = { f1_bname => File.open(@file1_fname, 'rb'){|f| f.read},
44
+ f2_bname =>File.open(@file2_fname, 'rb'){|f| f.read} }
45
+ @glue_env_mock = GlueEnvMock.new("attachment_spec__node_loc")
46
+ @node_key = :_id
47
+ @file_datas = [{:src_filename => @file1_fname}, {:src_filename => @file2_fname}]
48
+ end
49
+
50
+
51
+ before(:each) do
52
+ @node1_data = {:_id => 'spec_test1', :data => 'stuff1'}
53
+ node_key_value = @node1_data[@node_key]
54
+ @attach_handler = FilesMgr.new(@glue_env_mock, node_key_value)
55
+ @attach_handler.subtract_files(nil, :all)
56
+ end
57
+
58
+ after(:each) do
59
+ @attach_handler.destroy_file_container
60
+ end
61
+
62
+ it "1) should add and retrieve files" do
63
+ node = nil
64
+ #@attach_handler.subtract_files(:all)
65
+ #Test that buckets/files don't exist on AWS
66
+ @file_datas.each do |file_data|
67
+ file_basename = File.basename(file_data[:src_filename])
68
+ data = @attach_handler.get_raw_data(node, file_basename)
69
+ data.should be_nil
70
+ end
71
+
72
+ #Add the Files
73
+ @attach_handler.add(node, @file_datas)
74
+
75
+ #Get the files and verify data
76
+ @file_datas.each do |file_data|
77
+ file_basename = File.basename(file_data[:src_filename])
78
+ data = @attach_handler.get_raw_data(node, file_basename)
79
+ data.should == @file_stored_data[file_basename]
80
+ end
81
+ end
82
+ end