<commit_before>require "rails_helper"

describe "/api/v7/relations", :type => :api do
  let(:headers) do
    { "HTTP_ACCEPT" => "application/json; version=7" }
  end
  let(:jsonp_headers) do
    { "HTTP_ACCEPT" => "application/javascript; version=7" }
  end

  context "index" do
    context "JSON" do
      let!(:relations) { FactoryGirl.create_list(:relation, 5) }
      let(:uri) { "/api/relations" }

      it "JSON" do
        get uri, nil, headers
        expect(last_response.status).to eq(200)

        response = JSON.parse(last_response.body)
        expect(response["relations"].length).to eq(5)

        item = response["relations"].first
        expect(item["source_id"]).to eq("citeulike")
        expect(item["work_id"]).to be_present
        expect(item["title"]).to eq("Defrosting the Digital Library: Bibliographic Tools for the Next Generation Web")
        expect(item["events"]).to eq({})
      end

      it "JSONP" do
        get "#{uri}?callback=_func", nil, jsonp_headers
        expect(last_response.status).to eql(200)

        # remove jsonp wrapper
        response = JSON.parse(last_response.body[6...-1])
        expect(response["relations"].length).to eq(5)

        item = response["relations"].first
        expect(item["source_id"]).to eq("citeulike")
        expect(item["work_id"]).to be_present
        expect(item["title"]).to eq("Defrosting the Digital Library: Bibliographic Tools for the Next Generation Web")
        expect(item["events"]).to eq({})
      end

      it "can be sorted by works.created_at using the created_at query parameter" do
        get "#{uri}?sort=created_at", nil, headers
        response = JSON.parse(last_response.body)
        data = response["relations"]
        actual_work_dois = data.map{ |work| work["DOI"] }

        expected_work_dois = Relation.includes(:related_work)
          .order("works.created_at ASC")
          .map(&:related_work)
          .map(&:doi)

        expect(actual_work_dois).to eq(expected_work_dois)
      end
    end

    context "show work_id" do
      let(:work) { FactoryGirl.create(:work, :with_events) }
      let!(:relation) { FactoryGirl.create(:relation, work: work) }
      let(:uri) { "/api/works/#{work.pid}/relations" }

      it "JSON" do
        get uri, nil, headers
        expect(last_response.status).to eq(200)

        response = JSON.parse(last_response.body)
        expect(response["meta"]["total"]).to eq(6)

        item = response["relations"].first
        expect(item["source_id"]).to eq("citeulike")
        expect(item["title"]).to eq("Defrosting the Digital Library: Bibliographic Tools for the Next Generation Web")
        expect(item["events"]).to eq({})
      end

      it "JSONP" do
        get "#{uri}?callback=_func", nil, jsonp_headers
        expect(last_response.status).to eql(200)

        # remove jsonp wrapper
        response = JSON.parse(last_response.body[6...-1])
        expect(response["meta"]["total"]).to eq(6)

        item = response["relations"].first
        expect(item["source_id"]).to eq("citeulike")
        expect(item["title"]).to eq("Defrosting the Digital Library: Bibliographic Tools for the Next Generation Web")
        expect(item["events"]).to eq({})
      end
    end
  end
end<commit_msg>fixed some specs<commit_after>require "rails_helper"

describe "/api/v7/relations", :type => :api do
  let(:headers) do
    { "HTTP_ACCEPT" => "application/json; version=7" }
  end
  let(:jsonp_headers) do
    { "HTTP_ACCEPT" => "application/javascript; version=7" }
  end

  context "index" do
    context "JSON" do
      let!(:relations) { FactoryGirl.create_list(:relation, 5) }
      let(:uri) { "/api/relations" }

      it "JSON" do
        get uri, nil, headers
        expect(last_response.status).to eq(200)

        response = JSON.parse(last_response.body)
        expect(response["relations"].length).to eq(5)

        item = response["relations"].first
        expect(item["source_id"]).to eq("citeulike")
        expect(item["subj_id"]).to be_present
        expect(item["title"]).to eq("Defrosting the Digital Library: Bibliographic Tools for the Next Generation Web")
        expect(item["events"]).to eq({})
      end

      it "JSONP" do
        get "#{uri}?callback=_func", nil, jsonp_headers
        expect(last_response.status).to eql(200)

        # remove jsonp wrapper
        response = JSON.parse(last_response.body[6...-1])
        expect(response["relations"].length).to eq(5)

        item = response["relations"].first
        expect(item["source_id"]).to eq("citeulike")
        expect(item["subj_id"]).to be_present
        expect(item["title"]).to eq("Defrosting the Digital Library: Bibliographic Tools for the Next Generation Web")
        expect(item["events"]).to eq({})
      end
    end

    context "show subj_id" do
      let(:work) { FactoryGirl.create(:work, :with_events) }
      let!(:relation) { FactoryGirl.create_list(:relation, 5, work: work) }
      let(:uri) { "/api/works/#{work.pid}/relations" }

      it "JSON" do
        get uri, nil, headers
        expect(last_response.status).to eq(200)

        response = JSON.parse(last_response.body)
        expect(response["meta"]["total"]).to eq(5)

        item = response["relations"].first
        expect(item["source_id"]).to eq("citeulike")
        expect(item["title"]).to eq("Defrosting the Digital Library: Bibliographic Tools for the Next Generation Web")
        expect(item["events"]).to eq({})
      end

      it "JSONP" do
        get "#{uri}?callback=_func", nil, jsonp_headers
        expect(last_response.status).to eql(200)

        # remove jsonp wrapper
        response = JSON.parse(last_response.body[6...-1])
        expect(response["meta"]["total"]).to eq(5)

        item = response["relations"].first
        expect(item["source_id"]).to eq("citeulike")
        expect(item["title"]).to eq("Defrosting the Digital Library: Bibliographic Tools for the Next Generation Web")
        expect(item["events"]).to eq({})
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.14738063660477455, 'fraction_numerical': 0.008123342175066313, 'mean_word_length': 2.950884086444008, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4714777', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1823, 'n_words': 405}