|
1306 | 1306 | end |
1307 | 1307 | end |
1308 | 1308 | end |
| 1309 | + |
| 1310 | + context 'GET test_results' do |
| 1311 | + let(:grouping) { create(:grouping_with_inviter, assignment: assignment) } |
| 1312 | + let(:test_group) { create(:test_group, assignment: assignment) } |
| 1313 | + let(:submission) { create(:version_used_submission, grouping: grouping) } |
| 1314 | + |
| 1315 | + context 'when the group has test results' do |
| 1316 | + let!(:test_run) do |
| 1317 | + create(:test_run, grouping: grouping, role: instructor, status: :complete, submission: submission) |
| 1318 | + end |
| 1319 | + let!(:test_group_result) do |
| 1320 | + create(:test_group_result, test_run: test_run, test_group: test_group, |
| 1321 | + marks_earned: 5.0, marks_total: 10.0, time: 1000) |
| 1322 | + end |
| 1323 | + |
| 1324 | + before do |
| 1325 | + create(:test_result, test_group_result: test_group_result, name: 'Test 1', |
| 1326 | + status: 'pass', marks_earned: 3.0, marks_total: 5.0, position: 1) |
| 1327 | + end |
| 1328 | + |
| 1329 | + context 'expecting json response' do |
| 1330 | + before do |
| 1331 | + request.env['HTTP_ACCEPT'] = 'application/json' |
| 1332 | + get :test_results, params: { id: grouping.group.id, assignment_id: assignment.id, course_id: course.id } |
| 1333 | + end |
| 1334 | + |
| 1335 | + it 'should be successful' do |
| 1336 | + expect(response).to have_http_status(:ok) |
| 1337 | + end |
| 1338 | + |
| 1339 | + it 'should return data grouped by test group name' do |
| 1340 | + expect(response.parsed_body).to have_key(test_group.name) |
| 1341 | + end |
| 1342 | + |
| 1343 | + it 'should return test results for the group' do |
| 1344 | + test_results = response.parsed_body[test_group.name] |
| 1345 | + expect(test_results).to be_an(Array) |
| 1346 | + expect(test_results.first).to include( |
| 1347 | + 'test_result_name' => 'Test 1', |
| 1348 | + 'status' => 'pass', |
| 1349 | + 'marks_earned' => 3.0, |
| 1350 | + 'marks_total' => 5.0 |
| 1351 | + ) |
| 1352 | + end |
| 1353 | + end |
| 1354 | + |
| 1355 | + context 'expecting xml response' do |
| 1356 | + before do |
| 1357 | + request.env['HTTP_ACCEPT'] = 'application/xml' |
| 1358 | + get :test_results, params: { id: grouping.group.id, assignment_id: assignment.id, course_id: course.id } |
| 1359 | + end |
| 1360 | + |
| 1361 | + it 'should be successful' do |
| 1362 | + expect(response).to have_http_status(:ok) |
| 1363 | + end |
| 1364 | + |
| 1365 | + it 'should return xml content' do |
| 1366 | + xml_data = Hash.from_xml(response.body) |
| 1367 | + expect(xml_data).to have_key('test_results') |
| 1368 | + end |
| 1369 | + end |
| 1370 | + |
| 1371 | + context 'with multiple test groups' do |
| 1372 | + let(:test_group_two) { create(:test_group, assignment: assignment, name: 'Group B') } |
| 1373 | + let!(:test_group_result_two) do |
| 1374 | + create(:test_group_result, test_run: test_run, test_group: test_group_two) |
| 1375 | + end |
| 1376 | + |
| 1377 | + before do |
| 1378 | + create(:test_result, test_group_result: test_group_result_two, name: 'Test B1', |
| 1379 | + status: 'pass', marks_earned: 2.0, marks_total: 5.0, position: 1) |
| 1380 | + request.env['HTTP_ACCEPT'] = 'application/json' |
| 1381 | + get :test_results, params: { id: grouping.group.id, assignment_id: assignment.id, course_id: course.id } |
| 1382 | + end |
| 1383 | + |
| 1384 | + it 'should be successful' do |
| 1385 | + expect(response).to have_http_status(:ok) |
| 1386 | + end |
| 1387 | + |
| 1388 | + it 'should return results keyed by each test group name' do |
| 1389 | + expect(response.parsed_body.keys).to contain_exactly(test_group.name, test_group_two.name) |
| 1390 | + end |
| 1391 | + |
| 1392 | + it 'should return correct test results for each group' do |
| 1393 | + expect(response.parsed_body[test_group.name].first['test_result_name']).to eq('Test 1') |
| 1394 | + expect(response.parsed_body[test_group_two.name].first['test_result_name']).to eq('Test B1') |
| 1395 | + end |
| 1396 | + end |
| 1397 | + end |
| 1398 | + |
| 1399 | + context 'authorization check' do |
| 1400 | + it_behaves_like 'for a different course' do |
| 1401 | + before do |
| 1402 | + request.env['HTTP_ACCEPT'] = 'application/json' |
| 1403 | + get :test_results, params: { id: grouping.group.id, assignment_id: assignment.id, course_id: course.id } |
| 1404 | + end |
| 1405 | + end |
| 1406 | + end |
| 1407 | + |
| 1408 | + context 'when the group has no test results' do |
| 1409 | + before do |
| 1410 | + request.env['HTTP_ACCEPT'] = 'application/json' |
| 1411 | + get :test_results, params: { id: grouping.group.id, assignment_id: assignment.id, course_id: course.id } |
| 1412 | + end |
| 1413 | + |
| 1414 | + it 'should return 404 status' do |
| 1415 | + expect(response).to have_http_status(:not_found) |
| 1416 | + end |
| 1417 | + end |
| 1418 | + |
| 1419 | + context 'when the group does not exist' do |
| 1420 | + before do |
| 1421 | + request.env['HTTP_ACCEPT'] = 'application/json' |
| 1422 | + get :test_results, params: { id: 999_999, assignment_id: assignment.id, course_id: course.id } |
| 1423 | + end |
| 1424 | + |
| 1425 | + it 'should return 404 status' do |
| 1426 | + expect(response).to have_http_status(:not_found) |
| 1427 | + end |
| 1428 | + end |
| 1429 | + |
| 1430 | + context 'when multiple test runs exist' do |
| 1431 | + let!(:older_test_run) do |
| 1432 | + create(:test_run, grouping: grouping, role: instructor, created_at: 2.days.ago, status: :complete, |
| 1433 | + submission: submission) |
| 1434 | + end |
| 1435 | + let!(:newer_test_run) do |
| 1436 | + create(:test_run, grouping: grouping, role: instructor, created_at: 1.hour.ago, status: :complete, |
| 1437 | + submission: submission) |
| 1438 | + end |
| 1439 | + let!(:older_test_group_result) do |
| 1440 | + create(:test_group_result, test_run: older_test_run, test_group: test_group) |
| 1441 | + end |
| 1442 | + let!(:newer_test_group_result) do |
| 1443 | + create(:test_group_result, test_run: newer_test_run, test_group: test_group) |
| 1444 | + end |
| 1445 | + |
| 1446 | + before do |
| 1447 | + create(:test_result, test_group_result: older_test_group_result, name: 'Old Test', |
| 1448 | + marks_earned: 1.0, marks_total: 5.0, status: 'pass', position: 1) |
| 1449 | + create(:test_result, test_group_result: newer_test_group_result, name: 'New Test', |
| 1450 | + marks_earned: 4.0, marks_total: 5.0, status: 'pass', position: 1) |
| 1451 | + request.env['HTTP_ACCEPT'] = 'application/json' |
| 1452 | + get :test_results, params: { id: grouping.group.id, assignment_id: assignment.id, course_id: course.id } |
| 1453 | + end |
| 1454 | + |
| 1455 | + it 'should return only the latest test run results' do |
| 1456 | + test_results = response.parsed_body[test_group.name] |
| 1457 | + expect(test_results.length).to eq(1) |
| 1458 | + expect(test_results.first['test_result_name']).to eq('New Test') |
| 1459 | + expect(test_results.first['marks_earned']).to eq(4.0) |
| 1460 | + end |
| 1461 | + end |
| 1462 | + end |
1309 | 1463 | end |
1310 | 1464 | end |
0 commit comments