Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lisc/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from lisc.io.db import create_file_structure
from lisc.modutils.dependencies import safe_import

from lisc.tests.tdata import create_term_files, create_api_files
from lisc.tests.tobjs import (TestDB, load_base, load_counts1d, load_counts, load_words,
from lisc.tests.tfiles import create_term_files, create_api_files
from lisc.tests.tdata import (TestDB, load_base, load_counts1d, load_counts, load_words,
load_arts, load_arts_all, load_tag, load_term, load_meta_dict)
from lisc.tests.tsettings import TEST_WAIT_TIME, TESTS_PATH, TEST_DB_PATH, TEST_DB_NAME

Expand All @@ -20,6 +20,8 @@
###################################################################################################
###################################################################################################

## TEST SETUP

def pytest_configure(config):

# Set backend for matplotlib tests, if mpl is available
Expand All @@ -39,6 +41,8 @@ def check_db():
create_term_files(tdb)
create_api_files(tdb)

## TEST OBJECTS

@pytest.fixture(scope='function')
def test_req():
return Requester(wait_time=TEST_WAIT_TIME)
Expand Down
187 changes: 166 additions & 21 deletions lisc/tests/tdata.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,182 @@
"""Utilities to create data files for testing."""
"""Utilties to create tests data and objects."""

import os
from copy import deepcopy
from itertools import repeat

from lisc.io.db import check_directory
from bs4.element import Tag

import numpy as np

from lisc.objects.base import Base
from lisc.objects import Counts1D, Counts, Words
from lisc.data import Articles, ArticlesAll, Term

from lisc.io.db import SCDB

from lisc.tests.tsettings import TEST_DB_PATH

###################################################################################################
###################################################################################################

def create_term_files(directory):
"""Creates some test term files."""
class TestDB(SCDB):
"""Overloads the SCDB object as database object for tests."""

def __init__(self):

# Initialize from normal database object
SCDB.__init__(self, base=TEST_DB_PATH)

def load_tag():
"""Helper function to create a complex bs4 tag for testing."""

tag = Tag(name='Out')
inn1a = Tag(name='Inn1')
inn1b = Tag(name='Inn1')

inn1a.append('words words')
inn1b.append('more words')

tag.append(inn1a)
tag.append(inn1b)

return tag

def load_term():
"""Helper function to create a Term object for testing."""

return Term(label='label', search=['test', 'synonym'],
inclusions=['incl', 'incl_synonym'],
exclusions=['excl', 'excl_synonym'])

def load_meta_dict():
"""Helper function to create a dictionary of meta data information."""

return {
'date': '1999-12-31_00:00:00',
'log': None,
'requester_n_requests': 1,
'requester_wait_time': 1,
'requester_start_time': '00:00:00 Friday 01 January 2000',
'requester_end_time': '00:00:00 Monday 01 January 2000',
'requester_logging': None,
'db_info_dbname': 'pubmed',
'db_info_menuname': 'PubMed',
'db_info_description': 'PubMed bibliographic record',
'db_info_dbbuild': 'Build-#####',
'db_info_count': '123456789',
'db_info_lastupdate': '1999/12/31 00:00',
'settings_setting1' : True,
'settings_setting2' : 42,
}

def load_base(add_terms=False, add_clusions=False, add_labels=False, n_terms=2):
"""Helper function to load Base object for testing."""

base = Base()

if add_terms:
base.add_terms(repeat_data(['test', 'synonym'], n_terms))

if add_clusions:
base.add_terms(repeat_data(['incl', 'incl_synonym'], n_terms), 'inclusions')
base.add_terms(repeat_data(['excl', 'excl_synonym'], n_terms), 'exclusions')

if add_labels:
base.add_labels([val[0] for val in repeat_data(['label'], n_terms)])

return base

def load_counts1d(add_terms=False, add_data=False, n_terms=2):
"""Helper function to load Counts1D object for testing."""

counts1d = Counts1D()

if add_terms:
counts1d.add_terms(repeat_data(['test', 'synonym'], n_terms))

if add_data:
counts1d.counts = np.random.randint(0, 100, (2))

return counts1d

def load_counts(add_terms=False, add_data=False, n_terms=(2, 2)):
"""Helper function to load Counts object for testing."""

counts = Counts()

if add_terms:
counts1d.add_terms(repeat_data(['test', 'synonym'], n_terms[0]), dim='A')
counts1d.add_terms(repeat_data(['test', 'synonym'], n_terms[1]), dim='B')

Check warning on line 109 in lisc/tests/tdata.py

View check run for this annotation

Codecov / codecov/patch

lisc/tests/tdata.py#L108-L109

Added lines #L108 - L109 were not covered by tests

if add_data:
counts.terms['A'].counts = np.random.randint(0, 100, (n_terms[0]))
counts.terms['B'].counts = np.random.randint(0, 100, (n_terms[1]))
counts.counts = np.random.randint(0, 100, n_terms)

Check warning on line 114 in lisc/tests/tdata.py

View check run for this annotation

Codecov / codecov/patch

lisc/tests/tdata.py#L112-L114

Added lines #L112 - L114 were not covered by tests

return counts

def load_words(add_terms=False, add_data=False, n_terms=2):
"""Helper function to load Words object for testing."""

words = Words()

if add_terms:
words.add_terms(repeat_data(['test', 'synonym'], n_terms))

if add_data:
arts = load_arts(add_data=True, n_data=2)
words.results = [deepcopy(arts) for ind in range(n_terms)]

return words

def load_arts(add_data=False, n_data=1, add_none=False):
"""Helper function to load Articles object for testing."""

arts = Articles(Term('label', ['search'], ['inclusion'], ['exclusion']))

if add_data:
for ind in range(n_data):

path = check_directory(directory, 'terms')
arts.add_data('ids', ind)
arts.add_data('titles', 'title')
arts.add_data('journals', ['science', 'sc'])
arts.add_data('authors', [['ln1', 'fn1', 'in1', 'af1'], ['ln2', 'fn2', 'in2', 'af2']])
arts.add_data('words', 'Lots of words data. Just a continuous text.')
arts.add_data('keywords', ['lots', 'of', 'keywords'])
arts.add_data('years', 2112)
arts.add_data('dois', 'doi_str')

with open(os.path.join(path, 'test_terms.txt'), 'w') as term_file:
term_file.write('word\nthing, same')
if add_none:
arts.dois[-1] = None
arts.years[-1] = None
arts.authors[-1] = None
arts.words[-1] = str()
arts.keywords[-1] = list()

with open(os.path.join(path, 'test_inclusions.txt'), 'w') as incl_file:
incl_file.write('need\nrequired')
return arts

with open(os.path.join(path, 'test_exclusions.txt'), 'w') as excl_file:
excl_file.write('not\navoid')
def load_arts_all():
"""Helper function to load ArticlesAll object for testing."""

with open(os.path.join(path, 'test_exclusions_line.txt'), 'w') as excl_file2:
excl_file2.write('not\n')
arts = load_arts(add_data=True, n_data=2)
arts_all = ArticlesAll(arts)

with open(os.path.join(path, 'test_labels.txt'), 'w') as labels_file:
labels_file.write('label1\nlabel2')
return arts_all

def repeat_data(terms, n_times):
"""Repeat a list of data, appending index number, a specified number of times.

def create_api_files(directory):
"""Create test API key file."""
Parameters
----------
terms : list of str
List of elements to repeat.
n_times : int
Number of times to repeat.

path = check_directory(directory, 'base')
Returns
-------
list of list of str
List of repeated elements.
"""

with open(os.path.join(path, 'api_key.txt'), 'w') as term_file:
term_file.write('123abc')
return [[val + str(ind) for val in vals] for ind, vals in enumerate(repeat(terms, n_times))]
37 changes: 37 additions & 0 deletions lisc/tests/tfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Utilities to create data files for testing."""

import os

from lisc.io.db import check_directory

###################################################################################################
###################################################################################################

def create_term_files(directory):
"""Creates some test term files."""

path = check_directory(directory, 'terms')

with open(os.path.join(path, 'test_terms.txt'), 'w') as term_file:
term_file.write('word\nthing, same')

with open(os.path.join(path, 'test_inclusions.txt'), 'w') as incl_file:
incl_file.write('need\nrequired')

with open(os.path.join(path, 'test_exclusions.txt'), 'w') as excl_file:
excl_file.write('not\navoid')

with open(os.path.join(path, 'test_exclusions_line.txt'), 'w') as excl_file2:
excl_file2.write('not\n')

with open(os.path.join(path, 'test_labels.txt'), 'w') as labels_file:
labels_file.write('label1\nlabel2')


def create_api_files(directory):
"""Create test API key file."""

path = check_directory(directory, 'base')

with open(os.path.join(path, 'api_key.txt'), 'w') as term_file:
term_file.write('123abc')
Loading
Loading