Skip to content

Commit

Permalink
Use factori in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Mar 22, 2024
1 parent 857176f commit 436bc13
Show file tree
Hide file tree
Showing 89 changed files with 1,082 additions and 944 deletions.
28 changes: 17 additions & 11 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ else
debug_errors: get_env("DEBUG_ERRORS", :boolean)
end

ecto_ipv6? = get_env("ECTO_IPV6", :boolean)

config :accent, Accent.Repo,
timeout: get_env("DATABASE_TIMEOUT", :integer) || 29_000,
queue_target: get_env("DATABASE_QUEUE_TARGET", :integer) || 500,
queue_interval: get_env("DATABASE_QUEUE_INTERVAL", :integer) || 2000,
pool_size: get_env("DATABASE_POOL_SIZE", :integer),
ssl: get_env("DATABASE_SSL", :boolean),
ssl_opts: [verify: :verify_none],
url: get_env("DATABASE_URL") || "postgres://localhost/accent_development",
socket_options: if(ecto_ipv6?, do: [:inet6], else: [])
if config_env() === :test do
config :accent, Accent.Repo,
pool_size: System.schedulers_online() * 2,
url: get_env("DATABASE_URL")
else
ecto_ipv6? = get_env("ECTO_IPV6", :boolean)

config :accent, Accent.Repo,
timeout: get_env("DATABASE_TIMEOUT", :integer) || 29_000,
queue_target: get_env("DATABASE_QUEUE_TARGET", :integer) || 500,
queue_interval: get_env("DATABASE_QUEUE_INTERVAL", :integer) || 2000,
pool_size: get_env("DATABASE_POOL_SIZE", :integer),
ssl: get_env("DATABASE_SSL", :boolean),
ssl_opts: [verify: :verify_none],
url: get_env("DATABASE_URL") || "postgres://localhost/accent_development",
socket_options: if(ecto_ipv6?, do: [:inet6], else: [])
end

config :accent, Accent.MachineTranslations,
default_providers_config: %{
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/resolvers/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule Accent.GraphQL.Resolvers.Project do
"name" => args.name,
"main_color" => args.main_color,
"logo" => args.logo,
"locked_file_operations" => args.is_file_operations_locked
"locked_file_operations" => args.is_file_operations_locked || false
}

case ProjectUpdater.update(
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/resolvers/version.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule Accent.GraphQL.Resolvers.Version do
|> Version.changeset(%{
name: args[:name],
tag: args[:tag],
copy_on_update_translation: args[:copy_on_update_translation]
copy_on_update_translation: args[:copy_on_update_translation] || false
})
|> Repo.update()
|> case do
Expand Down
6 changes: 3 additions & 3 deletions lib/movement/migration/translation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Movement.Migration.Translation do
locked: operation.locked,
file_index: operation.file_index,
file_comment: operation.file_comment,
removed: operation.previous_translation && operation.previous_translation.removed,
removed: (operation.previous_translation && operation.previous_translation.removed) || false,
translated: is_nil(operation.translation_id),
revision_id: operation.revision_id,
document_id: operation.document_id,
Expand All @@ -82,11 +82,11 @@ defmodule Movement.Migration.Translation do
proposed_text: operation.text,
corrected_text: operation.text,
translated: (operation.previous_translation && operation.previous_translation.translated) || false,
conflicted: operation.previous_translation && operation.previous_translation.conflicted,
conflicted: (operation.previous_translation && operation.previous_translation.conflicted) || false,
value_type: operation.value_type,
file_index: operation.file_index,
file_comment: operation.file_comment,
removed: operation.previous_translation && operation.previous_translation.removed,
removed: (operation.previous_translation && operation.previous_translation.removed) || false,
revision_id: operation.revision_id,
document_id: operation.document_id,
version_id: operation.version_id,
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +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},

# Google API authentication
{:goth, "~> 1.4"},
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"eternal": {:hex, :eternal, "1.2.2", "d1641c86368de99375b98d183042dd6c2b234262b8d08dfd72b9eeaafc2a1abd", [:mix], [], "hexpm", "2c9fe32b9c3726703ba5e1d43a1d255a4f3f2d8f8f9bc19f094c7cb1a7a9e782"},
"excoveralls": {:hex, :excoveralls, "0.18.0", "b92497e69465dc51bc37a6422226ee690ab437e4c06877e836f1c18daeb35da9", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1109bb911f3cb583401760be49c02cbbd16aed66ea9509fc5479335d284da60b"},
"exile": {:hex, :exile, "0.9.1", "832b6340cf800661e90e52cebc760b795450f803c0e9265ccdc54150423fbb32", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "553a1847b27118c843d3dc6912adbc36d60336811d15ad70a31b82eb5a416328"},
"factori": {:hex, :factori, "0.12.0", "55cddfcaa12619a5864bbfe23112d6e852d992ed1c53118606923ba6ed0f97ac", [:make, :mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:faker, "~> 0.16", [hex: :faker, repo: "hexpm", optional: false]}], "hexpm", "bd1456a7c06823160ffb047c4f1f265c330210620aa41d940b952dcde14f87cd"},
"faker": {:hex, :faker, "0.18.0", "943e479319a22ea4e8e39e8e076b81c02827d9302f3d32726c5bf82f430e6e14", [:mix], [], "hexpm", "bfbdd83958d78e2788e99ec9317c4816e651ad05e24cfd1196ce5db5b3e81797"},
"fast_yaml": {:git, "https://github.com/processone/fast_yaml.git", "e789f68895f71b7ad31057177810ca0161bf790e", [ref: "e789f68895f71b7ad31057177810ca0161bf790e"]},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"finch": {:hex, :finch, "0.17.0", "17d06e1d44d891d20dbd437335eebe844e2426a0cd7e3a3e220b461127c73f70", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "8d014a661bb6a437263d4b5abf0bcbd3cf0deb26b1e8596f2a271d22e48934c7"},
Expand Down
86 changes: 86 additions & 0 deletions priv/repo/migrations/20240315115937_add_non_nullable_checks.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
defmodule Accent.Repo.Migrations.AddNonNullableChecks do
@moduledoc false
use Ecto.Migration

def down do
end

# credo:disable-for-next-line
def up do
drop(constraint(:documents, :documents_project_id_fkey))

alter table(:documents) do
modify(:path, :string, null: false)
modify(:format, :string, null: false)
modify(:project_id, references(:projects, type: :uuid), null: false)
end

alter table(:languages) do
modify(:name, :string, null: false)
modify(:slug, :string, null: false)
modify(:iso_639_1, :string, null: false)
modify(:iso_639_3, :string, null: false)
modify(:locale, :string, null: false)
modify(:android_code, :string, null: false)
modify(:osx_code, :string, null: false)
modify(:osx_locale, :string, null: false)
end

drop(constraint(:auth_access_tokens, :auth_access_tokens_user_id_fkey))

alter table(:auth_access_tokens) do
modify(:token, :string, null: false)
modify(:user_id, references(:users, type: :uuid), null: false)
end

drop(constraint(:auth_providers, :auth_providers_user_id_fkey))

alter table(:auth_providers) do
modify(:name, :string, null: false)
modify(:uid, :string, null: false)
modify(:user_id, references(:users, type: :uuid), null: false)
end

drop(constraint(:collaborators, :collaborators_project_id_fkey))

alter table(:collaborators) do
modify(:role, :string, null: false)
modify(:project_id, references(:projects, type: :uuid), null: false)
end

drop(constraint(:comments, :comments_user_id_fkey))

alter table(:comments) do
modify(:text, :text, null: false)
modify(:user_id, references(:users, type: :uuid), null: false)
end

alter table(:projects) do
modify(:name, :string, null: false)
modify(:locked_file_operations, :boolean, null: false, default: false)
modify(:sync_lock_version, :integer, null: false, default: 1)
end

drop(constraint(:revisions, :revisions_project_id_fkey))
drop(constraint(:revisions, :revisions_language_id_fkey))

alter table(:revisions) do
modify(:project_id, references(:projects, type: :uuid), null: false)
modify(:language_id, references(:languages, type: :uuid), null: false)
modify(:master, :boolean, null: false, default: true)
end

alter table(:operations) do
modify(:rollbacked, :boolean, null: false, default: false)
modify(:batch, :boolean, null: false, default: false)
modify(:action, :string, null: false)
end

alter table(:translations) do
modify(:key, :string, null: false)
modify(:removed, :boolean, null: false, default: false)
modify(:conflicted, :boolean, null: false, default: false)
modify(:comments_count, :integer, null: false, default: 0)
end
end
end
12 changes: 6 additions & 6 deletions test/auth/user_remote/authenticator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ defmodule AccentTest.UserRemote.Authenticator do
end

test "normalize collaborators with email" do
assigner = Repo.insert!(%User{email: "[email protected]"})
language = Repo.insert!(%Language{name: "french"})
project = Repo.insert!(%Project{main_color: "#f00", name: "My project", language_id: language.id})
assigner = Factory.insert(User, email: "[email protected]")
language = Factory.insert(Language)
project = Factory.insert(Project, language_id: language.id)

collaborator =
Repo.insert!(%Collaborator{
Expand All @@ -45,9 +45,9 @@ defmodule AccentTest.UserRemote.Authenticator do
end

test "normalize collaborators with uppercased email" do
assigner = Repo.insert!(%User{email: "[email protected]"})
language = Repo.insert!(%Language{name: "french"})
project = Repo.insert!(%Project{main_color: "#f00", name: "My project", language_id: language.id})
assigner = Factory.insert(User, email: "[email protected]")
language = Factory.insert(Language)
project = Factory.insert(Project, language_id: language.id)

collaborator =
Repo.insert!(%Collaborator{
Expand Down
8 changes: 4 additions & 4 deletions test/auth/user_remote/persister_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ defmodule AccentTest.UserRemote.Persister do
end

test "persist with existing user existing provider" do
existing_user = Repo.insert!(%User{email: @user.email})
Repo.insert!(%AuthProvider{name: @user.provider, uid: @user.uid})
existing_user = Factory.insert(User, email: @user.email)
Factory.insert(AuthProvider, name: @user.provider, uid: @user.uid)

user = Persister.persist(@user)

Expand All @@ -28,8 +28,8 @@ defmodule AccentTest.UserRemote.Persister do
end

test "persist with existing user new provider" do
existing_user = Repo.insert!(%User{email: @user.email})
Repo.insert!(%AuthProvider{name: "dummy", uid: @user.email})
existing_user = Factory.insert(User, email: @user.email)
Factory.insert(AuthProvider, name: "dummy", uid: @user.email)

user = Persister.persist(@user)

Expand Down
7 changes: 3 additions & 4 deletions test/auth/user_remote/token_giver_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ defmodule AccentTest.UserRemote.TokenGiver do
alias Accent.User
alias Accent.UserRemote.TokenGiver

@user %User{email: "[email protected]"}
@token %AccessToken{revoked_at: nil, token: "1234"}

test "revoke existing token" do
user = Repo.insert!(@user)
user = Factory.insert(User)
token = Repo.insert!(Map.put(@token, :user_id, user.id))

existing_revoked_token =
Repo.insert!(%AccessToken{token: "revoked", revoked_at: NaiveDateTime.utc_now(:second), user_id: user.id})
Factory.insert(AccessToken, token: "revoked", revoked_at: NaiveDateTime.utc_now(:second), user_id: user.id)

TokenGiver.grant_token(user)

Expand All @@ -27,7 +26,7 @@ defmodule AccentTest.UserRemote.TokenGiver do
end

test "create token" do
user = Repo.insert!(@user)
user = Factory.insert(User)

TokenGiver.grant_token(user)

Expand Down
Loading

0 comments on commit 436bc13

Please sign in to comment.