memento 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,55 +1,55 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
2
 
3
3
  describe Memento::State do
4
-
4
+
5
5
  before do
6
6
  setup_db
7
7
  setup_data
8
8
  @session = Memento::Session.create(:user => @user)
9
9
  end
10
-
10
+
11
11
  it "should belong to session" do
12
12
  Memento::State.new(:session => @session).session.should eql(@session)
13
13
  end
14
-
14
+
15
15
  it "should require session" do
16
16
  Memento::State.create.errors[:session].should eql(["can't be blank"])
17
17
  end
18
-
18
+
19
19
  it "should require action_type to be one of Memento::State::RECORD_CAUSES" do
20
20
  Memento::State.create.errors[:action_type].should eql(["can't be blank"])
21
21
  Memento::State.create(:action_type => "move").errors[:action_type].should eql(["is not included in the list"])
22
22
  end
23
-
23
+
24
24
  it "should belong to polymorphic record" do
25
25
  Memento::State.new(:record => @user).record.should eql(@user)
26
26
  Memento::State.new(:record => @session).record.should eql(@session)
27
27
  end
28
-
28
+
29
29
  it "should require record" do
30
30
  Memento::State.create.errors[:record].should eql(["can't be blank"])
31
31
  end
32
-
33
-
32
+
33
+
34
34
  describe "valid State" do
35
35
  before do
36
36
  @state = @session.states.create!(:action_type => "destroy", :record => @project = Project.create(:name => "A") )
37
37
  end
38
-
38
+
39
39
  it "should give back Memento::Result on undo" do
40
40
  result = @state.undo
41
41
  result.should be_a(Memento::Result)
42
42
  result.object.should be_a(Project)
43
43
  result.state.should eql(@state)
44
44
  end
45
-
45
+
46
46
  it "should give back old data on record_data" do
47
47
  @state.record_data.should == (@project.attributes_for_memento)
48
48
  end
49
49
  end
50
-
50
+
51
51
  after do
52
52
  shutdown_db
53
53
  end
54
-
54
+
55
55
  end
data/spec/memento_spec.rb CHANGED
@@ -5,165 +5,206 @@ describe Memento do
5
5
  setup_db
6
6
  setup_data
7
7
  end
8
-
8
+
9
9
  after do
10
10
  shutdown_db
11
11
  end
12
-
13
- it "should be a singleton" do
14
- Memento.instance.should be_kind_of(Singleton)
12
+
13
+ it "should be (like) a singleton" do
15
14
  Memento.instance.should eql(Memento.instance)
15
+ Memento.instance.should eql(Memento)
16
16
  lambda{ Memento.new }.should raise_error(NoMethodError)
17
17
  end
18
-
18
+
19
19
  it "should not be memento by default" do
20
- Memento.instance.should_not be_active
20
+ Memento.should_not be_active
21
21
  end
22
-
22
+
23
23
  describe "start" do
24
-
24
+
25
25
  before do
26
- Memento.instance.start(@user)
27
- @session = Memento.instance.instance_variable_get("@session")
26
+ Memento.start(@user)
27
+ @session = Memento.send(:session)
28
28
  end
29
-
29
+
30
30
  it "should require user or user_id on start" do
31
- lambda{ Memento.instance.start }.should raise_error(ArgumentError)
31
+ lambda{ Memento.start }.should raise_error(ArgumentError)
32
32
  end
33
-
33
+
34
34
  it "should set an unsaved memento_session when starting" do
35
35
  Memento::Session.count.should eql(0)
36
36
  @session.should be_kind_of(Memento::Session)
37
37
  @session.should be_new_record
38
38
  end
39
-
39
+
40
40
  it "should set user on session" do
41
41
  @session.user.should eql(User.first)
42
42
  end
43
-
43
+
44
44
  it "should set user when passing in id as integer" do
45
- Memento.instance.start(User.create(:name => "MyUser2").id)
46
- Memento.instance.instance_variable_get("@session").user.should eql(User.last)
45
+ Memento.start(User.create(:name => "MyUser2").id)
46
+ Memento.send(:session).user.should eql(User.last)
47
47
  end
48
-
48
+
49
49
  it "should not start memento when user does not exists/is invalid" do
50
- Memento.instance.stop
51
- Memento.instance.start(123333)
52
- Memento.instance.should_not be_active
53
- Memento.instance.start("123")
54
- Memento.instance.should_not be_active
50
+ Memento.stop
51
+ Memento.start(123333)
52
+ Memento.should_not be_active
53
+ Memento.start("123")
54
+ Memento.should_not be_active
55
55
  end
56
-
56
+
57
57
  it "should be memento" do
58
- Memento.instance.should be_active
58
+ Memento.should be_active
59
59
  end
60
-
60
+
61
61
  end
62
-
62
+
63
63
  describe "stop" do
64
64
  before do
65
- Memento.instance.start(@user)
66
- Memento.instance.stop
65
+ Memento.start(@user)
66
+ Memento.stop
67
67
  end
68
-
68
+
69
69
  it "should not be memento" do
70
- Memento.instance.should_not be_active
70
+ Memento.should_not be_active
71
71
  end
72
-
72
+
73
73
  it "should remove session if no states created" do
74
74
  Memento::Session.count.should eql(0)
75
75
  end
76
76
  end
77
-
77
+
78
78
  describe "memento block" do
79
-
79
+
80
80
  it "should record inside of block and stop after" do
81
- Memento.instance.should_not be_active
82
- Memento.instance.memento(@user) do
83
- Memento.instance.should be_active
81
+ Memento.should_not be_active
82
+ Memento(@user) do
83
+ Memento.should be_active
84
84
  end
85
- Memento.instance.should_not be_active
85
+ Memento.should_not be_active
86
86
  end
87
-
87
+
88
88
  it "should give back session when states created" do
89
- Memento.instance.memento(@user) do
89
+ Memento(@user) do
90
90
  Project.create!
91
91
  end.should be_a(Memento::Session)
92
92
  end
93
-
93
+
94
94
  it "should give back false when no states created" do
95
- Memento.instance.memento(@user) do
95
+ Memento(@user) do
96
96
  1 + 1
97
97
  end.should be_false
98
98
  end
99
-
99
+
100
100
  it "should raise error in block and stop session" do
101
101
  lambda {
102
- Memento.instance.memento(@user) do
102
+ Memento(@user) do
103
103
  raise StandardError
104
104
  end.should be_nil
105
105
  }.should raise_error(StandardError)
106
- Memento.instance.should_not be_active
106
+ Memento.should_not be_active
107
107
  end
108
-
108
+
109
109
  end
110
-
110
+
111
111
  describe "when active" do
112
112
  before do
113
113
  @project = Project.create(:name => "P1")
114
- Memento.instance.start(@user)
114
+ Memento.start(@user)
115
115
  end
116
-
116
+
117
117
  after do
118
- Memento.instance.stop
118
+ Memento.stop
119
119
  end
120
-
120
+
121
121
  it "should create memento_state for ar-object with action_type" do
122
122
  Memento::State.count.should eql(0)
123
- Memento.instance.add_state :destroy, @project
123
+ Memento.add_state :destroy, @project
124
124
  Memento::State.count.should eql(1)
125
125
  Memento::State.first.action_type.should eql("destroy")
126
126
  Memento::State.first.record.should eql(Project.last)
127
127
  end
128
-
128
+
129
129
  it "should save session on first added state" do
130
130
  Memento::Session.count.should eql(0)
131
- Memento.instance.add_state :destroy, @project
131
+ Memento.add_state :destroy, @project
132
132
  Memento::Session.count.should eql(1)
133
133
  end
134
-
134
+
135
135
  describe "when ignoring" do
136
136
  it "should NOT create memento_state for ar-object with action_type" do
137
- Memento.instance.ignore do
138
- Memento.instance.add_state :destroy, Project.create(:name => "P1")
137
+ Memento.ignore do
138
+ Memento.add_state :destroy, Project.create(:name => "P1")
139
139
  end
140
-
140
+
141
141
  Memento::State.count.should eql(0)
142
142
  end
143
143
  end
144
-
144
+
145
145
  end
146
-
146
+
147
147
  describe "when not active" do
148
-
148
+
149
149
  it "should NOT create memento_state for ar-object with action_type" do
150
- Memento.instance.add_state :destroy, Project.create(:name => "P1")
150
+ Memento.add_state :destroy, Project.create(:name => "P1")
151
151
  Memento::State.count.should eql(0)
152
152
  end
153
-
153
+
154
154
  end
155
-
155
+
156
156
  context "serializer" do
157
-
157
+
158
158
  it "should default to yaml" do
159
159
  Memento.serializer.should eql(YAML)
160
160
  end
161
-
161
+
162
162
  it "should be changeable" do
163
163
  Memento.serializer = Marshal
164
164
  Memento.serializer.should eql(Marshal)
165
165
  end
166
-
166
+
167
167
  end
168
-
168
+
169
+ describe "multiple threads" do
170
+ describe "start" do
171
+ before do
172
+ Thread.new do
173
+ Memento.start(@user)
174
+ end
175
+ end
176
+
177
+ it "should start Memento not in main thread" do
178
+ sleep(0.1)
179
+ Memento.should_not be_active
180
+ end
181
+
182
+ it "should start Memento not in separat thread" do
183
+ sleep(0.1)
184
+ t = Thread.new do
185
+ Memento.should_not be_active
186
+ end
187
+ t.join
188
+ end
189
+ end
190
+
191
+ describe "ignore" do
192
+ before do
193
+ @t = Thread.new do
194
+ Memento.ignore { Memento.should be_ignore;sleep(0.2) }
195
+ end
196
+ end
197
+
198
+ after do
199
+ @t.join
200
+ end
201
+
202
+ it "should set ignore status by thread" do
203
+ Memento.should_not be_ignore
204
+ sleep(0.1)
205
+ Memento.should_not be_ignore
206
+ end
207
+ end
208
+ end
209
+
169
210
  end
data/spec/spec_helper.rb CHANGED
@@ -26,27 +26,27 @@ def setup_db
26
26
  t.integer :ignore_this
27
27
  t.timestamps
28
28
  end
29
-
29
+
30
30
  create_table :users do |t|
31
31
  t.column :email, :string
32
32
  t.column :name, :string
33
33
  t.timestamps
34
34
  end
35
-
35
+
36
36
  create_table :customers do |t|
37
37
  t.column :name, :string
38
38
  t.timestamps
39
39
  end
40
-
40
+
41
41
  create_table :timestampless_objects do |t|
42
42
  t.column :name, :string
43
43
  end
44
-
44
+
45
45
  create_table :memento_sessions do |t|
46
46
  t.references :user
47
47
  t.timestamps
48
48
  end
49
-
49
+
50
50
  create_table :memento_states do |t|
51
51
  t.string :action_type
52
52
  t.binary :record_data, :limit => 16777215
@@ -54,20 +54,20 @@ def setup_db
54
54
  t.references :session
55
55
  t.timestamps
56
56
  end
57
-
57
+
58
58
  end
59
59
  end
60
60
 
61
61
  def setup_data
62
62
  @user = User.create(:name => "MyUser")
63
63
  end
64
-
64
+
65
65
  def shutdown_db
66
66
  ActiveRecord::Base.connection.tables.each do |table|
67
67
  ActiveRecord::Base.connection.drop_table(table)
68
68
  end
69
69
  end
70
-
70
+
71
71
  class User < ActiveRecord::Base
72
72
  end unless defined?(User)
73
73
 
@@ -77,7 +77,7 @@ end unless defined?(Customer)
77
77
 
78
78
  class Project < ActiveRecord::Base
79
79
  belongs_to :customer
80
-
80
+
81
81
  memento_changes :ignore => :ignore_this
82
82
  end unless defined?(Project)
83
83
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memento
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-13 00:00:00.000000000 Z
12
+ date: 2012-10-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord