forked from neo4jrb/carrierwave-neo4j
/
neo4j_spec.rb
124 lines (97 loc) · 2.69 KB
/
neo4j_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require "spec_helper"
def reset_class(uploader = DefaultUploader)
class_name = "User"
Object.send(:remove_const, class_name) rescue nil
user_class = Object.const_set(class_name, Class.new(Neo4j::Rails::Model))
user_class.class_eval do
mount_uploader :image, uploader
end
user_class
end
class DefaultUploader < CarrierWave::Uploader::Base; end
class PngUploader < CarrierWave::Uploader::Base
def extension_white_list
%w(png)
end
end
class ProcessingErrorUploader < CarrierWave::Uploader::Base
process :end_on_an_era
def end_on_an_era
raise CarrierWave::ProcessingError, "Bye Tarja"
end
end
describe CarrierWave::Neo4j do
let(:user_class) { reset_class }
let(:user) { user_class.new }
after do
User.destroy_all
end
describe "#image" do
let(:record) { user_class.new }
subject { record.image }
context "when nothing is assigned" do
it { should be_blank }
end
context "when an empty string is assigned" do
before do
record.image = ""
record.save
record.reload
end
it { should be_blank }
end
context "when a filename is stored" do
before do
record.image = File.open(file_path("tarja.jpg"))
record.save
record.reload
end
it { should be_an_instance_of DefaultUploader }
its(:current_path) { should == public_path("uploads/tarja.jpg") }
end
end
describe "#save" do
context "when remove_image? is true" do
let(:record) { user_class.new }
before do
Neo4j::Transaction.run do
record.image = File.open(file_path("tarja.jpg"))
record.save
record.remove_image = true
record.save # Getting NotInTransactionException here if not wrapped in a transaction block, maybe Neo4j bug?
record.reload
end
end
subject { record }
its(:image) { should be_blank }
end
context "when validating integrity" do
subject do
user = reset_class(PngUploader).new
user.image = File.open(file_path("tarja.jpg"))
user
end
it { should_not be_valid }
end
context "when validating processing" do
subject do
user = reset_class(ProcessingErrorUploader).new
user.image = File.open(file_path("tarja.jpg"))
user
end
it { should_not be_valid }
end
end
describe "#destroy" do
let(:record) { user_class.new }
before do
record.image = File.open(file_path("tarja.jpg"))
record.save
end
it "also destroys the image" do
expect { record.destroy }.to change {
File.exist? public_path("uploads/tarja.jpg")
}.from(true).to(false)
end
end
end