From 0f3725940b9d8460f9d354fd32a935ed217f48a6 Mon Sep 17 00:00:00 2001 From: Victor Martinez <49537445+JasterV@users.noreply.github.com> Date: Fri, 29 Mar 2024 22:32:40 +0100 Subject: [PATCH] write tests for the session server --- test/intisync/session_server_test.exs | 74 +++++++++++++++++++++++++++ test/support/conn_case.ex | 7 --- 2 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 test/intisync/session_server_test.exs diff --git a/test/intisync/session_server_test.exs b/test/intisync/session_server_test.exs new file mode 100644 index 0000000..0e68a4b --- /dev/null +++ b/test/intisync/session_server_test.exs @@ -0,0 +1,74 @@ +defmodule Intisync.SessionServerTest do + use ExUnit.Case, async: true + + alias Intisync.SessionServer + + setup do + session_id = Intisync.Puid.generate() + pid = start_supervised!({SessionServer, {session_id}}) + %{session_server_pid: pid, session_id: session_id} + end + + test "A session starts with the proper initial state", %{ + session_server_pid: pid, + session_id: session_id + } do + assert SessionServer.get_id(pid) == session_id + assert SessionServer.get_devices(pid) == %{} + assert SessionServer.full?(pid) + end + + test "Calling device_connected adds the device to the state", %{ + session_server_pid: pid + } do + index = 0 + device = %{name: "Dummy", vibration: 0} + assert SessionServer.device_connected(pid, index, device) == :ok + assert SessionServer.get_devices(pid) == %{index => device} + end + + test "Calling device_disconnected removes the device to the state", %{ + session_server_pid: pid + } do + index = 0 + device = %{name: "Dummy", vibration: 0} + assert SessionServer.device_connected(pid, index, device) == :ok + assert SessionServer.device_disconnected(pid, index) == :ok + assert SessionServer.get_devices(pid) == %{} + end + + test "Calling remote_connected sets the session as full", %{ + session_server_pid: pid + } do + SessionServer.remote_connected(pid) + assert SessionServer.full?(pid) + end + + test "Calling remote_disconnected sets the session as not full", %{ + session_server_pid: pid + } do + SessionServer.remote_connected(pid) + assert SessionServer.full?(pid) + SessionServer.remote_disconnected(pid) + refute SessionServer.full?(pid) + end + + test "Calling empty_devices resets the devices state", %{ + session_server_pid: pid + } do + assert SessionServer.device_connected(pid, 0, %{name: "Dummy", vibration: 0}) + assert SessionServer.device_connected(pid, 1, %{name: "Dummy2", vibration: 0}) + assert map_size(SessionServer.get_devices(pid)) == 2 + assert SessionServer.empty_devices(pid) == :ok + assert SessionServer.get_devices(pid) == %{} + end + + test "Calling vibrate device updates the vibration of that device", %{ + session_server_pid: pid + } do + assert SessionServer.device_connected(pid, 0, %{name: "Dummy", vibration: 0}) + assert SessionServer.device_connected(pid, 1, %{name: "Dummy2", vibration: 0}) + assert SessionServer.vibrate_device(pid, 0, 45) == :ok + assert %{0 => %{vibration: 45}, 1 => %{vibration: 0}} = SessionServer.get_devices(pid) + end +end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 99917fa..b31aa88 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -6,13 +6,6 @@ defmodule IntisyncWeb.ConnCase do Such tests rely on `Phoenix.ConnTest` and also import other functionality to make it easier to build common data structures and query the data layer. - - Finally, if the test case interacts with the database, - we enable the SQL sandbox, so changes done to the database - are reverted at the end of every test. If you are using - PostgreSQL, you can even run database tests asynchronously - by setting `use IntisyncWeb.ConnCase, async: true`, although - this option is not recommended for other databases. """ use ExUnit.CaseTemplate