<commit_before>defmodule Core.ViewTestHelpers do
  @moduledoc """
  Helper functions to check views.

  Use this functions to check if an API retrieved value has been changed or not.

  Update this file when the API has been correctly.
  """

  @doc """
  Try to match a JSON `response` with a schema.
  """
  def check_view(response, "annotation.json") do
    %{"id" => _,
      "author" => _,
      "article_id" => _,
      "target" => _,
      "tags" => tags} = response

    check_array_view(tags, "tag.json")
  end

  def check_view(response, "error.json") do
    %{"errors" => _} = response
  end

  @doc """
  Try to match all elements of a JSON array `response` with a schema
  """
  def check_array_view(response, asset_or_fun \\ fn x -> x end)

  def check_array_view(response, "annotation.json") do
    check_array_view response, fn s ->
      %{"id" => _,
        "article_id" => _,
        "author" => _} = s
    end
  end

  def check_array_view(response, "tag.json") do
    check_array_view response, fn t ->
      %{"id" => _,
        "title" => _} = t
    end
  end

  def check_array_view(response, fun) do
    Enum.all?(response, fun)
  end
end
<commit_msg>Add ViewTestHelpers.check_view/2 header<commit_after>defmodule Core.ViewTestHelpers do
  @moduledoc """
  Helper functions to check views.

  Use this functions to check if an API retrieved value has been changed or not.

  Update this file when the API has been correctly.
  """

  @doc """
  Try to match a JSON `response` with a schema.
  """
  def check_view(response, schema)

  def check_view(response, "annotation.json") do
    %{"id" => _,
      "author" => _,
      "article_id" => _,
      "target" => _,
      "tags" => tags} = response

    check_array_view(tags, "tag.json")
  end

  def check_view(response, "error.json") do
    %{"errors" => _} = response
  end

  @doc """
  Try to match all elements of a JSON array `response` with a schema
  """
  def check_array_view(response, asset_or_fun \\ fn x -> x end)

  def check_array_view(response, "annotation.json") do
    check_array_view response, fn s ->
      %{"id" => _,
        "article_id" => _,
        "author" => _} = s
    end
  end

  def check_array_view(response, "tag.json") do
    check_array_view response, fn t ->
      %{"id" => _,
        "title" => _} = t
    end
  end

  def check_array_view(response, fun) do
    Enum.all?(response, fun)
  end
end

{'fraction_non_alphanumeric': 0.1271748135874068, 'fraction_numerical': 0.00041425020712510354, 'mean_word_length': 2.755832037325039, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '5981446', 'n_tokens_mistral': 811, 'n_tokens_neox': 790, 'n_words': 245}