• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

source-academy / backend / 18dc689a4df4836fc6967bf0f74dc252964bd175-PR-1180

08 Sep 2024 06:14PM UTC coverage: 79.088% (-15.3%) from 94.372%
18dc689a4df4836fc6967bf0f74dc252964bd175-PR-1180

Pull #1180

github

josh1248
Change appropriate routes into admin scope
Pull Request #1180: Transfer groundControl (and admin panel) from staff to admin route

7 of 12 new or added lines in 1 file covered. (58.33%)

499 existing lines in 25 files now uncovered.

2602 of 3290 relevant lines covered (79.09%)

1023.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.09
/lib/cadet/helpers/shared_helper.ex
1
defmodule Cadet.SharedHelper do
2
  @moduledoc """
3
  Contains utility functions that may be commonly used across Cadet and CadetWeb..
4
  """
5

6
  defmacro is_ecto_id(id) do
7
    quote do
8
      is_integer(unquote(id)) or is_binary(unquote(id))
9
    end
10
  end
11

12
  def rename_keys(map, key_map) do
UNCOV
13
    Enum.reduce(key_map, map, fn {from, to}, map ->
×
UNCOV
14
      if Map.has_key?(map, from) do
×
UNCOV
15
        {v, map} = Map.pop!(map, from)
×
UNCOV
16
        Map.put(map, to, v)
×
17
      else
UNCOV
18
        map
×
19
      end
20
    end)
21
  end
22

23
  @doc """
24
  Snake-casifies string keys.
25

26
  Meant for use when accepting a JSON map from the frontend, where keys are
27
  usually camel-case.
28
  """
29
  def snake_casify_string_keys(map = %{}) do
30
    for {key, val} <- map,
13✔
31
        into: %{},
32
        do: {if(is_binary(key), do: Recase.to_snake(key), else: key), val}
99✔
33
  end
34

35
  def to_snake_case_atom_keys(map = %{}) do
36
    map
37
    |> snake_casify_string_keys()
38
    |> (&for({key, val} <- &1, into: %{}, do: {String.to_atom(key), val})).()
13✔
39
  end
40

41
  @doc """
42
  Snake-casifies string keys, recursively.
43

44
  Meant for use when accepting a JSON map from the frontend, where keys are
45
  usually camel-case.
46
  """
47
  def snake_casify_string_keys_recursive(map = %{}) when not is_struct(map) do
48
    for {key, val} <- map,
9✔
49
        into: %{},
50
        do:
51
          {if(is_binary(key), do: Recase.to_snake(key), else: key),
20✔
52
           snake_casify_string_keys_recursive(val)}
53
  end
54

55
  def snake_casify_string_keys_recursive(list) when is_list(list) do
56
    for e <- list, do: snake_casify_string_keys_recursive(e)
2✔
57
  end
58

59
  def snake_casify_string_keys_recursive(other), do: other
18✔
60

61
  @doc """
62
  Camel-casifies atom keys and converts them to strings.
63

64
  Meant for use when sending an Elixir map, which usually has snake-case keys,
65
  to the frontend.
66
  """
67
  def camel_casify_atom_keys(map = %{}) do
68
    for {key, val} <- map,
1✔
69
        into: %{},
70
        do: {if(is_atom(key), do: key |> Atom.to_string() |> Recase.to_camel(), else: key), val}
3✔
71
  end
72

73
  @doc """
74
  Converts a map like `%{"a" => 123}` into a keyword list like [a: 123]. Returns
75
  nil if any keys are not existing atoms.
76

77
  Meant for use for GET endpoints that filter based on the query string.
78
  """
UNCOV
79
  def try_keywordise_string_keys(map) do
×
UNCOV
80
    for {key, val} <- map,
×
UNCOV
81
        do: {if(is_binary(key), do: String.to_existing_atom(key), else: key), val}
×
82
  rescue
83
    ArgumentError -> nil
×
84
  end
85

86
  @doc """
87
  Sends an error to Sentry. The error can be anything.
88
  """
89
  def send_sentry_error(error) do
90
    {_, stacktrace} = Process.info(self(), :current_stacktrace)
3✔
91
    # drop 2 elements off the stacktrace: the frame of Process.info, and the
92
    # frame of this function
93
    stacktrace = stacktrace |> Enum.drop(2)
3✔
94

95
    error = Exception.normalize(:error, error, stacktrace)
3✔
96

97
    Sentry.capture_exception(error, stacktrace: stacktrace)
3✔
98
  end
99
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc