<commit_before>from mock import patch

import sseclient
from sseclient import Event as E


# Some tests of parsing a single event string
def test_round_trip_parse():
    m1 = E(
        data='hi there\nsexy developer',
        event='salutation',
        id='abcdefg',
        retry=10000
    )

    dumped = m1.dump()
    m2 = E.parse(dumped)
    assert m1.id == m2.id
    assert m1.data == m2.data
    assert m1.retry == m2.retry
    assert m1.event == m2.event


def test_no_colon():
    m = E.parse('data')
    assert m.data == ''


def test_no_space():
    m = E.parse('data:hi')
    assert m.data == 'hi'


def test_comment():
    raw = ":this is a comment\ndata: this is some data"
    m = E.parse(raw)
    assert m.data == 'this is some data'


def test_retry_is_integer():
    m = E.parse('data: hi\nretry: 4000')
    assert m.retry == 4000


def test_default_event():
    m = E.parse('data: blah')
    assert m.event == 'message'


# A couple mocks for Requests
class FakeRequests(object):
    def __init__(self, status_code, content):
        self.status_code = status_code
        self.content = content

    def get(self, url, *args, **kwargs):
        return FakeResponse(self.status_code, self.content)


class FakeResponse(object):
    def __init__(self, status_code, content):
        self.status_code = status_code
        if not isinstance(content, unicode):
            content = content.decode('utf8')
        self.stream = content

    def iter_content(self, chunk_size=1, *args, **kwargs):
        try:
            c = self.stream[0]
            self.stream = self.stream[1:]
            yield c
        except IndexError:
            raise StopIteration

    def raise_for_status(self):
        pass


def join_events(*events):
    """
    Given a bunch of Event objects, dump them all to strings and join them
    together.
    """
    return ''.join(e.dump() for e in events)


# Tests of parsing a multi event stream
def test_last_id_remembered():
    content = "data: message 1\nid: abcdef\n\ndata: message 2\n\n"<commit_msg>Use six in tests for unicode<commit_after>import six
from mock import patch

import sseclient
from sseclient import Event as E


# Some tests of parsing a single event string
def test_round_trip_parse():
    m1 = E(
        data='hi there\nsexy developer',
        event='salutation',
        id='abcdefg',
        retry=10000
    )

    dumped = m1.dump()
    m2 = E.parse(dumped)
    assert m1.id == m2.id
    assert m1.data == m2.data
    assert m1.retry == m2.retry
    assert m1.event == m2.event


def test_no_colon():
    m = E.parse('data')
    assert m.data == ''


def test_no_space():
    m = E.parse('data:hi')
    assert m.data == 'hi'


def test_comment():
    raw = ":this is a comment\ndata: this is some data"
    m = E.parse(raw)
    assert m.data == 'this is some data'


def test_retry_is_integer():
    m = E.parse('data: hi\nretry: 4000')
    assert m.retry == 4000


def test_default_event():
    m = E.parse('data: blah')
    assert m.event == 'message'


# A couple mocks for Requests
class FakeRequests(object):
    def __init__(self, status_code, content):
        self.status_code = status_code
        self.content = content

    def get(self, url, *args, **kwargs):
        return FakeResponse(self.status_code, self.content)


class FakeResponse(object):
    def __init__(self, status_code, content):
        self.status_code = status_code
        if not isinstance(content, six.text_type):
            content = content.decode('utf8')
        self.stream = content

    def iter_content(self, chunk_size=1, *args, **kwargs):
        try:
            c = self.stream[0]
            self.stream = self.stream[1:]
            yield c
        except IndexError:
            raise StopIteration

    def raise_for_status(self):
        pass


def join_events(*events):
    """
    Given a bunch of Event objects, dump them all to strings and join them
    together.
    """
    return ''.join(e.dump() for e in events)


# Tests of parsing a multi event stream
def test_last_id_remembered():
    content = "data: message 1\nid: abcdef\n\ndata: message 2\n\n"
{'fraction_non_alphanumeric': 0.11333977767037216, 'fraction_numerical': 0.014499758337361043, 'mean_word_length': 2.5773552290406223, 'pattern_counts': {'":': 2, '<': 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': '871133', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1340, 'n_words': 390}