<commit_before># -*- encoding: utf-8 -*-

import os
from uuid import UUID

import pytest

import archive_start_ingest as archive_ingest


TABLE_NAME = 'archive-storage-progress-table'


def test_post_sends_location_to_sns(sns_client, topic_arn):
    request = ingest_request(upload_url='s3://wellcomecollection-assets-archive-ingest/test-bag.zip')

    response = archive_ingest.main(event=request, sns_client=sns_client)

    id = str(UUID(response['id']))
    assert id

    assert response['location'] == f"/ingests/{id}"

    messages = sns_client.list_messages()
    assert len(messages) == 1
    assert messages[0][':message'] == {
        'archiveRequestId': id,
        'bagLocation': {
            'namespace': 'wellcomecollection-assets-archive-ingest',
            'key': 'test-bag.zip'
        }
    }


def test_sends_request_to_sns_with_callback(sns_client, topic_arn):
    request = ingest_request(upload_url='s3://wellcomecollection-assets-archive-ingest/test-bag.zip',
                             callback_url='https://workflow.wellcomecollection.org/callback?id=b1234567')

    response = archive_ingest.main(event=request, sns_client=sns_client)

    actual_id = str(UUID(response['id']))
    assert actual_id

    messages = sns_client.list_messages()
    assert len(messages) == 1
    assert messages[0][':message'] == {
        'archiveRequestId': actual_id,
        'bagLocation': {
            'namespace': 'wellcomecollection-assets-archive-ingest',
            'key': 'test-bag.zip'
        },
        'callbackUrl': 'https://workflow.wellcomecollection.org/callback?id=b1234567'
    }


def test_invalid_url_fails(sns_client):
    request = ingest_request('invalidUrl')

    with pytest.raises(ValueError, match="\[BadRequest\] Unrecognised url scheme: invalid"):
        archive_ingest.main(event=request, sns_client=sns_client)

    assert len(sns_client.list_messages()) == 0


def test_missing_url_fails(sns_client):
    request = {"body": {'unknownKey': 'aValue'}}

    with pytest.raises(KeyError,
                       match="\[BadRequest\] Invalid request missing 'uploadUrl' in {'unknownKey': 'aValue'}"):
        archive_ingest.main(event=request, sns_client=sns_client)

    assert len(sns_client.list_messages()) == 0


def test_invalid_json_fails(sns_client):
    request = {"body": "not_json"}

    with pytest.raises(TypeError, match="\[BadRequest\] Invalid request not json: not_json"):
        archive_ingest.main(event=request, sns_client=sns_client)

    assert len(sns_client.list_messages()) == 0


def ingest_request(upload_url, callback_url=None):
    body = {
        'uploadUrl': upload_url
    }<commit_msg>Give the test import a better name<commit_after># -*- encoding: utf-8 -*-

import os
from uuid import UUID

import pytest

import archive_start_ingest as start_ingest


TABLE_NAME = 'archive-storage-progress-table'


def test_post_sends_location_to_sns(sns_client, topic_arn):
    request = ingest_request(upload_url='s3://wellcomecollection-assets-archive-ingest/test-bag.zip')

    response = start_ingest.main(event=request, sns_client=sns_client)

    id = str(UUID(response['id']))
    assert id

    assert response['location'] == f"/ingests/{id}"

    messages = sns_client.list_messages()
    assert len(messages) == 1
    assert messages[0][':message'] == {
        'archiveRequestId': id,
        'bagLocation': {
            'namespace': 'wellcomecollection-assets-archive-ingest',
            'key': 'test-bag.zip'
        }
    }


def test_sends_request_to_sns_with_callback(sns_client, topic_arn):
    request = ingest_request(upload_url='s3://wellcomecollection-assets-archive-ingest/test-bag.zip',
                             callback_url='https://workflow.wellcomecollection.org/callback?id=b1234567')

    response = start_ingest.main(event=request, sns_client=sns_client)

    actual_id = str(UUID(response['id']))
    assert actual_id

    messages = sns_client.list_messages()
    assert len(messages) == 1
    assert messages[0][':message'] == {
        'archiveRequestId': actual_id,
        'bagLocation': {
            'namespace': 'wellcomecollection-assets-archive-ingest',
            'key': 'test-bag.zip'
        },
        'callbackUrl': 'https://workflow.wellcomecollection.org/callback?id=b1234567'
    }


def test_invalid_url_fails(sns_client):
    request = ingest_request('invalidUrl')

    with pytest.raises(ValueError, match="\[BadRequest\] Unrecognised url scheme: invalid"):
        start_ingest.main(event=request, sns_client=sns_client)

    assert len(sns_client.list_messages()) == 0


def test_missing_url_fails(sns_client):
    request = {"body": {'unknownKey': 'aValue'}}

    with pytest.raises(KeyError,
                       match="\[BadRequest\] Invalid request missing 'uploadUrl' in {'unknownKey': 'aValue'}"):
        start_ingest.main(event=request, sns_client=sns_client)

    assert len(sns_client.list_messages()) == 0


def test_invalid_json_fails(sns_client):
    request = {"body": "not_json"}

    with pytest.raises(TypeError, match="\[BadRequest\] Invalid request not json: not_json"):
        start_ingest.main(event=request, sns_client=sns_client)

    assert len(sns_client.list_messages()) == 0


def ingest_request(upload_url, callback_url=None):
    body = {
        'uploadUrl': upload_url
    }
{'fraction_non_alphanumeric': 0.12975810988186762, 'fraction_numerical': 0.009000562535158447, 'mean_word_length': 3.7078552515445717, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3400906', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1701, 'n_words': 278}