saviour 0.3.0 → 0.3.1

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.
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe "persisted path" do
4
+ before { allow(Saviour::Config).to receive(:storage).and_return(Saviour::LocalStorage.new(local_prefix: @tmpdir, public_url_prefix: "http://domain.com")) }
5
+
6
+ context "can change the default_path on the uploader and previous instances are not affected" do
7
+ it do
8
+ uploader = Class.new(Saviour::BaseUploader) { store_dir { "/store/dir" } }
9
+ klass = Class.new(Test) { include Saviour::Model }
10
+ klass.attach_file :file, uploader
11
+
12
+ with_test_file("example.xml") do |example|
13
+ a = klass.create!
14
+ expect(a.update_attributes(file: example)).to be_truthy
15
+ expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
16
+ expect(File.dirname(a[:file])).to eq "/store/dir"
17
+
18
+
19
+ uploader.class_eval { store_dir { "/another/dir" } }
20
+
21
+ with_test_file("camaloon.jpg") do |example_2|
22
+ b = klass.create!
23
+ expect(b.update_attributes(file: example_2)).to be_truthy
24
+
25
+ expect(Saviour::Config.storage.exists?(b[:file])).to be_truthy
26
+ expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
27
+
28
+ expect(File.dirname(b[:file])).to eq "/another/dir"
29
+ expect(File.dirname(a[:file])).to eq "/store/dir"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe "reload model" do
4
+ before { allow(Saviour::Config).to receive(:storage).and_return(Saviour::LocalStorage.new(local_prefix: @tmpdir, public_url_prefix: "http://domain.com")) }
5
+
6
+ context "updates the Saviour::File instance" do
7
+ it do
8
+ uploader = Class.new(Saviour::BaseUploader) { store_dir { "/store/dir" } }
9
+ klass = Class.new(Test) { include Saviour::Model }
10
+ klass.attach_file :file, uploader
11
+ a = klass.create!
12
+ b = klass.find(a.id)
13
+
14
+ with_test_file("example.xml") do |example|
15
+ a.update_attributes! file: example
16
+ expect(a.file.exists?).to be_truthy
17
+ expect(b.file.exists?).to be_falsey
18
+
19
+ b.reload
20
+ expect(b.file.exists?).to be_truthy
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,178 @@
1
+ require 'spec_helper'
2
+
3
+ describe "validations saving a new file" do
4
+ before { allow(Saviour::Config).to receive(:storage).and_return(Saviour::LocalStorage.new(local_prefix: @tmpdir, public_url_prefix: "http://domain.com")) }
5
+
6
+ let(:uploader) {
7
+ Class.new(Saviour::BaseUploader) {
8
+ store_dir { "/store/dir" }
9
+ }
10
+ }
11
+
12
+ let(:base_klass) {
13
+ a = Class.new(Test) { include Saviour::Model }
14
+ a.attach_file :file, uploader
15
+ a
16
+ }
17
+
18
+ it "raises error if used for non existing attachments" do
19
+ klass = Class.new(base_klass) do
20
+ attach_validation :does_not_exists, :something
21
+ end
22
+ expect { klass.new.valid? }.to raise_error.with_message "There is no attachment defined as 'does_not_exists'"
23
+ end
24
+
25
+ it "fails at block validation" do
26
+ klass = Class.new(base_klass) do
27
+ attach_validation(:file) do |contents, _|
28
+ errors.add(:file, "Cannot start with X") if contents[0] == 'X'
29
+ end
30
+ end
31
+
32
+ with_test_file("example.xml") do |example|
33
+ allow(example).to receive(:read).and_return("X-Extra contents for the file")
34
+ a = klass.new
35
+ a.file = example
36
+ expect(a).not_to be_valid
37
+ expect(a.errors[:file][0]).to eq "Cannot start with X"
38
+ end
39
+
40
+ with_test_file("example.xml") do |example|
41
+ a = klass.new
42
+ a.file = example
43
+ expect(a).to be_valid
44
+ expect(a.save).to be_truthy
45
+ end
46
+ end
47
+
48
+
49
+ it "fails at method validation" do
50
+ klass = Class.new(base_klass) do
51
+ attach_validation :file, :check_filesize
52
+
53
+ def check_filesize(contents, _)
54
+ errors.add(:file, "Filesize must be less than 10 bytes") if contents.bytesize >= 10
55
+ end
56
+ end
57
+
58
+ with_test_file("example.xml") do |example|
59
+ allow(example).to receive(:read).and_return("1234567890")
60
+ a = klass.new
61
+ a.file = example
62
+ expect(a).not_to be_valid
63
+ expect(a.errors[:file][0]).to eq "Filesize must be less than 10 bytes"
64
+ end
65
+
66
+ with_test_file("example.xml") do |example|
67
+ allow(example).to receive(:read).and_return("123456789")
68
+ a = klass.new
69
+ a.file = example
70
+ expect(a).to be_valid
71
+ end
72
+ end
73
+
74
+ it "combined validatinos" do
75
+ klass = Class.new(base_klass) do
76
+ attach_validation :file, :check_filesize
77
+ attach_validation(:file) do |contents, _|
78
+ errors.add(:file, "Cannot start with X") if contents[0] == 'X'
79
+ end
80
+
81
+ def check_filesize(contents, _)
82
+ errors.add(:file, "Filesize must be less than 10 bytes") if contents.bytesize >= 10
83
+ end
84
+ end
85
+
86
+ with_test_file("example.xml") do |example|
87
+ allow(example).to receive(:read).and_return("X-Ex")
88
+ a = klass.new
89
+ a.file = example
90
+ expect(a).not_to be_valid
91
+ expect(a.errors[:file][0]).to eq "Cannot start with X"
92
+ end
93
+
94
+ with_test_file("example.xml") do |example|
95
+ allow(example).to receive(:read).and_return("Ex too long content")
96
+ a = klass.new
97
+ a.file = example
98
+ expect(a).not_to be_valid
99
+ expect(a.errors[:file][0]).to eq "Filesize must be less than 10 bytes"
100
+ end
101
+
102
+ # Consistent order
103
+ with_test_file("example.xml") do |example|
104
+ allow(example).to receive(:read).and_return("X-Ex too long content")
105
+ a = klass.new
106
+ a.file = example
107
+ expect(a).not_to be_valid
108
+ expect(a.errors[:file][0]).to eq "Filesize must be less than 10 bytes"
109
+ expect(a.errors[:file][1]).to eq "Cannot start with X"
110
+ end
111
+ end
112
+
113
+ it "validates by filename" do
114
+ klass = Class.new(base_klass) do
115
+ attach_validation :file, :check_filename
116
+
117
+ def check_filename(_, filename)
118
+ errors.add(:file, "Only .jpg files") unless filename =~ /\.jpg/
119
+ end
120
+ end
121
+
122
+ with_test_file("example.xml") do |example|
123
+ allow(example).to receive(:read).and_return("X-Ex")
124
+ a = klass.new
125
+ a.file = example
126
+ expect(a).not_to be_valid
127
+ expect(a.errors[:file][0]).to eq "Only .jpg files"
128
+ end
129
+
130
+ with_test_file("camaloon.jpg") do |example|
131
+ allow(example).to receive(:read).and_return("X-Ex")
132
+ a = klass.new
133
+ a.file = example
134
+ expect(a).to be_valid
135
+ end
136
+ end
137
+
138
+ it "receives the attached_as information" do
139
+ klass = Class.new(base_klass) do
140
+ attach_validation :file, :check_filename
141
+
142
+ def check_filename(_, _, opts)
143
+ errors.add(:file, "Received error in #{opts[:attached_as]}")
144
+ end
145
+ end
146
+
147
+ with_test_file("example.xml") do |example|
148
+ a = klass.new file: example
149
+ expect(a).not_to be_valid
150
+ expect(a.errors[:file][0]).to eq "Received error in file"
151
+ end
152
+ end
153
+
154
+ context "versions are validated" do
155
+ let(:uploader) {
156
+ Class.new(Saviour::BaseUploader) {
157
+ store_dir { "/store/dir" }
158
+ version(:thumb)
159
+ }
160
+ }
161
+ let(:klass) {
162
+ Class.new(base_klass) do
163
+ attach_validation(:file) do |contents, _, opts|
164
+ errors.add(:file, "Cannot start with X in version #{opts[:version]}") if contents[0] == 'X'
165
+ end
166
+ end
167
+ }
168
+
169
+ it do
170
+ a = klass.create!
171
+ a.file.assign Saviour::StringSource.new("correct contents")
172
+ a.file(:thumb).assign Saviour::StringSource.new("X Incorrect contents")
173
+
174
+ expect(a).not_to be_valid
175
+ expect(a.errors[:file][0]).to eq "Cannot start with X in version thumb"
176
+ end
177
+ end
178
+ end
@@ -14,7 +14,7 @@ describe "saving a new file" do
14
14
  }
15
15
 
16
16
  let(:klass) {
17
- a = Class.new { include Saviour::BasicModel }
17
+ a = Class.new(Test) { include Saviour::Model }
18
18
  a.attach_file :file, uploader
19
19
  a
20
20
  }
@@ -22,13 +22,9 @@ describe "saving a new file" do
22
22
  describe "creation following main file" do
23
23
  it do
24
24
  with_test_file("example.xml") do |example|
25
- a = klass.new
26
- a.file = example
27
- Saviour::LifeCycle.new(a).save!
28
-
29
- path = a.file(:thumb).persisted_path
30
- expect(path).not_to be_nil
31
- expect(Saviour::Config.storage.exists?(path)).to be_truthy
25
+ a = klass.create!
26
+ expect(a.update_attributes(file: example)).to be_truthy
27
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
32
28
  end
33
29
  end
34
30
  end
@@ -36,16 +32,14 @@ describe "saving a new file" do
36
32
  describe "deletion" do
37
33
  it do
38
34
  with_test_file("example.xml") do |example|
39
- a = klass.new
40
- a.file = example
41
- Saviour::LifeCycle.new(a).save!
42
-
43
- expect(Saviour::Config.storage.exists?(a.file(:thumb).persisted_path)).to be_truthy
44
- expect(Saviour::Config.storage.exists?(a.file.persisted_path)).to be_truthy
45
-
46
- Saviour::LifeCycle.new(a).delete!
47
- expect(Saviour::Config.storage.exists?(a.file(:thumb).persisted_path)).to be_falsey
48
- expect(Saviour::Config.storage.exists?(a.file.persisted_path)).to be_falsey
35
+ a = klass.create!
36
+ a.update_attributes(file: example)
37
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
38
+ expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
39
+
40
+ a.destroy
41
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_falsey
42
+ expect(Saviour::Config.storage.exists?(a[:file])).to be_falsey
49
43
  end
50
44
  end
51
45
  end
@@ -53,18 +47,13 @@ describe "saving a new file" do
53
47
  describe "changes following main file" do
54
48
  it do
55
49
  with_test_file("example.xml") do |example|
56
- a = klass.new
57
- a.file = example
58
- Saviour::LifeCycle.new(a).save!
59
- path = a.file(:thumb).persisted_path
60
- expect(Saviour::Config.storage.exists?(path)).to be_truthy
50
+ a = klass.create!
51
+ expect(a.update_attributes(file: example)).to be_truthy
52
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
61
53
 
62
54
  with_test_file("camaloon.jpg") do |file|
63
- a.file = file
64
- Saviour::LifeCycle.new(a).save!
65
- path = a.file(:thumb).persisted_path
66
-
67
- expect(Saviour::Config.storage.exists?(path)).to be_truthy
55
+ a.update_attributes(file: file)
56
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
68
57
  file.rewind
69
58
  expect(a.file(:thumb).read).to eq file.read
70
59
  end
@@ -86,9 +75,8 @@ describe "saving a new file" do
86
75
 
87
76
  it "#url" do
88
77
  with_test_file("example.xml") do |example, name|
89
- a = klass.new
90
- a.file = example
91
- Saviour::LifeCycle.new(a).save!
78
+ a = klass.create!
79
+ expect(a.update_attributes(file: example)).to be_truthy
92
80
 
93
81
  versioned_name = "#{File.basename(name, ".*")}_thumb#{File.extname(name)}"
94
82
  expect(a.file(:thumb).url).to eq "http://domain.com/versions/store/dir/#{versioned_name}"
@@ -97,9 +85,8 @@ describe "saving a new file" do
97
85
 
98
86
  it "#read" do
99
87
  with_test_file("text.txt") do |example|
100
- a = klass.new
101
- a.file = example
102
- Saviour::LifeCycle.new(a).save!
88
+ a = klass.create!
89
+ a.update_attributes(file: example)
103
90
 
104
91
  expect(a.file(:thumb).read).to eq "Hello world\n_for_version_thumb"
105
92
  end
@@ -107,24 +94,21 @@ describe "saving a new file" do
107
94
 
108
95
  it "#delete" do
109
96
  with_test_file("example.xml") do |example|
110
- a = klass.new
111
- a.file = example
112
- Saviour::LifeCycle.new(a).save!
113
- expect(Saviour::Config.storage.exists?(a.file(:thumb).persisted_path)).to be_truthy
114
- expect(Saviour::Config.storage.exists?(a.file.persisted_path)).to be_truthy
97
+ a = klass.create!
98
+ expect(a.update_attributes(file: example)).to be_truthy
99
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
100
+ expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
115
101
 
116
102
  a.file(:thumb).delete
117
-
118
- expect(Saviour::Config.storage.exists?(a.file(:thumb).persisted_path)).to be_falsey
119
- expect(Saviour::Config.storage.exists?(a.file.persisted_path)).to be_truthy
103
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_falsey
104
+ expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
120
105
  end
121
106
  end
122
107
 
123
108
  it "#exists?" do
124
109
  with_test_file("example.xml") do |example|
125
- a = klass.new
126
- a.file = example
127
- Saviour::LifeCycle.new(a).save!
110
+ a = klass.create!
111
+ expect(a.update_attributes(file: example)).to be_truthy
128
112
  expect(a.file(:thumb).exists?).to be_truthy
129
113
  end
130
114
  end
@@ -133,21 +117,18 @@ describe "saving a new file" do
133
117
  describe "assign specific version after first creation" do
134
118
  it do
135
119
  with_test_file("example.xml") do |example|
136
- a = klass.new
137
- a.file = example
138
- Saviour::LifeCycle.new(a).save!
139
-
140
- thumb_path = a.file(:thumb).persisted_path
141
- expect(Saviour::Config.storage.exists?(thumb_path)).to be_truthy
142
- expect(thumb_path).to eq "/versions/store/dir/#{File.basename(example, ".*")}_thumb.xml"
120
+ a = klass.create!
121
+ expect(a.update_attributes(file: example)).to be_truthy
122
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
123
+ expect(a[:file_thumb]).to eq "/versions/store/dir/#{File.basename(example, ".*")}_thumb.xml"
143
124
 
144
125
  with_test_file("camaloon.jpg") do |ex2, filename|
145
126
  a.file(:thumb).assign(ex2)
146
- Saviour::LifeCycle.new(a).save!
147
- thumb_path = a.file(:thumb).persisted_path
148
127
 
149
- expect(Saviour::Config.storage.exists?(thumb_path)).to be_truthy
150
- expect(thumb_path).to eq "/versions/store/dir/#{File.basename(filename, ".*")}.jpg"
128
+ expect(a.save!).to be_truthy
129
+
130
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
131
+ expect(a[:file_thumb]).to eq "/versions/store/dir/#{File.basename(filename, ".*")}.jpg"
151
132
  end
152
133
  end
153
134
  end
@@ -166,23 +147,19 @@ describe "saving a new file" do
166
147
 
167
148
  it "runs the processors for that version only" do
168
149
  with_test_file("example.xml") do |example|
169
- a = klass.new
170
- a.file = example
171
- Saviour::LifeCycle.new(a).save!
172
- thumb_path = a.file(:thumb).persisted_path
173
-
174
- expect(Saviour::Config.storage.exists?(thumb_path)).to be_truthy
175
- expect(thumb_path).to eq "/versions/store/dir/#{File.basename(example, ".*")}_thumb.xml"
150
+ a = klass.create!
151
+ expect(a.update_attributes(file: example)).to be_truthy
152
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
153
+ expect(a[:file_thumb]).to eq "/versions/store/dir/#{File.basename(example, ".*")}_thumb.xml"
176
154
 
177
155
  with_test_file("camaloon.jpg") do |ex2, filename|
178
156
  a.file(:thumb).assign(ex2)
179
157
 
180
- Saviour::LifeCycle.new(a).save!
181
- thumb_path = a.file(:thumb).persisted_path
158
+ expect(a.save!).to be_truthy
182
159
 
183
- expect(Saviour::Config.storage.exists?(thumb_path)).to be_truthy
184
- expect(thumb_path).to eq "/versions/store/dir/#{File.basename(filename, ".*")}.jpg"
185
- expect(Saviour::Config.storage.read(thumb_path)).to eq "modified_content"
160
+ expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
161
+ expect(a[:file_thumb]).to eq "/versions/store/dir/#{File.basename(filename, ".*")}.jpg"
162
+ expect(Saviour::Config.storage.read(a[:file_thumb])).to eq "modified_content"
186
163
  end
187
164
  end
188
165
  end
@@ -191,16 +168,16 @@ describe "saving a new file" do
191
168
 
192
169
  describe "respects version assignation vs main file assignation on conflict" do
193
170
  it do
194
- a = klass.new
171
+ a = klass.create!
195
172
 
196
173
  with_test_file("example.xml") do |file1, fname1|
197
174
  with_test_file("camaloon.jpg") do |file2, fname2|
198
175
  a.file.assign(file1)
199
176
  a.file(:thumb).assign(file2)
200
- Saviour::LifeCycle.new(a).save!
177
+ a.save!
201
178
 
202
- expect(a.file.persisted_path).to eq "/store/dir/#{fname1}"
203
- expect(a.file(:thumb).persisted_path).to eq "/versions/store/dir/#{fname2}"
179
+ expect(a[:file]).to eq "/store/dir/#{fname1}"
180
+ expect(a[:file_thumb]).to eq "/versions/store/dir/#{fname2}"
204
181
  end
205
182
  end
206
183
  end