<commit_before># frozen_string_literal: true

require 'spec_helpers/client'
require 'rest/api_request'

RSpec.describe FinApps::REST::Screenings do
  include SpecHelpers::Client

  let(:results) { subject[0] }
  let(:error_messages) { subject[1] }

  describe '#list' do
    subject(:list) { described_class.new(client).list(params) }

    context 'with valid params' do
      let(:params) { {} }

      RSpec.shared_examples 'a GET request' do
        it { expect(results).to have_key(:records) }
      end

      RSpec.shared_examples 'a correct query builder' do |filter|
        it_behaves_like 'an API request'
        it_behaves_like 'a successful request'
        it_behaves_like 'a GET request'
        it 'builds query and sends proper request' do
          list
          encoded_filter = ERB::Util.url_encode filter.to_json
          url = "#{versioned_api_path}/screenings?filter=#{encoded_filter}"

          expect(WebMock).to have_requested(:get, url)
        end
      end

      context 'without searchTerm' do
        let(:params) { {searchTerm: nil, page: 2} }

        it 'builds query and sends proper request' do
          list
          url = "#{versioned_api_path}/screenings?page=2"

          expect(WebMock).to have_requested(:get, url)
        end
      end

      context 'with from date' do
        let(:params) { {fromDate: 'from'} }

        it_behaves_like 'a correct query builder', {
          '*date_created': {'$gte': 'from'}
        }
      end

      context 'with to date' do
        let(:params) { {toDate: 'to'} }

        it_behaves_like 'a correct query builder', {
          '*date_created': {'$lt': 'to'}
        }
      end

      context 'with date range' do
        let(:params) { {fromDate: 'from', toDate: 'to'} }

        it_behaves_like 'a correct query builder', {
          '*date_created': {'$gte': 'from', '$lt': 'to'}
        }
      end

      context 'with invalid progress' do
        let(:params) { {progress: 'xyz'} }

        it 'builds query and sends proper request' do
          list
          url = "#{versioned_api_path}/screenings"

          expect(WebMock).to have_requested(:get, url)
        end
      end

      context 'with valid progress' do
        let(:params) { {progress: '10'} }

        it_behaves_like 'a correct query builder', {progress: 10}
      end

      context 'with searchTerm' do
        let(:params) { {searchTerm: 'le term'} }

        it_behaves_like 'a correct query builder', {
          '$or': [{'consumer.public_id': 'le term'},
                  {'consumer.email': 'le term'},
                  {'consumer.first_name': 'le term'},
                  {'consumer.last_name': 'le term'},
                  {'consumer.external_id': 'le term'},
                  {'consumer.first_name': 'le'},
                  {'consumer.last_name': 'le'},
                  {'consumer.first_name': 'term'},
                  {'consumer.last_name': 'term'}]
        }
      end
    end

    context 'with invalid params' do
      let(:params) { ['invalid array'] }

      it { expect { list }.to raise_error(FinAppsCore::InvalidArgumentsError) }
    end

    context 'with missing params' do
      let(:params) { nil }

      it_behaves_like 'an API request'
      it_behaves_like 'a successful request'
      it_behaves_like 'a GET request'
    end
  end

  describe '#tenant_schemas' do
    subject(:tenant_schemas) { described_class.new(client).tenant_schemas }

    it_behaves_like 'an API request'
    it_behaves_like 'a successful request'
    it 'performs a get and returns the response' do
      expect(results[0]).to have_key(:external_url)
    end

    it 'sends proper request' do<commit_msg>Refactor: DRY<commit_after># frozen_string_literal: true

require 'spec_helpers/client'
require 'spec_helpers/api_request'

RSpec.describe FinApps::REST::Screenings do
  include SpecHelpers::Client

  let(:results) { subject[RESULTS] }
  let(:error_messages) { subject[ERROR_MESSAGES] }

  describe '#list' do
    subject(:list) { described_class.new(client).list(params) }

    context 'with valid params' do
      let(:params) { {} }

      RSpec.shared_examples 'a correct query builder' do |filter|
        it_behaves_like 'an API request'
        it_behaves_like 'a successful request'
        it_behaves_like 'a GET index request'
        it 'builds query and sends proper request' do
          list
          encoded_filter = ERB::Util.url_encode filter.to_json
          url = "#{versioned_api_path}/screenings?filter=#{encoded_filter}"

          expect(WebMock).to have_requested(:get, url)
        end
      end

      context 'without searchTerm' do
        let(:params) { {searchTerm: nil, page: 2} }

        it 'builds query and sends proper request' do
          list
          url = "#{versioned_api_path}/screenings?page=2"

          expect(WebMock).to have_requested(:get, url)
        end
      end

      context 'with from date' do
        let(:params) { {fromDate: 'from'} }

        it_behaves_like 'a correct query builder', {
          '*date_created': {'$gte': 'from'}
        }
      end

      context 'with to date' do
        let(:params) { {toDate: 'to'} }

        it_behaves_like 'a correct query builder', {
          '*date_created': {'$lt': 'to'}
        }
      end

      context 'with date range' do
        let(:params) { {fromDate: 'from', toDate: 'to'} }

        it_behaves_like 'a correct query builder', {
          '*date_created': {'$gte': 'from', '$lt': 'to'}
        }
      end

      context 'with invalid progress' do
        let(:params) { {progress: 'xyz'} }

        it 'builds query and sends proper request' do
          list
          url = "#{versioned_api_path}/screenings"

          expect(WebMock).to have_requested(:get, url)
        end
      end

      context 'with valid progress' do
        let(:params) { {progress: '10'} }

        it_behaves_like 'a correct query builder', {progress: 10}
      end

      context 'with searchTerm' do
        let(:params) { {searchTerm: 'le term'} }

        it_behaves_like 'a correct query builder', {
          '$or': [{'consumer.public_id': 'le term'},
                  {'consumer.email': 'le term'},
                  {'consumer.first_name': 'le term'},
                  {'consumer.last_name': 'le term'},
                  {'consumer.external_id': 'le term'},
                  {'consumer.first_name': 'le'},
                  {'consumer.last_name': 'le'},
                  {'consumer.first_name': 'term'},
                  {'consumer.last_name': 'term'}]
        }
      end
    end

    context 'with invalid params' do
      let(:params) { ['invalid array'] }

      it { expect { list }.to raise_error(FinAppsCore::InvalidArgumentsError) }
    end

    context 'with missing params' do
      let(:params) { nil }

      it_behaves_like 'an API request'
      it_behaves_like 'a successful request'
      it_behaves_like 'a GET index request'
    end
  end

  describe '#tenant_schemas' do
    subject(:tenant_schemas) { described_class.new(client).tenant_schemas }

    it_behaves_like 'an API request'
    it_behaves_like 'a successful request'
    it 'performs a get and returns the response' do
      expect(results[0]).to have_key(:external_url)
    end

    it 'sends proper request' do
{'fraction_non_alphanumeric': 0.11948479035352151, 'fraction_numerical': 0.0021923814743765417, 'mean_word_length': 2.317727272727273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '215846', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2145, 'n_words': 589}