<commit_before>#!/usr/bin/env python

from asyncio import test_utils
from asyncio.futures import Future
from asyncio.protocols import SubprocessProtocol
from asyncio.tasks import gather

from asyncio_redis import RedisProtocol, RedisBytesProtocol, Connection, Transaction, RedisException, StatusReply, ZRangeReply, ZScoreBoundary, BlockingPopReply, SubscribeReply, PubSubReply, ListReply, SetReply, DictReply, TransactionError
from threading import Thread
from time import sleep

import asyncio
import subprocess
import unittest
import os

PORT = int(os.environ['REDIS_PORT'])


@asyncio.coroutine
def connect(loop, protocol=RedisProtocol):
    transport, protocol = yield from loop.create_connection(protocol, 'localhost', PORT)
    return transport, protocol


def redis_test(function):
    function = asyncio.coroutine(function)

    def wrapper(self):
        @asyncio.coroutine
        def c():
            # Create connection
            transport, protocol = yield from connect(self.loop, self.protocol_class)

            yield from function(self, transport, protocol)

        self.loop.run_until_complete(c())
    return wrapper


class RedisProtocolTest(unittest.TestCase):
    def setUp(self):
        #self.loop = test_utils.TestLoop()
        self.loop = asyncio.get_event_loop()
        self.protocol_class = RedisProtocol

    def tearDown(self):
        #self.loop.close()
        pass

    @redis_test
    def test_ping(self, transport, protocol):
        result = yield from protocol.ping()
        self.assertEqual(result, StatusReply('PONG'))

    @redis_test
    def test_echo(self, transport, protocol):
        result = yield from protocol.echo(u'my string')
        self.assertEqual(result, u'my string')

    @redis_test
    def test_set_and_get(self, transport, protocol):
        # Set
        value = yield from protocol.set(u'my_key', u'my_value')
        self.assertEqual(value, StatusReply('OK'))

        # Get
        value = yield from protocol.get(u'my_key')
        self.assertEqual(value, u'my_value')

        # Getset
        value = yield from protocol.getset(u'my_key', u'new_value')
        self.assertEqual(value, u'my_value')

        value = yield from protocol.get(u'my_key')
        self.assertEqual(value, u'new_value')

    @redis_test
    def test_setnx(self, transport, protocol):
        # Setnx while key does not exists
        value = yield from protocol.setnx(u'my_key', u'my_value')
        self.assertEqual(value, True)

        # Get<commit_msg>remove key in tests just to be sure<commit_after>#!/usr/bin/env python

from asyncio import test_utils
from asyncio.futures import Future
from asyncio.protocols import SubprocessProtocol
from asyncio.tasks import gather

from asyncio_redis import RedisProtocol, RedisBytesProtocol, Connection, Transaction, RedisException, StatusReply, ZRangeReply, ZScoreBoundary, BlockingPopReply, SubscribeReply, PubSubReply, ListReply, SetReply, DictReply, TransactionError
from threading import Thread
from time import sleep

import asyncio
import subprocess
import unittest
import os

PORT = int(os.environ['REDIS_PORT'])


@asyncio.coroutine
def connect(loop, protocol=RedisProtocol):
    transport, protocol = yield from loop.create_connection(protocol, 'localhost', PORT)
    return transport, protocol


def redis_test(function):
    function = asyncio.coroutine(function)

    def wrapper(self):
        @asyncio.coroutine
        def c():
            # Create connection
            transport, protocol = yield from connect(self.loop, self.protocol_class)

            yield from function(self, transport, protocol)

        self.loop.run_until_complete(c())
    return wrapper


class RedisProtocolTest(unittest.TestCase):
    def setUp(self):
        #self.loop = test_utils.TestLoop()
        self.loop = asyncio.get_event_loop()
        self.protocol_class = RedisProtocol

    def tearDown(self):
        #self.loop.close()
        pass

    @redis_test
    def test_ping(self, transport, protocol):
        result = yield from protocol.ping()
        self.assertEqual(result, StatusReply('PONG'))

    @redis_test
    def test_echo(self, transport, protocol):
        result = yield from protocol.echo(u'my string')
        self.assertEqual(result, u'my string')

    @redis_test
    def test_set_and_get(self, transport, protocol):
        # Set
        value = yield from protocol.set(u'my_key', u'my_value')
        self.assertEqual(value, StatusReply('OK'))

        # Get
        value = yield from protocol.get(u'my_key')
        self.assertEqual(value, u'my_value')

        # Getset
        value = yield from protocol.getset(u'my_key', u'new_value')
        self.assertEqual(value, u'my_value')

        value = yield from protocol.get(u'my_key')
        self.assertEqual(value, u'new_value')

    @redis_test
    def test_setnx(self, transport, protocol):
        yield from protocol.delete(u'my_key')

        # Setnx while key does not exists
        value = yield from protocol.setnx(u'my_key', u'my_value')
        self.assertEqual(value, True)

        # Get
{'fraction_non_alphanumeric': 0.09273479031305375, 'fraction_numerical': 0.0, 'mean_word_length': 3.345594525235244, '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': '1605459', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1455, 'n_words': 377}