113 lines
3.2 KiB
Python
113 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
import io
|
|
import json
|
|
import sys
|
|
import contextlib
|
|
import importlib
|
|
|
|
|
|
def _run(mod, argv):
|
|
old = sys.argv[:]
|
|
try:
|
|
sys.argv = argv
|
|
buf = io.StringIO()
|
|
with contextlib.redirect_stdout(buf):
|
|
rc = mod.main()
|
|
return rc, json.loads(buf.getvalue())
|
|
finally:
|
|
sys.argv = old
|
|
|
|
|
|
def test_recommend_smsvn_filters_and_sorts(monkeypatch):
|
|
mod = importlib.import_module("scripts.sms_activation")
|
|
|
|
class FakeClient:
|
|
def services(self, country):
|
|
assert country == 12
|
|
return [
|
|
{"id": "am", "name": "Amazon", "price": 0.16, "quantity": 100, "deliverability": 50},
|
|
{"id": "go", "name": "Google", "price": 0.04, "quantity": 200, "deliverability": 12},
|
|
{"id": "tg", "name": "Telegram", "price": 0.74, "quantity": 1000, "deliverability": 95},
|
|
{"id": "xx", "name": "LowQty", "price": 0.01, "quantity": 0, "deliverability": 99},
|
|
]
|
|
|
|
monkeypatch.setattr(mod, "get_client", lambda provider: FakeClient())
|
|
monkeypatch.setattr(mod, "read_state", lambda path: {})
|
|
|
|
rc, out = _run(
|
|
mod,
|
|
[
|
|
"sms_activation.py",
|
|
"--provider",
|
|
"smsvn",
|
|
"recommend",
|
|
"--country",
|
|
"12",
|
|
"--max-price",
|
|
"0.2",
|
|
"--min-qty",
|
|
"1",
|
|
"--top-n",
|
|
"2",
|
|
],
|
|
)
|
|
assert rc == 0
|
|
recs = out["recommendations"]
|
|
assert len(recs) == 2
|
|
assert recs[0]["service"] == "go"
|
|
assert recs[1]["service"] == "am"
|
|
|
|
|
|
def test_recommend_5sim_filters_and_service(monkeypatch):
|
|
mod = importlib.import_module("scripts.sms_activation")
|
|
|
|
class FakeClient:
|
|
def guest_products(self, country, operator):
|
|
assert country == "usa"
|
|
assert operator == "any"
|
|
return {
|
|
"amazon": {"Category": "activation", "Qty": 100, "Price": 0.13},
|
|
"telegram": {"Category": "activation", "Qty": 2, "Price": 0.74},
|
|
"bad": {"Category": "activation", "Qty": 0, "Price": 0.01},
|
|
}
|
|
|
|
monkeypatch.setattr(mod, "get_client", lambda provider: FakeClient())
|
|
monkeypatch.setattr(mod, "read_state", lambda path: {})
|
|
|
|
rc, out = _run(
|
|
mod,
|
|
[
|
|
"sms_activation.py",
|
|
"--provider",
|
|
"5sim",
|
|
"recommend",
|
|
"--country",
|
|
"usa",
|
|
"--operator",
|
|
"any",
|
|
"--service",
|
|
"amazon",
|
|
"--min-qty",
|
|
"10",
|
|
],
|
|
)
|
|
assert rc == 0
|
|
recs = out["recommendations"]
|
|
assert len(recs) == 1
|
|
assert recs[0]["service"] == "amazon"
|
|
|
|
|
|
def test_recommend_grizzly_not_supported(monkeypatch):
|
|
mod = importlib.import_module("scripts.sms_activation")
|
|
|
|
class FakeClient:
|
|
pass
|
|
|
|
monkeypatch.setattr(mod, "get_client", lambda provider: FakeClient())
|
|
monkeypatch.setattr(mod, "read_state", lambda path: {})
|
|
|
|
rc, out = _run(mod, ["sms_activation.py", "--provider", "grizzly", "recommend"])
|
|
assert rc == 0
|
|
assert out["error"] == "recommend_not_supported"
|