<commit_before>defmodule Helix.Account.Controller.AccountSettingTest do

  use ExUnit.Case, async: true

  alias Helix.Account.Controller.AccountSetting, as: AccountSettingController
  alias Helix.Account.Model.AccountSetting
  alias Helix.Account.Model.Setting
  alias Helix.Account.Repo

  alias Helix.Account.Factory

  setup_all do
    if Repo.all(Setting) == [],
      do: Factory.insert_list(3, :setting)
    :ok
  end

  defp format_settings(settings, mapper \\ nil) do
    Enum.into(settings, %{}, fn setting ->
      setting_tuple =
        case setting do
          setting = %Setting{} ->
            {setting.setting_id, setting.default_value}
          setting = %AccountSetting{} ->
            {setting.setting_id, setting.setting_value}
          {setting_id, setting_value} ->
            {setting_id, setting_value}
        end

      if mapper,
        do: mapper.(setting_tuple),
        else: setting_tuple
    end)
  end

  describe "changing specific settings" do
    test "succeeds with valid params" do
      account = Factory.insert(:account)
      setting = Factory.insert(:setting)
      %{setting_value: value} = Factory.params_for(:account_setting)

      {:ok, put_result} = AccountSettingController.put(account, setting, value)
      {:ok, get_result} = AccountSettingController.get(account, setting)

      # put yields sent value
      assert value == put_result

      # put and get yields the same value
      assert get_result == put_result
    end

    test "fails when setting is invalid" do
      account = Factory.insert(:account)
      setting = Factory.build(:setting)
      %{setting_value: val} = Factory.params_for(:account_setting)

      assert {:error, cs} = AccountSettingController.put(account, setting, val)
      assert :setting_id in Keyword.keys(cs.errors)
    end
  end

  describe "fetching specific settings" do
    test "returns custom value when setting is changed" do
      account = Factory.insert(:account)
      setting = Factory.insert(:setting)
      %{setting_value: value} = Factory.params_for(:account_setting)

      AccountSettingController.put(account, setting, value)

      {:ok, return} = AccountSettingController.get(account, setting)

      assert value === return
    end

    test "returns default value when setting is unchanged" do
      account = Factory.insert(:account)
      setting = Factory.insert(:setting)

      {:ok, setting_value} = AccountSettingController.get(account, setting)

      assert setting.default_value == setting_value
    end
  end

  describe "fetching every setting" do
    test "includes modified settings" do
      account = Factory.insert(:account)

      default_settings =
        Setting
        |> Repo.all()
        |> format_settings()

      expected_settings =
        Setting
        |> Repo.all()
        |> format_settings(fn {setting_id, _} ->
          %{setting_value: value} = Factory.params_for(:account_setting)
          {setting_id, value}
        end)

      Enum.each(expected_settings, fn {setting, value} ->
        AccountSettingController.put(account, setting, value)
      end)

      received_settings =
        account
        |> AccountSettingController.get_settings()
        |> format_settings()

      assert expected_settings == received_settings
      refute received_settings == default_settings
    end

    test "includes unchanged settings" do
      account = Factory.insert(:account)

      account_settings =
        account
        |> AccountSettingController.get_settings()
        |> format_settings()

      default_settings =
        Setting
        |> Repo.all()
        |> format_settings()

      assert account_settings == default_settings
    end
  end
end<commit_msg>Simplify `AccountSettingController.get_settings/1` tests.<commit_after>defmodule Helix.Account.Controller.AccountSettingTest do

  use ExUnit.Case, async: true

  alias HELL.TestHelper.Random
  alias Helix.Account.Controller.AccountSetting, as: AccountSettingController
  alias Helix.Account.Model.Setting
  alias Helix.Account.Repo

  alias Helix.Account.Factory

  setup_all do
    if Repo.all(Setting) == [],
      do: Factory.insert_list(3, :setting)
    :ok
  end

  describe "changing specific settings" do
    test "succeeds with valid params" do
      account = Factory.insert(:account)
      setting = Factory.insert(:setting)
      %{setting_value: value} = Factory.params_for(:account_setting)

      {:ok, put_result} = AccountSettingController.put(account, setting, value)
      {:ok, get_result} = AccountSettingController.get(account, setting)

      # put yields sent value
      assert value == put_result

      # put and get yields the same value
      assert get_result == put_result
    end

    test "fails when setting is invalid" do
      account = Factory.insert(:account)
      setting = Factory.build(:setting)
      %{setting_value: val} = Factory.params_for(:account_setting)

      assert {:error, cs} = AccountSettingController.put(account, setting, val)
      assert :setting_id in Keyword.keys(cs.errors)
    end
  end

  describe "fetching specific settings" do
    test "returns custom value when setting is changed" do
      account = Factory.insert(:account)
      setting = Factory.insert(:setting)
      %{setting_value: value} = Factory.params_for(:account_setting)

      AccountSettingController.put(account, setting, value)

      {:ok, return} = AccountSettingController.get(account, setting)

      assert value === return
    end

    test "returns default value when setting is unchanged" do
      account = Factory.insert(:account)
      setting = Factory.insert(:setting)

      {:ok, setting_value} = AccountSettingController.get(account, setting)

      assert setting.default_value == setting_value
    end
  end

  describe "fetching every setting" do
    test "includes default and modified settings" do
      account = Factory.insert(:account)
      settings = Repo.all(Setting)
      value = Random.string()

      settings
      |> Enum.take_random(3)
      |> Enum.each(&AccountSettingController.put(account, &1.setting_id, value))

      account_settings = AccountSettingController.get_settings(account)
      defaults = Enum.into(settings, %{}, &{&1.setting_id, &1.default_value})
      diff = Map.values(account_settings) -- Map.values(defaults)

      assert Map.keys(account_settings) == Map.keys(defaults)
      assert 3 == Enum.count(diff)
    end

    test "includes every unchanged setting" do
      account = Factory.insert(:account)

      account_settings = AccountSettingController.get_settings(account)
      default_settings =
        Setting
        |> Repo.all()
        |> Enum.into(%{}, &{&1.setting_id, &1.default_value})

      assert account_settings == default_settings
    end
  end
end
{'fraction_non_alphanumeric': 0.09532215357458076, 'fraction_numerical': 0.0014710208884966167, 'mean_word_length': 3.0470238095238096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '2270365', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1828, 'n_words': 468}