@@ -381,6 +381,63 @@ def create_build(name, queued_at = current, started_from = 0)
381
381
end
382
382
end
383
383
384
+ shared_context 'with some empty pipelines' do
385
+ before do
386
+ create_pipeline ( :canceled , 'ref' , 'A' )
387
+ create_pipeline ( :success , 'ref' , 'A' )
388
+ create_pipeline ( :failed , 'ref' , 'B' )
389
+ create_pipeline ( :skipped , 'feature' , 'C' )
390
+ end
391
+
392
+ def create_pipeline ( status , ref , sha )
393
+ create ( :ci_empty_pipeline , status : status , ref : ref , sha : sha )
394
+ end
395
+ end
396
+
397
+ describe '.latest' do
398
+ include_context 'with some empty pipelines'
399
+
400
+ context 'when no ref is specified' do
401
+ let ( :pipelines ) { Ci ::Pipeline . latest . all }
402
+
403
+ it 'returns the latest pipeline for the same ref and different sha' do
404
+ expect ( pipelines . map ( &:sha ) ) . to contain_exactly ( 'A' , 'B' , 'C' )
405
+ expect ( pipelines . map ( &:status ) ) .
406
+ to contain_exactly ( 'success' , 'failed' , 'skipped' )
407
+ end
408
+ end
409
+
410
+ context 'when ref is specified' do
411
+ let ( :pipelines ) { Ci ::Pipeline . latest ( 'ref' ) . all }
412
+
413
+ it 'returns the latest pipeline for ref and different sha' do
414
+ expect ( pipelines . map ( &:sha ) ) . to contain_exactly ( 'A' , 'B' )
415
+ expect ( pipelines . map ( &:status ) ) .
416
+ to contain_exactly ( 'success' , 'failed' )
417
+ end
418
+ end
419
+ end
420
+
421
+ describe '.latest_status' do
422
+ include_context 'with some empty pipelines'
423
+
424
+ context 'when no ref is specified' do
425
+ let ( :latest_status ) { Ci ::Pipeline . latest_status }
426
+
427
+ it 'returns the latest status for the same ref and different sha' do
428
+ expect ( latest_status ) . to eq ( Ci ::Pipeline . latest . status )
429
+ end
430
+ end
431
+
432
+ context 'when ref is specified' do
433
+ let ( :latest_status ) { Ci ::Pipeline . latest_status ( 'ref' ) }
434
+
435
+ it 'returns the latest status for ref and different sha' do
436
+ expect ( latest_status ) . to eq ( Ci ::Pipeline . latest_status ( 'ref' ) )
437
+ end
438
+ end
439
+ end
440
+
384
441
describe '#status' do
385
442
let! ( :build ) { create ( :ci_build , :created , pipeline : pipeline , name : 'test' ) }
386
443
0 commit comments