-
Notifications
You must be signed in to change notification settings - Fork 1
/
studio54_test.exs
128 lines (108 loc) · 3.84 KB
/
studio54_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
defmodule Studio54Test do
use ExUnit.Case, async: false
import ExUnit.CaptureLog
doctest Studio54
alias Studio54.Starter, as: Starter
alias Studio54.Worker, as: Worker
alias Studio54.Db, as: Db
alias Studio54.DbSetup, as: DbSetup
setup_all do
{status, pid} = Starter.start_link(%{})
assert status == :ok
assert is_pid(pid)
:ok
on_exit(fn ->
{status, result} = Studio54.empty_index()
assert status == :ok or status == :error
assert result == true or Regex.match?(~r/\d+/, result)
[{_, worker_pid}] = Registry.lookup(Studio54.Processes, "worker")
state = :sys.get_state(pid)
assert state.worker == worker_pid
capture_log(fn ->
Process.exit(worker_pid, :error)
end) =~ "went down"
end)
end
describe "database" do
test "adding incomming messages is successful" do
assert {:atomic, :ok} == Db.add_incomming_message("989120228207")
end
test "adding message event working" do
{:ok, idx} = Db.add_message_event("989120228207", 3, IO, :inspect, "\\d{5}")
{:atomic, ev} = Db.get_message_event(idx)
assert elem(ev, 1) == idx
{:ok, idx} = Db.add_message_event("989120228207", 3, IO, :inspect)
end
test "reading messages table is ok" do
{:atomic, :ok} = Db.add_incomming_message("989120228207")
{:ok, msgs} = Db.read_incomming_messages_from("989120228207")
end
test "updating message event result is ok" do
{:atomic, :ok} = Db.add_incomming_message("989120228207")
{:ok, e_idx} = Db.add_message_event("989120228207", 3, IO, :inspect, "\\d{5}")
assert {:atomic, :ok} == Db.update_message_event_result(e_idx, %{})
{:atomic, ev} = Db.get_message_event(e_idx)
assert elem(ev, 11) == %{}
{:atomic, ev} = Db.get_message_event(e_idx)
end
test "retiring events works correct" do
{:ok, e_idx} = Db.add_message_event("989120228207", 300, IO, :inspect, "\\d{5}")
{:atomic, ev} = Db.get_message_event(e_idx)
assert elem(ev, 8) == false
{:atomic, :ok} = Db.retire_message_event(e_idx)
{:atomic, ev} = Db.get_message_event(e_idx)
assert elem(ev, 8) == true
{:ok, e_idx} = Db.add_message_event("989120228207", 1, IO, :inspect, "\\d{5}")
Process.sleep(2000)
:ok = Db.retire_expired_message_events()
end
end
describe "when application starts -> " do
test "worker gets inbox correctly" do
[{_, worker_pid}] = Registry.lookup(Studio54.Processes, "worker")
#
end
end
describe "general tools -> " do
test "special hash is correct" do
target = "c34045c1a1db8d1b3fca8a692198466952daae07eaf6104b4c87ed3b55b6af1b"
assert Studio54.gethash("cool") == target
end
test "fetchig message count is correct" do
{:ok, new_count} = Studio54.get_new_count()
assert is_integer(new_count)
end
test "msisdn normalization" do
assert_raise MatchError, fn -> Studio54.normalize_msisdn("") end
assert Studio54.normalize_msisdn("RighTel") == "rightel"
assert Studio54.normalize_msisdn(" 989120228207") == "9120228207"
end
end
describe "sms core tools -> " do
setup do
:ok
end
test "sending sms is successful" do
message_code = UUID.uuid4()
{:ok, true} =
Studio54.send_sms(98_307_000, "test message send from Studio54\n#{message_code}")
# check the outbox
{:ok, count, msgs} = Studio54.get_outbox()
lma = msgs |> List.first()
lm = Studio54.get_last_message_from(98_307_000)
assert lm == nil or is_map(lm)
assert lma == nil or is_map(lma)
assert is_list(msgs)
assert is_integer(count)
end
end
describe "USSD tools -> " do
setup do
:ok
end
test "sending ussd is successful" do
{:ok, result} = Studio54.send_ussd("*800*1#")
assert result =~ "مشترک گرامی"
end
end
end