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