<commit_before>require 'test_helper'

module TestHelpers
  #
  # JSON Helpers
  #
  module JsonHelper
    #
    # POSTs a hash data as JSON with content-type "application/json"
    #
    def post_json(endpoint, data)
      post endpoint, data.to_json, 'CONTENT_TYPE' => 'application/json'
    end

    #
    # PUTs a hash data as JSON with content-type "application/json"
    #
    def put_json(endpoint, data)
      put endpoint, data.to_json, 'CONTENT_TYPE' => 'application/json'
    end

    #
    # Assert that a JSON response matches the model and keys provided
    #
    def assert_json_matches_model(response_json, model, keys)
      keys.each { |k| assert response_json.has_key?(k), "Response has key #{k}"}
      keys.each { |k| assert_equal model[k], response_json[k], "Values for key #{k} match" }
    end

    module_function :assert_json_matches_model
    module_function :post_json
    module_function :put_json
  end
end<commit_msg>QUALITY: Add extra json helpers<commit_after>require 'test_helper'

module TestHelpers
  #
  # JSON Helpers
  #
  module JsonHelper
    #
    # POSTs a hash data as JSON with content-type "application/json"
    #
    def post_json(endpoint, data)
      post endpoint, data.to_json, 'CONTENT_TYPE' => 'application/json'
    end

    #
    # PUTs a hash data as JSON with content-type "application/json"
    #
    def put_json(endpoint, data)
      put endpoint, data.to_json, 'CONTENT_TYPE' => 'application/json'
    end

    #
    # Assert that a JSON response matches the model and keys provided
    #
    def assert_json_matches_model(response_json, model, keys)
      keys.each { |k| assert response_json.has_key?(k), "Response has key #{k}"}
      keys.each { |k| assert_equal model[k], response_json[k], "Values for key #{k} match" }
    end

    #
    # Last response body parsed from JSON
    #
    def last_response_body
      JSON.parse(last_response.body)
    end

    #
    # Converts from an ActiveRelation to JSON (without Ruby objects inside the hash)
    #
    def json_hashed(hash)
      JSON.parse(hash.to_json)
    end

    #
    # Assert that the lefthand matches the right-hand as json hash
    #
    def assert_json_equal(lhs, rhs)
      assert_equal json_hashed(lhs), json_hashed(rhs)
    end

    module_function :assert_json_matches_model
    module_function :post_json
    module_function :put_json
    module_function :last_response_body
    module_function :json_hashed
    module_function :assert_json_equal
  end
end
{'fraction_non_alphanumeric': 0.08507223113964688, 'fraction_numerical': 0.0, 'mean_word_length': 3.1070840197693577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5601411', 'n_tokens_mistral': 800, 'n_tokens_neox': 792, 'n_words': 224}