Skip to content

Commit

Permalink
nore in name that unicode file has null bytes; and hatch fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
macpd committed Nov 21, 2024
1 parent 29e4956 commit 10a18c7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
32 changes: 24 additions & 8 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,39 @@ def file_contents(filename):


@pytest.fixture
def testdata_api_videos_response_file_contents():
return file_contents("tests/testdata/api_videos_response.json")
def testdata_api_videos_response_page_1_of_2_file_contents():
return file_contents("tests/testdata/api_videos_response_page_1_of_2.json")


@pytest.fixture
def testdata_api_videos_response_json(testdata_api_videos_response_file_contents):
return json.loads(testdata_api_videos_response_file_contents)
def testdata_api_videos_response_page_1_of_2_json(
testdata_api_videos_response_page_1_of_2_file_contents,
):
return json.loads(testdata_api_videos_response_page_1_of_2_file_contents)


@pytest.fixture
def testdata_api_videos_response_unicode_file_contents():
return file_contents("tests/testdata/api_videos_response_unicode.json")
def testdata_api_videos_response_page_2_of_2_file_contents():
return file_contents("tests/testdata/api_videos_response_page_2_of_2.json")


@pytest.fixture
def testdata_api_videos_response_unicode_json(testdata_api_videos_response_unicode_file_contents):
return json.loads(testdata_api_videos_response_unicode_file_contents)
def testdata_api_videos_response_page_2_of_2_json(
testdata_api_videos_response_page_2_of_2_file_contents,
):
return json.loads(testdata_api_videos_response_page_2_of_2_file_contents)


@pytest.fixture
def testdata_api_videos_response_unicode_with_null_bytes_file_contents():
return file_contents("tests/testdata/api_videos_response_unicode_with_null_bytes.json")


@pytest.fixture
def testdata_api_videos_response_unicode_with_null_bytes_json(
testdata_api_videos_response_unicode_with_null_bytes_file_contents,
):
return json.loads(testdata_api_videos_response_unicode_with_null_bytes_file_contents)


@pytest.fixture
Expand Down
14 changes: 8 additions & 6 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def test_tiktok_request_client_removes_null_chars(
basic_video_query,
mocked_access_token_fetch,
mocked_video_responses,
testdata_api_videos_response_unicode_json,
testdata_api_videos_response_unicode_with_null_bytes_json,
):
request_client = api_client.TikTokApiRequestClient.from_credentials_file(
FAKE_SECRETS_YAML_FILE,
Expand Down Expand Up @@ -848,17 +848,19 @@ def test_TikTokVideoRequest_as_json(basic_video_query_config):
}


def test_NullByteRemovingJSONDencoder(testdata_api_videos_response_unicode_file_contents):
def test_NullByteRemovingJSONDencoder(
testdata_api_videos_response_unicode_with_null_bytes_file_contents,
):
assert (
"\x00"
in json.loads(testdata_api_videos_response_unicode_file_contents)["data"]["videos"][0][
"username"
]
in json.loads(testdata_api_videos_response_unicode_with_null_bytes_file_contents)["data"][
"videos"
][0]["username"]
)
assert (
"\x00"
not in json.loads(
testdata_api_videos_response_unicode_file_contents,
testdata_api_videos_response_unicode_with_null_bytes_file_contents,
cls=api_client.NullByteRemovingJSONDencoder,
)["data"]["videos"][0]["username"]
)
10 changes: 6 additions & 4 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@ def mocked_access_token_responses(responses_mock):


@pytest.fixture
def mocked_video_responses(responses_mock, testdata_api_videos_response_unicode_json):
def mocked_video_responses(
responses_mock, testdata_api_videos_response_unicode_with_null_bytes_json
):
return responses_mock.post(
re.compile("https://open.tiktokapis.com/v2/*"),
json=testdata_api_videos_response_unicode_json,
json=testdata_api_videos_response_unicode_with_null_bytes_json,
)


def test_tiktok_request_client_removes_null_chars(
basic_video_query,
basic_video_query_config,
basic_acquisition_config,
testdata_api_videos_response_unicode_json,
testdata_api_videos_response_unicode_with_null_bytes_json,
test_database_engine,
mocked_access_token_responses,
mocked_video_responses,
Expand All @@ -66,5 +68,5 @@ def test_tiktok_request_client_removes_null_chars(
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"]
testdata_api_videos_response_unicode_with_null_bytes_json["data"]["videos"][0]["id"]
]

0 comments on commit 10a18c7

Please sign in to comment.