aboutsummaryrefslogtreecommitdiff
path: root/test/mod_last_mock.exs
diff options
context:
space:
mode:
Diffstat (limited to 'test/mod_last_mock.exs')
-rw-r--r--test/mod_last_mock.exs65
1 files changed, 65 insertions, 0 deletions
diff --git a/test/mod_last_mock.exs b/test/mod_last_mock.exs
new file mode 100644
index 000000000..7e3dc5a1d
--- /dev/null
+++ b/test/mod_last_mock.exs
@@ -0,0 +1,65 @@
+ # mod_last mock
+ ######################
+
+
+defmodule ModLastMock do
+
+ require Record
+ Record.defrecord :session, Record.extract(:session,
+ from: "ejabberd_sm.hrl")
+ Record.defrecord :jid, Record.extract(:jid,
+ from: "jlib.hrl")
+
+ @author "jsautret@process-one.net"
+ @agent __MODULE__
+
+ def init do
+ try do
+ Agent.stop(@agent)
+ catch
+ :exit, _e -> :ok
+ end
+
+ {:ok, _pid} = Agent.start_link(fn -> %{} end, name: @agent)
+
+ mock(:mod_last, :get_last_info,
+ fn (user, domain) ->
+ Agent.get(@agent, fn last ->
+ case Map.get(last, {user, domain}, :not_found) do
+ {ts, status} -> {:ok, ts, status}
+ result -> result
+ end
+ end)
+ end)
+ end
+
+ def set_last(user, domain, status) do
+ set_last(user, domain, status, now)
+ end
+
+ def set_last(user, domain, status, timestamp) do
+ Agent.update(@agent, fn last ->
+ Map.put(last, {user, domain}, {timestamp, status})
+ end)
+ end
+
+ ####################################################################
+ # Helpers
+ ####################################################################
+ def now() do
+ {megasecs, secs, _microsecs} = :os.timestamp
+ megasecs * 1000000 + secs
+ end
+
+ # TODO refactor: Move to ejabberd_test_mock
+ def mock(module, function, fun) do
+ try do
+ :meck.new(module)
+ catch
+ :error, {:already_started, _pid} -> :ok
+ end
+
+ :meck.expect(module, function, fun)
+ end
+
+end