souls 0.17.9 → 0.20.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,63 @@
1
+ module Souls
2
+ module Generate
3
+ class << self
4
+ ## Generate Rspec Factory
5
+ def rspec_factory_head class_name: "souls"
6
+ file_path = "./spec/factories/#{class_name.pluralize}.rb"
7
+ File.open(file_path, "w") do |f|
8
+ f.write <<~EOS
9
+ FactoryBot.define do
10
+ factory :#{class_name} do
11
+ EOS
12
+ end
13
+ end
14
+
15
+ def rspec_factory_params class_name: "souls"
16
+ file_path = "./spec/factories/#{class_name.pluralize}.rb"
17
+ path = "./db/schema.rb"
18
+ @on = false
19
+ File.open(file_path, "a") do |new_line|
20
+ File.open(path, "r") do |f|
21
+ f.each_line.with_index do |line, i|
22
+ if @on
23
+ new_line.write "\n" && break if line.include?("end") || line.include?("t.index")
24
+ field = '["tag1", "tag2", "tag3"]' if line.include?("array: true")
25
+ type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
26
+ field ||= get_test_type type
27
+ if type == "bigint" && name.include?("_id")
28
+ id_name = name.gsub("_id", "")
29
+ new_line.write " association :#{id_name}, factory: :#{id_name}\n"
30
+ else
31
+ new_line.write " #{name} { #{field} }\n"
32
+ end
33
+ end
34
+ if table_check(line: line, class_name: class_name)
35
+ @on = true
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ def rspec_factory_end class_name: "souls"
43
+ file_path = "./spec/factories/#{class_name.pluralize}.rb"
44
+ File.open(file_path, "a") do |f|
45
+ f.write <<~EOS
46
+ end
47
+ end
48
+ EOS
49
+ end
50
+ file_path
51
+ end
52
+
53
+ def rspec_factory class_name: "souls"
54
+ file_path = "./spec/factories/#{class_name.pluralize}.rb"
55
+ return ["Factory aleady exist! #{file_path}"] if File.exist? file_path
56
+ singularized_class_name = class_name.singularize
57
+ rspec_factory_head class_name: singularized_class_name
58
+ rspec_factory_params class_name: singularized_class_name
59
+ rspec_factory_end class_name: singularized_class_name
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,23 @@
1
+ module Souls
2
+ module Generate
3
+ class << self
4
+ ## Generate Rspec Model
5
+ def rspec_model class_name: "souls"
6
+ file_path = "./spec/models/#{class_name}_spec.rb"
7
+ return ["Aleady Exist!"] if File.exist? file_path
8
+ File.open(file_path, "w") do |f|
9
+ f.write <<~EOS
10
+ RSpec.describe "#{class_name.camelize} Model テスト", type: :model do
11
+ describe "#{class_name.camelize} データを書き込む" do
12
+ it "valid #{class_name.camelize} Model" do
13
+ expect(FactoryBot.build(:#{class_name.singularize})).to be_valid
14
+ end
15
+ end
16
+ end
17
+ EOS
18
+ end
19
+ file_path
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,247 @@
1
+ module Souls
2
+ module Generate
3
+ class << self
4
+ ## Generate Rspec Mutation
5
+ def rspec_mutation_head class_name: "souls"
6
+ file_path = "./spec/mutations/#{class_name.singularize}_spec.rb"
7
+ File.open(file_path, "w") do |f|
8
+ f.write <<~EOS
9
+ RSpec.describe \"#{class_name.camelize} Mutation テスト\" do
10
+ describe "#{class_name.camelize} データを登録する" do
11
+ EOS
12
+ end
13
+ end
14
+
15
+ def rspec_mutation_after_head class_name: "souls"
16
+ file_path = "./spec/mutations/#{class_name.singularize}_spec.rb"
17
+ path = "./db/schema.rb"
18
+ @on = false
19
+ @user_exist = false
20
+ @relation_params = []
21
+ File.open(file_path, "a") do |new_line|
22
+ File.open(path, "r") do |f|
23
+ f.each_line.with_index do |line, i|
24
+ if @on
25
+ if line.include?("end") || line.include?("t.index")
26
+ if @relation_params.empty?
27
+ new_line.write <<-EOS
28
+ let(:#{class_name}) { FactoryBot.attributes_for(:#{class_name}) }
29
+
30
+ let(:mutation) do
31
+ %(mutation {
32
+ create#{class_name.camelize}(input: {
33
+ EOS
34
+ else
35
+ new_line.write <<-EOS
36
+ let(:#{class_name}) { FactoryBot.attributes_for(:#{class_name}, #{@relation_params.join(", ")}) }
37
+
38
+ let(:mutation) do
39
+ %(mutation {
40
+ create#{class_name.camelize}(input: {
41
+ EOS
42
+ end
43
+ break
44
+ end
45
+ _, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
46
+ case name
47
+ when "user_id"
48
+ relation_col = name.gsub("_id", "")
49
+ new_line.write " let(:#{relation_col}) { FactoryBot.create(:#{relation_col}) }\n"
50
+ when /$*_id\z/
51
+ relation_col = name.gsub("_id", "")
52
+ @relation_params << "#{name}: get_global_key(\"#{name.singularize.camelize.gsub("Id", "")}\", #{relation_col}.id)"
53
+ new_line.write " let(:#{relation_col}) { FactoryBot.create(:#{relation_col}) }\n"
54
+ end
55
+ end
56
+ if table_check(line: line, class_name: class_name)
57
+ @on = true
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def rspec_mutation_params class_name: "souls"
65
+ file_path = "./spec/mutations/#{class_name.singularize}_spec.rb"
66
+ path = "./db/schema.rb"
67
+ @on = false
68
+ @user_exist = false
69
+ File.open(file_path, "a") do |new_line|
70
+ File.open(path, "r") do |f|
71
+ f.each_line.with_index do |line, i|
72
+ if @on
73
+ if line.include?("end") || line.include?("t.index")
74
+ new_line.write " }) {\n #{class_name.singularize.camelize(:lower)}Edge {\n node {\n"
75
+ new_line.write " id\n"
76
+ break
77
+ end
78
+ type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
79
+ array_true = line.include?("array: true")
80
+ case name
81
+ when "created_at", "updated_at"
82
+ next
83
+ when "user_id"
84
+ @user_exist = true
85
+ when /$*_id\z/
86
+ new_line.write " #{name.singularize.camelize(:lower)}: \"\#{#{class_name.singularize}[:#{name.singularize.underscore}]}\"\n"
87
+ else
88
+ case type
89
+ when "string", "text", "date", "datetime"
90
+ if array_true
91
+ new_line.write " #{name.pluralize.camelize(:lower)}: \#{#{class_name.singularize}[:#{name.pluralize.underscore}]}\n"
92
+ else
93
+ new_line.write " #{name.singularize.camelize(:lower)}: \"\#{#{class_name.singularize}[:#{name.singularize.underscore}]}\"\n"
94
+ end
95
+ when "bigint", "integer", "float", "boolean"
96
+ new_line.write " #{name.singularize.camelize(:lower)}: \#{#{class_name.singularize}[:#{name.singularize.underscore}]}\n"
97
+ end
98
+ end
99
+ end
100
+ if table_check(line: line, class_name: class_name)
101
+ @on = true
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ def rspec_mutation_params_response class_name: "souls"
109
+ file_path = "./spec/mutations/#{class_name.singularize}_spec.rb"
110
+ path = "./db/schema.rb"
111
+ @on = false
112
+ File.open(file_path, "a") do |new_line|
113
+ File.open(path, "r") do |f|
114
+ f.each_line.with_index do |line, i|
115
+ if @on
116
+ if line.include?("end") || line.include?("t.index")
117
+ if @user_exist
118
+ new_line.write <<-EOS
119
+ }
120
+ }
121
+ }
122
+ }
123
+ )
124
+ end
125
+
126
+ subject(:result) do
127
+ context = {
128
+ user: user
129
+ }
130
+ SoulsApiSchema.execute(mutation, context: context).as_json
131
+ end
132
+
133
+ it "return #{class_name.camelize} Data" do
134
+ begin
135
+ a1 = result.dig("data", "create#{class_name.singularize.camelize}", "#{class_name.singularize.camelize(:lower)}Edge", "node")
136
+ raise unless a1.present?
137
+ rescue
138
+ raise StandardError, result
139
+ end
140
+ expect(a1).to include(
141
+ "id" => be_a(String),
142
+ EOS
143
+ else
144
+ new_line.write <<-EOS
145
+ }
146
+ }
147
+ }
148
+ }
149
+ )
150
+ end
151
+
152
+ subject(:result) do
153
+ SoulsApiSchema.execute(mutation).as_json
154
+ end
155
+
156
+ it "return #{class_name.camelize} Data" do
157
+ begin
158
+ a1 = result.dig("data", "create#{class_name.singularize.camelize}", "#{class_name.singularize.camelize(:lower)}Edge", "node")
159
+ raise unless a1.present?
160
+ rescue
161
+ raise StandardError, result
162
+ end
163
+ expect(a1).to include(
164
+ "id" => be_a(String),
165
+ EOS
166
+ end
167
+ break
168
+ end
169
+ _, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
170
+ array_true = line.include?("array: true")
171
+ case name
172
+ when "user_id", "created_at", "updated_at", /$*_id\z/
173
+ next
174
+ else
175
+ if array_true
176
+ new_line.write " #{name.pluralize.camelize(:lower)}\n"
177
+ else
178
+ new_line.write " #{name.singularize.camelize(:lower)}\n"
179
+ end
180
+ end
181
+ end
182
+ if table_check(line: line, class_name: class_name)
183
+ @on = true
184
+ end
185
+ end
186
+ end
187
+ end
188
+ end
189
+
190
+ def rspec_mutation_end class_name: "souls"
191
+ file_path = "./spec/mutations/#{class_name.singularize}_spec.rb"
192
+ path = "./db/schema.rb"
193
+ @on = false
194
+ File.open(file_path, "a") do |new_line|
195
+ File.open(path, "r") do |f|
196
+ f.each_line.with_index do |line, i|
197
+ if @on
198
+ if line.include?("end") || line.include?("t.index")
199
+ new_line.write <<~EOS
200
+ )
201
+ end
202
+ end
203
+ end
204
+ EOS
205
+ break
206
+ end
207
+ type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
208
+ field ||= type_check type
209
+ array_true = line.include?("array: true")
210
+ case name
211
+ when "user_id", "created_at", "updated_at", /$*_id\z/
212
+ next
213
+ else
214
+ case type
215
+ when "text", "date", "datetime"
216
+ if array_true
217
+ new_line.write " \"#{name.pluralize.camelize(:lower)}\" => be_all(String),\n"
218
+ else
219
+ new_line.write " \"#{name.singularize.camelize(:lower)}\" => be_a(String),\n"
220
+ end
221
+ when "boolean"
222
+ new_line.write " \"#{name.singularize.camelize(:lower)}\" => be_in([true, false]),\n"
223
+ when "string", "bigint", "integer", "float"
224
+ new_line.write " \"#{name.singularize.camelize(:lower)}\" => be_a(#{field}),\n"
225
+ end
226
+ end
227
+ end
228
+ if table_check(line: line, class_name: class_name)
229
+ @on = true
230
+ end
231
+ end
232
+ end
233
+ end
234
+ file_path
235
+ end
236
+
237
+ def rspec_mutation class_name: "souls"
238
+ singularized_class_name = class_name.singularize
239
+ rspec_mutation_head class_name: singularized_class_name
240
+ rspec_mutation_after_head class_name: singularized_class_name
241
+ rspec_mutation_params class_name: singularized_class_name
242
+ rspec_mutation_params_response class_name: singularized_class_name
243
+ rspec_mutation_end class_name: singularized_class_name
244
+ end
245
+ end
246
+ end
247
+ end
@@ -0,0 +1,50 @@
1
+ module Souls
2
+ module Generate
3
+ class << self
4
+ ## Generate Rspec Policy
5
+ def rspec_policy class_name: "souls"
6
+ dir_name = "./spec/policies"
7
+ FileUtils.mkdir_p dir_name unless Dir.exist? dir_name
8
+ file_path = "./spec/policies/#{class_name}_policy_spec.rb"
9
+ File.open(file_path, "w") do |new_line|
10
+ new_line.write <<~EOS
11
+ describe #{class_name.camelize}Policy do
12
+ subject { described_class.new(user, #{class_name.underscore}) }
13
+
14
+ let(:#{class_name.underscore}) { FactoryBot.create(:#{class_name.underscore}) }
15
+
16
+ context "being a visitor" do
17
+ let(:user) { FactoryBot.create(:user) }
18
+
19
+ it { is_expected.to permit_action(:index) }
20
+ it { is_expected.to permit_action(:show) }
21
+ it { is_expected.to forbid_actions([:create, :update, :delete]) }
22
+ end
23
+
24
+ context "being a retailer" do
25
+ let(:user) { FactoryBot.create(:user, user_role: 1) }
26
+
27
+ it { is_expected.to permit_action(:index) }
28
+ it { is_expected.to permit_action(:show) }
29
+ it { is_expected.to forbid_actions([:create, :update, :delete]) }
30
+ end
31
+
32
+ context "being a staff" do
33
+ let(:user) { FactoryBot.create(:user, user_role: 3) }
34
+
35
+ it { is_expected.to permit_actions([:create, :update, :delete]) }
36
+ end
37
+
38
+ context "being an administrator" do
39
+ let(:user) { FactoryBot.create(:user, user_role: 4) }
40
+
41
+ it { is_expected.to permit_actions([:create, :update, :delete]) }
42
+ end
43
+ end
44
+ EOS
45
+ end
46
+ file_path
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,169 @@
1
+ module Souls
2
+ module Generate
3
+ class << self
4
+ ## Generate Rspec Query
5
+ def rspec_query_head class_name: "souls"
6
+ file_path = "./spec/queries/#{class_name.singularize}_spec.rb"
7
+ File.open(file_path, "w") do |f|
8
+ f.write <<~EOS
9
+ RSpec.describe \"#{class_name.camelize} Query テスト\" do
10
+ describe "#{class_name.camelize} データを取得する" do
11
+ EOS
12
+ end
13
+ end
14
+
15
+ def rspec_query_after_head class_name: "souls"
16
+ file_path = "./spec/queries/#{class_name.singularize}_spec.rb"
17
+ path = "./db/schema.rb"
18
+ @on = false
19
+ @user_exist = false
20
+ @relation_params = []
21
+ File.open(file_path, "a") do |new_line|
22
+ File.open(path, "r") do |f|
23
+ f.each_line.with_index do |line, i|
24
+ if @on
25
+ if line.include?("end") || line.include?("t.index")
26
+ if @relation_params.empty?
27
+ new_line.write <<-EOS
28
+ let!(:#{class_name}) { FactoryBot.create(:#{class_name}) }
29
+
30
+ let(:query) do
31
+ data_id = Base64.encode64("#{class_name.camelize}:\#{#{class_name.singularize.underscore}.id}")
32
+ %(query {
33
+ #{class_name.singularize.camelize(:lower)}(id: \\"\#{data_id}\\") {
34
+ id
35
+ EOS
36
+ break
37
+ else
38
+ new_line.write <<-EOS
39
+ let(:#{class_name}) { FactoryBot.create(:#{class_name}, #{@relation_params.join(", ")}) }
40
+
41
+ let(:query) do
42
+ data_id = Base64.encode64("#{class_name.camelize}:\#{#{class_name.singularize.underscore}.id}")
43
+ %(query {
44
+ #{class_name.singularize.camelize(:lower)}(id: \\"\#{data_id}\\") {
45
+ id
46
+ EOS
47
+ break
48
+ end
49
+ end
50
+ _, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
51
+ case name
52
+ when /$*_id\z/
53
+ relation_col = name.gsub("_id", "")
54
+ @relation_params << "#{name}: #{relation_col}.id"
55
+ new_line.write " let(:#{relation_col}) { FactoryBot.create(:#{relation_col}) }\n"
56
+ end
57
+ end
58
+ if table_check(line: line, class_name: class_name)
59
+ @on = true
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ def rspec_query_params class_name: "souls"
67
+ file_path = "./spec/queries/#{class_name.singularize}_spec.rb"
68
+ path = "./db/schema.rb"
69
+ @on = false
70
+ File.open(file_path, "a") do |new_line|
71
+ File.open(path, "r") do |f|
72
+ f.each_line.with_index do |line, i|
73
+ if @on
74
+ if line.include?("end") || line.include?("t.index")
75
+ new_line.write <<-EOS
76
+ }
77
+ }
78
+ )
79
+ end
80
+
81
+ subject(:result) do
82
+ SoulsApiSchema.execute(query).as_json
83
+ end
84
+
85
+ it "return #{class_name.camelize} Data" do
86
+ begin
87
+ a1 = result.dig("data", "#{class_name.singularize.camelize(:lower)}")
88
+ raise unless a1.present?
89
+ rescue
90
+ raise StandardError, result
91
+ end
92
+ expect(a1).to include(
93
+ "id" => be_a(String),
94
+ EOS
95
+ break
96
+ end
97
+ _, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
98
+ case name
99
+ when "user_id", "created_at", "updated_at", /$*_id\z/
100
+ next
101
+ else
102
+ new_line.write " #{name.camelize(:lower)}\n"
103
+ end
104
+ end
105
+ if table_check(line: line, class_name: class_name)
106
+ @on = true
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ def rspec_query_end class_name: "souls"
114
+ file_path = "./spec/queries/#{class_name.singularize}_spec.rb"
115
+ path = "./db/schema.rb"
116
+ @on = false
117
+ File.open(file_path, "a") do |new_line|
118
+ File.open(path, "r") do |f|
119
+ f.each_line.with_index do |line, i|
120
+ if @on
121
+ if line.include?("end") || line.include?("t.index")
122
+ new_line.write <<~EOS
123
+ )
124
+ end
125
+ end
126
+ end
127
+ EOS
128
+ break
129
+ end
130
+ type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
131
+ field ||= type_check type
132
+ array_true = line.include?("array: true")
133
+ case name
134
+ when "user_id", "created_at", "updated_at", /$*_id\z/
135
+ next
136
+ else
137
+ case type
138
+ when "text", "date", "datetime"
139
+ if array_true
140
+ new_line.write " \"#{name.camelize(:lower)}\" => be_all(String),\n"
141
+ else
142
+ new_line.write " \"#{name.camelize(:lower)}\" => be_a(String),\n"
143
+ end
144
+ when "boolean"
145
+ new_line.write " \"#{name.singularize.camelize(:lower)}\" => be_in([true, false]),\n"
146
+ when "string", "bigint", "integer", "float"
147
+ new_line.write " \"#{name.singularize.camelize(:lower)}\" => be_a(#{field}),\n"
148
+ end
149
+ end
150
+ end
151
+ if table_check(line: line, class_name: class_name)
152
+ @on = true
153
+ end
154
+ end
155
+ end
156
+ end
157
+ file_path
158
+ end
159
+
160
+ def rspec_query class_name: "souls"
161
+ singularized_class_name = class_name.singularize
162
+ rspec_query_head class_name: singularized_class_name
163
+ rspec_query_after_head class_name: singularized_class_name
164
+ rspec_query_params class_name: singularized_class_name
165
+ rspec_query_end class_name: singularized_class_name
166
+ end
167
+ end
168
+ end
169
+ end