aboutsummaryrefslogblamecommitdiff
path: root/utils/rust_monolith_test.py
blob: a66eea453037df94e03b85f4f66bc530dca90af7 (plain) (tree)



























































































                                                                                       
# 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()