Skip to content

Commit

Permalink
Remove remaining Repo.insert in test to use factori
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Mar 22, 2024
1 parent 436bc13 commit 4db40df
Show file tree
Hide file tree
Showing 22 changed files with 130 additions and 137 deletions.
46 changes: 32 additions & 14 deletions lib/accent/telemetry_ui/ecto_psql_extras.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@ defmodule Accent.TelemetryUI.EctoPSQLExtras do

@queries [
bloat: EctoPSQLExtras.Bloat,
blocking: EctoPSQLExtras.Blocking,
cache_hit: EctoPSQLExtras.CacheHit,
connections: EctoPSQLExtras.Connections,
table_cache_hit: EctoPSQLExtras.TableCacheHit,
index_cache_hit: EctoPSQLExtras.IndexCacheHit,
index_size: EctoPSQLExtras.IndexSize,
index_usage: EctoPSQLExtras.IndexUsage,
locks: EctoPSQLExtras.Locks,
all_locks: EctoPSQLExtras.AllLocks,
long_running_queries: EctoPSQLExtras.LongRunningQueries,
records_rank: EctoPSQLExtras.RecordsRank,
seq_scans: EctoPSQLExtras.SeqScans,
table_indexes_size: EctoPSQLExtras.TableIndexesSize,
table_size: EctoPSQLExtras.TableSize,
total_index_size: EctoPSQLExtras.TotalIndexSize,
table_indexes_size: EctoPSQLExtras.TableIndexesSize,
total_table_size: EctoPSQLExtras.TotalTableSize,
unused_indexes: EctoPSQLExtras.UnusedIndexes,
duplicate_indexes: EctoPSQLExtras.DuplicateIndexes,
null_indexes: EctoPSQLExtras.NullIndexes,
vacuum_stats: EctoPSQLExtras.VacuumStats
unused_indexes: EctoPSQLExtras.UnusedIndexes
]

def all(repo), do: Enum.map(Keyword.keys(@queries), &new(repo, &1))
Expand Down Expand Up @@ -51,7 +43,7 @@ defmodule Accent.TelemetryUI.EctoPSQLExtras do
Enum.map(result.rows, &parse_row(&1, types))
end

TableRex.quick_render!(rows, names)
{rows, names}
end

defp parse_row(list, types) do
Expand Down Expand Up @@ -102,13 +94,39 @@ defmodule Accent.TelemetryUI.EctoPSQLExtras do
end

def to_html(metric, _assigns) do
{rows, names} = metric.data

names =
Enum.map_join(names, "", fn name ->
"<td style='font-weight: bold; padding: 6px 14px; background-color: color-mix(in srgb, currentColor 20%, transparent);'>" <>
to_string(name) <> "</td>"
end)

rows =
for cells <- rows do
"<tr style='border-bottom: 1px solid color-mix(in srgb, currentColor 10%, transparent);'>" <>
Enum.map_join(cells, "", fn cell ->
"<td style='padding: 6px 14px; border-right: 1px solid color-mix(in srgb, currentColor 10%, transparent); '>" <>
cell <> "</td>"
end) <> "</tr>"
end

{:safe,
"""
<details class="relative flex flex-col bg-white dark:bg-black/40 text-slate dark:text-white p-3 pt-2 shadow">
<summary class="flex items-baseline gap-2 text-base opacity-80 cursor-pointer">
<h2 class="">#{metric.title}</h2>
<h2>#{metric.title}</h2>
</summary>
<pre class="p-3 font-mono" style="font-size: 11px; overflow-x: scroll;">#{metric.data}</pre>
<table class="mt-2" style='font-size: 11px; overflow-x: scroll; width: 100%; border: 1px solid color-mix(in srgb, currentColor 10%, transparent); border-bottom: 0;'>
<thead style='border-bottom: 1px solid color-mix(in srgb, currentColor 10%, transparent);'>
#{names}
</thead>
<tbody class="font-mono">
#{rows}
</tbody>
</table>
</details>
"""}
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ defmodule Accent.Mixfile do
# Mock testing
{:mox, "~> 1.0", only: :test},
{:mock, "~> 0.3.0", only: :test},
{:factori, path: "../mirego/factori", only: :test},
{:factori, "~> 0.13", only: :test},

# Google API authentication
{:goth, "~> 1.4"},
Expand Down
34 changes: 17 additions & 17 deletions mix.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions test/auth/user_remote/authenticator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ defmodule AccentTest.UserRemote.Authenticator do
project = Factory.insert(Project, language_id: language.id)

collaborator =
Repo.insert!(%Collaborator{
Factory.insert(Collaborator,
project_id: project.id,
role: "admin",
assigner_id: assigner.id,
email: "[email protected]"
})
)

{:ok, _token} = Authenticator.authenticate(%{provider: :dummy, info: %{email: "[email protected]"}})
user = Repo.get_by(User, email: "[email protected]")
Expand All @@ -50,12 +50,12 @@ defmodule AccentTest.UserRemote.Authenticator do
project = Factory.insert(Project, language_id: language.id)

collaborator =
Repo.insert!(%Collaborator{
Factory.insert(Collaborator,
project_id: project.id,
role: "admin",
assigner_id: assigner.id,
email: "[email protected]"
})
)

{:ok, _token} = Authenticator.authenticate(%{provider: :dummy, info: %{email: "[email protected]"}})
user = Repo.get_by(User, email: "[email protected]")
Expand Down
4 changes: 2 additions & 2 deletions test/graphql/helpers/authorization_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ defmodule AccentTest.GraphQL.Helpers.Authorization do
collaborator = Factory.insert(Collaborator, project_id: project.id, user_id: user.id, role: "owner")

integration =
Repo.insert!(%Integration{
Factory.insert(Integration,
project_id: project.id,
user_id: user.id,
service: "slack",
data: %{url: "http://example.com"}
})
)

translation_comments_subscription =
Factory.insert(TranslationCommentsSubscription, translation_id: translation.id, user_id: user.id)
Expand Down
9 changes: 1 addition & 8 deletions test/graphql/requests/project_integrations_request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ defmodule AccentTest.GraphQL.Requests.ProjectIntegrations do

setup do
user = Factory.insert(User)

project =
Repo.insert!(%Project{
main_color: "#f00",
name: "My project",
last_synced_at: DateTime.from_naive!(~N[2017-01-01T00:00:00], "Etc/UTC")
})

project = Factory.insert(Project, last_synced_at: DateTime.from_naive!(~N[2017-01-01T00:00:00], "Etc/UTC"))
user = %{user | permissions: %{project.id => "admin"}}

Factory.insert(Collaborator, project_id: project.id, user_id: user.id, role: "admin")
Expand Down
14 changes: 3 additions & 11 deletions test/graphql/requests/project_revisions_request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,25 @@ defmodule AccentTest.GraphQL.Requests.ProjectRevisions do
alias Accent.Collaborator
alias Accent.Language
alias Accent.Project
alias Accent.Repo
alias Accent.Revision
alias Accent.User

setup do
user = Factory.insert(User)
french_language = Factory.insert(Language)

project =
Repo.insert!(%Project{
main_color: "#f00",
name: "My project",
last_synced_at: DateTime.from_naive!(~N[2017-01-01T00:00:00], "Etc/UTC")
})

project = Factory.insert(Project, last_synced_at: DateTime.from_naive!(~N[2017-01-01T00:00:00], "Etc/UTC"))
user = %{user | permissions: %{project.id => "admin"}}

Factory.insert(Collaborator, project_id: project.id, user_id: user.id, role: "admin")

revision =
Repo.insert!(%Revision{
Factory.insert(Revision,
language_id: french_language.id,
name: "foo",
slug: "bar",
project_id: project.id,
master: true
})
)

{:ok, [user: user, project: project, revision: revision]}
end
Expand Down
9 changes: 1 addition & 8 deletions test/graphql/requests/projects_request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@ defmodule AccentTest.GraphQL.Requests.Projects do
alias Accent.Collaborator
alias Accent.Language
alias Accent.Project
alias Accent.Repo
alias Accent.Revision
alias Accent.Translation
alias Accent.User

setup do
user = Factory.insert(User)
french_language = Factory.insert(Language)

project =
Repo.insert!(%Project{
main_color: "#f00",
name: "My project",
last_synced_at: DateTime.from_naive!(~N[2017-01-01T00:00:00], "Etc/UTC")
})
project = Factory.insert(Project, last_synced_at: DateTime.from_naive!(~N[2017-01-01T00:00:00], "Etc/UTC"))

Factory.insert(Collaborator, project_id: project.id, user_id: user.id, role: "admin")
revision = Factory.insert(Revision, language_id: french_language.id, project_id: project.id, master: true)
Expand Down
17 changes: 7 additions & 10 deletions test/graphql/resolvers/activity_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule AccentTest.GraphQL.Resolvers.Activity do
alias Accent.Language
alias Accent.Operation
alias Accent.Project
alias Accent.Repo
alias Accent.Revision
alias Accent.Translation
alias Accent.User
Expand Down Expand Up @@ -34,15 +33,15 @@ defmodule AccentTest.GraphQL.Resolvers.Activity do
test "list activities", %{user: user, project: project, translation: translation, revision: revision} do
operation = Factory.insert(Operation, user_id: user.id, project_id: project.id, action: "sync")

Repo.insert!(%Operation{
Factory.insert(Operation,
user_id: user.id,
translation_id: translation.id,
revision_id: revision.id,
key: translation.key,
text: "foo",
action: "update",
batch_operation_id: operation.id
})
)

{:ok, %{entries: entries, meta: meta}} = Resolver.list_operations(operation, %{}, %{})

Expand All @@ -55,14 +54,14 @@ defmodule AccentTest.GraphQL.Resolvers.Activity do
end

test "list project", %{user: user, project: project, translation: translation, revision: revision} do
Repo.insert!(%Operation{
Factory.insert(Operation,
user_id: user.id,
translation_id: translation.id,
revision_id: revision.id,
key: translation.key,
text: "foo",
action: "update"
})
)

Factory.insert(Operation, user_id: user.id, project_id: project.id, action: "sync")
{:ok, %{entries: entries, meta: meta}} = Resolver.list_project(project, %{}, %{})
Expand All @@ -76,9 +75,7 @@ defmodule AccentTest.GraphQL.Resolvers.Activity do
end

test "list project paginated", %{user: user, project: project} do
for _index <- 1..100 do
Factory.insert(Operation, user_id: user.id, project_id: project.id, action: "sync")
end
Factory.seed(Operation, 100, user_id: user.id, project_id: project.id, action: "sync")

{:ok, %{entries: entries, meta: meta}} = Resolver.list_project(project, %{page: 3}, %{})

Expand Down Expand Up @@ -116,14 +113,14 @@ defmodule AccentTest.GraphQL.Resolvers.Activity do
end

test "list translation", %{user: user, project: project, translation: translation, revision: revision} do
Repo.insert!(%Operation{
Factory.insert(Operation,
user_id: user.id,
translation_id: translation.id,
revision_id: revision.id,
key: translation.key,
text: "foo",
action: "update"
})
)

Factory.insert(Operation, user_id: user.id, project_id: project.id, action: "sync")
{:ok, %{entries: entries, meta: meta}} = Resolver.list_translation(translation, %{}, %{})
Expand Down
12 changes: 6 additions & 6 deletions test/graphql/resolvers/document_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ defmodule AccentTest.GraphQL.Resolvers.Document do
revision = Factory.insert(Revision, language_id: french_language.id, project_id: project.id, master: true)

document =
Repo.insert!(%Document{
Factory.insert(Document,
project_id: project.id,
path: "test",
format: "json",
updated_at: DateTime.from_unix!(1_432_560_368_868_569, :microsecond)
})
)

{:ok, [user: user, project: project, document: document, revision: revision]}
end
Expand Down Expand Up @@ -69,12 +69,12 @@ defmodule AccentTest.GraphQL.Resolvers.Document do

test "update with existing path", %{document: document, project: project, user: user} do
other_document =
Repo.insert!(%Document{
Factory.insert(Document,
project_id: project.id,
path: "test2",
format: "json",
updated_at: DateTime.add(document.updated_at, 3600, :second)
})
)

context = %{context: %{conn: %PlugConn{assigns: %{current_user: user}}}}
{:ok, result} = Resolver.update(document, %{path: other_document.path}, context)
Expand Down Expand Up @@ -104,12 +104,12 @@ defmodule AccentTest.GraphQL.Resolvers.Document do

test "list project", %{document: document, project: project, revision: revision} do
other_document =
Repo.insert!(%Document{
Factory.insert(Document,
project_id: project.id,
path: "test2",
format: "json",
updated_at: DateTime.add(document.updated_at, 3600, :second)
})
)

_empty_document = Factory.insert(Document, project_id: project.id, path: "test3", format: "json")

Expand Down
12 changes: 6 additions & 6 deletions test/graphql/resolvers/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ defmodule AccentTest.GraphQL.Resolvers.Integration do
context = %{context: %{conn: %PlugConn{assigns: %{current_user: user}}}}

integration =
Repo.insert!(%Integration{
Factory.insert(Integration,
project_id: project.id,
user_id: user.id,
service: "slack",
events: ["sync"],
data: %{url: "http://google.ca"}
})
data: %{id: Ecto.UUID.generate(), url: "http://google.ca"}
)

{:ok, updated_integration} = Resolver.update(integration, %{data: %{url: "http://example.com/update"}}, context)

Expand All @@ -103,13 +103,13 @@ defmodule AccentTest.GraphQL.Resolvers.Integration do
context = %{context: %{conn: %PlugConn{assigns: %{current_user: user}}}}

integration =
Repo.insert!(%Integration{
Factory.insert(Integration,
project_id: project.id,
user_id: user.id,
service: "slack",
events: ["sync"],
data: %{url: "http://google.ca"}
})
data: %{id: Ecto.UUID.generate(), url: "http://google.ca"}
)

{:ok, deleted_integration} = Resolver.delete(integration, %{}, context)

Expand Down
6 changes: 3 additions & 3 deletions test/graphql/resolvers/project_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule AccentTest.GraphQL.Resolvers.Project do
end

test "list viewer", %{user: user, project: project} do
Factory.insert(Project, main_color: "#f00", name: "Other project")
Factory.insert(Project)

{:ok, result} = Resolver.list_viewer(user, %{}, %{})

Expand All @@ -48,7 +48,7 @@ defmodule AccentTest.GraphQL.Resolvers.Project do
end

test "list viewer search", %{user: user, language: language} do
Factory.insert(Project, main_color: "#f00", name: "Other project")
Factory.insert(Project)

{:ok, project_two} =
ProjectCreator.create(
Expand Down Expand Up @@ -111,7 +111,7 @@ defmodule AccentTest.GraphQL.Resolvers.Project do
end

test "list viewer ordering", %{user: user, language: language, project: project_one} do
Factory.insert(Project, main_color: "#f00", name: "Other project")
Factory.insert(Project)

{:ok, project_two} =
ProjectCreator.create(
Expand Down
Loading

0 comments on commit 4db40df

Please sign in to comment.