upgrow 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/test_unit/input/input_generator.rb +22 -0
  3. data/lib/generators/test_unit/input/templates/input_test.rb.tt +8 -0
  4. data/lib/generators/test_unit/install/install_generator.rb +48 -0
  5. data/lib/generators/test_unit/record/record_generator.rb +22 -0
  6. data/lib/generators/test_unit/record/templates/record_test.rb.tt +8 -0
  7. data/lib/generators/test_unit/repository/repository_generator.rb +23 -0
  8. data/lib/generators/test_unit/repository/templates/repository_test.rb.tt +8 -0
  9. data/lib/generators/upgrow.rb +54 -0
  10. data/lib/generators/upgrow/action/USAGE +13 -0
  11. data/lib/generators/upgrow/action/action_generator.rb +24 -0
  12. data/lib/generators/upgrow/action/templates/action.rb.tt +9 -0
  13. data/lib/generators/upgrow/input/USAGE +10 -0
  14. data/lib/generators/upgrow/input/input_generator.rb +26 -0
  15. data/lib/generators/upgrow/input/templates/input.rb.tt +6 -0
  16. data/lib/generators/upgrow/install/USAGE +25 -0
  17. data/lib/generators/upgrow/install/install_generator.rb +56 -0
  18. data/lib/generators/upgrow/install/templates/app/actions/application_action.rb.tt +4 -0
  19. data/lib/generators/upgrow/install/templates/app/inputs/application_input.rb.tt +4 -0
  20. data/lib/generators/upgrow/install/templates/app/models/application_model.rb.tt +4 -0
  21. data/lib/generators/upgrow/install/templates/app/repositories/application_repository.rb.tt +4 -0
  22. data/lib/generators/upgrow/model/USAGE +10 -0
  23. data/lib/generators/upgrow/model/model_generator.rb +24 -0
  24. data/lib/generators/upgrow/model/templates/model.rb.tt +6 -0
  25. data/lib/generators/upgrow/record/record_generator.rb +34 -0
  26. data/lib/generators/upgrow/record/templates/record.rb.tt +6 -0
  27. data/lib/generators/upgrow/repository/USAGE +10 -0
  28. data/lib/generators/upgrow/repository/repository_generator.rb +27 -0
  29. data/lib/generators/upgrow/repository/templates/repository.rb.tt +6 -0
  30. data/lib/upgrow.rb +3 -0
  31. data/lib/upgrow/active_record_conversion.rb +2 -3
  32. data/lib/upgrow/active_record_queries.rb +1 -2
  33. data/lib/upgrow/active_record_schema.rb +1 -3
  34. data/lib/upgrow/basic_model.rb +11 -0
  35. data/lib/upgrow/model.rb +2 -1
  36. data/lib/upgrow/naming.rb +40 -0
  37. data/lib/upgrow/railtie.rb +9 -0
  38. data/lib/upgrow/record.rb +121 -0
  39. data/test/dummy/app/records/application_record.rb +2 -0
  40. data/test/dummy/app/records/article_record.rb +2 -3
  41. data/test/dummy/app/records/comment_record.rb +2 -3
  42. data/test/dummy/app/records/user_record.rb +2 -4
  43. data/test/dummy/app/repositories/article_repository.rb +3 -3
  44. data/test/test_unit/generators/input_generator_test.rb +56 -0
  45. data/test/test_unit/generators/install_generator_test.rb +138 -0
  46. data/test/test_unit/generators/record_generator_test.rb +56 -0
  47. data/test/test_unit/generators/repository_generator_test.rb +57 -0
  48. data/test/upgrow/active_record_conversion_test.rb +2 -2
  49. data/test/upgrow/active_record_schema_test.rb +1 -1
  50. data/test/upgrow/basic_model_test.rb +85 -0
  51. data/test/upgrow/generators/action_generator_test.rb +59 -0
  52. data/test/upgrow/generators/helper_test.rb +86 -0
  53. data/test/upgrow/generators/input_generator_test.rb +65 -0
  54. data/test/upgrow/generators/install_generator_test.rb +158 -0
  55. data/test/upgrow/generators/model_generator_test.rb +39 -0
  56. data/test/upgrow/generators/record_generator_test.rb +65 -0
  57. data/test/upgrow/generators/repository_generator_test.rb +50 -0
  58. data/test/upgrow/naming_test.rb +22 -0
  59. data/test/upgrow/record_test.rb +176 -0
  60. metadata +60 -3
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'rails/generators/test_case'
5
+ require 'generators/test_unit/install/install_generator'
6
+
7
+ module TestUnit
8
+ module Generators
9
+ class InstallGeneratorTest < Rails::Generators::TestCase
10
+ tests InstallGenerator
11
+ destination File.expand_path('tmp')
12
+ setup :prepare_destination
13
+
14
+ test 'generate directories' do
15
+ run_generator
16
+
17
+ assert_file 'test/inputs/.keep'
18
+ assert_file 'test/repositories/.keep'
19
+ end
20
+
21
+ test 'transform existing Model tests into Record tests' do
22
+ create_file 'test/models/application_record_test.rb', <<~RUBY
23
+ # frozen_string_literal: true
24
+
25
+ require 'test_helper'
26
+
27
+ class ApplicationRecordTest < ActiveSupport::TestCase
28
+ end
29
+ RUBY
30
+
31
+ create_file 'test/models/article_test.rb', <<~RUBY
32
+ # frozen_string_literal: true
33
+
34
+ require 'test_helper'
35
+
36
+ class ArticleTest < ActiveSupport::TestCase
37
+ end
38
+ RUBY
39
+
40
+ create_file 'test/models/users/user_test.rb', <<~RUBY
41
+ # frozen_string_literal: true
42
+
43
+ require 'test_helper'
44
+
45
+ module Users
46
+ class UserTest < ActiveSupport::TestCase
47
+ end
48
+ end
49
+ RUBY
50
+
51
+ Thor::LineEditor.stub(:readline, 'yes') { run_generator }
52
+
53
+ assert_file 'test/records/application_record_test.rb', <<~RUBY
54
+ # frozen_string_literal: true
55
+
56
+ require 'test_helper'
57
+
58
+ class ApplicationRecordTest < ActiveSupport::TestCase
59
+ end
60
+ RUBY
61
+
62
+ assert_file 'test/records/article_record_test.rb', <<~RUBY
63
+ # frozen_string_literal: true
64
+
65
+ require 'test_helper'
66
+
67
+ class ArticleRecordTest < ActiveSupport::TestCase
68
+ end
69
+ RUBY
70
+
71
+ assert_file 'test/records/users/user_record_test.rb', <<~RUBY
72
+ # frozen_string_literal: true
73
+
74
+ require 'test_helper'
75
+
76
+ module Users
77
+ class UserRecordTest < ActiveSupport::TestCase
78
+ end
79
+ end
80
+ RUBY
81
+
82
+ assert_no_file 'test/models/application_record_test.rb'
83
+ assert_no_file 'test/models/article_test.rb'
84
+ assert_no_file 'test/models/users/user_test.rb'
85
+ end
86
+
87
+ test 'does not transform existing Model tests into Record tests when user answers no' do
88
+ create_file 'test/models/application_record_test.rb', <<~RUBY
89
+ # frozen_string_literal: true
90
+
91
+ require 'test_helper'
92
+
93
+ class ApplicationRecordTest < ActiveSupport::TestCase
94
+ end
95
+ RUBY
96
+
97
+ create_file 'test/models/article_test.rb', <<~RUBY
98
+ # frozen_string_literal: true
99
+
100
+ require 'test_helper'
101
+
102
+ class ArticleTest < ActiveSupport::TestCase
103
+ end
104
+ RUBY
105
+
106
+ Thor::LineEditor.stub(:readline, 'no') { run_generator }
107
+
108
+ assert_file 'test/models/application_record_test.rb', <<~RUBY
109
+ # frozen_string_literal: true
110
+
111
+ require 'test_helper'
112
+
113
+ class ApplicationRecordTest < ActiveSupport::TestCase
114
+ end
115
+ RUBY
116
+
117
+ assert_file 'test/models/article_test.rb', <<~RUBY
118
+ # frozen_string_literal: true
119
+
120
+ require 'test_helper'
121
+
122
+ class ArticleTest < ActiveSupport::TestCase
123
+ end
124
+ RUBY
125
+
126
+ assert_no_directory 'test/records'
127
+ end
128
+
129
+ private
130
+
131
+ def create_file(name, content)
132
+ path = File.expand_path(name, destination_root)
133
+ mkdir_p(File.dirname(path))
134
+ File.write(path, content)
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'rails/generators/test_case'
5
+ require 'generators/test_unit/record/record_generator'
6
+
7
+ module TestUnit
8
+ module Generators
9
+ class RecordGeneratorTest < Rails::Generators::TestCase
10
+ tests RecordGenerator
11
+ destination File.expand_path('tmp')
12
+ setup :prepare_destination
13
+
14
+ test 'generate Record test file' do
15
+ run_generator(['article'])
16
+
17
+ assert_file 'test/records/article_record_test.rb', <<~RUBY
18
+ # frozen_string_literal: true
19
+
20
+ require 'test_helper'
21
+
22
+ class ArticleRecordTest < ActiveSupport::TestCase
23
+ end
24
+ RUBY
25
+ end
26
+
27
+ test 'generate Record test file when suffix is specified' do
28
+ run_generator(['ArticleRecord'])
29
+
30
+ assert_file 'test/records/article_record_test.rb', <<~RUBY
31
+ # frozen_string_literal: true
32
+
33
+ require 'test_helper'
34
+
35
+ class ArticleRecordTest < ActiveSupport::TestCase
36
+ end
37
+ RUBY
38
+ end
39
+
40
+ test 'generate namespaced Record test file' do
41
+ run_generator(['articles/article'])
42
+
43
+ assert_file 'test/records/articles/article_record_test.rb', <<~RUBY
44
+ # frozen_string_literal: true
45
+
46
+ require 'test_helper'
47
+
48
+ module Articles
49
+ class ArticleRecordTest < ActiveSupport::TestCase
50
+ end
51
+ end
52
+ RUBY
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'rails/generators/test_case'
5
+ require 'generators/test_unit/repository/repository_generator'
6
+
7
+ module TestUnit
8
+ module Generators
9
+ class RepositoryGeneratorTest < Rails::Generators::TestCase
10
+ tests RepositoryGenerator
11
+ destination File.expand_path('tmp')
12
+ setup :prepare_destination
13
+
14
+ test 'generate Repository test file' do
15
+ run_generator(['article'])
16
+
17
+ assert_file 'test/repositories/article_repository_test.rb', <<~RUBY
18
+ # frozen_string_literal: true
19
+
20
+ require 'test_helper'
21
+
22
+ class ArticleRepositoryTest < ActiveSupport::TestCase
23
+ end
24
+ RUBY
25
+ end
26
+
27
+ test 'generate Repository test file when suffix is specified' do
28
+ run_generator(['ArticleRepository'])
29
+
30
+ assert_file 'test/repositories/article_repository_test.rb', <<~RUBY
31
+ # frozen_string_literal: true
32
+
33
+ require 'test_helper'
34
+
35
+ class ArticleRepositoryTest < ActiveSupport::TestCase
36
+ end
37
+ RUBY
38
+ end
39
+
40
+ test 'generate namespaced Repository test file' do
41
+ run_generator(['articles/article'])
42
+
43
+ assert_file 'test/repositories/articles/article_repository_test.rb',
44
+ <<~RUBY
45
+ # frozen_string_literal: true
46
+
47
+ require 'test_helper'
48
+
49
+ module Articles
50
+ class ArticleRepositoryTest < ActiveSupport::TestCase
51
+ end
52
+ end
53
+ RUBY
54
+ end
55
+ end
56
+ end
57
+ end
@@ -13,7 +13,7 @@ module Upgrow
13
13
  end
14
14
 
15
15
  class ArticleRecord < TestRecord
16
- belongs_to :user_record
16
+ belongs_to :user
17
17
  end
18
18
 
19
19
  class User < BasicModel
@@ -62,7 +62,7 @@ module Upgrow
62
62
  user_record = UserRecord.new(id: 666, name: 'JRR Tolkien')
63
63
 
64
64
  record = ArticleRecord.new(
65
- id: 1, title: 'The Hobbit', user_record: user_record
65
+ id: 1, title: 'The Hobbit', user: user_record
66
66
  )
67
67
 
68
68
  model = @repository.to_model(record)
@@ -11,7 +11,7 @@ module Upgrow
11
11
  end
12
12
 
13
13
  def reflections
14
- { 'article_record' => :reflection, 'tag_records' => :reflection }
14
+ { 'article' => :reflection, 'tags' => :reflection }
15
15
  end
16
16
  end
17
17
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'test_helper'
4
+ require 'upgrow/basic_model'
4
5
 
5
6
  module Upgrow
6
7
  class BasicModelTest < ActiveSupport::TestCase
@@ -15,6 +16,13 @@ module Upgrow
15
16
  has_many :tags
16
17
  end
17
18
 
19
+ class Tag < BasicModel
20
+ belongs_to :user
21
+ end
22
+
23
+ class User < BasicModel
24
+ end
25
+
18
26
  setup do
19
27
  @original_schema = BasicModel.schema.dup
20
28
  end
@@ -94,5 +102,82 @@ module Upgrow
94
102
 
95
103
  assert_equal 'volmer', model.title
96
104
  end
105
+
106
+ test '#to_h serializes model without associations' do
107
+ model = SampleModel.new(
108
+ title: 'volmer',
109
+ body: 'My long body',
110
+ id: 1
111
+ )
112
+ expected = {
113
+ title: 'volmer',
114
+ body: 'My long body',
115
+ id: 1,
116
+ }
117
+ assert_equal expected, model.to_h
118
+ end
119
+
120
+ test '#to_h serializes belongs_to association' do
121
+ model = SampleModel.new(
122
+ title: 'volmer',
123
+ body: 'My long body',
124
+ id: 1,
125
+ user: User.new(id: 1)
126
+ )
127
+ expected = {
128
+ title: 'volmer',
129
+ body: 'My long body',
130
+ id: 1,
131
+ user: { id: 1 },
132
+ }
133
+ assert_equal expected, model.to_h
134
+ end
135
+
136
+ test '#to_h serializes model with single level association' do
137
+ model = SubSampleModel.new(
138
+ title: 'volmer',
139
+ body: 'My long body',
140
+ id: 1,
141
+ tags: [
142
+ Tag.new(id: 1),
143
+ Tag.new(id: 2),
144
+ ]
145
+ )
146
+ expected = {
147
+ title: 'volmer',
148
+ body: 'My long body',
149
+ id: 1,
150
+ tags: [
151
+ { id: 1 },
152
+ { id: 2 },
153
+ ],
154
+ }
155
+ assert_equal expected, model.to_h
156
+ end
157
+
158
+ test '#to_h serializes model with two level associations' do
159
+ model = SubSampleModel.new(
160
+ title: 'volmer',
161
+ body: 'My long body',
162
+ id: 1,
163
+ tags: [
164
+ Tag.new(id: 1, user: User.new(id: 2)),
165
+ ]
166
+ )
167
+ expected = {
168
+ title: 'volmer',
169
+ body: 'My long body',
170
+ id: 1,
171
+ tags: [
172
+ {
173
+ id: 1,
174
+ user: {
175
+ id: 2,
176
+ },
177
+ },
178
+ ],
179
+ }
180
+ assert_equal expected, model.to_h
181
+ end
97
182
  end
98
183
  end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'rails/generators/test_case'
5
+ require 'generators/upgrow/action/action_generator'
6
+
7
+ module Upgrow
8
+ module Generators
9
+ class ActionGeneratorTest < Rails::Generators::TestCase
10
+ tests ActionGenerator
11
+ destination File.expand_path('tmp')
12
+ setup :prepare_destination
13
+
14
+ test 'generate Action file' do
15
+ run_generator(['index'])
16
+
17
+ assert_file 'app/actions/index_action.rb', <<~RUBY
18
+ # frozen_string_literal: true
19
+
20
+ class IndexAction < ApplicationAction
21
+ def perform(*args)
22
+ # Do something here.
23
+ end
24
+ end
25
+ RUBY
26
+ end
27
+
28
+ test 'generate Action when suffix is specified' do
29
+ run_generator(['IndexAction'])
30
+
31
+ assert_file 'app/actions/index_action.rb', <<~RUBY
32
+ # frozen_string_literal: true
33
+
34
+ class IndexAction < ApplicationAction
35
+ def perform(*args)
36
+ # Do something here.
37
+ end
38
+ end
39
+ RUBY
40
+ end
41
+
42
+ test 'generate namespaced Action file' do
43
+ run_generator(['articles/index'])
44
+
45
+ assert_file 'app/actions/articles/index_action.rb', <<~RUBY
46
+ # frozen_string_literal: true
47
+
48
+ module Articles
49
+ class IndexAction < ApplicationAction
50
+ def perform(*args)
51
+ # Do something here.
52
+ end
53
+ end
54
+ end
55
+ RUBY
56
+ end
57
+ end
58
+ end
59
+ end