BigW Consortium Gitlab

pipeline_serializer_spec.rb 4.23 KB
Newer Older
1 2 3
require 'spec_helper'

describe PipelineSerializer do
4 5
  let(:user) { create(:user) }

6
  let(:serializer) do
7
    described_class.new(current_user: user)
8 9
  end

10
  subject { serializer.represent(resource) }
11

12
  describe '#represent' do
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
    context 'when used without pagination' do
      it 'created a not paginated serializer' do
        expect(serializer).not_to be_paginated
      end

      context 'when a single object is being serialized' do
        let(:resource) { create(:ci_empty_pipeline) }

        it 'serializers the pipeline object' do
          expect(subject[:id]).to eq resource.id
        end
      end

      context 'when multiple objects are being serialized' do
        let(:resource) { create_list(:ci_pipeline, 2) }

        it 'serializers the array of pipelines' do
          expect(subject).not_to be_empty
        end
      end
    end

    context 'when used with pagination' do
      let(:request) { spy('request') }
      let(:response) { spy('response') }
      let(:pagination) { {} }

      before do
        allow(request)
          .to receive(:query_parameters)
          .and_return(pagination)
      end

      let(:serializer) do
47
        described_class.new(current_user: user)
48 49 50 51 52 53 54
          .with_pagination(request, response)
      end

      it 'created a paginated serializer' do
        expect(serializer).to be_paginated
      end

55
      context 'when resource is not paginatable' do
56 57 58 59 60
        context 'when a single pipeline object is being serialized' do
          let(:resource) { create(:ci_empty_pipeline) }
          let(:pagination) { { page: 1, per_page: 1 } }

          it 'raises error' do
61 62
            expect { subject }.to raise_error(
              Gitlab::Serializer::Pagination::InvalidResourceError)
63 64 65 66 67 68 69 70 71
          end
        end
      end

      context 'when resource is paginatable relation' do
        let(:resource) { Ci::Pipeline.all }
        let(:pagination) { { page: 1, per_page: 2 } }

        context 'when a single pipeline object is present in relation' do
72 73 74
          before do
            create(:ci_empty_pipeline)
          end
75 76 77 78 79 80 81

          it 'serializes pipeline relation' do
            expect(subject.first).to have_key :id
          end
        end

        context 'when a multiple pipeline objects are being serialized' do
82 83 84
          before do
            create_list(:ci_empty_pipeline, 3)
          end
85 86 87 88 89 90 91 92 93

          it 'serializes appropriate number of objects' do
            expect(subject.count).to be 2
          end

          it 'appends relevant headers' do
            expect(response).to receive(:[]=).with('X-Total', '3')
            expect(response).to receive(:[]=).with('X-Total-Pages', '2')
            expect(response).to receive(:[]=).with('X-Per-Page', '2')
94

95 96 97 98
            subject
          end
        end
      end
99
    end
100 101 102

    context 'number of queries' do
      let(:resource) { Ci::Pipeline.all }
Kamil Trzcinski committed
103
      let(:project) { create(:empty_project) }
104 105 106 107 108 109 110

      before do
        Ci::Pipeline::AVAILABLE_STATUSES.each do |status|
          create_pipeline(status)
        end
      end

111
      it 'verifies number of queries', :request_store do
112
        recorded = ActiveRecord::QueryRecorder.new { subject }
113
        expect(recorded.count).to be_within(1).of(57)
Kamil Trzcinski committed
114
        expect(recorded.cached_count).to eq(0)
115 116 117
      end

      def create_pipeline(status)
Kamil Trzcinski committed
118
        create(:ci_empty_pipeline, project: project, status: status).tap do |pipeline|
119 120 121 122 123 124 125 126 127 128 129 130
          Ci::Build::AVAILABLE_STATUSES.each do |status|
            create_build(pipeline, status, status)
          end
        end
      end

      def create_build(pipeline, stage, status)
        create(:ci_build, :tags, :triggered, :artifacts,
          pipeline: pipeline, stage: stage,
          name: stage, status: status)
      end
    end
131
  end
132

133 134
  describe '#represent_status' do
    context 'when represents only status' do
135 136
      let(:resource) { create(:ci_pipeline) }
      let(:status) { resource.detailed_status(double('user')) }
137 138

      subject { serializer.represent_status(resource) }
139 140

      it 'serializes only status' do
Shinya Maeda committed
141 142 143
        expect(subject[:text]).to eq(status.text)
        expect(subject[:label]).to eq(status.label)
        expect(subject[:icon]).to eq(status.icon)
144
        expect(subject[:favicon]).to eq("/assets/ci_favicons/#{status.favicon}.ico")
145 146
      end
    end
147
  end
148
end