aboutsummaryrefslogtreecommitdiff
path: root/utils/rust_monolith_test.py
blob: a66eea453037df94e03b85f4f66bc530dca90af7 (plain) (blame)
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
# Chris Xiong 2024
# License: Expat (MIT)
#
# shitty tests for the Rust Monolith representation

import os
import sys
import random
import tempfile
import subprocess

import monolith
import monolith_test
from config import conf

def make_random_monolith(dir):
    filename = "posts.monolith"
    m = monolith.Monolith(os.path.join(dir, filename))
    nposts = random.randint(10, 100)
    last_time = 0
    posts = []
    for _ in range(0, nposts):
        p = monolith_test.randpost(last_time)
        last_time = p.date
        posts.append(p)
        m.append(p)
    m.write_index()
    m.generate_page_index()
    with open(os.path.join(dir, "notekins.conf"), "w"): pass
    return posts

def run_rust_monolith_debug(dir, method, param):
    p = subprocess.run([sys.argv[1], method, str(param)], capture_output=True, cwd=dir)
    return p.stdout.decode("utf-8")

def dbg_output(p):
    pyout = p.content + '\n'
    pyout += str(p.date) + '\n'
    for m in p.media:
        if m.type == monolith.MediaType.IMAGE:
            pyout += f"Image {m.thumbnail} {m.original}\n"
    for t in p.tags:
        pyout += t + '\n'
    return pyout

def run_tests(dir):
    posts = make_random_monolith(dir)
    failed = False
    for p in posts:
        o = run_rust_monolith_debug(dir, "get_post", p.date)
        e = dbg_output(p)
        if o != e:
            print(f"get_post failed, date: {p.date}")
            print(f"expected\n{e}\ngot\n{o}")
            failed = True
            input()
        o = run_rust_monolith_debug(dir, "get_post2", p.date)
        e = dbg_output(p)
        if o != e:
            print(f"get_post2 failed, date: {p.date}")
            print(f"expected\n{e}\ngot\n{o}")
            failed = True
            input()
    posts_per_page = conf.POSTS_PER_PAGE
    for page, ub in enumerate(range(len(posts), 0, -posts_per_page)):
        pl = max(ub - posts_per_page, 0)
        pr = ub - 1
        if (pr - pl + 1 > posts_per_page):
            failed = True
            print(f"paging error ???")
            input()
        e = ""
        for x in range(pl, pr + 1):
            e += dbg_output(posts[x])
        o = run_rust_monolith_debug(dir, "get_page", page)
        if o != e:
            print(f"get_page failed, page: {page}")
            print(f"expected\n{e}\ngot\n{o}")
            failed = True
            input()
    if not failed:
        print(f"test of monolith with {len(posts)} posts passed.")

def test_rust_monolith():
    if len(sys.argv) < 2:
        print("missing path to executable")
    for _ in range(0, 100):
        with tempfile.TemporaryDirectory() as dir:
            run_tests(dir)

if __name__ == "__main__":
    test_rust_monolith()