File size: 1,975 Bytes
c69cba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import pytest
import os
from discord_bot.client.utils import ( \
    find_max_split_index, \
    find_max_split_index_from_sequence, \
    split_text_into_chunks
)


@pytest.fixture(scope='module')
def test_chunk() -> str:
    return 't. , \n .'


@pytest.fixture(scope='module')
def test_text() -> str:
    with open('tests/discord_bot/client/lorem_ipsum.txt', 'r') as f:
        text = f.read()
    assert text is not None, 'test text is empty'
    return text


def test_find_max_splitting_index(test_chunk: str):
    index = find_max_split_index(test_chunk, char='\n')
    assert index == 6, 'index should be 6'
    index = find_max_split_index(test_chunk, char='. ')
    assert index == 3, 'index should be 3'
    index = find_max_split_index(test_chunk, char='.')
    assert index == 8, 'index should be 8'


def test_find_max_split_index_from_sequence(test_chunk: str):
    index = find_max_split_index_from_sequence(
        test_chunk,
        split_characters=['\n']
    )
    assert index == 6, 'index should be 6'
    index = find_max_split_index_from_sequence(
        test_chunk,
        split_characters=['.', ', ', '\n']
    )
    assert index == 8, 'index should be 8'


def test_split_text_into_chunks_with_split_characters(test_text: str):
    max_chunk_size = 250
    chunks = split_text_into_chunks(
        test_text,
        split_characters=['. ', ', ', '\n'],
        min_size=20,
        max_size=max_chunk_size
    )
    for chunk in chunks:
        assert len(chunk) > 0, 'Chunk length is zero'
        assert len(chunk) <= max_chunk_size, 'Chunk length exceeds maximum limit'


def test_split_text_into_chunks_without_split_characters():
    test_text = 'a' * 1000
    max_chunk_size = 250
    chunks = split_text_into_chunks(
        test_text,
        split_characters=[],
        min_size=20,
        max_size=max_chunk_size
    )
    for chunk in chunks:
        assert len(chunk) == max_chunk_size, \
            'Chunk length is too small'