File size: 14,713 Bytes
65dfa4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""Tests for SQLite storage layer."""

import pytest

from src.ingestion.base_loader import PaperRecord
from src.storage.sqlite_db import SQLiteDB


class TestSQLiteSchema:
    def test_create_schema(self, tmp_db):
        # Schema should be created without error
        assert tmp_db.get_paper_count() == 0
        assert tmp_db.get_chunk_count() == 0

    def test_create_schema_idempotent(self, tmp_db):
        # Calling create_schema again should not error
        tmp_db.create_schema()
        assert tmp_db.get_paper_count() == 0


class TestPaperIngestion:
    def test_insert_papers(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        assert tmp_db.get_paper_count() == 3

    def test_insert_duplicate_papers_ignored(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        tmp_db.insert_papers(sample_papers)  # same papers again
        assert tmp_db.get_paper_count() == 3

    def test_get_paper_by_id(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        paper = tmp_db.get_paper_by_id("hf_acl_ocl::P18-1001")
        assert paper is not None
        assert paper["title"] == "Attention Is All You Need (Not Really)"
        assert paper["year"] == 2018
        assert paper["venue"] == "acl"
        assert len(paper["authors"]) == 2
        assert "Alice Smith" in paper["authors"]

    def test_get_paper_by_id_not_found(self, tmp_db):
        assert tmp_db.get_paper_by_id("nonexistent") is None

    def test_paper_sources_stored(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        p1 = tmp_db.get_paper_by_id("hf_acl_ocl::P18-1001")
        p2 = tmp_db.get_paper_by_id("acl_anthology::2022.acl-long.100")
        assert p1["source"] == "hf_acl_ocl"
        assert p2["source"] == "acl_anthology"


class TestChunkOperations:
    def test_insert_and_count_chunks(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        chunks = [
            {
                "paper_id": "hf_acl_ocl::P18-1001",
                "chunk_text": "We propose a novel transformer architecture.",
                "chunk_type": "abstract",
                "chunk_index": 0,
                "token_count": 8,
            },
            {
                "paper_id": "hf_acl_ocl::P18-1001",
                "chunk_text": "The model uses self-attention.",
                "chunk_type": "method",
                "chunk_index": 1,
                "token_count": 6,
            },
        ]
        tmp_db.insert_chunks(chunks)
        assert tmp_db.get_chunk_count() == 2

    def test_get_all_chunks_joined(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        chunks = [
            {
                "paper_id": "hf_acl_ocl::P18-1001",
                "chunk_text": "Transformer abstract text.",
                "chunk_type": "abstract",
                "chunk_index": 0,
                "token_count": 4,
            },
        ]
        tmp_db.insert_chunks(chunks)
        results = tmp_db.get_all_chunks()
        assert len(results) == 1
        assert results[0]["title"] == "Attention Is All You Need (Not Really)"
        assert results[0]["year"] == 2018
        assert results[0]["venue"] == "acl"

    def test_get_chunk_texts_and_ids(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        chunks = [
            {"paper_id": "hf_acl_ocl::P18-1001", "chunk_text": "Text A", "chunk_type": "abstract", "chunk_index": 0, "token_count": 2},
            {"paper_id": "hf_acl_ocl::D19-1234", "chunk_text": "Text B", "chunk_type": "abstract", "chunk_index": 0, "token_count": 2},
        ]
        tmp_db.insert_chunks(chunks)
        texts, ids = tmp_db.get_chunk_texts_and_ids()
        assert len(texts) == 2
        assert len(ids) == 2
        assert "Text A" in texts
        assert "Text B" in texts


class TestBrowsePapers:
    def test_browse_all(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        results = tmp_db.browse_papers()
        assert len(results) == 3

    def test_browse_by_venue(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        results = tmp_db.browse_papers(venue="acl")
        assert len(results) == 2
        assert all(r["venue"] == "acl" for r in results)

    def test_browse_by_year(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        results = tmp_db.browse_papers(year=2018)
        assert len(results) == 1
        assert results[0]["year"] == 2018

    def test_browse_limit_offset(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        results = tmp_db.browse_papers(limit=1, offset=0)
        assert len(results) == 1


class TestEnrichmentOperations:
    def test_insert_and_query_methods(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        pid = "hf_acl_ocl::P18-1001"
        tmp_db.insert_methods(pid, [
            {"method_name": "transformer", "method_type": "model"},
            {"method_name": "self-attention", "method_type": "technique"},
        ])
        paper = tmp_db.get_paper_by_id(pid)
        assert len(paper["methods"]) == 2

    def test_insert_and_query_datasets(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        pid = "hf_acl_ocl::P18-1001"
        tmp_db.insert_datasets(pid, [
            {"dataset_name": "WMT14", "task_type": "translation"},
        ])
        paper = tmp_db.get_paper_by_id(pid)
        assert len(paper["datasets"]) == 1
        assert paper["datasets"][0]["dataset_name"] == "WMT14"

    def test_insert_tasks(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        pid = "hf_acl_ocl::P18-1001"
        tmp_db.insert_tasks(pid, ["machine translation", "language modeling"])
        # Tasks are not returned by get_paper_by_id, just verify no error
        assert tmp_db.get_paper_count() == 3

    def test_insert_and_query_topics(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        pid = "hf_acl_ocl::P18-1001"
        tmp_db.insert_topics(pid, ["multimodal", "fairness"])
        # Verify no error and count
        stats = tmp_db.get_enrichment_stats()
        assert stats["total_topics"] == 2

    def test_enrichment_stats(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        pid = "hf_acl_ocl::P18-1001"
        tmp_db.insert_methods(pid, [{"method_name": "BERT", "method_type": "model"}])
        tmp_db.insert_datasets(pid, [{"dataset_name": "SQuAD", "task_type": "QA"}])
        tmp_db.insert_tasks(pid, ["question answering"])
        tmp_db.insert_topics(pid, ["multimodal"])
        stats = tmp_db.get_enrichment_stats()
        assert stats["total_papers"] == 3
        assert stats["total_methods"] == 1
        assert stats["total_datasets"] == 1
        assert stats["total_tasks"] == 1
        assert stats["total_topics"] == 1
        assert stats["papers_with_methods"] == 1


class TestAnalytics:
    def test_papers_per_venue_per_year(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        results = tmp_db.papers_per_venue_per_year()
        assert len(results) > 0
        # Should have venue and year keys
        assert "venue" in results[0]
        assert "year" in results[0]
        assert "paper_count" in results[0]

    def test_top_methods_by_year(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        tmp_db.insert_methods("hf_acl_ocl::P18-1001", [{"method_name": "BERT", "method_type": "model"}])
        tmp_db.insert_methods("hf_acl_ocl::D19-1234", [{"method_name": "BERT", "method_type": "model"}])
        results = tmp_db.top_methods_by_year(top_n=5)
        assert len(results) > 0
        assert results[0]["method_name"] == "BERT"

    def test_method_trend(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        tmp_db.insert_methods("hf_acl_ocl::P18-1001", [{"method_name": "BERT", "method_type": "model"}])
        results = tmp_db.method_trend("BERT")
        assert len(results) == 1
        assert results[0]["year"] == 2018
        assert results[0]["paper_count"] == 1

    def test_method_trend_empty(self, tmp_db):
        results = tmp_db.method_trend("nonexistent")
        assert results == []

    def test_top_datasets_by_year(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        tmp_db.insert_datasets("hf_acl_ocl::P18-1001", [{"dataset_name": "SQuAD", "task_type": "QA"}])
        tmp_db.insert_datasets("hf_acl_ocl::D19-1234", [{"dataset_name": "SQuAD", "task_type": "QA"}])
        results = tmp_db.top_datasets_by_year(top_n=5)
        assert len(results) > 0
        assert results[0]["dataset_name"] == "SQuAD"

    def test_dataset_trend(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        tmp_db.insert_datasets("hf_acl_ocl::P18-1001", [{"dataset_name": "GLUE", "task_type": "NLI"}])
        results = tmp_db.dataset_trend("GLUE")
        assert len(results) == 1
        assert results[0]["year"] == 2018


class TestCooccurrenceAnalytics:
    def _setup_enrichment(self, db, sample_papers):
        """Helper to populate enrichment tables for testing."""
        db.insert_papers(sample_papers)
        # Paper 1: BERT + SQuAD + QA
        db.insert_methods("hf_acl_ocl::P18-1001", [
            {"method_name": "BERT", "method_type": "model"},
            {"method_name": "Transformer", "method_type": "model"},
        ])
        db.insert_datasets("hf_acl_ocl::P18-1001", [
            {"dataset_name": "SQuAD", "task_type": "QA"},
        ])
        db.insert_tasks("hf_acl_ocl::P18-1001", ["machine translation"])
        db.insert_topics("hf_acl_ocl::P18-1001", ["multimodal", "low-resource"])

        # Paper 2: BERT + LoRA + GLUE
        db.insert_methods("hf_acl_ocl::D19-1234", [
            {"method_name": "BERT", "method_type": "model"},
            {"method_name": "LoRA", "method_type": "technique"},
        ])
        db.insert_datasets("hf_acl_ocl::D19-1234", [
            {"dataset_name": "GLUE", "task_type": "NLI"},
            {"dataset_name": "SQuAD", "task_type": "QA"},
        ])
        db.insert_tasks("hf_acl_ocl::D19-1234", ["text classification"])
        db.insert_topics("hf_acl_ocl::D19-1234", ["low-resource", "efficiency"])

        # Paper 3: contrastive learning + no datasets
        db.insert_methods("acl_anthology::2022.acl-long.100", [
            {"method_name": "contrastive learning", "method_type": "technique"},
        ])
        db.insert_tasks("acl_anthology::2022.acl-long.100", ["text classification"])
        db.insert_topics("acl_anthology::2022.acl-long.100", ["multimodal"])

    def test_method_dataset_cooccurrence(self, tmp_db, sample_papers):
        self._setup_enrichment(tmp_db, sample_papers)
        results = tmp_db.method_dataset_cooccurrence(top_n=10)
        assert len(results) > 0
        # BERT+SQuAD should appear (co-occurs in 2 papers)
        top = results[0]
        assert "method_name" in top
        assert "dataset_name" in top
        assert "co_count" in top
        assert top["co_count"] >= 1

    def test_method_task_cooccurrence(self, tmp_db, sample_papers):
        self._setup_enrichment(tmp_db, sample_papers)
        results = tmp_db.method_task_cooccurrence(top_n=10)
        assert len(results) > 0
        assert "method_name" in results[0]
        assert "task_name" in results[0]
        assert "co_count" in results[0]

    def test_top_authors(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        results = tmp_db.top_authors(top_n=5)
        assert len(results) > 0
        assert "name" in results[0]
        assert "paper_count" in results[0]

    def test_author_collaboration_pairs(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        results = tmp_db.author_collaboration_pairs(top_n=5)
        # Paper 1 has 2 authors, paper 3 has 2 authors → at least 2 pairs
        assert len(results) >= 2
        assert "author_a" in results[0]
        assert "author_b" in results[0]
        assert "shared_papers" in results[0]

    def test_top_tasks_by_year(self, tmp_db, sample_papers):
        self._setup_enrichment(tmp_db, sample_papers)
        results = tmp_db.top_tasks_by_year(top_n=5)
        assert len(results) > 0
        assert "task_name" in results[0]
        assert "year" in results[0]
        assert "count" in results[0]

    def test_task_trend(self, tmp_db, sample_papers):
        self._setup_enrichment(tmp_db, sample_papers)
        results = tmp_db.task_trend("text classification")
        assert len(results) > 0
        assert "year" in results[0]
        assert "paper_count" in results[0]

    def test_task_trend_empty(self, tmp_db):
        results = tmp_db.task_trend("nonexistent_task")
        assert results == []

    def test_venue_method_profile(self, tmp_db, sample_papers):
        self._setup_enrichment(tmp_db, sample_papers)
        results = tmp_db.venue_method_profile("acl", top_n=5)
        assert len(results) > 0
        assert "method_name" in results[0]
        assert "paper_count" in results[0]

    def test_venue_method_profile_empty(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        results = tmp_db.venue_method_profile("nonexistent", top_n=5)
        assert results == []

    def test_top_topics_by_year(self, tmp_db, sample_papers):
        self._setup_enrichment(tmp_db, sample_papers)
        results = tmp_db.top_topics_by_year(top_n=5)
        assert len(results) > 0
        assert "topic_name" in results[0]
        assert "year" in results[0]
        assert "count" in results[0]

    def test_topic_trend(self, tmp_db, sample_papers):
        self._setup_enrichment(tmp_db, sample_papers)
        results = tmp_db.topic_trend("multimodal")
        assert len(results) > 0
        assert "year" in results[0]
        assert "paper_count" in results[0]

    def test_topic_trend_empty(self, tmp_db):
        results = tmp_db.topic_trend("nonexistent_topic")
        assert results == []

    def test_year_over_year_growth(self, tmp_db, sample_papers):
        tmp_db.insert_papers(sample_papers)
        results = tmp_db.year_over_year_growth()
        assert len(results) > 0
        assert "year" in results[0]
        assert "paper_count" in results[0]
        # First year has no previous → growth_pct is None
        assert results[0]["growth_pct"] is None
        # Subsequent years should have growth_pct
        if len(results) > 1:
            assert "growth_pct" in results[1]