Skip to content

testing

plantdb.commons.testing Link

This module provides a set of classes useful for testing procedures.

DummyDBTestCase Link

Bases: TestCase

A dummy test database.

Attributes:

Name Type Description
db FSDB

The temporary directory.

tmpclone TemporaryCloneDB

A local temporary copy of a dummy test database.

get_test_db Link

get_test_db(db_path=None)

Return the test FSDB database.

Parameters:

Name Type Description Default
db_path str

If None (default), return the dummy database. Else, should be the location of the source database to clone in the temporary folder.

None

Returns:

Type Description
FSDB

The database to test.

Source code in plantdb/commons/testing.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def get_test_db(self, db_path=None):
    """Return the test ``FSDB`` database.

    Parameters
    ----------
    db_path : str, optional
        If `None` (default), return the dummy database.
        Else, should be the location of the source database to clone in the temporary folder.

    Returns
    -------
    plantdb.commons.FSDB
        The database to test.
    """
    if db_path is not None:
        self.tmpclone = TemporaryCloneDB(db_path)
        self.db = FSDB(self.tmpclone.tmpdir.name)

    return self.db

get_test_fileset Link

get_test_fileset()

Return the default test Fileset object named 'fileset_001'.

Returns:

Type Description
Scan

The default fileset instance to test.

Source code in plantdb/commons/testing.py
122
123
124
125
126
127
128
129
130
131
132
def get_test_fileset(self):
    """Return the default test ``Fileset`` object named 'fileset_001'.

    Returns
    -------
    plantdb.commons.Scan
        The default fileset instance to test.
    """
    scan = self.get_test_scan()
    fileset = scan.get_fileset("fileset_001")
    return fileset

get_test_image_file Link

get_test_image_file()

Return the default test File object named 'test_image'.

Returns:

Type Description
File

The default image file instance to test.

Source code in plantdb/commons/testing.py
134
135
136
137
138
139
140
141
142
143
144
def get_test_image_file(self):
    """Return the default test ``File`` object named 'test_image'.

    Returns
    -------
    plantdb.commons.File
        The default image file instance to test.
    """
    fileset = self.get_test_fileset()
    file = fileset.get_file("test_image")
    return file

get_test_scan Link

get_test_scan()

Return the default test Scan object named 'myscan_001'.

Returns:

Type Description
Scan

The default scan instance to test.

Source code in plantdb/commons/testing.py
110
111
112
113
114
115
116
117
118
119
120
def get_test_scan(self):
    """Return the default test ``Scan`` object named 'myscan_001'.

    Returns
    -------
    plantdb.commons.Scan
        The default scan instance to test.
    """
    db = self.get_test_db()
    scan = db.get_scan("myscan_001")
    return scan

setUp Link

setUp()

Set up a dummy database with fake scan, fileset & files.

Source code in plantdb/commons/testing.py
76
77
78
79
def setUp(self):
    """Set up a dummy database with fake scan, fileset & files."""
    self.db = dummy_db(with_scan=True, with_fileset=True, with_file=True)
    self.tmpclone = None

tearDown Link

tearDown()

Clean up after test.

Source code in plantdb/commons/testing.py
81
82
83
84
85
86
87
88
def tearDown(self):
    """Clean up after test."""
    try:
        self.db.disconnect()
    except:
        return
    from shutil import rmtree
    rmtree(self.db.path(), ignore_errors=True)

FSDBTestCase Link

Bases: TestCase

A local FSDB test database.

Attributes:

Name Type Description
db FSDB

The temporary test database with the 'real_plant_analyzed' dataset.

get_task_fileset Link

get_task_fileset(task_name)

Return the fileset for the corresponding task.

Parameters:

Name Type Description Default
task_name str

The name of the task to get the fileset for.

required

Returns:

Type Description
Fileset

The Fileset corresponding to the given task.

Source code in plantdb/commons/testing.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
def get_task_fileset(self, task_name):
    """Return the fileset for the corresponding task.

    Parameters
    ----------
    task_name : str
        The name of the task to get the fileset for.

    Returns
    -------
    plantdb.commons.fsdb.Fileset
        The ``Fileset`` corresponding to the given task.
    """
    scan = self.get_test_scan()
    return scan.get_fileset(self.get_task_fileset_id(task_name))

get_task_fileset_id Link

get_task_fileset_id(task_name)

Return the fileset id for the corresponding task.

Parameters:

Name Type Description Default
task_name str

The name of the task to get the fileset for.

required

Returns:

Type Description
str

The Fileset name corresponding to the given task.

Source code in plantdb/commons/testing.py
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def get_task_fileset_id(self, task_name):
    """Return the fileset id for the corresponding task.

    Parameters
    ----------
    task_name : str
        The name of the task to get the fileset for.

    Returns
    -------
    str
        The ``Fileset`` name corresponding to the given task.
    """
    scan = self.get_test_scan()
    return locate_task_filesets(scan, [task_name])[task_name]

get_test_db Link

get_test_db()

Return the test FSDB database.

Source code in plantdb/commons/testing.py
169
170
171
172
def get_test_db(self) -> FSDB:
    """Return the test ``FSDB`` database."""
    self.db.connect()
    return self.db

get_test_fileset Link

get_test_fileset(fs_id, create=False)

Return the default test Fileset object named 'fileset_001'.

Returns:

Type Description
Scan

The default fileset instance to test.

Source code in plantdb/commons/testing.py
186
187
188
189
190
191
192
193
194
195
196
def get_test_fileset(self, fs_id, create=False):
    """Return the default test ``Fileset`` object named 'fileset_001'.

    Returns
    -------
    plantdb.commons.Scan
        The default fileset instance to test.
    """
    scan = self.get_test_scan()
    fileset = scan.get_fileset(fs_id, create=create)
    return fileset

get_test_scan Link

get_test_scan()

Return the default test Scan object named 'real_plant_analyzed'.

Returns:

Type Description
Scan

The default Scan instance to test.

Source code in plantdb/commons/testing.py
174
175
176
177
178
179
180
181
182
183
184
def get_test_scan(self):
    """Return the default test ``Scan`` object named 'real_plant_analyzed'.

    Returns
    -------
    plantdb.commons.fsdb.Scan
        The default ``Scan`` instance to test.
    """
    db = self.get_test_db()
    scan = db.get_scan("real_plant_analyzed")
    return scan

setUp Link

setUp()

Set up a test database with the 'real_plant_analyzed' dataset.

Source code in plantdb/commons/testing.py
156
157
158
def setUp(self):
    """Set up a test database with the 'real_plant_analyzed' dataset."""
    self.db = test_database('real_plant_analyzed')

tearDown Link

tearDown()

Clean up after test.

Source code in plantdb/commons/testing.py
160
161
162
163
164
165
166
167
def tearDown(self):
    """Clean up after test."""
    try:
        self.db.disconnect()
    except:
        pass
    from shutil import rmtree
    rmtree(self.db.path(), ignore_errors=True)

TemporaryCloneDB Link

TemporaryCloneDB(db_location)

Bases: object

Class for doing tests on a copy of a local DB.

Parameters:

Name Type Description Default
db_location str

Location of the source database to clone in the temporary folder.

required

Attributes:

Name Type Description
tmpdir TemporaryDirectory

The temporary directory.

Source code in plantdb/commons/testing.py
54
55
56
def __init__(self, db_location):
    self.tmpdir = tempfile.TemporaryDirectory()
    shutil.copytree(db_location, self.tmpdir.name)