-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathtest_runner_flow_test.rb
More file actions
372 lines (316 loc) · 14.2 KB
/
test_runner_flow_test.rb
File metadata and controls
372 lines (316 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
require 'test_helper'
class TestRunnerFlowTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
test "runs the tests for a basic submission" do
solution = create(:concept_solution)
submission = Submission::Create.(solution, files, :cli)
perform_enqueued_jobs
assert_equal 'queued', submission.tests_status
assert_equal :not_queued, solution.reload.latest_iteration_head_tests_status
assert_equal :not_queued, solution.reload.published_iteration_head_tests_status
##
## Simulate the job coming back and being processed
##
perform_enqueued_jobs do
job = create_test_run_job(submission)
Submission::TestRun::Process.(job)
end
assert_equal 'passed', submission.reload.tests_status
assert_equal :not_queued, solution.reload.latest_iteration_head_tests_status
assert_equal :not_queued, solution.reload.published_iteration_head_tests_status
end
test "runs the tests for an iteration submission" do
# Stub things we don't care about here
Iteration::GenerateSnippet.any_instance.stubs(:call)
Solution::UpdateNumLoc.stubs(:defer)
Solution::UpdateTags.stubs(:defer)
Solution::UpdatePublishedExerciseRepresentation.stubs(:defer)
Solution::UpdateSnippet.stubs(:defer)
Iteration::CountLinesOfCode.any_instance.stubs(:call)
solution = create(:concept_solution)
user_track = create :user_track, user: solution.user, track: solution.track
submission = Submission::Create.(solution, files, :cli)
Iteration::Create.(solution, submission)
perform_enqueued_jobs
solution.reload
submission.reload
assert_equal 'queued', submission.tests_status
assert_equal :queued, solution.latest_iteration_head_tests_status
assert_equal :not_queued, solution.published_iteration_head_tests_status
##
## Simulate the job coming back and being processed
##
perform_enqueued_jobs do
job = create_test_run_job(submission)
Submission::TestRun::Process.(job)
end
solution.reload
submission.reload
assert_equal 'passed', submission.tests_status
assert_equal :passed, solution.latest_iteration_head_tests_status
assert_equal :not_queued, solution.published_iteration_head_tests_status
##
## Now publish the solution
##
perform_enqueued_jobs do
Solution::Publish.(solution, user_track, nil)
end
solution.reload
submission.reload
assert_equal 'passed', submission.tests_status
assert_equal :passed, solution.latest_iteration_head_tests_status
assert_equal :passed, solution.published_iteration_head_tests_status
end
test "handles a new git sha being pushed: failing" do
# Stub things we don't care about here
Iteration::GenerateSnippet.any_instance.stubs(:call)
Solution::UpdateNumLoc.stubs(:defer)
Solution::UpdateTags.stubs(:defer)
Solution::UpdatePublishedExerciseRepresentation.stubs(:defer)
Solution::UpdateSnippet.stubs(:defer)
Iteration::CountLinesOfCode.any_instance.stubs(:call)
exercise = create :practice_exercise, git_sha: '0b04b8976650d993ecf4603cf7413f3c6b898eff'
solution = create(:practice_solution, exercise:)
user_track = create :user_track, user: solution.user, track: solution.track
# Let's get to a starting state
submission = Submission::Create.(solution, files, :cli)
Iteration::Create.(solution, submission)
job = create_test_run_job(submission)
Submission::TestRun::Process.(job)
Solution::Publish.(solution, user_track, nil)
perform_enqueued_jobs
submission.reload
solution.reload
# Stub a representation so we don't get jobs for it later.
create(:submission_representation, submission:)
# Store real values
git_sha = exercise.git_sha
git_important_files_hash = exercise.git_important_files_hash
old_git_sha = "foobar"
old_git_important_files_hash = "barfoo"
# These need to be updated in this reverse order for things to work :)
submission.test_run.update_columns(git_sha: old_git_sha, git_important_files_hash: old_git_important_files_hash)
submission.update_columns(git_sha: old_git_sha, git_important_files_hash: old_git_important_files_hash)
solution.update_columns(git_sha: old_git_sha, git_important_files_hash: old_git_important_files_hash)
exercise.update_columns(git_sha: old_git_sha, git_important_files_hash: old_git_important_files_hash)
# And perform some sanity checks
assert_equal 'passed', submission.tests_status
assert_equal :passed, solution.latest_iteration_head_tests_status
assert_equal :passed, solution.published_iteration_head_tests_status
##
## Simulate exercise update
##
Exercise::MarkSolutionsAsOutOfDateInIndex.expects(:call)
ToolingJob::Create.expects(:call).with(
submission,
:test_runner,
git_sha:,
run_in_background: true
)
perform_enqueued_jobs do
exercise.update(git_sha:, git_important_files_hash:)
end
solution.reload
submission.reload
# These shouldn't be changed
assert_equal old_git_sha, submission.git_sha
assert_equal old_git_important_files_hash, submission.git_important_files_hash
assert_equal 'passed', submission.tests_status
assert_equal :passed, solution.latest_iteration_head_tests_status
assert_equal :passed, solution.published_iteration_head_tests_status
##
## Simulate the job coming back and being processed
##
perform_enqueued_jobs do
job = create_test_run_job(submission, status: :fail, git_sha:)
Submission::TestRun::Process.(job)
end
solution.reload
submission.reload
# The original solution is still passing as it's test run is still
# the original one but the iteration is now failing latest head status
assert_equal old_git_sha, solution.git_sha
assert_equal old_git_important_files_hash, solution.git_important_files_hash
assert_equal old_git_sha, submission.git_sha
assert_equal old_git_important_files_hash, submission.git_important_files_hash
assert_equal 'passed', submission.tests_status
assert_equal :failed, solution.latest_iteration_head_tests_status
assert_equal :failed, solution.published_iteration_head_tests_status
##
## Update the solution to the latest version
##
Solution::UpdateToLatestExerciseVersion.(solution)
perform_enqueued_jobs
solution.reload
submission.reload
# The status should sync up properly.
assert_equal git_sha, solution.git_sha
assert_equal git_important_files_hash, solution.git_important_files_hash
assert_equal git_sha, submission.git_sha
assert_equal git_important_files_hash, submission.git_important_files_hash
assert_equal 'failed', submission.tests_status
assert_equal :failed, solution.latest_iteration_head_tests_status
assert_equal :failed, solution.published_iteration_head_tests_status
end
test "handles a new git sha being pushed: passing auto updates" do
# Stub things we don't care about here
Iteration::GenerateSnippet.any_instance.stubs(:call)
Solution::UpdateNumLoc.stubs(:defer)
Solution::UpdateTags.stubs(:defer)
Solution::UpdatePublishedExerciseRepresentation.stubs(:defer)
Solution::UpdateSnippet.stubs(:defer)
Iteration::CountLinesOfCode.any_instance.stubs(:call)
exercise = create :practice_exercise, git_sha: '0b04b8976650d993ecf4603cf7413f3c6b898eff'
solution = create(:practice_solution, exercise:)
user_track = create :user_track, user: solution.user, track: solution.track
# Let's get to a starting state
submission = Submission::Create.(solution, files, :cli)
Iteration::Create.(solution, submission)
job = create_test_run_job(submission)
Submission::TestRun::Process.(job)
Solution::Publish.(solution, user_track, nil)
perform_enqueued_jobs
submission.reload
solution.reload
# Stub a representation so we don't get jobs for it later.
create(:submission_representation, submission:)
# Store real values
git_sha = exercise.git_sha
git_important_files_hash = exercise.git_important_files_hash
old_git_sha = "foobar"
old_git_important_files_hash = "barfoo"
# These need to be updated in this reverse order for things to work :)
submission.test_run.update_columns(git_sha: old_git_sha, git_important_files_hash: old_git_important_files_hash)
submission.update_columns(git_sha: old_git_sha, git_important_files_hash: old_git_important_files_hash)
solution.update_columns(git_sha: old_git_sha, git_important_files_hash: old_git_important_files_hash)
exercise.update_columns(git_sha: old_git_sha, git_important_files_hash: old_git_important_files_hash)
# And perform some sanity checks
assert_equal 'passed', submission.tests_status
assert_equal :passed, solution.latest_iteration_head_tests_status
assert_equal :passed, solution.published_iteration_head_tests_status
##
## Simulate exercise update
##
Exercise::MarkSolutionsAsOutOfDateInIndex.expects(:call)
ToolingJob::Create.expects(:call).with(
submission,
:test_runner,
git_sha:,
run_in_background: true
)
# ToolingJob::Create.expects(:call).with(
# submission,
# :representer,
# git_sha:,
# run_in_background: true,
# context: {}
# )
perform_enqueued_jobs do
exercise.update(git_sha:, git_important_files_hash:)
end
submission.reload
solution.reload
# These shouldn't be changed
assert_equal old_git_sha, submission.git_sha
assert_equal old_git_important_files_hash, submission.git_important_files_hash
assert_equal 'passed', submission.tests_status
assert_equal :passed, solution.latest_iteration_head_tests_status
assert_equal :passed, solution.published_iteration_head_tests_status
##
## Simulate the job coming back and being processed
##
perform_enqueued_jobs do
job = create_test_run_job(submission, status: :pass, git_sha:)
Submission::TestRun::Process.(job)
end
solution.reload
submission.reload
# The status should sync up properly.
assert_equal git_sha, solution.git_sha
assert_equal git_important_files_hash, solution.git_important_files_hash
assert_equal git_sha, submission.git_sha
assert_equal git_important_files_hash, submission.git_important_files_hash
assert_equal 'passed', submission.tests_status
assert_equal :passed, solution.latest_iteration_head_tests_status
assert_equal :passed, solution.published_iteration_head_tests_status
end
test "honours [no important files changed] and auto-updates" do
# Stub things we don't care about here
Iteration::GenerateSnippet.any_instance.stubs(:call)
Solution::UpdateNumLoc.stubs(:defer)
Solution::UpdateTags.stubs(:defer)
Solution::UpdatePublishedExerciseRepresentation.stubs(:defer)
Solution::UpdateSnippet.stubs(:defer)
Iteration::CountLinesOfCode.any_instance.stubs(:call)
# This exercise contains the right set of things to
# go with this commit for this test.
exercise_slug = 'satellite'
# Store real values
old_git_sha = "0b04b8976650d993ecf4603cf7413f3c6b898eff"
old_git_important_files_hash = "6a0ca80a08e8451991e09a2483ddceb0f698b21a"
git_sha = "cfd8cf31bb9c90fd9160c82db69556a47f7c2a54"
git_important_files_hash = "cb8f62b17f71f9c95d1753ec542c186a3cc6a08e"
exercise = create :practice_exercise, slug: exercise_slug, git_sha: old_git_sha,
git_important_files_hash: old_git_important_files_hash
solution = create(:practice_solution, exercise:)
user_track = create :user_track, user: solution.user, track: solution.track
# Let's get to a starting state
submission = Submission::Create.(solution, files, :cli)
Iteration::Create.(solution, submission)
job = create_test_run_job(submission)
Submission::TestRun::Process.(job)
Solution::Publish.(solution, user_track, nil)
perform_enqueued_jobs
exercise.reload
submission.reload
solution.reload
# Perform some sanity checks
assert_equal old_git_sha, exercise.git_sha
assert_equal old_git_sha, solution.git_sha
assert_equal old_git_sha, submission.git_sha
assert_equal old_git_sha, submission.test_run.git_sha
assert_equal old_git_important_files_hash, exercise.git_important_files_hash
assert_equal old_git_important_files_hash, solution.git_important_files_hash
assert_equal old_git_important_files_hash, submission.git_important_files_hash
assert_equal old_git_important_files_hash, submission.test_run.git_important_files_hash
assert_equal 'passed', submission.tests_status
assert_equal 'passed', submission.tests_status
assert_equal :passed, solution.latest_iteration_head_tests_status
assert_equal :passed, solution.published_iteration_head_tests_status
##
## Simulate exercise update with the flag used
##
# Both of these should explicitly not get called.
Exercise::MarkSolutionsAsOutOfDateInIndex.expects(:call).never
ToolingJob::Create.expects(:call).never
exercise.update!(git_sha:, git_important_files_hash:)
perform_enqueued_jobs
solution.reload
submission.reload
# Everything should be updated
assert_equal git_sha, exercise.git_sha
assert_equal git_sha, solution.git_sha
assert_equal git_sha, submission.git_sha
assert_equal git_sha, submission.test_run.git_sha
assert_equal git_important_files_hash, exercise.git_important_files_hash
assert_equal git_important_files_hash, solution.git_important_files_hash
assert_equal git_important_files_hash, submission.git_important_files_hash
assert_equal git_important_files_hash, submission.test_run.git_important_files_hash
# The tests should be as they were without a test run being created
assert_equal 'passed', submission.tests_status
assert_equal :passed, solution.latest_iteration_head_tests_status
assert_equal :passed, solution.published_iteration_head_tests_status
end
private
def files
[{ filename: "subdir/foobar.rb", content: "'I think' = 'I am'" }]
end
def create_test_run_job(submission, status: :pass, git_sha: nil)
ops_status = 200
message = "some barfoo message"
version = 5
tests = [{ 'foo' => 'bar' }]
results = { 'version' => version, 'status' => status, 'message' => message, 'tests' => tests }
create_test_runner_job!(submission, execution_status: ops_status, results:, git_sha:)
end
end