|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +def is_need_present(need_id, html, needs_list, is_present=True): |
| 8 | + assert ( |
| 9 | + is_need_in_html(need_id, html) is is_present |
| 10 | + ), f"{need_id} should {'not ' if not is_present else ''}be present in html" |
| 11 | + |
| 12 | + assert ( |
| 13 | + is_need_in_needtable(need_id, html) is is_present |
| 14 | + ), f"{need_id} should {'not ' if not is_present else ''}be present in needtable" |
| 15 | + |
| 16 | + assert ( |
| 17 | + is_need_in_needlist(need_id, html) is is_present |
| 18 | + ), f"{need_id} should {'not ' if not is_present else ''}be present in needlist" |
| 19 | + |
| 20 | + assert ( |
| 21 | + need_id in needs_list |
| 22 | + ) is is_present, f"{need_id} should {'not ' if not is_present else ''}be present in needs.json" |
| 23 | + |
| 24 | + |
| 25 | +def is_need_in_html(need_id, html): |
| 26 | + return 'id="' + need_id + '">' in html |
| 27 | + |
| 28 | + |
| 29 | +def is_need_in_needtable(need_id, html): |
| 30 | + return ( |
| 31 | + '<td class="needs_id"><p><a class="reference internal" href="#' + need_id + '">' + need_id + "</a></p></td>" |
| 32 | + in html |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def is_need_in_needlist(need_id, html): |
| 37 | + return '<div class="line"><a class="reference external" href="#' + need_id + '">' + need_id + ":" in html |
| 38 | + |
| 39 | + |
| 40 | +def get_json_needs(needs_file: Path): |
| 41 | + with open(needs_file) as file: |
| 42 | + data = json.load(file) |
| 43 | + return data["versions"][data["current_version"]]["needs"] |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize( |
| 47 | + "test_app", |
| 48 | + [ |
| 49 | + {"buildername": "html", "srcdir": "doc_test/doc_directive_only", "tags": ["tag_a"]}, |
| 50 | + ], |
| 51 | + indirect=True, |
| 52 | +) |
| 53 | +def test_need_excluded_under_only_a(test_app): |
| 54 | + app = test_app |
| 55 | + app.build() |
| 56 | + html = Path(app.outdir, "index.html").read_text() |
| 57 | + needs_list = get_json_needs(Path(app.outdir, "needs.json")) |
| 58 | + |
| 59 | + is_need_present("REQ_000", html, needs_list) |
| 60 | + is_need_present("REQ_001", html, needs_list) |
| 61 | + is_need_present("REQ_001_1", html, needs_list) |
| 62 | + is_need_present("REQ_002", html, needs_list, False) |
| 63 | + is_need_present("REQ_003", html, needs_list) |
| 64 | + is_need_present("REQ_004", html, needs_list) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "test_app", |
| 69 | + [ |
| 70 | + {"buildername": "html", "srcdir": "doc_test/doc_directive_only", "tags": ["tag_b"]}, |
| 71 | + ], |
| 72 | + indirect=True, |
| 73 | +) |
| 74 | +def test_need_excluded_under_only_b(test_app): |
| 75 | + app = test_app |
| 76 | + app.build() |
| 77 | + html = Path(app.outdir, "index.html").read_text() |
| 78 | + needs_list = get_json_needs(Path(app.outdir, "needs.json")) |
| 79 | + |
| 80 | + is_need_present("REQ_000", html, needs_list) |
| 81 | + is_need_present("REQ_001", html, needs_list, False) |
| 82 | + is_need_present("REQ_001_1", html, needs_list, False) |
| 83 | + is_need_present("REQ_002", html, needs_list) |
| 84 | + is_need_present("REQ_003", html, needs_list) |
| 85 | + is_need_present("REQ_004", html, needs_list) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize("test_app", [{"buildername": "html", "srcdir": "doc_test/doc_directive_only"}], indirect=True) |
| 89 | +def test_need_excluded_under_only_no_tag(test_app): |
| 90 | + app = test_app |
| 91 | + app.build() |
| 92 | + html = Path(app.outdir, "index.html").read_text() |
| 93 | + needs_list = get_json_needs(Path(app.outdir, "needs.json")) |
| 94 | + |
| 95 | + is_need_present("REQ_000", html, needs_list) |
| 96 | + is_need_present("REQ_001", html, needs_list, False) |
| 97 | + is_need_present("REQ_001_1", html, needs_list, False) |
| 98 | + is_need_present("REQ_002", html, needs_list, False) |
| 99 | + is_need_present("REQ_003", html, needs_list, False) |
| 100 | + is_need_present("REQ_004", html, needs_list) |
0 commit comments