Skip to content

Commit

Permalink
hatch fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
macpd committed Nov 20, 2024
1 parent e92275a commit 2cd2df7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
6 changes: 4 additions & 2 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
select,
)

from tiktok_research_api_helper import query
from tiktok_research_api_helper.api_client import ApiClientConfig, VideoQueryConfig
from tiktok_research_api_helper.models import (
Base,
Crawl,
Expand All @@ -21,13 +23,12 @@
Video,
get_engine_and_create_tables,
)
from tiktok_research_api_helper.api_client import (ApiClientConfig, VideoQueryConfig)
from tiktok_research_api_helper import query

_IN_MEMORY_SQLITE_DATABASE_URL = "sqlite://"

FAKE_SECRETS_YAML_FILE = Path("tests/testdata/fake_secrets.yaml")


@pytest.fixture
def test_database_engine(database_url_command_line_arg) -> Engine:
if database_url_command_line_arg:
Expand Down Expand Up @@ -91,6 +92,7 @@ def all_videos(session):
def all_crawls(session):
return session.scalars(select(Crawl)).all()


@pytest.fixture
def basic_acquisition_config():
return ApiClientConfig(
Expand Down
20 changes: 11 additions & 9 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
import json
import re
import unittest
from pathlib import Path
from unittest.mock import MagicMock, Mock, PropertyMock, call

import pendulum
import pytest
import responses
import requests
import responses
from sqlalchemy.orm import Session

from tests.common import (
FAKE_SECRETS_YAML_FILE,
all_crawls,
all_videos,
FAKE_SECRETS_YAML_FILE,
)
from tiktok_research_api_helper import api_client, query, utils

from tiktok_research_api_helper import api_client, utils

# TODO(macpd): use response library to mock out requests to API such that they return contents of
# "tests/testdata/api_videos_response_unicode.json"
Expand Down Expand Up @@ -417,23 +415,27 @@ def test_tiktok_api_client_api_results_iter(
assert mock_tiktok_request_client.fetch_videos.call_count == len(mock_tiktok_video_responses)
assert mock_tiktok_request_client.fetch_videos.mock_calls == expected_fetch_video_calls


@responses.activate
def test_tiktok_request_client_removes_null_chars(
basic_video_query,
mock_access_token_fetcher_session,
testdata_api_videos_response_unicode_json,
):
responses.post(re.compile('https://open.tiktokapis.com/v2/*'),
json=testdata_api_videos_response_unicode_json)
responses.post(
re.compile("https://open.tiktokapis.com/v2/*"),
json=testdata_api_videos_response_unicode_json,
)
request_client = api_client.TikTokApiRequestClient.from_credentials_file(
FAKE_SECRETS_YAML_FILE,
api_request_session=requests.Session(),
access_token_fetcher_session=mock_access_token_fetcher_session,
)
response = request_client.fetch_videos(
api_client.TikTokVideoRequest(query=basic_video_query, start_date=None, end_date=None))
api_client.TikTokVideoRequest(query=basic_video_query, start_date=None, end_date=None)
)
assert response.videos
assert "\x00" not in response.videos[0]['username']
assert "\x00" not in response.videos[0]["username"]


@pytest.fixture
Expand Down
46 changes: 21 additions & 25 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
"""Tests integration of database storage of api client retrun values (using mocked http responses).
"""
import attrs
import pendulum
import pytest
"""Tests integration of database storage of api client retrun values (using mocked http responses)."""

import re

import attrs
import responses
from sqlalchemy import (
select,
)
from sqlalchemy.orm import Session

from tests.common import (
all_crawls,
all_hashtag_names_sorted,
all_hashtags,
all_videos,
FAKE_SECRETS_YAML_FILE,
)
from tiktok_research_api_helper import api_client
from tiktok_research_api_helper.models import (
Crawl,
CrawlTag,
Effect,
Hashtag,
Video,
upsert_videos,
)
from tiktok_research_api_helper import query


@responses.activate
Expand All @@ -37,22 +26,29 @@ def test_tiktok_request_client_removes_null_chars(
testdata_api_videos_response_unicode_json,
test_database_engine,
):
responses.post("https://open.tiktokapis.com/v2/oauth/token/", json= {
"access_token": "mock_access_token_1",
"expires_in": 7200,
"token_type": "Bearer",
})
responses.post(re.compile('https://open.tiktokapis.com/v2/*'),
json=testdata_api_videos_response_unicode_json)
responses.post(
"https://open.tiktokapis.com/v2/oauth/token/",
json={
"access_token": "mock_access_token_1",
"expires_in": 7200,
"token_type": "Bearer",
},
)
responses.post(
re.compile("https://open.tiktokapis.com/v2/*"),
json=testdata_api_videos_response_unicode_json,
)
request_client = api_client.TikTokApiRequestClient.from_credentials_file(
FAKE_SECRETS_YAML_FILE,
api_request_session=None,
access_token_fetcher_session=None,
)
client = api_client.TikTokApiClient(
request_client=request_client, config=attrs.evolve(basic_acquisition_config,
engine=test_database_engine)
request_client=request_client,
config=attrs.evolve(basic_acquisition_config, engine=test_database_engine),
)
client.fetch_and_store_all(basic_video_query_config)
with Session(test_database_engine) as session:
assert session.scalars(select(Video.id).order_by(Video.id)).all() == [testdata_api_videos_response_unicode_json['data']['videos'][0]['id']]
assert session.scalars(select(Video.id).order_by(Video.id)).all() == [
testdata_api_videos_response_unicode_json["data"]["videos"][0]["id"]
]

0 comments on commit 2cd2df7

Please sign in to comment.