<commit_before>import pytest
import asyncio
import socket
import ipaddress
import aiodns
from aiohttp.resolver import AsyncResolver, DefaultResolver


@pytest.fixture
def loop():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(None)
    return loop


def test_async_resolver_positive_lookup(loop):
    @asyncio.coroutine
    def go():
        resolver = AsyncResolver(loop=loop)
        real = yield from resolver.resolve('www.python.org')
        ipaddress.ip_address(real[0]['host'])

    loop.run_until_complete(go())

def test_async_resolver_multiple_replies(loop):
    @asyncio.coroutine
    def go():
        resolver = AsyncResolver(loop=loop)
        real = yield from resolver.resolve('www.google.com')
        ips = [ipaddress.ip_address(x['host']) for x in real]
        assert len(ips) > 3, "Expecting multiple addresses"
    loop.run_until_complete(go())

def test_async_negative_lookup(loop):
    @asyncio.coroutine
    def go():
        resolver = AsyncResolver(loop=loop)
        try:
            yield from resolver.resolve('doesnotexist.bla')
            assert False, "Expecting aiodns.error.DNSError"
        except aiodns.error.DNSError:
            pass

    loop.run_until_complete(go())

def test_default_resolver_positive_lookup(loop):
    @asyncio.coroutine
    def go():
        resolver = DefaultResolver(loop=loop)
        real = yield from resolver.resolve('www.python.org')
        ipaddress.ip_address(real[0]['host'])

    loop.run_until_complete(go())

def test_default_resolver_multiple_replies(loop):
    @asyncio.coroutine
    def go():
        resolver = DefaultResolver(loop=loop)
        real = yield from resolver.resolve('www.google.com')
        ips = [ipaddress.ip_address(x['host']) for x in real]
        assert len(ips) > 3, "Expecting multiple addresses"
    loop.run_until_complete(go())

def test_default_negative_lookup(loop):
    @asyncio.coroutine
    def go():
        resolver = DefaultResolver(loop=loop)
        try:
            yield from resolver.resolve('doesnotexist.bla')
            assert False, "Expecting socket.gaierror"
        except socket.gaierror:<commit_msg>Mocking async resolver tests<commit_after>import pytest
import asyncio
import socket
import ipaddress
import aiodns
from aiohttp.resolver import AsyncResolver, DefaultResolver
from unittest.mock import patch, Mock


class FakeResult:
    def __init__(self, host):
        self.host = host


@pytest.fixture
def loop():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(None)
    return loop

@asyncio.coroutine
def fake_result(result):
    return [FakeResult(host=h)
            for h in result]

def test_async_resolver_positive_lookup(loop):
    @asyncio.coroutine
    def go():
        with patch('aiodns.DNSResolver.query') as mock_query:
            mock_query.return_value = fake_result(['127.0.0.1'])
            resolver = AsyncResolver(loop=loop)
            real = yield from resolver.resolve('www.python.org')
            ipaddress.ip_address(real[0]['host'])
    loop.run_until_complete(go())

def test_async_resolver_multiple_replies(loop):
    @asyncio.coroutine
    def go():
        with patch('aiodns.DNSResolver.query') as mock_query:
            ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4']
            mock_query.return_value = fake_result(ips)
            resolver = AsyncResolver(loop=loop)
            real = yield from resolver.resolve('www.google.com')
            ips = [ipaddress.ip_address(x['host']) for x in real]
            assert len(ips) > 3, "Expecting multiple addresses"
    loop.run_until_complete(go())

def test_async_negative_lookup(loop):
    @asyncio.coroutine
    def go():
        with patch('aiodns.DNSResolver.query') as mock_query:
            mock_query.side_effect = aiodns.error.DNSError()
            resolver = AsyncResolver(loop=loop)
            try:
                yield from resolver.resolve('doesnotexist.bla')
                assert False, "Expecting aiodns.error.DNSError"
            except aiodns.error.DNSError:
                pass

    loop.run_until_complete(go())

def test_default_resolver_positive_lookup(loop):
    @asyncio.coroutine
    def go():
        resolver = DefaultResolver(loop=loop)
        real = yield from resolver.resolve('www.python.org')
        ipaddress.ip_address(real[0]['host'])

    loop.run_until_complete(go())

def test_default_resolver_multiple_replies(loop):
    @asyncio.coroutine
    def go():
        resolver = DefaultResolver(loop=loop)
        real = yield from resolver.resolve('www.google.com')
        ips = [ipaddress.ip_address(x['host']) for x in real]
        assert len(ips) > 3, "Expecting multiple addresses"
    loop.run_until_complete(go())

def test_default_negative_lookup(loop):
    @asyncio.coroutine
    def go():
        resolver = DefaultResolver(loop=loop)
        try:
            yield from resolver.resolve('doesnotexist.bla')
            assert False, "Expecting socket.gaierror"
        except socket.gaierror:
{'fraction_non_alphanumeric': 0.10672252144424496, 'fraction_numerical': 0.007580291242768801, 'mean_word_length': 3.059919028340081, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 5, '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': '6164279', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1556, 'n_words': 312}