Skip to content

testing

Testing Utilities for PlantDBLink

Provides a collection of helper classes and unittest mix‑ins that simplify writing tests for PlantDB databases. By offering temporary database clones and ready‑made test cases, it enables developers to create isolated, repeatable test environments without affecting the original data.

Key FeaturesLink

  • TemporaryCloneDB: Creates a temporary copy of an existing PlantDB on disk, ensuring tests run against an isolated sandbox that is automatically cleaned up.
  • DummyDBTestCase: A unittest.TestCase subclass that builds a lightweight dummy database containing a sample scan, fileset, and file, with convenience methods to retrieve them.
  • FSDBTestCase: A unittest.TestCase subclass for real‑world datasets; it handles connecting, authenticating, and cleaning up a temporary FSDB instance.
  • Helper methods (get_test_db, get_test_scan, get_test_fileset, get_test_image_file, get_task_fileset, etc.) to quickly access common test objects and locate task‑specific filesets.

Usage ExamplesLink

>>> from plantdb.commons.testing import DummyDBTestCase
>>> class MyTest(DummyDBTestCase):
>>>     def test_scan_properties(self):
>>>         db = self.get_test_db()
>>>         scan = self.get_test_scan()
>>>         # Perform assertions on the scan object
>>>         self.assertEqual(scan.name, "myscan_001")

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 Link

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
151
152
153
154
155
156
157
158
159
160
161
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
163
164
165
166
167
168
169
170
171
172
173
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
139
140
141
142
143
144
145
146
147
148
149
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
105
106
107
108
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
110
111
112
113
114
115
116
117
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 Link

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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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.core.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 Link

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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
198
199
200
201
202
def get_test_db(self) -> FSDB:
    """Return the test ``FSDB`` database."""
    self.db.connect()
    self.db.login('guest', 'guest')
    return self.db

get_test_fileset Link

get_test_fileset(fs_id)

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
216
217
218
219
220
221
222
223
224
225
226
def get_test_fileset(self, fs_id):
    """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)
    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
204
205
206
207
208
209
210
211
212
213
214
def get_test_scan(self):
    """Return the default test ``Scan`` object named 'real_plant_analyzed'.

    Returns
    -------
    plantdb.commons.fsdb.core.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
185
186
187
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
189
190
191
192
193
194
195
196
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 Link

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
83
84
85
def __init__(self, db_location):
    self.tmpdir = tempfile.TemporaryDirectory()
    shutil.copytree(db_location, self.tmpdir.name)