Skip to content

Commit

Permalink
add types checker; fix typing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Jan 5, 2024
1 parent d78e33d commit ae2b15d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ lint:
@ruff check --select I --fix .
@ruff format .
@ruff check .
@pyright .

test:
@pytest -s --cov=twscrape tests/

check:
@make lint
@make test

test-cov:
@pytest -s --cov=twscrape tests/
@coverage html
Expand Down
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ dependencies = [

[project.optional-dependencies]
dev = [
"pyright>=1.1.344",
"pytest-asyncio>=0.23.3",
"pytest-cov>=4.1.0",
"pytest-httpx>=0.28.0",
"pytest>=7.4.4",
"ruff>=0.1.11"
"ruff>=0.1.11",
]

[project.urls]
Expand All @@ -58,5 +59,3 @@ line-length = 99

[tool.ruff.lint]
ignore = ["E501"]

[tool.ruff.format]
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


@pytest.fixture
def pool_mock(tmp_path) -> AccountsPool:
def pool_mock(tmp_path):
db_path = tmp_path / "test.db"
yield AccountsPool(db_path) # type: ignore
yield AccountsPool(db_path)


@pytest.fixture
Expand Down
14 changes: 11 additions & 3 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
set_log_level("DEBUG")


class FakeRep:
text: str

def __init__(self, text: str):
self.text = text

def json(self):
return json.loads(self.text)


def load_mock(name: str):
file = os.path.join(os.path.dirname(__file__), f"mocked-data/{name}.json")
with open(file) as f:
Expand All @@ -28,9 +38,7 @@ def fake_rep(fn: str, filename: str):
with open(filename) as fp:
data = fp.read()

rep = lambda: None # noqa: E731
rep.text = data
rep.json = lambda: json.loads(data)
rep = FakeRep(data)
return rep


Expand Down
4 changes: 4 additions & 0 deletions tests/test_queue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ async def test_do_not_switch_account_on_200(httpx_mock: HTTPXMock, client_fixtur
for x in range(1):
httpx_mock.add_response(url=URL, json={"foo": x}, status_code=200)
rep = await client.get(URL)
assert rep is not None
assert rep.json() == {"foo": x}

# account should not be switched
Expand All @@ -82,6 +83,7 @@ async def test_switch_acc_on_http_error(httpx_mock: HTTPXMock, client_fixture: C
httpx_mock.add_response(url=URL, json={"foo": "2"}, status_code=200)

rep = await client.get(URL)
assert rep is not None
assert rep.json() == {"foo": "2"}

locked2 = await get_locked(pool)
Expand All @@ -107,6 +109,7 @@ async def test_retry_with_same_acc_on_network_error(httpx_mock: HTTPXMock, clien
httpx_mock.add_response(url=URL, json={"foo": "2"}, status_code=200)

rep = await client.get(URL)
assert rep is not None
assert rep.json() == {"foo": "2"}

locked2 = await get_locked(pool)
Expand Down Expand Up @@ -141,6 +144,7 @@ async def get_data_stream():
elif before_ctx is not None:
assert before_ctx == c.ctx

assert rep is not None
assert rep.json() == {"counter": counter}
yield rep.json()["counter"]

Expand Down
2 changes: 1 addition & 1 deletion twscrape/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def main(args):
api = API(pool, debug=args.debug)

if args.command == "accounts":
print_table(await pool.accounts_info())
print_table([dict(x) for x in await pool.accounts_info()])
return

if args.command == "stats":
Expand Down
6 changes: 4 additions & 2 deletions twscrape/queue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ async def _close_ctx(self, reset_at=-1, inactive=False, msg: str | None = None):

await self.pool.unlock(ctx.acc.username, self.queue, ctx.req_count)

async def _get_ctx(self) -> Ctx:
async def _get_ctx(self):
if self.ctx:
return self.ctx

acc = await self.pool.get_for_queue_or_wait(self.queue)
if acc is None:
return None

clt = acc.make_client()
self.ctx = Ctx(acc, clt)
return self.ctx
Expand Down Expand Up @@ -129,7 +132,6 @@ async def _check_rep(self, rep: httpx.Response) -> None:
err_msg = "; ".join(list(err_msg))

log_msg = f"{rep.status_code:3d} - {req_id(rep)} - {err_msg}"
print(log_msg)
logger.trace(log_msg)

# for dev: need to add some features in api.py
Expand Down

0 comments on commit ae2b15d

Please sign in to comment.