<commit_before>from unittest import TestCase
from monocyte import handler
from mock import patch, Mock
import boto.ec2
import boto.exception


class EC2_HandlerTest(TestCase):

    @patch("monocyte.handler.boto")
    def setUp(self, boto_mock):
        self.positive_fake_region = Mock(boto.ec2.regioninfo)
        self.positive_fake_region.name = "foo"
        negative_fake_region = Mock(boto.ec2.regioninfo)
        negative_fake_region.name = "bar"

        boto_mock.ec2.regions.return_value = [self.positive_fake_region, negative_fake_region]
        self.ec2_handler_filter = handler.EC2(lambda region_name: region_name == self.positive_fake_region.name)

    @patch("monocyte.handler.boto")
    def test_fetch_all_resources_filtered(self, boto_mock):
        fake_instance = Mock(boto.ec2.instance, image_id="ami-1111")
        boto_mock.ec2.connect_to_region.return_value.get_only_instances.return_value = [fake_instance]
        resources = list(self.ec2_handler_filter.fetch_all_resources())

        self.assertEquals(resources[0].wrapped, fake_instance)

    @patch("monocyte.handler.boto")
    def test_to_string(self, boto_mock):
        fake_instance = Mock(boto.ec2.instance, image_id="ami-1112")
        fake_instance.id = "id-12345"
        fake_instance.instance_type = "m1.small"
        fake_instance.launch_time = "01.01.2015"
        fake_instance.public_dns_name = "test.aws.rz.is"
        fake_instance.key_name = "test-ssh-key"
        fake_instance.region = self.positive_fake_region
        boto_mock.ec2.connect_to_region.return_value.get_only_instances.return_value = [fake_instance]
        resources = list(self.ec2_handler_filter.fetch_all_resources())
        string = self.ec2_handler_filter.to_string(resources[0])
        self.assertEquals(string, "ec2 instance found in foo\n\tid-12345 [ami-1112] - m1.small, since 01.01.2015\n\tdnsname test.aws.rz.is, key test-ssh-key")

    @patch("monocyte.handler.boto")
    def test_delete(self, boto_mock):
        fake_instance = Mock(boto.ec2.instance, image_id="ami-1112")
        fake_instance.id = "id-12345"
        resource = handler.Resource(fake_instance, "forbidden_region")
        connection = boto_mock.ec2.connect_to_region.return_value

        connection.terminate_instances.side_effect = boto.exception.EC2ResponseError(412, 'boom')

        deleted_resources = self.ec2_handler_filter.delete(resource)
        self.assertEquals(fake_instance, deleted_resources[0])


class S3_HandlerTest(TestCase):

    @patch("monocyte.handler.boto")
    def setUp(self, boto_mock):
        pass<commit_msg>refactors test for better understanding.<commit_after>from unittest import TestCase
from monocyte import handler
from mock import patch, Mock
import boto.ec2
import boto.exception


class EC2HandlerTest(TestCase):

    @patch("monocyte.handler.boto")
    def setUp(self, boto_mock):
        self.positive_fake_region = Mock(boto.ec2.regioninfo)
        self.positive_fake_region.name = "foo"
        negative_fake_region = Mock(boto.ec2.regioninfo)
        negative_fake_region.name = "bar"

        boto_mock.ec2.regions.return_value = [self.positive_fake_region, negative_fake_region]
        self.ec2_handler_filter = handler.EC2(lambda region_name: region_name == self.positive_fake_region.name)

    @patch("monocyte.handler.boto")
    def test_fetch_all_resources_filtered(self, boto_mock):
        instance_mock = self._given_instance_mock()
        boto_mock.ec2.connect_to_region.return_value.get_only_instances.return_value = [instance_mock]
        only_resource = list(self.ec2_handler_filter.fetch_all_resources())[0]

        self.assertEquals(only_resource.wrapped, instance_mock)

    @patch("monocyte.handler.boto")
    def test_to_string(self, boto_mock):
        instance_mock = self._given_instance_mock()
        boto_mock.ec2.connect_to_region.return_value.get_only_instances.return_value = [instance_mock]
        only_resource = list(self.ec2_handler_filter.fetch_all_resources())[0]
        resource_string = self.ec2_handler_filter.to_string(only_resource)

        self.assertTrue(instance_mock.id in resource_string)
        self.assertTrue(instance_mock.image_id in resource_string)
        self.assertTrue(instance_mock.instance_type in resource_string)
        self.assertTrue(instance_mock.launch_time in resource_string)
        self.assertTrue(instance_mock.public_dns_name in resource_string)
        self.assertTrue(instance_mock.key_name in resource_string)
        self.assertTrue(instance_mock.region.name in resource_string)

    @patch("monocyte.handler.boto")
    def test_delete(self, boto_mock):
        instance_mock = self._given_instance_mock()
        resource = handler.Resource(instance_mock, "forbidden_region")
        connection = boto_mock.ec2.connect_to_region.return_value
        connection.terminate_instances.side_effect = boto.exception.EC2ResponseError(412, 'boom')

        deleted_resource = self.ec2_handler_filter.delete(resource)[0]
        self.assertEquals(instance_mock, deleted_resource)

    def _given_instance_mock(self):
        instance_mock = Mock(boto.ec2.instance, image_id="ami-1112")
        instance_mock.id = "id-12345"
        instance_mock.instance_type = "m1.small"
        instance_mock.launch_time = "01.01.2015"
        instance_mock.public_dns_name = "test.aws.rz.is"
        instance_mock.key_name = "test-ssh-key"
        instance_mock.region = self.positive_fake_region
        return instance_mock


class S3HandlerTest(TestCase):

    @patch("monocyte.handler.boto")
    def setUp(self, boto_mock):
        pass
{'fraction_non_alphanumeric': 0.09671076153021094, 'fraction_numerical': 0.020736503396496245, 'mean_word_length': 3.9601063829787235, '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': '2475439', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1775, 'n_words': 249}