Skip to content

Commit

Permalink
Fix more tests (beyond-all-reason#522)
Browse files Browse the repository at this point in the history
* Fix the tests related to rename error messages

* Disable call to geoip command in tests

* Refactor: extract the function to clear a given cache

* Avoid cache between tests leading to foreign key errors

The usual issue when caches are used so much.

* Fix spec for update_site_config
  • Loading branch information
geekingfrog authored Nov 9, 2024
1 parent 91b1b3a commit 9e8ad71
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/teiserver/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ defmodule Teiserver.Config do
Repo.one(query)
end

@spec update_site_config(String.t(), String.t()) :: :ok
@spec update_site_config(String.t(), term()) :: :ok
def update_site_config(key, value) do
query =
from site_config in SiteConfig,
Expand Down
1 change: 1 addition & 0 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ defmodule TeiserverWeb.ConnCase do
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Teiserver.Repo)
Teiserver.TeiserverTestLib.clear_all_con_caches()
Teiserver.Config.update_site_config("system.Use geoip", false)
on_exit(&Teiserver.TeiserverTestLib.clear_all_con_caches/0)

unless tags[:async] do
Expand Down
1 change: 1 addition & 0 deletions test/support/server_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ defmodule Teiserver.ServerCase do
# more false failures.
Teiserver.TeiserverTestLib.clear_all_con_caches()
Teiserver.DataCase.setup_sandbox(tags)
Teiserver.Config.update_site_config("system.Use geoip", false)
on_exit(&Teiserver.TeiserverTestLib.clear_all_con_caches/0)
:ok
end
Expand Down
14 changes: 8 additions & 6 deletions test/support/teiserver_test_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,18 @@ defmodule Teiserver.TeiserverTestLib do
:telemetry_simple_server_event_types_cache
]

Enum.each(cache_list, fn cache ->
cache
|> ConCache.ets()
|> :ets.tab2list()
|> Enum.each(fn {key, _} -> ConCache.delete(cache, key) end)
end)
Enum.each(cache_list, &clear_cache/1)

:ok
end

def clear_cache(cache) do
cache
|> ConCache.ets()
|> :ets.tab2list()
|> Enum.each(fn {key, _} -> ConCache.delete(cache, key) end)
end

def seed_matchmaking_queues() do
alias Teiserver.Game

Expand Down
19 changes: 7 additions & 12 deletions test/teiserver/data/user_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ defmodule Teiserver.Data.UserTest do
test "renaming" do
user = TeiserverTestLib.new_user()

expected_rename_error =
{:error, "Rename limit reached (2 times in 5 days or 3 times in 30 days)"}

assert CacheUser.rename_user(user.id, "rename1") == :success
assert CacheUser.rename_user(user.id, "rename2") == :success

assert CacheUser.rename_user(user.id, "rename3") ==
{:error,
"If you keep changing your name people won't know who you are; give it a bit of time (5 days)"}
assert CacheUser.rename_user(user.id, "rename3") == expected_rename_error

# Lets make it so they can do it again
Account.update_user_stat(user.id, %{
Expand All @@ -86,9 +87,7 @@ defmodule Teiserver.Data.UserTest do
assert CacheUser.rename_user(user.id, "rename4") == :success
assert CacheUser.rename_user(user.id, "rename44") == :success

assert CacheUser.rename_user(user.id, "rename5") ==
{:error,
"If you keep changing your name people won't know who you are; give it a bit of time (5 days)"}
assert CacheUser.rename_user(user.id, "rename5") == expected_rename_error

# What if they've done it many times before but nothing recent?
Account.update_user_stat(user.id, %{
Expand All @@ -98,9 +97,7 @@ defmodule Teiserver.Data.UserTest do
assert CacheUser.rename_user(user.id, "rename6") == :success
assert CacheUser.rename_user(user.id, "rename66") == :success

assert CacheUser.rename_user(user.id, "rename7") ==
{:error,
"If you keep changing your name people won't know who you are; give it a bit of time (5 days)"}
assert CacheUser.rename_user(user.id, "rename7") == expected_rename_error

# Nothing in the last 15 days but enough in the last 30
now = System.system_time(:second)
Expand All @@ -114,9 +111,7 @@ defmodule Teiserver.Data.UserTest do
]
})

assert CacheUser.rename_user(user.id, "rename8") ==
{:error,
"If you keep changing your name people won't know who you are; give it a bit of time (30 days)"}
assert CacheUser.rename_user(user.id, "rename8") == expected_rename_error
end

test "valid_email?" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule TeiserverWeb.API.SpadsControllerTest do

test "existing user", %{conn: conn} do
user = new_user()
Teiserver.TeiserverTestLib.clear_cache(:teiserver_game_rating_types)
rating_type_id = MatchRatingLib.rating_type_name_lookup()["Team"]

{:ok, _} =
Expand Down

0 comments on commit 9e8ad71

Please sign in to comment.