active_snapshot 0.2.4 → 0.3.0

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.
@@ -57,7 +57,7 @@ class SnapshotItemTest < ActiveSupport::TestCase
57
57
 
58
58
  @snapshot_item = @snapshot.snapshot_items.first
59
59
 
60
- assert @snapshot_item.object.is_a?(HashWithIndifferentAccess)
60
+ assert @snapshot_item.object.is_a?(Hash)
61
61
 
62
62
  @snapshot_item.object = {foo: :bar}
63
63
 
@@ -45,7 +45,7 @@ class SnapshotTest < ActiveSupport::TestCase
45
45
 
46
46
  instance.valid?
47
47
 
48
- [:item_id, :item_type, :identifier].each do |attr|
48
+ [:item_id, :item_type].each do |attr|
49
49
  assert instance.errors[attr].present? ### presence error
50
50
  end
51
51
 
@@ -63,11 +63,11 @@ class SnapshotTest < ActiveSupport::TestCase
63
63
  def test_metadata
64
64
  @snapshot = @snapshot_klass.first
65
65
 
66
- assert @snapshot.metadata.is_a?(HashWithIndifferentAccess)
66
+ assert @snapshot.metadata.is_a?(Hash)
67
67
 
68
68
  @snapshot.metadata = {foo: :bar}
69
69
 
70
- assert_equal :bar, @snapshot.metadata['foo']
70
+ assert_equal "bar", @snapshot.metadata['foo']
71
71
  end
72
72
 
73
73
  def test_build_snapshot_item
@@ -101,7 +101,7 @@ class SnapshotTest < ActiveSupport::TestCase
101
101
 
102
102
  children_hash = reified_items.last
103
103
 
104
- assert children_hash.is_a?(HashWithIndifferentAccess)
104
+ assert children_hash.is_a?(Hash)
105
105
 
106
106
  assert children_hash.all?{|k,v| v.all?{|x| x.readonly?} }
107
107
  end
@@ -110,7 +110,7 @@ class SnapshotTest < ActiveSupport::TestCase
110
110
  post = SubPost.create!(a: 1, b: 2)
111
111
  comment_content = 'Example comment'
112
112
  post.comments.create!(content: comment_content)
113
- post.create_snapshot!('v1')
113
+ post.create_snapshot!(identifier: 'v1')
114
114
  snapshot = post.snapshots.first
115
115
  reified_items = snapshot.fetch_reified_items
116
116
 
@@ -122,9 +122,9 @@ class SnapshotTest < ActiveSupport::TestCase
122
122
  def test_single_model_snapshots_without_children
123
123
  instance = ParentWithoutChildren.create!({a: 1, b: 2})
124
124
 
125
- previous_attributes = instance.attributes
125
+ prev_attrs = instance.attributes
126
126
 
127
- instance.create_snapshot!('v1')
127
+ instance.create_snapshot!(identifier: 'v1')
128
128
 
129
129
  instance.update!(a: 9, b: 9)
130
130
 
@@ -134,7 +134,15 @@ class SnapshotTest < ActiveSupport::TestCase
134
134
 
135
135
  assert_equal [instance, {}], reified_items
136
136
 
137
- assert_equal previous_attributes, reified_items.first.attributes
137
+ new_attrs = reified_items.first.attributes
138
+
139
+ prev_time_attrs = prev_attrs.extract!("created_at","updated_at")
140
+ new_time_attrs = new_attrs.extract!("created_at","updated_at")
141
+
142
+ assert_equal new_time_attrs.values.map{|x| x.round(3)}, new_time_attrs.values
143
+
144
+ ### rounding to 3 sometimes fails due to millisecond precision so we just test for 2 decimal places here
145
+ assert_equal prev_time_attrs.values.map{|x| x.round(2)}, new_time_attrs.values.map{|x| x.round(2)}
138
146
  end
139
147
 
140
148
  end
@@ -35,17 +35,17 @@ class SnapshotsConcernTest < ActiveSupport::TestCase
35
35
 
36
36
  #@user = User.first
37
37
 
38
- snapshot = @post.create_snapshot!("foobar 1", user: @user, metadata: {foo: :bar})
38
+ snapshot = @post.create_snapshot!(identifier: "foobar 1", user: @user, metadata: {foo: :bar})
39
39
  assert_not snapshot.new_record?
40
40
 
41
- snapshot = @post.create_snapshot!("foobar 2", user: @user)
41
+ snapshot = @post.create_snapshot!(identifier: "foobar 2", user: @user)
42
42
  assert_not snapshot.new_record?
43
43
 
44
- snapshot = @post.create_snapshot!("foobar 3")
44
+ snapshot = @post.create_snapshot!(identifier: "foobar 3")
45
45
  assert_not snapshot.new_record?
46
46
 
47
47
  assert_raise do
48
- @post.create_snapshot!("foobar 3")
48
+ @post.create_snapshot!(identifier: "foobar 3")
49
49
  end
50
50
  end
51
51
 
@@ -115,4 +115,29 @@ class SnapshotsConcernTest < ActiveSupport::TestCase
115
115
  assert klass.new.children_to_snapshot.count == 2
116
116
  end
117
117
 
118
+ def test_legacy_positional_identifier_argument
119
+ call_count = 0
120
+
121
+ allow_any_instance_of(ActiveSupport::Deprecation).to receive(:warn).and_wrap_original do |m, *args|
122
+ if args.first == ActiveSnapshot::SnapshotsConcern::LEGACY_POSITIONAL_ARGUMENT_WARNING
123
+ call_count += 1
124
+ end
125
+ end
126
+
127
+ assert_difference ->{ ActiveSnapshot::Snapshot.count }, 1 do
128
+ @snapshot = Post.first.create_snapshot!("snapshot-1")
129
+ end
130
+
131
+ assert_equal call_count, 1
132
+ end
133
+
134
+ def test_optional_identifier
135
+ post = Post.first
136
+
137
+ assert_difference ->{ ActiveSnapshot::Snapshot.count }, 2 do
138
+ post.create_snapshot!
139
+ post.create_snapshot!
140
+ end
141
+ end
142
+
118
143
  end
data/test/test_helper.rb CHANGED
@@ -3,6 +3,10 @@ ENV["RAILS_ENV"] = "test"
3
3
 
4
4
  require "active_snapshot"
5
5
 
6
+ if ENV["ACTIVE_SNAPSHOT_STORAGE_METHOD"].present?
7
+ ActiveSnapshot.config.storage_method = ENV["ACTIVE_SNAPSHOT_STORAGE_METHOD"]
8
+ end
9
+
6
10
  begin
7
11
  require 'warning'
8
12
 
@@ -25,6 +29,8 @@ end
25
29
 
26
30
  Rails.backtrace_cleaner.remove_silencers!
27
31
 
32
+ require 'minitest-spec-rails' ### for describe blocks
33
+
28
34
  require 'minitest/reporters'
29
35
  Minitest::Reporters.use!(
30
36
  Minitest::Reporters::DefaultReporter.new,
@@ -43,6 +49,24 @@ else
43
49
  ActiveRecord::Migrator.migrate File.expand_path("dummy_app/db/migrate/", __dir__)
44
50
  end
45
51
 
52
+ require 'rspec/mocks'
53
+ module MinitestRSpecMocksIntegration
54
+ include RSpec::Mocks::ExampleMethods
55
+
56
+ def before_setup
57
+ RSpec::Mocks.setup
58
+ super
59
+ end
60
+
61
+ def after_teardown
62
+ super
63
+ RSpec::Mocks.verify
64
+ ensure
65
+ RSpec::Mocks.teardown
66
+ end
67
+ end
68
+ Minitest::Test.send(:include, MinitestRSpecMocksIntegration)
69
+
46
70
  klasses = [
47
71
  Post,
48
72
  ActiveSnapshot::Snapshot,
@@ -61,9 +85,9 @@ end
61
85
  DATA = {}.with_indifferent_access
62
86
 
63
87
  DATA[:shared_post] = Post.find_or_create_by!(a: 1, b: 3)
64
- DATA[:shared_post].create_snapshot!('v1')
88
+ DATA[:shared_post].create_snapshot!(identifier: 'v1')
65
89
  DATA[:shared_post].update_columns(a: 2, b: 4)
66
- DATA[:shared_post].create_snapshot!('v2')
90
+ DATA[:shared_post].create_snapshot!(identifier: 'v2')
67
91
 
68
92
  def assert_time_match(a, b)
69
93
  format = "%d-%m-%Y %h:%M:%S.%L" ### MUST LIMIT THE MILLISECONDS TO 3 decimal places of accuracy, the rest are dropped
@@ -24,7 +24,7 @@ class ActiveSnapshotTest < ActiveSupport::TestCase
24
24
 
25
25
  assert_difference ->{ ActiveSnapshot::Snapshot.count }, 1 do
26
26
  assert_difference ->{ ActiveSnapshot::SnapshotItem.count }, 2 do
27
- @snapshot = parent.create_snapshot!(identifier)
27
+ @snapshot = parent.create_snapshot!(identifier: identifier)
28
28
  end
29
29
  end
30
30
 
@@ -53,4 +53,5 @@ class ActiveSnapshotTest < ActiveSupport::TestCase
53
53
  assert_time_match original_parent_updated_at, parent.updated_at
54
54
  assert_time_match original_child_updated_at, parent.children_to_snapshot[:comments][:records].first.updated_at
55
55
  end
56
+
56
57
  end
@@ -0,0 +1,66 @@
1
+ require "test_helper"
2
+
3
+ class ActiveSnapshot::ConfigTest < ActiveSupport::TestCase
4
+
5
+ describe "storage_method" do
6
+ def setup
7
+ @orig_storage_method = ActiveSnapshot.config.storage_method
8
+ end
9
+
10
+ def teardown
11
+ ActiveSnapshot.config.storage_method = @orig_storage_method
12
+ end
13
+
14
+ def test_defaults_to_serialized_json
15
+ assert_equal 'serialized_json', ActiveSnapshot.config.storage_method
16
+
17
+ assert_equal false, ActiveSnapshot.config.storage_method_yaml?
18
+ assert_equal true, ActiveSnapshot.config.storage_method_json?
19
+ assert_equal false, ActiveSnapshot.config.storage_method_native_json?
20
+ end
21
+
22
+ def test_accepts_to_serialized_json
23
+ ActiveSnapshot.config.storage_method = 'serialized_json'
24
+
25
+ assert_equal 'serialized_json', ActiveSnapshot.config.storage_method
26
+
27
+ assert_equal false, ActiveSnapshot.config.storage_method_yaml?
28
+ assert_equal true, ActiveSnapshot.config.storage_method_json?
29
+ assert_equal false, ActiveSnapshot.config.storage_method_native_json?
30
+ end
31
+
32
+
33
+ def test_accepts_serialized_yaml
34
+ ActiveSnapshot.config.storage_method = 'serialized_yaml'
35
+
36
+ assert_equal 'serialized_yaml', ActiveSnapshot.config.storage_method
37
+
38
+ assert_equal true, ActiveSnapshot.config.storage_method_yaml?
39
+ assert_equal false, ActiveSnapshot.config.storage_method_json?
40
+ assert_equal false, ActiveSnapshot.config.storage_method_native_json?
41
+ end
42
+
43
+ def test_accepts_native_json
44
+ ActiveSnapshot.config.storage_method = "native_json"
45
+
46
+ assert_equal "native_json", ActiveSnapshot.config.storage_method, "native_json"
47
+
48
+ assert_equal false, ActiveSnapshot.config.storage_method_yaml?
49
+ assert_equal false, ActiveSnapshot.config.storage_method_json?
50
+ assert_equal true, ActiveSnapshot.config.storage_method_native_json?
51
+ end
52
+
53
+ def test_config_doesnt_accept_not_specified_storage_methods
54
+ assert_raise do
55
+ ActiveSnapshot.config.storage_method = 'foobar'
56
+ end
57
+ assert_equal "serialized_json", ActiveSnapshot.config.storage_method
58
+ end
59
+
60
+ def test_converts_symbol_to_string
61
+ ActiveSnapshot.config.storage_method = "serialized_yaml"
62
+ assert_equal "serialized_yaml", ActiveSnapshot.config.storage_method
63
+ end
64
+ end
65
+
66
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_snapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weston Ganger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-25 00:00:00.000000000 Z
11
+ date: 2022-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -95,7 +95,21 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: appraisal
98
+ name: minitest-spec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-mocks
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
115
  - - ">="
@@ -134,6 +148,7 @@ files:
134
148
  - README.md
135
149
  - Rakefile
136
150
  - lib/active_snapshot.rb
151
+ - lib/active_snapshot/config.rb
137
152
  - lib/active_snapshot/models/concerns/snapshots_concern.rb
138
153
  - lib/active_snapshot/models/snapshot.rb
139
154
  - lib/active_snapshot/models/snapshot_item.rb
@@ -179,6 +194,7 @@ files:
179
194
  - test/models/snapshots_concern_test.rb
180
195
  - test/test_helper.rb
181
196
  - test/unit/active_snapshot_test.rb
197
+ - test/unit/config_test.rb
182
198
  - test/unit/errors_test.rb
183
199
  homepage: https://github.com/westonganger/active_snapshot
184
200
  licenses:
@@ -201,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
217
  - !ruby/object:Gem::Version
202
218
  version: '0'
203
219
  requirements: []
204
- rubygems_version: 3.2.32
220
+ rubygems_version: 3.3.7
205
221
  signing_key:
206
222
  specification_version: 4
207
223
  summary: Dead simple snapshot versioning for ActiveRecord models and associations.
@@ -244,4 +260,5 @@ test_files:
244
260
  - test/models/snapshots_concern_test.rb
245
261
  - test/test_helper.rb
246
262
  - test/unit/active_snapshot_test.rb
263
+ - test/unit/config_test.rb
247
264
  - test/unit/errors_test.rb