gnuside-mongoid-grid_fs 1.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,247 @@
1
+ require_relative 'helper'
2
+
3
+ Testing Mongoid::GridFs do
4
+ ##
5
+ #
6
+ GridFS =
7
+ GridFs =
8
+ Mongoid::GridFS
9
+
10
+ prepare do
11
+ GridFS::File.delete_all
12
+ GridFS::Chunk.delete_all
13
+ end
14
+
15
+ ##
16
+ #
17
+ context '#put' do
18
+
19
+ test 'default' do
20
+ filename = __FILE__
21
+ basename = File.basename(filename)
22
+
23
+ g = assert{ GridFS.put(filename) }
24
+
25
+ assert{ g.filename =~ %r| #{ object_id_re } / #{ basename } \Z|imox }
26
+ assert{ g.content_type == "application/x-ruby" }
27
+ assert{ g.data == IO.read(filename) }
28
+ end
29
+
30
+ test 'with a :filename' do
31
+ filename = 'path/info/a.rb'
32
+
33
+ g = assert{ GridFS.put(__FILE__, :filename => filename) }
34
+
35
+ assert{ g.filename == filename }
36
+ end
37
+
38
+ test 'with your own attributes' do
39
+ my_value = "my_value"
40
+
41
+ g = assert{ GridFS.put(__FILE__, :my_value => my_value) }
42
+
43
+ assert{ g.my_value == my_value }
44
+ end
45
+
46
+ test 'when error occurs (eg. missing file)' do
47
+ file = '/path/to/missing'
48
+
49
+ assert_raises(Errno::ENOENT){ GridFs.put(file) }
50
+ end
51
+
52
+ end
53
+
54
+
55
+ ##
56
+ #
57
+ context '#get' do
58
+
59
+ test 'default' do
60
+ id = assert{ GridFS::File.last.id }
61
+ g = assert{ GridFs.get(id) }
62
+ end
63
+
64
+ end
65
+
66
+ ##
67
+ #
68
+ context '#delete' do
69
+
70
+ test 'default' do
71
+ id = assert{ GridFS::File.last.id }
72
+ g = assert{ GridFs.get(id) }
73
+ assert{ GridFs.delete(id) }
74
+ assert_raises( Mongoid::Errors::DocumentNotFound){ GridFs.get(id) }
75
+ end
76
+
77
+ end
78
+
79
+ ##
80
+ #
81
+ context '[] and []=' do
82
+
83
+ test 'default' do
84
+ path = 'a.rb'
85
+ data = IO.read(__FILE__)
86
+
87
+ sio = SIO.new(path, data)
88
+
89
+ g = assert{ GridFs[path] = sio and GridFs[path] }
90
+
91
+ assert{ g.data == data }
92
+ assert{ g.content_type == "application/x-ruby" }
93
+
94
+ before = GridFs::File.count
95
+
96
+ assert{ GridFs[path] = SIO.new(path, 'foobar') }
97
+ assert{ GridFs[path].data == 'foobar' }
98
+
99
+ after = GridFs::File.count
100
+
101
+ created = after - before
102
+
103
+ assert{ created.zero? }
104
+ end
105
+
106
+ ##
107
+ #
108
+ context 'data uris' do
109
+
110
+ test 'default' do
111
+ id = assert{ GridFS::File.last.id }
112
+ g = assert{ GridFs.get(id) }
113
+
114
+ content_type = g.content_type
115
+ base64 = [g.to_s].pack('m').chomp
116
+
117
+ data_uri = "data:#{ content_type };base64,".concat(base64)
118
+
119
+ assert{ g.data_uri == data_uri }
120
+ end
121
+
122
+ end
123
+
124
+ ##
125
+ #
126
+ context 'slicing and dicing' do
127
+
128
+ test 'range' do
129
+ id = assert { GridFS::File.last.id }
130
+ g = assert { GridFs.get(id) }
131
+ assert { g.data[1..3] == g.slice(1..3) }
132
+ end
133
+
134
+ test 'start and length' do
135
+ id = assert { GridFS::File.last.id }
136
+ g = assert { GridFs.get(id) }
137
+ assert { g.data[1, 3] == g.slice(1, 3) }
138
+ end
139
+
140
+ test 'just a single param' do
141
+ id = assert { GridFS::File.last.id }
142
+ g = assert {GridFs.get(id) }
143
+
144
+ assert {g.data[1] == g.slice(1) }
145
+ end
146
+
147
+ test 'getting the last index' do
148
+ id = assert { GridFS::File.last.id }
149
+ g = assert {GridFs.get(id) }
150
+ assert {g.data[-1] == g.slice(-1) }
151
+ end
152
+
153
+ test 'yanking from the end of the data' do
154
+ id = assert { GridFS::File.last.id }
155
+ g = assert {GridFs.get(id) }
156
+ assert {g.data[-3, 2] == g.slice(-3, 2) }
157
+ end
158
+
159
+ test 'multiple chunks...' do
160
+ path = 'slice_and_dice.txt'
161
+
162
+ assert { GridFs[path] = SIO.new(path, "foobar" * 256 * 1024) }
163
+
164
+ g = GridFs[path]
165
+
166
+ assert { g.chunks.count > 0 }
167
+ assert { g.data[10, (256 * 1024 * 2)] == g.slice(10, (256 * 1024 * 2)) }
168
+ end
169
+ end
170
+
171
+ ##
172
+ #
173
+ context 'iterating each chunk' do
174
+ test 'having file size more than 42mb' do
175
+ require 'tempfile'
176
+
177
+ orig, copy = %w{orig copy}.map do |suffix|
178
+ Tempfile.new("mongoid-grid_fs~43mb.#{suffix}")
179
+ end
180
+
181
+ assert system("dd if=/dev/zero of=#{orig.path} bs=#{43*1024*1024} count=1 &> /dev/null")
182
+
183
+ GridFs.get(GridFs.put(orig.path).id).each do |chunk|
184
+ copy.print(chunk.to_s)
185
+ end
186
+
187
+ assert { File.size(copy.path) == File.size(orig.path) }
188
+ end
189
+ end
190
+
191
+ ##
192
+ #
193
+ context 'namespaces' do
194
+ test 'default' do
195
+ assert{ GridFs.namespace.prefix == 'fs' }
196
+ assert{ GridFs.file_model.collection_name.to_s == 'fs.files' }
197
+ assert{ GridFs.chunk_model.collection_name.to_s == 'fs.chunks' }
198
+ end
199
+
200
+ test 'new' do
201
+ ns = GridFs.namespace_for(:ns)
202
+
203
+ assert{ ns.prefix == 'ns' }
204
+
205
+ assert{ ns.file_model < Mongoid::Document }
206
+ assert{ ns.file_model.collection_name.to_s == 'ns.files' }
207
+
208
+ assert{ ns.chunk_model < Mongoid::Document }
209
+ assert{ ns.chunk_model.collection_name.to_s == 'ns.chunks' }
210
+
211
+ assert{ ns.file_model.destroy_all }
212
+
213
+ count = GridFs::File.count
214
+
215
+ assert{ ns.file_model.count == 0}
216
+ assert{ ns.put __FILE__ }
217
+ assert{ ns.file_model.count == 1}
218
+
219
+ assert{ count == GridFs::File.count }
220
+ end
221
+ end
222
+
223
+ end
224
+
225
+ ##
226
+ #
227
+ context 'rails' do
228
+ test 'paths' do
229
+ testdir = File.dirname(__FILE__)
230
+ gemdir = File.dirname(testdir)
231
+ libdir = File.join(gemdir, 'lib')
232
+
233
+ expanded = proc{|paths| Array(paths).map{|path| File.expand_path(path)}}
234
+
235
+ assert{
236
+ expanded[ Mongoid::GridFS::Engine.paths['app/models'] ] == expanded[ libdir ]
237
+ }
238
+ end
239
+ end
240
+
241
+ protected
242
+ def object_id_re
243
+ object_id = defined?(Moped::BSON) ? Moped::BSON::ObjectId.new : BSON::ObjectId.new
244
+
245
+ %r| \w{#{ object_id.to_s.size }} |iomx
246
+ end
247
+ end
data/test/testing.rb ADDED
@@ -0,0 +1,196 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test/unit'
3
+
4
+ testdir = File.expand_path(File.dirname(__FILE__))
5
+ rootdir = File.dirname(testdir)
6
+ libdir = File.join(rootdir, 'lib')
7
+
8
+ STDOUT.sync = true
9
+
10
+ $:.unshift(testdir) unless $:.include?(testdir)
11
+ $:.unshift(libdir) unless $:.include?(libdir)
12
+ $:.unshift(rootdir) unless $:.include?(rootdir)
13
+
14
+ class Testing
15
+ class Slug < ::String
16
+ def Slug.for(*args)
17
+ string = args.flatten.compact.join('-')
18
+ words = string.to_s.scan(%r/\w+/)
19
+ words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
20
+ words.delete_if{|word| word.nil? or word.strip.empty?}
21
+ new(words.join('-').downcase)
22
+ end
23
+ end
24
+
25
+ class Context
26
+ attr_accessor :name
27
+
28
+ def initialize(name, *args)
29
+ @name = name
30
+ end
31
+
32
+ def to_s
33
+ Slug.for(name)
34
+ end
35
+ end
36
+ end
37
+
38
+ def Testing(*args, &block)
39
+ Class.new(::Test::Unit::TestCase) do
40
+
41
+ ## class methods
42
+ #
43
+ class << self
44
+ def contexts
45
+ @contexts ||= []
46
+ end
47
+
48
+ def context(*args, &block)
49
+ return contexts.last if(args.empty? and block.nil?)
50
+
51
+ context = Testing::Context.new(*args)
52
+ contexts.push(context)
53
+
54
+ begin
55
+ block.call(context)
56
+ ensure
57
+ contexts.pop
58
+ end
59
+ end
60
+
61
+ def slug_for(*args)
62
+ string = [context, args].flatten.compact.join('-')
63
+ words = string.to_s.scan(%r/\w+/)
64
+ words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
65
+ words.delete_if{|word| word.nil? or word.strip.empty?}
66
+ words.join('-').downcase.sub(/_$/, '')
67
+ end
68
+
69
+ def name() const_get(:Name) end
70
+
71
+ def testno()
72
+ '%05d' % (@testno ||= 0)
73
+ ensure
74
+ @testno += 1
75
+ end
76
+
77
+ def testing(*args, &block)
78
+ method = ["test", testno, slug_for(*args)].delete_if{|part| part.empty?}.join('_')
79
+ define_method(method, &block)
80
+ end
81
+
82
+ def test(*args, &block)
83
+ testing(*args, &block)
84
+ end
85
+
86
+ def setup(&block)
87
+ define_method(:setup, &block) if block
88
+ end
89
+
90
+ def teardown(&block)
91
+ define_method(:teardown, &block) if block
92
+ end
93
+
94
+ def prepare(&block)
95
+ @prepare ||= []
96
+ @prepare.push(block) if block
97
+ @prepare
98
+ end
99
+
100
+ def cleanup(&block)
101
+ @cleanup ||= []
102
+ @cleanup.push(block) if block
103
+ @cleanup
104
+ end
105
+ end
106
+
107
+ ## configure the subclass!
108
+ #
109
+ const_set(:Testno, '0')
110
+ slug = slug_for(*args).gsub(%r/-/,'_')
111
+ name = ['TESTING', '%03d' % const_get(:Testno), slug].delete_if{|part| part.empty?}.join('_')
112
+ name = name.upcase!
113
+ const_set(:Name, name)
114
+ const_set(:Missing, Object.new.freeze)
115
+
116
+ ## instance methods
117
+ #
118
+ alias_method('__assert__', 'assert')
119
+
120
+ def assert(*args, &block)
121
+ if args.size == 1 and args.first.is_a?(Hash)
122
+ options = args.first
123
+ expected = getopt(:expected, options){ missing }
124
+ actual = getopt(:actual, options){ missing }
125
+ if expected == missing and actual == missing
126
+ actual, expected, *ignored = options.to_a.flatten
127
+ end
128
+ expected = expected.call() if expected.respond_to?(:call)
129
+ actual = actual.call() if actual.respond_to?(:call)
130
+ assert_equal(expected, actual)
131
+ end
132
+
133
+ if block
134
+ label = "assert(#{ args.join(' ') })"
135
+ result = nil
136
+ assert_nothing_raised{ result = block.call }
137
+ __assert__(result, label)
138
+ result
139
+ else
140
+ result = args.shift
141
+ label = "assert(#{ args.join(' ') })"
142
+ __assert__(result, label)
143
+ result
144
+ end
145
+ end
146
+
147
+ def missing
148
+ self.class.const_get(:Missing)
149
+ end
150
+
151
+ def getopt(opt, hash, options = nil, &block)
152
+ [opt.to_s, opt.to_s.to_sym].each do |key|
153
+ return hash[key] if hash.has_key?(key)
154
+ end
155
+ default =
156
+ if block
157
+ block.call
158
+ else
159
+ options.is_a?(Hash) ? options[:default] : nil
160
+ end
161
+ return default
162
+ end
163
+
164
+ def subclass_of exception
165
+ class << exception
166
+ def ==(other) super or self > other end
167
+ end
168
+ exception
169
+ end
170
+
171
+ ##
172
+ #
173
+ module_eval(&block)
174
+
175
+ self.setup()
176
+ self.prepare.each{|b| b.call()}
177
+
178
+ at_exit{
179
+ self.teardown()
180
+ self.cleanup.each{|b| b.call()}
181
+ }
182
+
183
+ self
184
+ end
185
+ end
186
+
187
+
188
+ if $0 == __FILE__
189
+
190
+ Testing 'Testing' do
191
+ testing('foo'){ assert true }
192
+ test{ assert true }
193
+ p instance_methods.grep(/test/)
194
+ end
195
+
196
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gnuside-mongoid-grid_fs
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Ara T. Howard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: mime-types
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.19'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.19'
47
+ description: a mongoid 3/moped compatible implementation of the grid_fs specification
48
+ email: ara.t.howard@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - README.md
54
+ - Rakefile
55
+ - lib/app/models/mongoid/grid_fs.rb
56
+ - lib/app/models/mongoid/grid_fs/fs/chunk.rb
57
+ - lib/app/models/mongoid/grid_fs/fs/file.rb
58
+ - lib/mongoid-grid_fs.rb
59
+ - gnuside-mongoid-grid_fs.gemspec
60
+ - test/helper.rb
61
+ - test/mongoid-grid_fs_test.rb
62
+ - test/testing.rb
63
+ homepage: https://github.com/ahoward/mongoid-grid_fs
64
+ licenses:
65
+ - Ruby
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project: codeforpeople
83
+ rubygems_version: 2.0.3
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: mongoid-grid_fs
87
+ test_files: []