<commit_before># -*- coding: utf-8 -*-
"""tests -- Unit tests for duo.

Mocking AWS services is HARD.
"""
from __future__ import unicode_literals
from six import with_metaclass, string_types, text_type, iteritems
try:
    import unittest2 as unittest
except ImportError:
    import unittest

import datetime

import mock


class DynamoDBTests(unittest.TestCase):
    # Default settings for describing the table we want to work with,
    # in lieu of actual values from AWS.
    default_item_data = dict(
        table_name = 'test_table',
        hash_key_name = 'test_hash_key',
        range_key_name = 'test_range_key',
        hash_key_value = 'fred',
        range_key_value = 'flintstone',
        item_attrs = {},
        key = 'foo',
        secret = 'bar',
        )

    def setUp(self):
        super(DynamoDBTests, self).setUp()
        for key, value in iteritems(self.default_item_data):
            setattr(self, key, value)

        from boto.dynamodb import layer1
        reload(layer1)
        self.boto_layer1 = layer1
        # Mock out layer1 completely. This is where all the network interface occurs.
        MockLayer1 = mock.Mock(spec=layer1.Layer1)
        MockLayer1.return_value = MockLayer1

        layer1_patcher = self.layer1_patcher = mock.patch('boto.dynamodb.layer1.Layer1')
        self.mocked_layer1 = layer1_patcher.start()

        # Spy on layer2, making sure that it gets a mock layer1 object.
        from boto.dynamodb import layer2
        reload(layer2)
        self.boto_layer2 = layer2
        MockLayer2 = self.MockLayer2 = mock.Mock(
            spec = layer2.Layer2,
            wraps = layer2.Layer2())
        MockLayer2.layer1 = MockLayer1

        # Mock out the connection to return our special MockLayer2.
        connect_patcher = self.connect_dynamodb_patcher = mock.patch('boto.connect_dynamodb')
        self.mocked_connect_dynamodb = connect_patcher.start()
        self.mocked_connect_dynamodb.return_value = MockLayer2

        # Wire up
        self.mock_boto_tables()

    def mock_item_data(self):
        """Create a dict corresponding to an item description JSON from AWS.
        """
        data = {
            "Item": {
                self.hash_key_name: self.hash_key_value,
                self.range_key_name: self.range_key_value
                },
            "ConsumedCapacityUnits": 1
            }
        data['Item'].update(self.item_attrs)
        return data

    def mock_boto_tables(self):
        """Set up appropriate table-related stubs on the `boto.dynamodb.layer2` interface.
        """
        from boto.dynamodb import table, item
        reload(table)
        reload(item)
        self.boto_table = table
        self.boto_item = item

        # We want to be able to spy on the Table.
        MockTable = self.MockTable = mock.Mock(
            spec = table.Table,
            wraps = table.Table(self.MockLayer2, self.describe_table()))

        self.MockLayer2.describe_table.return_value = self.describe_table()
        self.MockLayer2.get_table.return_value = MockTable
        self.MockLayer2.layer1.get_item.return_value = self.mock_item_data()

        import duo
        reload(duo)
        self.duo = duo
        self.db = duo.DynamoDB(key=self.key, secret=self.secret)

    def describe_table(self):
        """Create a dict corresponding to a table description JSON from AWS.
        """
        return {'Table': {'CreationDateTime': 1343759006.036,
                          'ItemCount': 0,
                          'KeySchema': {'HashKeyElement': {'AttributeName': self.hash_key_name,
                                                           'AttributeType': 'S'},
                                        'RangeKeyElement': {'AttributeName': self.range_key_name,
                                                            'AttributeType': 'S'}},
                          'ProvisionedThroughput': {'ReadCapacityUnits': 10,
                                                    'WriteCapacityUnits': 5},
                          'TableName': self.table_name,<commit_msg>use py2/3 compatible reload()<commit_after># -*- coding: utf-8 -*-
"""tests -- Unit tests for duo.

Mocking AWS services is HARD.
"""
from __future__ import unicode_literals
from six import with_metaclass, string_types, text_type, iteritems
from six.moves import reload_module

try:
    import unittest2 as unittest
except ImportError:
    import unittest

import datetime

import mock


class DynamoDBTests(unittest.TestCase):
    # Default settings for describing the table we want to work with,
    # in lieu of actual values from AWS.
    default_item_data = dict(
        table_name = 'test_table',
        hash_key_name = 'test_hash_key',
        range_key_name = 'test_range_key',
        hash_key_value = 'fred',
        range_key_value = 'flintstone',
        item_attrs = {},
        key = 'foo',
        secret = 'bar',
        )

    def setUp(self):
        super(DynamoDBTests, self).setUp()
        for key, value in iteritems(self.default_item_data):
            setattr(self, key, value)

        from boto.dynamodb import layer1
        reload_module(layer1)
        self.boto_layer1 = layer1
        # Mock out layer1 completely. This is where all the network interface occurs.
        MockLayer1 = mock.Mock(spec=layer1.Layer1)
        MockLayer1.return_value = MockLayer1

        layer1_patcher = self.layer1_patcher = mock.patch('boto.dynamodb.layer1.Layer1')
        self.mocked_layer1 = layer1_patcher.start()

        # Spy on layer2, making sure that it gets a mock layer1 object.
        from boto.dynamodb import layer2
        reload_module(layer2)
        self.boto_layer2 = layer2
        MockLayer2 = self.MockLayer2 = mock.Mock(
            spec = layer2.Layer2,
            wraps = layer2.Layer2())
        MockLayer2.layer1 = MockLayer1

        # Mock out the connection to return our special MockLayer2.
        connect_patcher = self.connect_dynamodb_patcher = mock.patch('boto.connect_dynamodb')
        self.mocked_connect_dynamodb = connect_patcher.start()
        self.mocked_connect_dynamodb.return_value = MockLayer2

        # Wire up
        self.mock_boto_tables()

    def mock_item_data(self):
        """Create a dict corresponding to an item description JSON from AWS.
        """
        data = {
            "Item": {
                self.hash_key_name: self.hash_key_value,
                self.range_key_name: self.range_key_value
                },
            "ConsumedCapacityUnits": 1
            }
        data['Item'].update(self.item_attrs)
        return data

    def mock_boto_tables(self):
        """Set up appropriate table-related stubs on the `boto.dynamodb.layer2` interface.
        """
        from boto.dynamodb import table, item
        reload_module(table)
        reload_module(item)
        self.boto_table = table
        self.boto_item = item

        # We want to be able to spy on the Table.
        MockTable = self.MockTable = mock.Mock(
            spec = table.Table,
            wraps = table.Table(self.MockLayer2, self.describe_table()))

        self.MockLayer2.describe_table.return_value = self.describe_table()
        self.MockLayer2.get_table.return_value = MockTable
        self.MockLayer2.layer1.get_item.return_value = self.mock_item_data()

        import duo
        reload_module(duo)
        self.duo = duo
        self.db = duo.DynamoDB(key=self.key, secret=self.secret)

    def describe_table(self):
        """Create a dict corresponding to a table description JSON from AWS.
        """
        return {'Table': {'CreationDateTime': 1343759006.036,
                          'ItemCount': 0,
                          'KeySchema': {'HashKeyElement': {'AttributeName': self.hash_key_name,
                                                           'AttributeType': 'S'},
                                        'RangeKeyElement': {'AttributeName': self.range_key_name,
                                                            'AttributeType': 'S'}},
                          'ProvisionedThroughput': {'ReadCapacityUnits': 10,
                                                    'WriteCapacityUnits': 5},
                          'TableName': self.table_name,
{'fraction_non_alphanumeric': 0.08771504724981827, 'fraction_numerical': 0.01453840562151684, 'mean_word_length': 1.998547039593171, 'pattern_counts': {'":': 4, '<': 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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1006989', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2288, 'n_words': 558}