1#!/usr/bin/env python
2# coding=utf-8
3
4"""Unit tests"""
5
6import logging
7import tempfile
8
9logger = logging.getLogger(__name__)
10
11TESTDB_TMPDIR = None
12_URL = (
13 "http://www.idiap.ch/software/bob/data/bob/bob.ip.binseg/master/_testdb.zip"
14)
15_RCKEY = "bob.ip.binseg.stare.datadir"
16
17
18def teardown_package():
19 global TESTDB_TMPDIR
20 if TESTDB_TMPDIR is not None:
21 logger.info(f"Removing temporary directory {TESTDB_TMPDIR.name}...")
22 TESTDB_TMPDIR.cleanup()
23
24
25def mock_dataset():
26 global TESTDB_TMPDIR
27 from bob.extension import rc
28
29 if (TESTDB_TMPDIR is not None) or (_RCKEY in rc):
30 logger.info("Test database already set up - not downloading")
31 else:
32 logger.info("Test database not available, downloading...")
33 import urllib.request
34 import zipfile
35
36 # Download the file from `url` and save it locally under `file_name`:
37 with urllib.request.urlopen(_URL) as r, tempfile.TemporaryFile() as f:
38 f.write(r.read())
39 f.flush()
40 f.seek(0)
41 TESTDB_TMPDIR = tempfile.TemporaryDirectory(prefix=__name__ + "-")
42 print(f"Creating test database at {TESTDB_TMPDIR.name}...")
43 logger.info(f"Creating test database at {TESTDB_TMPDIR.name}...")
44 with zipfile.ZipFile(f) as zf:
45 zf.extractall(TESTDB_TMPDIR.name)
46
47 from ...binseg.data import stare
48
49 if TESTDB_TMPDIR is None:
50 # if the user has the STARE directory ready, then we do a normal return
51 return rc["bob.ip.binseg.stare.datadir"], stare.dataset
52
53 # else, we do a "mock" return
54 return (
55 TESTDB_TMPDIR.name,
56 stare._make_dataset(TESTDB_TMPDIR.name),
57 )