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

import json
import pytest

from bags import (
    fetch_bag
)


def test_looks_up_bag(dynamodb_client, dynamodb_resource, s3_client, bucket_bag, table_name_bag):
    bag_id = 'b12345678x'
    stored_bag = {'id': bag_id}

    given_bag_in_vhs(
        bag_id,
        stored_bag,
        dynamodb_client,
        s3_client,
        bucket_bag,
        table_name_bag
    )

    response = fetch_bag(
        dynamodb_resource,
        table_name_bag,
        s3_client,
        bucket_bag,
        bag_id
    )

    assert response == stored_bag


@pytest.yield_fixture(autouse=True)
def run_around_tests(dynamodb_client, s3_client, bucket_bag, table_name_bag):
    s3_client.create_bucket(Bucket=bucket_bag)

    yield

    objects = s3_client.list_objects(Bucket=bucket_bag)
    for content in objects['Contents']:
        s3_client.delete_object(Bucket=bucket_bag, Key=content['Key'])
    s3_client.delete_bucket(Bucket=bucket_bag)


def create_table(dynamodb_client, table_name_bag):
    try:
        dynamodb_client.create_table(
            TableName=table_name_bag,
            KeySchema=[
                {
                    'AttributeName': 'id',
                    'KeyType': 'HASH'
                }
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': 'id',
                    'AttributeType': 'S'
                }
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 1,
                'WriteCapacityUnits': 1
            }
        )
        dynamodb_client.get_waiter('table_exists').wait(TableName=table_name_bag)
    except dynamodb_client.exceptions.ResourceInUseException:
        pass


def given_bag_in_vhs(bag_id, stored_bag, dynamodb_client, s3_client, bucket_bag, table_name_bag):
    key = bag_id
    s3_client.put_object(Bucket=bucket_bag, Key=key, Body=json.dumps(stored_bag))

    dynamodb_client.put_item(
        TableName=table_name_bag,
        Item={
            'id': {
                'S': bag_id
            },
            'location': {
                'key': {
                    'S': key
                },
                'namespace': {
                    'S': bucket_bag
                }
            }
        }
    )<commit_msg>use dynamodb_resource for simpler put_item call.<commit_after># -*- encoding: utf-8 -*-

import json
import pytest

from bags import (
    fetch_bag
)


def test_looks_up_bag(dynamodb_resource, s3_client, bucket_bag, table_name_bag):
    bag_id = 'b12345678x'
    stored_bag = {'id': bag_id}

    given_bag_in_vhs(
        bag_id,
        stored_bag,
        dynamodb_resource,
        s3_client,
        bucket_bag,
        table_name_bag
    )

    response = fetch_bag(
        dynamodb_resource,
        table_name_bag,
        s3_client,
        bucket_bag,
        bag_id
    )

    assert response == stored_bag


@pytest.yield_fixture(autouse=True)
def run_around_tests(dynamodb_client, s3_client, bucket_bag, table_name_bag):
    s3_client.create_bucket(Bucket=bucket_bag)

    yield

    objects = s3_client.list_objects(Bucket=bucket_bag)
    for content in objects['Contents']:
        s3_client.delete_object(Bucket=bucket_bag, Key=content['Key'])
    s3_client.delete_bucket(Bucket=bucket_bag)


def create_table(dynamodb_client, table_name_bag):
    try:
        dynamodb_client.create_table(
            TableName=table_name_bag,
            KeySchema=[
                {
                    'AttributeName': 'id',
                    'KeyType': 'HASH'
                }
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': 'id',
                    'AttributeType': 'S'
                }
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 1,
                'WriteCapacityUnits': 1
            }
        )
        dynamodb_client.get_waiter('table_exists').wait(TableName=table_name_bag)
    except dynamodb_client.exceptions.ResourceInUseException:
        pass


def given_bag_in_vhs(bag_id, stored_bag, dynamodb_resource, s3_client, bucket_bag, table_name_bag):
    key = bag_id
    s3_client.put_object(Bucket=bucket_bag, Key=key, Body=json.dumps(stored_bag))

    table = dynamodb_resource.Table(table_name_bag)
    table.put_item(Item={
        'id': bag_id,
        'location': {
            'key': bag_id,
            'namespace': bucket_bag
        }
    })

{'fraction_non_alphanumeric': 0.09090909090909091, 'fraction_numerical': 0.009381282108554836, 'mean_word_length': 1.9675281643472498, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '521058', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1393, 'n_words': 213}