File size: 1,940 Bytes
d4167d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fbf836
 
d4167d9
 
 
 
 
0fbf836
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
import pytest
from PIL import Image
import numpy as np
from app import OCRDataCollector, sentences
import io

@pytest.fixture
def collector():
    return OCRDataCollector()

def test_get_random_text_block(collector):
    # Test that we get a non-empty string
    text_block = collector.get_random_text_block()
    assert isinstance(text_block, str)
    assert len(text_block) > 0
    
    # Test that the text block contains content from our sentences
    assert any(sentence in text_block for sentence in sentences)
    
    # Test that we get different blocks (probabilistic, but very likely)
    blocks = [collector.get_random_text_block() for _ in range(5)]
    assert len(set(blocks)) > 1, "Random blocks should be different"

def test_skip_text(collector):
    # Test that we get a different text block when skipping
    current_text = collector.get_random_text_block()
    new_text = collector.get_random_text_block()
    
    assert isinstance(new_text, str)
    assert len(new_text) > 0
    assert new_text != current_text  # This is probabilistic but very likely

def test_submit_image(collector):
    # Create a dummy test image using numpy array
    img_array = np.zeros((100, 100, 3), dtype=np.uint8)
    img_array.fill(255)  # White image
    
    # Convert numpy array to PIL Image
    test_image = Image.fromarray(img_array)
    
    # Test the current text block
    current_text = collector.get_random_text_block()
    
    # Test submission with valid image
    new_text = collector.submit_image(test_image, current_text)
    assert isinstance(new_text, str)
    assert len(new_text) > 0
    # Should not have added to collected_pairs since no username provided
    assert len(collector.collected_pairs) == 0
    
    # Test submission with no image
    new_text = collector.submit_image(None, current_text)
    assert isinstance(new_text, str)
    assert len(new_text) > 0
    assert len(collector.collected_pairs) == 0