db_to_file 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,68 @@
1
+ require_relative '../../../test_helper'
2
+
3
+ describe DbToFile::ValuesNormalizer::ObjectToHash do
4
+ describe 'normalizing' do
5
+ let(:normalizer) { DbToFile::ValuesNormalizer::ObjectToHash.new(@setting) }
6
+
7
+ before do
8
+ @setting = Setting.new(id: 1, value: ['blabla'])
9
+ end
10
+
11
+ it 'converts an object, to a hash with writeable values' do
12
+ normalizer.normalize.must_equal({'id'=>"1\n", 'key'=>"<NULL>\n", 'value'=>"---\n- blabla\n"})
13
+ end
14
+
15
+ it 'invokes all the functions for a field' do
16
+ normalizer.expects(:convert_yaml)
17
+ normalizer.expects(:convert_nil_value)
18
+ normalizer.expects(:convert_integer_to_string)
19
+ normalizer.expects(:add_trailing_newline)
20
+
21
+ normalizer.send(:normalize_field_value, 'bla')
22
+ end
23
+ end
24
+
25
+ describe 'convert model to hash-values' do
26
+ let(:normalizer) { DbToFile::ValuesNormalizer::ObjectToHash.new(@setting) }
27
+
28
+ before do
29
+ @setting = Setting.new(id: 1, value: ['blabla'])
30
+ end
31
+
32
+ it 'converts integer to a string' do
33
+ normalizer.send(:convert_integer_to_string, 1).must_equal('1')
34
+ end
35
+
36
+ describe 'nil conversion' do
37
+ it 'returns <NULL> for nil' do
38
+ normalizer.send(:convert_nil_value, nil).must_equal('<NULL>')
39
+ end
40
+
41
+ it 'returns original string, for all else' do
42
+ normalizer.send(:convert_nil_value, "Test\nString\n").must_equal("Test\nString\n")
43
+ end
44
+ end
45
+
46
+ describe 'convert yaml' do
47
+ it 'returns an array as yaml' do
48
+ normalizer.expects(:serialized_attribute?).returns(true)
49
+ normalizer.send(:convert_yaml, ["Test\nString"]).must_equal("---\n- |-\n Test\n String\n")
50
+ end
51
+
52
+ it 'returns the original string' do
53
+ normalizer.expects(:serialized_attribute?).returns(false)
54
+ normalizer.send(:convert_yaml, "Test\nString").must_equal("Test\nString")
55
+ end
56
+ end
57
+
58
+ describe 'add trailing newline' do
59
+ it 'adds a newline, when none is present' do
60
+ normalizer.send(:add_trailing_newline, 'Test String').must_equal("Test String\n")
61
+ end
62
+
63
+ it 'return original string' do
64
+ normalizer.send(:add_trailing_newline, "Test String\n").must_equal("Test String\n")
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,63 @@
1
+ require_relative '../../../test_helper'
2
+
3
+ describe DbToFile::ValuesNormalizer::ValueIntoObject do
4
+ describe 'normalizing' do
5
+ let(:normalizer) { DbToFile::ValuesNormalizer::ValueIntoObject.new(Setting) }
6
+
7
+ before do
8
+ @hash = {id: '1', key: '<NULL>', value: "---\n- blabla\n"}
9
+ end
10
+
11
+ it 'invokes all the functions for a field' do
12
+ normalizer.expects(:convert_yaml)
13
+ normalizer.expects(:strip_trailing_newline)
14
+ normalizer.expects(:convert_nil_value)
15
+
16
+ normalizer.send(:normalize_field_value, 'value', 'bla')
17
+ end
18
+ end
19
+
20
+ describe 'convert hash-values to object-values' do
21
+ let(:normalizer) { DbToFile::ValuesNormalizer::ValueIntoObject.new(Setting) }
22
+
23
+ describe 'nil conversion' do
24
+ it 'returns nil for <NULL>' do
25
+ normalizer.send(:convert_nil_value, '<NULL>').must_equal(nil)
26
+ end
27
+
28
+ it 'returns empty string for an empty string' do
29
+ normalizer.send(:convert_nil_value, '').must_equal('')
30
+ end
31
+
32
+ it 'returns original string, for all else' do
33
+ normalizer.send(:convert_nil_value, "Test\nString\n").must_equal("Test\nString\n")
34
+ end
35
+ end
36
+
37
+ describe 'trailing newline' do
38
+ it 'strips when present' do
39
+ normalizer.send(:strip_trailing_newline, "Test\nString\n").must_equal("Test\nString")
40
+ end
41
+
42
+ it 'return original string, when not present' do
43
+ normalizer.send(:strip_trailing_newline, "Test\nString").must_equal("Test\nString")
44
+ end
45
+
46
+ it 'return original string, when not present' do
47
+ normalizer.send(:strip_trailing_newline, "Test\nString").must_equal("Test\nString")
48
+ end
49
+ end
50
+
51
+ describe 'convert yaml' do
52
+ it 'returns an array as yaml' do
53
+ normalizer.expects(:serialized_attribute?).returns(true)
54
+ normalizer.send(:convert_yaml, "---\n- |-\n Test\n String\n").must_equal(["Test\nString"])
55
+ end
56
+
57
+ it 'returns the original string' do
58
+ normalizer.expects(:serialized_attribute?).returns(false)
59
+ normalizer.send(:convert_yaml, "Test\nString").must_equal("Test\nString")
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,132 @@
1
+ require_relative '../../test_helper'
2
+ require 'fileutils'
3
+
4
+ describe DbToFile::VersionController do
5
+ describe 'prepare code-versioning' do
6
+ it 'invokes the system-commander' do
7
+ executer = DbToFile::SystemExecuter.new('ls')
8
+ executer.expects(:execute).times(2)
9
+ DbToFile::SystemExecuter.expects(:new).with("git stash save 'db-to-file'").returns(executer)
10
+ DbToFile::SystemExecuter.expects(:new).with('git pull').returns(executer)
11
+ File::FileUtils.expects(:rm_rf)
12
+
13
+ unloader = instantiate_unloader
14
+ unloader.send(:prepare_code_version)
15
+ end
16
+ end
17
+
18
+ describe 'update_code_version' do
19
+ it 'invokes all the functions' do
20
+ controller = DbToFile::VersionController.new
21
+ controller.expects(:update_commit_stash)
22
+ controller.expects(:commitable_files_present?).returns(true)
23
+ controller.expects(:commit_changes)
24
+
25
+ controller.send(:update_code_version)
26
+ end
27
+
28
+ describe 'update commit_stash' do
29
+ before do
30
+ write_file('db/db_to_file/users/ewout-quax_1', 'id', '1')
31
+ write_file('db/db_to_file/users/ewout-quax_1', 'name', 'Ewout Quax')
32
+ write_file('db/db_to_file/users/test-example_2', 'id', '2')
33
+ write_file('db/db_to_file/users/test-example_2', 'name', 'Test Example')
34
+ end
35
+
36
+ after do
37
+ FileUtils.rm_rf('db/db_to_file')
38
+ end
39
+
40
+ it 'git adds new records' do
41
+ executer = DbToFile::SystemExecuter.new('')
42
+ executer.expects(:execute).times(5)
43
+ DbToFile::SystemExecuter.expects(:new).with('git status --porcelain').returns(executer)
44
+ DbToFile::SystemExecuter.expects(:new).with('git add db/db_to_file/users/ewout-quax_1/id').returns(executer)
45
+ DbToFile::SystemExecuter.expects(:new).with('git add db/db_to_file/users/ewout-quax_1/name').returns(executer)
46
+ DbToFile::SystemExecuter.expects(:new).with('git add db/db_to_file/users/test-example_2/id').returns(executer)
47
+ DbToFile::SystemExecuter.expects(:new).with('git add db/db_to_file/users/test-example_2/name').returns(executer)
48
+
49
+ DbToFile::VersionController.new.send(:update_commit_stash)
50
+ end
51
+ end
52
+
53
+ describe 'git commit changes' do
54
+ let(:git) { Minitest::Mock.new }
55
+ let(:controller) { DbToFile::VersionController.new }
56
+
57
+ it 'with custom commit message' do
58
+ controller.expects(:git).returns(git)
59
+
60
+ git.expect(:commit, nil, ['custom commit message'])
61
+ controller.send(:commit_changes, 'custom commit message')
62
+ git.verify
63
+ end
64
+
65
+ it 'with default commit message' do
66
+ controller.expects(:git).returns(git)
67
+
68
+ git.expect(:commit, nil, ['DbToFile: changes by customer'])
69
+ controller.send(:commit_changes, nil)
70
+ git.verify
71
+ end
72
+ end
73
+
74
+ it 'restores the stash' do
75
+ executer = DbToFile::SystemExecuter.new('')
76
+ executer.expects(:execute).times(1)
77
+ DbToFile::SystemExecuter.expects(:new).with('git stash pop').returns(executer)
78
+
79
+ DbToFile::VersionController.new.send(:restore_stash)
80
+ end
81
+ end
82
+
83
+ describe 'restore_local_stash' do
84
+ it 'invokes all the functions' do
85
+ controller = DbToFile::VersionController.new
86
+ controller.expects(:restore_stash)
87
+
88
+ controller.send(:restore_local_stash)
89
+ end
90
+
91
+ describe 'restore_stash' do
92
+ it 'invokes the correct command' do
93
+ executer = DbToFile::SystemExecuter.new('')
94
+ executer.expects(:execute).times(1)
95
+ DbToFile::SystemExecuter.expects(:new).with('git stash pop').returns(executer)
96
+
97
+ controller = DbToFile::VersionController.new
98
+ controller.send(:restore_stash)
99
+ end
100
+ end
101
+ end
102
+
103
+ describe 'merge_conflicts_present' do
104
+ let(:controller) { DbToFile::VersionController.new }
105
+ let(:executer) { Minitest::Mock.new }
106
+
107
+ before do
108
+ @output_without_merge_error = 'M test/lib/db_to_file/uploader_test.rb'
109
+ @output_with_merge_error = 'U test/lib/db_to_file/uploader_test.rb'
110
+ end
111
+
112
+ it 'return true, when merge-errors are found' do
113
+ DbToFile::SystemExecuter.expects(:new).with('git status --porcelain').returns(executer)
114
+
115
+ executer.expect(:execute, @output_with_merge_error)
116
+ controller.merge_conflicts_present?.must_equal(true)
117
+ end
118
+
119
+
120
+ it 'return false, when merge-errors are absent' do
121
+ DbToFile::SystemExecuter.expects(:new).with('git status --porcelain').returns(executer)
122
+
123
+ executer.expect(:execute, @output_without_merge_error)
124
+ controller.merge_conflicts_present?.must_equal(false)
125
+ end
126
+ end
127
+
128
+ def instantiate_unloader
129
+ DbToFile::Config.any_instance.stubs(:config_file).returns('test/fixtures/config.yml')
130
+ DbToFile::Unloader.new
131
+ end
132
+ end
@@ -0,0 +1,7 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe DbToFile do
4
+ it "must be defined" do
5
+ DbToFile::VERSION.wont_be_nil
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ class Setting < ActiveRecord::Base
2
+ serialize :value
3
+ end
@@ -0,0 +1,2 @@
1
+ class User < ActiveRecord::Base
2
+ end
@@ -0,0 +1,45 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'db_to_file'
5
+ require 'pry'
6
+ require 'minitest/unit'
7
+ require 'minitest/autorun'
8
+ require "mocha/setup"
9
+ # require 'turn'
10
+ require_relative 'lib/user'
11
+ require_relative 'lib/setting'
12
+ require File.expand_path('../../lib/db_to_file.rb', __FILE__)
13
+ # require 'minitest/reporters'
14
+ # MiniTest::Reporters.use!
15
+ # Turn.config do |c|
16
+ # c.format = :outline
17
+ # c.natural = true
18
+ # end
19
+
20
+ drop_users = 'drop table if exists `users`'
21
+ drop_settings = 'drop table if exists `settings`'
22
+ create_users = 'create table `users` (`id` integer primary key autoincrement, `name` varchar(255));'
23
+ create_settings = 'create table `settings` (`id` integer primary key autoincrement, `key` varchar(255), `value` varchar(255));'
24
+ ActiveRecord::Base.connection.execute(drop_users)
25
+ ActiveRecord::Base.connection.execute(drop_settings)
26
+ ActiveRecord::Base.connection.execute(create_users)
27
+ ActiveRecord::Base.connection.execute(create_settings)
28
+
29
+ User.delete_all
30
+ User.create(id: 1, name: 'Ewout Quax')
31
+ User.create(id: 2, name: 'Test Example')
32
+ User.create(id: 3, name: '')
33
+
34
+ Setting.delete_all
35
+ Setting.create(id: 1, key: 'key_1', value: ['Value_1'])
36
+ Setting.create(id: 2, key: 'key_2', value: nil)
37
+
38
+ def write_file(dir, file, value)
39
+ FileUtils.mkdir_p(dir)
40
+ full_file = File.join(dir, file)
41
+
42
+ handle = File.open(full_file, 'w')
43
+ handle.write(value)
44
+ handle.close
45
+ end
metadata ADDED
@@ -0,0 +1,252 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db_to_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.4
5
+ platform: ruby
6
+ authors:
7
+ - Ewout Quax
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: git
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.6
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 4.7.3
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 4.7.3
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest-reporters
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 0.5.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 0.5.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: mocha
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: sqlite3
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: turn
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: simplecov
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description: Unload and upload database-tables to a file-system
182
+ email:
183
+ - ewout.quax@quicknet.nl
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - Gemfile
190
+ - LICENSE.txt
191
+ - README.md
192
+ - Rakefile
193
+ - db/database.yml
194
+ - db/test.sqlite3
195
+ - db_to_file.gemspec
196
+ - lib/db_to_file.rb
197
+ - lib/db_to_file/config.rb
198
+ - lib/db_to_file/railtie.rb
199
+ - lib/db_to_file/system_executer.rb
200
+ - lib/db_to_file/unloader.rb
201
+ - lib/db_to_file/uploader.rb
202
+ - lib/db_to_file/values_normalizer/object_to_hash.rb
203
+ - lib/db_to_file/values_normalizer/value_into_object.rb
204
+ - lib/db_to_file/version.rb
205
+ - lib/db_to_file/version_controller.rb
206
+ - lib/tasks/unloader.rake
207
+ - lib/tasks/uploader.rake
208
+ - test/fixtures/config.yml
209
+ - test/lib/db_to_file/unloader_test.rb
210
+ - test/lib/db_to_file/uploader_test.rb
211
+ - test/lib/db_to_file/values_normalizer/object_to_hash_test.rb
212
+ - test/lib/db_to_file/values_normalizer/value_into_object_test.rb
213
+ - test/lib/db_to_file/version_controller_test.rb
214
+ - test/lib/db_to_file/version_test.rb
215
+ - test/lib/setting.rb
216
+ - test/lib/user.rb
217
+ - test/test_helper.rb
218
+ homepage: ''
219
+ licenses:
220
+ - MIT
221
+ metadata: {}
222
+ post_install_message:
223
+ rdoc_options: []
224
+ require_paths:
225
+ - lib
226
+ required_ruby_version: !ruby/object:Gem::Requirement
227
+ requirements:
228
+ - - ">="
229
+ - !ruby/object:Gem::Version
230
+ version: '0'
231
+ required_rubygems_version: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - ">="
234
+ - !ruby/object:Gem::Version
235
+ version: '0'
236
+ requirements: []
237
+ rubyforge_project:
238
+ rubygems_version: 2.4.5
239
+ signing_key:
240
+ specification_version: 4
241
+ summary: Unload and upload database-tables to a file-system
242
+ test_files:
243
+ - test/fixtures/config.yml
244
+ - test/lib/db_to_file/unloader_test.rb
245
+ - test/lib/db_to_file/uploader_test.rb
246
+ - test/lib/db_to_file/values_normalizer/object_to_hash_test.rb
247
+ - test/lib/db_to_file/values_normalizer/value_into_object_test.rb
248
+ - test/lib/db_to_file/version_controller_test.rb
249
+ - test/lib/db_to_file/version_test.rb
250
+ - test/lib/setting.rb
251
+ - test/lib/user.rb
252
+ - test/test_helper.rb