From d7a6c3a2df217b5f94f6f4bf94208b90ea25b53d Mon Sep 17 00:00:00 2001 From: Joshua Augustinus Date: Sun, 9 Jun 2024 06:35:50 +1000 Subject: [PATCH] Update after geekingfrog review Remove context --- lib/teiserver/battle/libs/balance_lib.ex | 19 ++---- mix.exs | 1 - .../battle/balance_lib_internal_test.exs | 63 +++++++++++-------- 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/lib/teiserver/battle/libs/balance_lib.ex b/lib/teiserver/battle/libs/balance_lib.ex index 07c560c2c..3a7b760d7 100644 --- a/lib/teiserver/battle/libs/balance_lib.ex +++ b/lib/teiserver/battle/libs/balance_lib.ex @@ -163,19 +163,12 @@ defmodule Teiserver.Battle.BalanceLib do groups |> Enum.map(fn group -> # Iterate over our map - {_ignored, better_map} = - Enum.map_reduce(group, %{}, fn {user_id, value}, acc -> - fixed_value = - cond do - # We're missing data so need to fetch it - is_number(value) -> get_user_rating_rank_old(user_id, value) - true -> value - end - - {nil, Map.put(acc, user_id, fixed_value)} - end) - - better_map + Map.new(group, fn {user_id, value} -> + cond do + is_number(value) -> {user_id, get_user_rating_rank_old(user_id, value)} + true -> {user_id, value} + end + end) end) end diff --git a/mix.exs b/mix.exs index d37a47be1..241e998fb 100644 --- a/mix.exs +++ b/mix.exs @@ -91,7 +91,6 @@ defmodule Teiserver.MixProject do {:libcluster, "~> 3.3"}, {:tzdata, "~> 1.1"}, {:ex_ulid, "~> 0.1.0"}, - {:mock, "~> 0.3.0", only: :test}, # Teiserver libs {:openskill, git: "https://github.com/StanczakDominik/openskill.ex.git", branch: "master"}, diff --git a/test/teiserver/battle/balance_lib_internal_test.exs b/test/teiserver/battle/balance_lib_internal_test.exs index c0bfbffa4..173f5737b 100644 --- a/test/teiserver/battle/balance_lib_internal_test.exs +++ b/test/teiserver/battle/balance_lib_internal_test.exs @@ -3,36 +3,30 @@ defmodule Teiserver.Battle.BalanceLibInternalTest do Can run all balance tests via mix test --only balance_test """ - use ExUnit.Case - import Mock + use Teiserver.DataCase, async: true @moduletag :balance_test alias Teiserver.Battle.BalanceLib - setup_with_mocks([ - {Teiserver.Account, [:passthrough], - [get_user_by_id: fn member_id -> get_user_by_id_mock(member_id) end]} - ]) do - :ok - end - test "Able to standardise groups with incomplete data" do + [user1, user2, user3, user4, user5] = create_test_users() + groups = [ - %{1 => 19, 2 => 20}, - %{3 => 18}, - %{4 => 15}, - %{5 => 11} + %{user1.id => 19, user2.id => 20}, + %{user3.id => 18}, + %{user4.id => 15}, + %{user5.id => 11} ] fixed_groups = BalanceLib.standardise_groups(groups) assert fixed_groups == [ %{ - 1 => %{name: "User_1", rank: 0, rating: 19}, - 2 => %{name: "User_2", rank: 0, rating: 20} + user1.id => %{name: "User_1", rank: 0, rating: 19}, + user2.id => %{name: "User_2", rank: 0, rating: 20} }, - %{3 => %{name: "User_3", rank: 0, rating: 18}}, - %{4 => %{name: "User_4", rank: 0, rating: 15}}, - %{5 => %{name: "User_5", rank: 0, rating: 11}} + %{user3.id => %{name: "User_3", rank: 0, rating: 18}}, + %{user4.id => %{name: "User_4", rank: 0, rating: 15}}, + %{user5.id => %{name: "User_5", rank: 0, rating: 11}} ] # loser_picks algo will hit the databases so let's just test with split_one_chevs @@ -40,12 +34,29 @@ defmodule Teiserver.Battle.BalanceLibInternalTest do assert result != nil end - test "Handle groups with incomplete data in create_balance" do + test "Handle groups with incomplete data in create_balance loser_pics" do + [user1, user2, user3, user4, user5] = create_test_users() + + groups = [ + %{user1.id => 19, user2.id => 20}, + %{user3.id => 18}, + %{user4.id => 15}, + %{user5.id => 11} + ] + + # loser_picks algo will hit the databases so let's just test with split_one_chevs + result = BalanceLib.create_balance(groups, 2, algorithm: "loser_picks") + assert result != nil + end + + test "Handle groups with incomplete data in create_balance split_one_chevs" do + [user1, user2, user3, user4, user5] = create_test_users() + groups = [ - %{1 => 19, 2 => 20}, - %{3 => 18}, - %{4 => 15}, - %{5 => 11} + %{user1.id => 19, user2.id => 20}, + %{user3.id => 18}, + %{user4.id => 15}, + %{user5.id => 11} ] # loser_picks algo will hit the databases so let's just test with split_one_chevs @@ -53,7 +64,9 @@ defmodule Teiserver.Battle.BalanceLibInternalTest do assert result != nil end - defp get_user_by_id_mock(user_id) do - %{rank: 0, name: "User_#{user_id}"} + defp create_test_users do + Enum.map(1..5, fn k -> + Teiserver.TeiserverTestLib.new_user("User_#{k}") + end) end end