aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test_llm.py
blob: 8fbaba6b9009fc34f0f26ad52b4492e2bcd4be00 (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
"""Test cases for procedures defined in llms """
# pylint: disable=C0301
import pytest
from gn3.llms.process import fetch_pubmed
from gn3.llms.process import parse_context
from gn3.llms.process import format_bibliography_info


@pytest.mark.unit_test
def test_parse_context():
    """test for parsing doc id context"""
    def mock_get_info(doc_id):
        return f"Info for {doc_id}"

    def mock_format_bib(doc_info):
        return f"Formatted Bibliography: {doc_info}"

    parsed_result = parse_context({
        "doc1": [{"text": "Summary 1"}, {"text": "Summary 2"}],
        "doc2": [{"text": "Summary 3"}, {"text": "Summary 4"}],
    }, mock_get_info, mock_format_bib)

    expected_result = [
        {
            "doc_id": "doc1",
            "bibInfo": "Formatted Bibliography: Info for doc1",
            "comboTxt": "\tSummary 1\tSummary 2",
        },
        {
            "doc_id": "doc2",
            "bibInfo": "Formatted Bibliography: Info for doc2",
            "comboTxt": "\tSummary 3\tSummary 4",
        },
    ]

    assert parsed_result == expected_result

@pytest.mark.unit_test
def test_format_bib_info():
    """Test for formatting bibliography info """
    mock_fahamu_bib_info = [
         {
             "author": "J.m",
             "firstName": "john",
             "title": "Genes and aging",
             "year": 2013,
             "doi": "https://Articles.com/12231"
         },
         "2019-Roy-Evaluation of Sirtuin-3 probe quality and co-expressed genes",
         "2015 - Differential regional and cellular distribution of TFF3 peptide in the human brain.txt"]
    expected_result = [
        "J.m.Genes and aging.2013.https://Articles.com/12231 ",
        "2019-Roy-Evaluation of Sirtuin-3 probe quality and co-expressed genes",
        "2015 - Differential regional and cellular distribution of TFF3 peptide in the human brain"
    ]

    assert all((format_bibliography_info(data) == expected
                for data, expected
                in zip(mock_fahamu_bib_info, expected_result)))


@pytest.mark.unit_test
def test_fetching_pubmed_info(monkeypatch):
    """Test for fetching and populating pubmed data with pubmed info"""
    def mock_load_file(_filename, _dir_path):
        return {
            "12121": {
                "Abstract": "items1",
                "Author": "A1"
            }
        }
    # patch the module with the mocked function

    monkeypatch.setattr("gn3.llms.process.load_file", mock_load_file)
    expected_results = [
        {
            "title": "Genes",
            "year": "2014",
            "doi": "https/article/genes/12121",
            "doc_id": "12121",
            "pubmed": {
                "Abstract": "items1",
                "Author": "A1"
            }
        },
        {
            "title": "Aging",
            "year": "2014",
            "doc_id": "12122"
        }
    ]

    data = [{
            "title": "Genes",
            "year": "2014",
            "doi": "https/article/genes/12121",
            "doc_id": "12121",
            },
            {
            "title": "Aging",
            "year": "2014",
            "doc_id": "12122"
            }]

    assert (fetch_pubmed(data, "/pubmed.json",  "data/")
            == expected_results)