summaryrefslogtreecommitdiffstats
path: root/tests/file_page_test.py
blob: 5c380b892d3b33a5469247863623954ad3965ecd (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from unittest.mock import MagicMock

from src.g2h.file_page import FilePage, bytes_to_content

def test_init():
    commit = MagicMock()
    commit.hexsha = '123'
    page = FilePage('test/file', 'file test', commit, '.test_file', 'path/test', '')
    assert(page.name == 'file test')
    assert(page.out_file_path == 'test/file/123/t/path/test/_test_file.md')
    assert(page.template.name == 'file.md')
    assert(page.commit == commit)
    assert(page.file_name == '.test_file')
    assert(page.file_path == 'path/test')
    commit.tree.join.assert_called_with('path/test')

def test_read_file():
    file = MagicMock()
    tree = MagicMock()
    commit = MagicMock()
    commit.hexsha = '123'
    commit.tree =  tree
    commit.tree.join.return_value = file
    page = FilePage('test/file', 'file test', commit, 'test_file', '', '')
    page.read_file()
    tree.join.assert_called_with('test_file')
    file.data_stream.read.assert_called()

def test_context():
    file = MagicMock()
    tree = MagicMock()
    commit = MagicMock()
    commit.hexsha = '123'
    commit.tree =  tree
    commit.tree.join.return_value = file
    page = FilePage('test/file', 'file test', commit, 'test_file.txt', '', 'file/content')
    file.data_stream.read.return_value = 'test'.encode()
    context = page.context()
    assert(context['content'] == 'test')
    assert(context['content_subdir'] == 'file/content')
    assert(context['file_extension'] == 'txt')
    assert(context['title'] == 'file test: test_file.txt')
    assert(context['type'] == 'text')
    assert(context['breadcrumbs'] == '[/](/file/content/123/t/)/[test_file.txt](/file/content/123/t/test_file.txt)')
    assert(context['this_commit'] == commit)

def test_context_dot_file():
    file = MagicMock()
    tree = MagicMock()
    commit = MagicMock()
    commit.hexsha = '123'
    commit.tree =  tree
    commit.tree.join.return_value = file
    page = FilePage('test/file', 'file test', commit, '.file', '', 'file/content')
    file.data_stream.read.return_value = 'test'.encode()
    context = page.context()
    assert(context['content'] == 'test')
    assert(context['content_subdir'] == 'file/content')
    assert(context['file_extension'] == '')
    assert(context['title'] == 'file test: .file')
    assert(context['type'] == 'text')
    assert(context['breadcrumbs'] == '[/](/file/content/123/t/)/[.file](/file/content/123/t/_file)')
    assert(context['backtick_max'] == 3)

def test_bytes_to_content_text():
    content, type = bytes_to_content('test'.encode())
    assert(content == 'test')
    assert(type == 'text')

def test_bytes_to_content_pic():
    webp_1px = bytes([0x52, 0x49, 0x46, 0x46, 0x1a, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x4c, 0x0e, 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0x11, 0xfd, 0x0f, 0x44, 0x44, 0xff, 0x03, 0x0a])
    content, type = bytes_to_content(webp_1px)
    assert(content == 'UklGRhoAAABXRUJQVlA4TA4AAAAvAAAAAAcQEf0PRET/Awo=')
    assert(type == 'image')

def test_bytes_to_content_empty():
    content, type = bytes_to_content(bytes())
    assert(content == None)
    assert(type == 'empty')

def test_bytes_to_content_bin():
    content, type = bytes_to_content(bytes([0xc3, 0x28]))
    assert(content == None)
    assert(type == None)

def test_backticks_in_text_content():
    file = MagicMock()
    tree = MagicMock()
    commit = MagicMock()
    commit.hexsha = '123'
    commit.tree =  tree
    commit.tree.join.return_value = file
    page = FilePage('test/file', 'file test', commit, '.file', '', 'file/content')
    file.data_stream.read.return_value = '```'.encode()
    context = page.context()
    assert(context['backtick_max'] == 4)

def test_markdown():
    file = MagicMock()
    tree = MagicMock()
    commit = MagicMock()
    commit.hexsha = '123'
    commit.tree =  tree
    commit.tree.join.return_value = file
    page = FilePage('test/file', 'file test', commit, 'file.Md', '', 'file/content')
    file.data_stream.read.return_value = '```'.encode()
    context = page.context()
    assert(context['backtick_max'] == 0)