55from os .path import join as pjoin , dirname
66from io import BytesIO
77from ..testing import suppress_warnings
8+ import sqlite3
89
910with suppress_warnings ():
1011 from .. import dft
@@ -29,6 +30,24 @@ def setUpModule():
2930 raise unittest .SkipTest ('Need pydicom for dft tests, skipping' )
3031
3132
33+ class Test_DBclass :
34+ """Some tests on the database manager class that don't get exercised through the API"""
35+ def setup_method (self ):
36+ self ._db = dft ._DB (fname = ":memory:" , verbose = False )
37+
38+ def test_repr (self ):
39+ assert repr (self ._db ) == "<DFT ':memory:'>"
40+
41+ def test_cursor_conflict (self ):
42+ rwc = self ._db .readwrite_cursor
43+ statement = ("INSERT INTO directory (path, mtime) VALUES (?, ?)" , ("/tmp" , 0 ))
44+ with pytest .raises (sqlite3 .IntegrityError ):
45+ # Whichever exits first will commit and make the second violate uniqueness
46+ with rwc () as c1 , rwc () as c2 :
47+ c1 .execute (* statement )
48+ c2 .execute (* statement )
49+
50+
3251@pytest .fixture
3352def db (monkeypatch ):
3453 """Build a dft database in memory to avoid cross-process races
@@ -41,20 +60,24 @@ def db(monkeypatch):
4160def test_init (db ):
4261 dft .clear_cache ()
4362 dft .update_cache (data_dir )
63+ # Verify a second update doesn't crash
64+ dft .update_cache (data_dir )
4465
4566
4667def test_study (db ):
47- studies = dft .get_studies (data_dir )
48- assert len (studies ) == 1
49- assert (studies [0 ].uid ==
50- '1.3.12.2.1107.5.2.32.35119.30000010011408520750000000022' )
51- assert studies [0 ].date == '20100114'
52- assert studies [0 ].time == '121314.000000'
53- assert studies [0 ].comments == 'dft study comments'
54- assert studies [0 ].patient_name == 'dft patient name'
55- assert studies [0 ].patient_id == '1234'
56- assert studies [0 ].patient_birth_date == '19800102'
57- assert studies [0 ].patient_sex == 'F'
68+ # First pass updates the cache, second pass reads it out
69+ for base_dir in (data_dir , None ):
70+ studies = dft .get_studies (base_dir )
71+ assert len (studies ) == 1
72+ assert (studies [0 ].uid ==
73+ '1.3.12.2.1107.5.2.32.35119.30000010011408520750000000022' )
74+ assert studies [0 ].date == '20100114'
75+ assert studies [0 ].time == '121314.000000'
76+ assert studies [0 ].comments == 'dft study comments'
77+ assert studies [0 ].patient_name == 'dft patient name'
78+ assert studies [0 ].patient_id == '1234'
79+ assert studies [0 ].patient_birth_date == '19800102'
80+ assert studies [0 ].patient_sex == 'F'
5881
5982
6083def test_series (db ):
@@ -83,10 +106,6 @@ def test_storage_instances(db):
83106 '1.3.12.2.1107.5.2.32.35119.2010011420300180088599504.1' )
84107
85108
86- def test_storage_instance (db ):
87- pass
88-
89-
90109@unittest .skipUnless (have_pil , 'could not import PIL.Image' )
91110def test_png (db ):
92111 studies = dft .get_studies (data_dir )
0 commit comments