sbf-dm-transactions 1.3.0.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,222 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe DataMapper::Resource, 'Transactions' do
4
+ before :all do
5
+ module ::Blog
6
+ class User
7
+ include DataMapper::Resource
8
+
9
+ property :name, String, :key => true
10
+ property :age, Integer
11
+ property :summary, Text
12
+ property :description, Text
13
+ property :admin, Boolean, :accessor => :private
14
+
15
+ belongs_to :parent, self, :required => false
16
+ has n, :children, self, :inverse => :parent
17
+
18
+ belongs_to :referrer, self, :required => false
19
+ has n, :comments
20
+
21
+ # FIXME: figure out a different approach than stubbing things out
22
+ def comment=(*)
23
+ # do nothing with comment
24
+ end
25
+ end
26
+
27
+ class Author < User; end
28
+
29
+ class Comment
30
+ include DataMapper::Resource
31
+
32
+ property :id, Serial
33
+ property :body, Text
34
+
35
+ belongs_to :user
36
+ end
37
+
38
+ class Article
39
+ include DataMapper::Resource
40
+
41
+ property :id, Serial
42
+ property :body, Text
43
+
44
+ has n, :paragraphs
45
+ end
46
+
47
+ class Paragraph
48
+ include DataMapper::Resource
49
+
50
+ property :id, Serial
51
+ property :text, String
52
+
53
+ belongs_to :article
54
+ end
55
+ end
56
+
57
+ class ::Default
58
+ include DataMapper::Resource
59
+
60
+ property :name, String, :key => true, :default => 'a default value'
61
+ end
62
+
63
+ @user_model = Blog::User
64
+ @author_model = Blog::Author
65
+ @comment_model = Blog::Comment
66
+ @article_model = Blog::Article
67
+ @paragraph_model = Blog::Paragraph
68
+ end
69
+
70
+ supported_by :postgres, :mysql, :sqlite, :oracle, :sqlserver do
71
+ before :all do
72
+ user = @user_model.create(:name => 'dbussink', :age => 25, :description => 'Test')
73
+
74
+ @user = @user_model.get(*user.key)
75
+ end
76
+
77
+ before do
78
+ # --- Temporary private api use to get around rspec limitations ---
79
+ @repository.scope do |repository|
80
+ transaction = DataMapper::Transaction.new(repository)
81
+ transaction.begin
82
+ repository.adapter.push_transaction(transaction)
83
+ end
84
+ end
85
+
86
+ after do
87
+ while @repository.adapter.current_transaction
88
+ @repository.adapter.pop_transaction.rollback
89
+ end
90
+ end
91
+
92
+ it_behaves_like 'A public Resource'
93
+ it_behaves_like 'A Resource supporting Strategic Eager Loading'
94
+ end
95
+
96
+ describe '#transaction' do
97
+ before :all do
98
+ class ::Author
99
+ include DataMapper::Resource
100
+
101
+ property :name, String, :key => true
102
+ end
103
+ end
104
+
105
+ supported_by :postgres, :mysql do
106
+ before do
107
+ @user_model.destroy!
108
+ end
109
+
110
+ it 'supports nested transactions' do
111
+ # can't assume dm-aggregates
112
+ begin
113
+ expect(@user_model.all.length).to eq 0
114
+
115
+ @user_model.transaction do # txn 1
116
+ @user_model.create(:name => 'jpr5')
117
+
118
+ expect(@user_model.all.length).to eq 1
119
+
120
+ begin
121
+ @user_model.transaction do # savepoint 1
122
+ @user_model.create(:name => 'dkubb')
123
+ expect(@user_model.all.length).to eq 2
124
+
125
+ raise
126
+ end
127
+ rescue => e
128
+ expect(@user_model.all.length).to eq 1
129
+ end
130
+
131
+ raise
132
+ end
133
+ rescue => e
134
+ expect(@user_model.all.length).to eq 0
135
+ end
136
+ end
137
+ end
138
+
139
+ supported_by :postgres, :mysql, :sqlite, :oracle, :sqlserver do
140
+ before do
141
+ @user_model.destroy!
142
+ end
143
+
144
+ it 'has access to resources presisted before the transaction' do
145
+ @user_model.create(:name => 'carllerche')
146
+ @user_model.transaction do
147
+ expect(@user_model.first.name).to eq 'carllerche'
148
+ end
149
+ end
150
+
151
+ it 'has access to resources persisted in the transaction' do
152
+ @user_model.transaction do
153
+ @user_model.create(:name => 'carllerche')
154
+ expect(@user_model.first.name).to eq 'carllerche'
155
+ end
156
+ end
157
+
158
+ it 'does not mark saved resources as new records' do
159
+ @user_model.transaction do
160
+ expect(@user_model.create(:name => 'carllerche')).not_to be_new
161
+ end
162
+ end
163
+
164
+ it 'rollback when an error is raised in a transaction' do
165
+ expect(@user_model.all.size).to eq 0
166
+ expect {
167
+ @user_model.transaction do
168
+ @user_model.create(:name => 'carllerche')
169
+ raise 'I love coffee'
170
+ end
171
+ }.to raise_error('I love coffee')
172
+ expect(@user_model.all.size).to eq 0
173
+ end
174
+
175
+ it 'closes the transaction if return is called within the closure' do
176
+ @txn = nil
177
+
178
+ def doit
179
+ @user_model.transaction do |transaction|
180
+ @txn = transaction
181
+ return
182
+ end
183
+ end
184
+ doit
185
+
186
+ expect(@txn.instance_variable_get(:@state)).to eq :commit
187
+ @txn = nil
188
+ end
189
+
190
+ it 'returns the last statement in the transaction block' do
191
+ expect(@user_model.transaction { 1 }).to eq 1
192
+ end
193
+
194
+ with_alternate_adapter do
195
+ before :all do
196
+ class ::Article
197
+ include DataMapper::Resource
198
+
199
+ def self.default_repository_name
200
+ :alternate
201
+ end
202
+
203
+ property :title, String, :key => true
204
+ end
205
+
206
+ DataMapper.auto_migrate!(:alternate)
207
+ end
208
+
209
+ it 'works with other repositories' do
210
+ expect {
211
+ DataMapper.repository.transaction.commit do
212
+ Author.create(:name => 'Dan Kubb')
213
+
214
+ # save a resource to another repository
215
+ Article.create(:title => 'DataMapper Rocks!')
216
+ end
217
+ }.not_to raise_error
218
+ end
219
+ end
220
+ end
221
+ end
222
+ end
data/spec/rcov.opts ADDED
@@ -0,0 +1,6 @@
1
+ --exclude "spec,^/"
2
+ --sort coverage
3
+ --callsites
4
+ --xrefs
5
+ --profile
6
+ --text-summary
@@ -0,0 +1,18 @@
1
+ require 'dm-transactions'
2
+
3
+ require 'dm-core/spec/setup'
4
+ require 'dm-core/spec/lib/spec_helper'
5
+ require 'dm-core/spec/lib/pending_helpers'
6
+ require 'dm-core/spec/lib/adapter_helpers'
7
+ require 'dm-core/spec/lib/counter_adapter'
8
+ require 'dm-core/spec/shared/resource_spec'
9
+ require 'dm-core/spec/shared/sel_spec'
10
+
11
+ RSpec.configure do |config|
12
+ config.extend(DataMapper::Spec::Adapters::Helpers)
13
+ config.include(DataMapper::Spec::PendingHelpers)
14
+
15
+ config.after :all do
16
+ DataMapper::Spec.cleanup_models
17
+ end
18
+ end
data/tasks/spec.rake ADDED
@@ -0,0 +1,21 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ begin
4
+ task(:default).clear
5
+ task(:spec).clear
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = 'spec/**/*_spec.rb'
9
+
10
+ require 'simplecov'
11
+ SimpleCov.start do
12
+ minimum_coverage 100
13
+ end
14
+ end
15
+ rescue LoadError
16
+ task :spec do
17
+ abort 'rspec is not available. In order to run spec, you must: gem install rspec'
18
+ end
19
+ end
20
+
21
+ task default: :spec
data/tasks/yard.rake ADDED
@@ -0,0 +1,9 @@
1
+ begin
2
+ require 'yard'
3
+
4
+ YARD::Rake::YardocTask.new
5
+ rescue LoadError
6
+ task :yard do
7
+ abort 'YARD is not available. In order to run yard, you must: gem install yard'
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'pathname'
3
+ require 'yardstick/rake/measurement'
4
+ require 'yardstick/rake/verify'
5
+
6
+ # yardstick_measure task
7
+ Yardstick::Rake::Measurement.new
8
+
9
+ # verify_measurements task
10
+ Yardstick::Rake::Verify.new do |verify|
11
+ verify.threshold = 100
12
+ end
13
+ rescue LoadError
14
+ %w[ yardstick_measure verify_measurements ].each do |name|
15
+ task name.to_s do
16
+ abort "Yardstick is not available. In order to run #{name}, you must: gem install yardstick"
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sbf-dm-transactions
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0.beta
5
+ platform: ruby
6
+ authors:
7
+ - Dirkjan Bussink (dbussink)
8
+ - Dan Kubb (dkubb)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2024-10-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sbf-dm-core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 1.3.0.beta
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 1.3.0.beta
28
+ description: Makes transaction support available for adapters that support them
29
+ email:
30
+ - gamsnjaga@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files:
34
+ - LICENSE
35
+ - README.rdoc
36
+ files:
37
+ - ".document"
38
+ - ".gitignore"
39
+ - ".rspec"
40
+ - ".rubocop.yml"
41
+ - Gemfile
42
+ - LICENSE
43
+ - README.rdoc
44
+ - Rakefile
45
+ - dm-transactions.gemspec
46
+ - lib/dm-transactions.rb
47
+ - lib/dm-transactions/adapters/dm-do-adapter.rb
48
+ - lib/dm-transactions/adapters/dm-mysql-adapter.rb
49
+ - lib/dm-transactions/adapters/dm-oracle-adapter.rb
50
+ - lib/dm-transactions/adapters/dm-postgres-adapter.rb
51
+ - lib/dm-transactions/adapters/dm-sqlite-adapter.rb
52
+ - lib/dm-transactions/adapters/dm-sqlserver-adapter.rb
53
+ - lib/dm-transactions/version.rb
54
+ - spec/isolated/require_after_setup_spec.rb
55
+ - spec/isolated/require_before_setup_spec.rb
56
+ - spec/isolated/require_spec.rb
57
+ - spec/public/dm-transactions_spec.rb
58
+ - spec/rcov.opts
59
+ - spec/spec_helper.rb
60
+ - tasks/spec.rake
61
+ - tasks/yard.rake
62
+ - tasks/yardstick.rake
63
+ homepage: https://datamapper.org
64
+ licenses:
65
+ - Nonstandard
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">"
79
+ - !ruby/object:Gem::Version
80
+ version: 1.3.1
81
+ requirements: []
82
+ rubygems_version: 3.4.10
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Adds transaction support to DataMapper
86
+ test_files: []