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
|
# Chris Xiong 2024
# License: Expat (MIT)
#
# Basic unit tests for the Python Monolith class
import monolith
import unittest
import random
import os
from mmap import mmap
def randstr(len):
return ''.join(random.choices(''.join([chr(i + ord('0')) for i in range(0, 75)]), k=len))
def randpost(last_time):
content = randstr(random.randint(10, 1024))
date = random.randint(last_time + 1, last_time + 999999)
media = []
tags = []
for _ in range(0, random.randint(0, 9)):
media.append(monolith.MediaInstance.makeImage(randstr(20), randstr(20)))
for _ in range(0, random.randint(0, 4)):
tags.append(randstr(random.randint(1, 8)))
return monolith.Post(content, date, media, tags)
def posteq(a, b):
if a is None or b is None:
return False
if len(a.media) != len(b.media) or len(a.tags) != len(b.tags):
return False
for x, y in zip(a.media, b.media):
if x.thumbnail != y.thumbnail or x.original != y.original:
return False
for x, y in zip(a.tags, b.tags):
if x != y: return False
return a.content == b. content and a.date == b.date
class TestMonolith(unittest.TestCase):
def test_replace(self):
posts = []
filename = "rep.monolith"
m = monolith.Monolith(filename)
p1 = randpost(123)
p2 = randpost(p1.date)
p3 = randpost(0)
p3.date = p1.date
m.append(p1)
m.append(p2)
m.replace_post(p1.date, p3)
self.assertTrue(posteq(m.get_post(p3.date), p3))
self.assertTrue(posteq(m.get_post(p2.date), p2))
def test_combined(self):
posts = []
filename = "test.monolith"
if True:
m = monolith.Monolith(filename)
last_time = 0
for _ in range(0, 100):
op = 1 if random.random() < 0.2 else 0
if op == 1 and len(posts) == 0:
op = 0
if op == 0:
p = randpost(last_time)
last_time = p.date
posts.append(p)
m.append(p)
elif op == 1:
p = randpost(0)
position = random.randint(0, len(posts) - 1)
p.date = posts[position].date
posts[position] = p
m.replace_post(p.date, p)
m.write_index()
m.generate_page_index()
for p in posts:
pp = m.get_post(p.date)
self.assertTrue(posteq(p, pp))
with open(filename, "r+b") as f:
d = mmap(f.fileno(), 0)
for _, _, r in m.postranges:
self.assertEqual(d[r - 1 : r], b'\v')
if True:
m = monolith.Monolith(filename)
m.load_index()
dates = m.get_all_dates()
self.assertEqual(len(dates), len(posts))
for t, p in zip(dates, posts):
self.assertEqual(t, p.date)
for t, p in zip(dates, posts):
self.assertTrue(posteq(p, m.get_post(t)))
if __name__ == "__main__":
unittest.main()
|