This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_field_spec.rb
93 lines (77 loc) · 2.79 KB
/
redis_field_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
require_relative './spec_helper.rb'
describe RedisField do
describe RedisField::ActiveRecord do
TestUser.has_redis_fields :test_field1, :test_field2
TestUser.has_redis_field :test_field3
def get_from_redis(id, key)
data = Redis::Namespace.new("ar_redis_field:test:test_user:#{id}", redis: RedisField::Base.redis)[key]
Marshal.load data if data
end
let(:klass) { TestUser }
subject { klass.new }
after(:all) do
RedisField::Testing.reset_keys!
end
it 'klass has redis fields' do
expect(
klass.instance_variable_get('@redis_fields').field_names
).to eq([:test_field1, :test_field2, :test_field3])
end
it 'klass\' redis fields are an instance of DirtyFieldSet' do
expect(
klass.instance_variable_get('@redis_fields')
).to be_instance_of(RedisField::DirtyFieldSet)
end
it { should respond_to :test_field1 }
it { should respond_to :test_field2 }
it { should respond_to :test_field3 }
it { should respond_to :test_field1= }
it { should respond_to :test_field2= }
it { should respond_to :test_field3= }
it { should respond_to :test_field1_redis_value }
it { should respond_to :test_field2_redis_value }
it { should respond_to :test_field3_redis_value }
it { should respond_to :test_field1_redis_value= }
it { should respond_to :test_field2_redis_value= }
it { should respond_to :test_field3_redis_value= }
it { should respond_to :sync_redis_fields! }
it { should respond_to :get_redis_fields }
it 'runs save callbacks' do
subject.run_callbacks(:save) do
subject.should_receive(:sync_redis_fields!).once
end
end
it 'runs create callbacks' do
subject.run_callbacks(:create) do
subject.should_receive(:sync_redis_fields!).once
end
end
it 'runs initialize callbacks' do
subject.run_callbacks(:initialize) do
subject.should_receive(:get_redis_fields).once
end
end
describe 'saving' do
before(:each) do
subject.save
@user = subject
end
it 'should not save in redis until the record is saved' do
@user.test_field1 = 'test'
get_from_redis(@user, 'test_field1').should be_nil
end
it 'should save to redis when the record is saved' do
@user.test_field1 = 'test1'
@user.test_field2 = 'test2'
@user.test_field3 = 'test3'
@user.save
get_from_redis(@user.id, 'test_field1').should eq('test1')
get_from_redis(@user.id, 'test_field2').should eq('test2')
get_from_redis(@user.id, 'test_field3').should eq('test3')
@user.reload.test_field1.should eq('test1')
@user.reload.test_field2.should eq('test2')
@user.reload.test_field3.should eq('test3')
end
end
end
end