<commit_before>require 'spec_helper'

describe JsonapiSpecHelpers do
  include JsonapiSpecHelpers
  attr_accessor :response

  let(:instance) { klass.new }

  let(:json) do
    {
      'data' => {
        'type' => 'posts',
        'id' => '1',
        'attributes' => {
          'title' => 'post title',
          'description' => 'post description',
          'views' => 100,
          'first_title_letter' => 'p'
        }
      }
    }
  end

  after do
    JsonapiSpecHelpers::Payload.registry = {}
  end

  describe '#assert_payload' do
    before do
      JsonapiSpecHelpers::Payload.register(:post) do
        key(:title)
        key(:description)
        key(:first_title_letter) { |p| p.title[0] }
        key(:views, Integer)
      end
    end

    context 'when payload is valid' do
      let(:post_record) do
        double \
          id: 1,
          title: 'post title',
          description: 'post description',
          views: 100
      end

      it 'passes assertion' do
        assert_payload(:post, post_record, json_item)
      end

      context 'when json value matches payload value, but wrong type' do
        before do
          json['data']['attributes']['views'] = '100'
          allow(post_record).to receive(:views) { '100' }
        end

        it 'does not pass assertion' do
          expect {
            assert_payload(:post, post_record, json_item)
          }.to raise_error(
            RSpec::Expectations::ExpectationNotMetError,
            /Expected JSON payload key 'views' to have type Integer but was String/
          )
        end
      end

      context 'when a condition that matches multiple types' do
        it 'passes assertion if one of the types passes' do
          json['data']['attributes']['is_foo'] = false
          allow(post_record).to receive(:is_foo) { false }
          expect {
            assert_payload(:post, post_record, json_item) do
              key(:is_foo, [TrueClass, FalseClass])
            end
          }.to_not raise_error

          json['data']['attributes']['is_foo'] = true
          allow(post_record).to receive(:is_foo) { true }
          expect {
            assert_payload(:post, post_record, json_item) do
              key(:is_foo, [TrueClass, FalseClass])
            end
          }.to_not raise_error

          json['data']['attributes']['is_foo'] = 'true'
          allow(post_record).to receive(:is_foo) { 'true' }
          expect {
            assert_payload(:post, post_record, json_item) do
              key(:is_foo, [TrueClass, FalseClass])
            end
          }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
        end
      end

      context 'when json value does not match payload value' do
        before do
          allow(post_record).to receive(:title) { 'foo' }
        end

        it 'does not pass assertion' do
          expect {
            assert_payload(:post, post_record, json_item)
          }.to raise_error(
            RSpec::Expectations::ExpectationNotMetError,
            /Expected JSON payload to have key 'title' == "foo" but was "post title"/
          )
        end
      end

      context 'when given timestamps! in payload' do
        let(:timestamp) { Time.now }

        before do
          JsonapiSpecHelpers::Payload.register(:post_with_timestamps) do
            key(:title)
            key(:description)<commit_msg>Fix spec<commit_after>require 'spec_helper'

describe JsonapiSpecHelpers do
  include JsonapiSpecHelpers
  attr_accessor :response

  let(:instance) { klass.new }

  let(:json) do
    {
      'data' => {
        'type' => 'posts',
        'id' => '1',
        'attributes' => {
          'title' => 'post title',
          'description' => 'post description',
          'views' => 100,
          'first_title_letter' => 'p'
        }
      }
    }
  end

  after do
    JsonapiSpecHelpers::Payload.registry = {}
  end

  describe '#assert_payload' do
    before do
      JsonapiSpecHelpers::Payload.register(:post) do
        key(:title)
        key(:description)
        key(:first_title_letter) { |p| p.title[0] }
        key(:views, Integer)
      end
    end

    context 'when payload is valid' do
      let(:post_record) do
        double \
          id: 1,
          title: 'post title',
          description: 'post description',
          views: 100
      end

      it 'passes assertion' do
        assert_payload(:post, post_record, json_item)
      end

      context 'when json value matches payload value, but wrong type' do
        before do
          json['data']['attributes']['views'] = '100'
          allow(post_record).to receive(:views) { '100' }
        end

        it 'does not pass assertion' do
          expect {
            assert_payload(:post, post_record, json_item)
          }.to raise_error(
            RSpec::Expectations::ExpectationNotMetError,
            /Expected JSON payload key 'views' to have type Integer but was String/
          )
        end
      end

      context 'when a condition that matches multiple types' do
        it 'passes assertion if one of the types passes' do
          json['data']['attributes']['is_foo'] = false
          allow(post_record).to receive(:is_foo) { false }
          expect {
            assert_payload(:post, post_record, json_item) do
              key(:is_foo, [TrueClass, FalseClass])
            end
          }.to_not raise_error

          json['data']['attributes']['is_foo'] = true
          allow(post_record).to receive(:is_foo) { true }
          expect {
            assert_payload(:post, post_record, json_item) do
              key(:is_foo, [TrueClass, FalseClass])
            end
          }.to_not raise_error

          json['data']['attributes']['is_foo'] = 'true'
          allow(post_record).to receive(:is_foo) { 'true' }
          expect {
            assert_payload(:post, post_record, json_item) do
              key(:is_foo, [TrueClass, FalseClass])
            end
          }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
        end
      end

      context 'when json value does not match payload value' do
        before do
          allow(post_record).to receive(:title) { 'foo' }
        end

        it 'does not pass assertion' do
          expect {
            assert_payload(:post, post_record, json_item)
          }.to raise_error(
            RSpec::Expectations::ExpectationNotMetError,
            /Expected JSON payload to have key 'title' == "foo" but was "post title"/
          )
        end
      end

      context 'when given timestamps! in payload' do
        let(:timestamp) { Time.now.to_json }

        before do
          JsonapiSpecHelpers::Payload.register(:post_with_timestamps) do
            key(:title)
            key(:description)
{'fraction_non_alphanumeric': 0.10576781236170527, 'fraction_numerical': 0.004425431479569258, 'mean_word_length': 1.9491083079599827, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '3715615', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1864, 'n_words': 502}