Debian Codegraph @

tests/test_coverage_improvements.py

raw symbols SCIP
1 """
2 Additional tests to improve coverage for atoms, shelx, cards, and misc modules.
3 """
4 import tempfile
5 import unittest
6 from pathlib import Path
7 from unittest import TestCase
8
9 from shelxfile import Shelxfile
10 from shelxfile.atoms.pairs import AtomPair
11 from shelxfile.misc.misc import (
12 split_fvar_and_parameter, flatten, remove_file, multiline_test, wrap_line
13 )
14 from shelxfile.shelx.cards import (
15 DAMP, HKLF, SUMP, BLOC, BOND, BIND, REM, FMAP, MOVE, MERG, SIZE,
16 HTAB, GRID, SHEL, STIR, TWIN, SWAT, WIGL, WPDB, PRIG, CONF,
17 XNPD, FVAR, ACTA, Restraints, SAME, RIGU, SIMU, DELU, ISOR, FLAT, BUMP, CHIV, EADP, EXYZ,
18 RTAB, HFIX, MORE, FREE,
19 )
20
21
22 # =============================================================================
23 # Tests for shelxfile/atoms/atom.py
24 # =============================================================================
25
26 class TestAtomProperties(TestCase):
27 """Tests for uncovered Atom properties."""
28
29 def setUp(self):
30 self.shx = Shelxfile()
31 self.shx.read_file('tests/resources/p21c.res')
32
33 def test_atomid_regular_atom(self):
34 a = self.shx.atoms.get_atom_by_name('O1_4')
35 self.assertGreater(a.atomid, 0)
36
37 def test_fullname_with_residue(self):
38 a = self.shx.atoms.get_atom_by_name('F1_2')
39 self.assertEqual('F1_2', a.fullname)
40
41 def test_fullname_short_residue_zero(self):
42 a = self.shx.atoms.get_atom_by_name('Ga1')
43 self.assertEqual('GA1', a.fullname_short)
44
45 def test_fullname_short_with_residue(self):
46 a = self.shx.atoms.get_atom_by_name('F1_2')
47 self.assertEqual('F1_2', a.fullname_short)
48
49 def test_chain_id(self):
50 a = self.shx.atoms.get_atom_by_name('C1_4')
51 # chain_id should be None for most atoms in this file
52 self.assertIsNone(a.chain_id)
53
54 def test_fvar(self):
55 a = self.shx.atoms.get_atom_by_name('C1_4')
56 # sof is -31.0 => fvar is -3
57 self.assertEqual(-3, a.fvar)
58
59 def test_fvar_regular(self):
60 a = self.shx.atoms.get_atom_by_name('Ga1')
61 # sof is 11.0 => fvar = 1
62 self.assertEqual(1, a.fvar)
63
64 def test_occupancy_positive_fvar(self):
65 a = self.shx.atoms.get_atom_by_name('C1_1')
66 # This atom has positive fvar occupancy
67 occ = a.occupancy
68 self.assertIsInstance(occ, float)
69 self.assertGreater(occ, 0)
70
71 def test_occupancy_negative_fvar(self):
72 a = self.shx.atoms.get_atom_by_name('C1_4')
73 # negative sof => should use negative fvar calc
74 occ = a.occupancy
75 self.assertIsInstance(occ, float)
76 self.assertGreater(occ, 0)
77
78 def test_occupancy_setter(self):
79 a = self.shx.atoms.get_atom_by_name('C1_4')
80 a.occupancy = 0.75
81 self.assertEqual(0.75, a._occupancy)
82
83 def test_is_hydrogen_true(self):
84 a = self.shx.atoms.get_atom_by_name('H34')
85 self.assertTrue(a.is_hydrogen)
86
87 def test_is_hydrogen_false(self):
88 a = self.shx.atoms.get_atom_by_name('C1_4')
89 self.assertFalse(a.is_hydrogen)
90
91 def test_ishydrogen_alias(self):
92 a = self.shx.atoms.get_atom_by_name('H34')
93 self.assertTrue(a.ishydrogen)
94
95 def test_is_isotropic_true(self):
96 a = self.shx.atoms.get_atom_by_name('H34')
97 self.assertTrue(a.is_isotropic)
98
99 def test_is_isotropic_false(self):
100 a = self.shx.atoms.get_atom_by_name('C1_4')
101 self.assertFalse(a.is_isotropic)
102
103 def test_uvals_as_list(self):
104 a = self.shx.atoms.get_atom_by_name('C1_4')
105 ulist = a.uvals_as_list
106 self.assertEqual(3, len(ulist))
107 self.assertEqual(3, len(ulist[0]))
108 # Check symmetry: U12 == U21 etc.
109 self.assertEqual(ulist[0][1], ulist[1][0]) # U12 == U21
110 self.assertEqual(ulist[0][2], ulist[2][0]) # U13 == U31
111 self.assertEqual(ulist[1][2], ulist[2][1]) # U23 == U32
112
113 def test_atom_iter(self):
114 a = self.shx.atoms.get_atom_by_name('C1_4')
115 parts = list(a)
116 self.assertIn('Atom', parts)
117
118 def test_atom_repr(self):
119 a = self.shx.atoms.get_atom_by_name('C1_4')
120 self.assertTrue(repr(a).startswith('Atom ID:'))
121
122 def test_frac_coords_setter(self):
123 a = self.shx.atoms.get_atom_by_name('C1_4')
124 a.frac_coords = [0.1, 0.2, 0.3]
125 self.assertEqual((0.1, 0.2, 0.3), a.frac_coords)
126
127 def test_element_property(self):
128 a = self.shx.atoms.get_atom_by_name('F1_2')
129 self.assertEqual('F', a.element)
130
131 def test_an_property(self):
132 a = self.shx.atoms.get_atom_by_name('O1')
133 self.assertEqual(8, a.an)
134
135 def test_radius(self):
136 a = self.shx.atoms.get_atom_by_name('O1')
137 self.assertEqual(0.73, a.radius)
138
139 def test_ueq(self):
140 a = self.shx.atoms.get_atom_by_name('Ga1')
141 ueq = a.ueq
142 self.assertIsInstance(ueq, float)
143 self.assertGreater(ueq, 0)
144
145 def test_ucif(self):
146 a = self.shx.atoms.get_atom_by_name('Ga1')
147 ucif = a.ucif
148 self.assertIsNotNone(ucif)
149
150 def test_qpeak_atom(self):
151 qpeaks = self.shx.atoms.q_peaks
152 self.assertGreater(len(qpeaks), 0)
153 q = qpeaks[0]
154 self.assertTrue(q.qpeak)
155 self.assertIn('Q', str(q))
156
157
158 class TestAtomNameProperty(TestCase):
159 def setUp(self):
160 self.shx = Shelxfile()
161 self.shx.read_file('tests/resources/p21c.res')
162
163 def test_name_getter(self):
164 a = self.shx.atoms.get_atom_by_name('C1_4')
165 self.assertEqual('C1', a.name)
166
167 def test_name_setter_valid(self):
168 a = self.shx.atoms.get_atom_by_name('C1_4')
169 a.name = 'C99'
170 self.assertEqual('C99', a.name)
171
172 def test_name_setter_invalid(self):
173 a = self.shx.atoms.get_atom_by_name('C1_4')
174 a.name = 'C99_3' # Invalid: contains underscore
175 self.assertEqual('C1', a.name) # Should stay C1
176
177
178 # =============================================================================
179 # Tests for shelxfile/atoms/atoms.py
180 # =============================================================================
181
182 class TestAtomsCollection(TestCase):
183 """Tests for Atoms class uncovered methods."""
184
185 def setUp(self):
186 self.shx = Shelxfile()
187 self.shx.read_file('tests/resources/p21c.res')
188
189 def test_repr_non_empty(self):
190 r = repr(self.shx.atoms)
191 self.assertIn('O1', r)
192
193 def test_repr_empty(self):
194 shx = Shelxfile()
195 r = repr(shx.atoms)
196 self.assertEqual('No Atoms in file.', r)
197
198 def test_getitem(self):
199 a = self.shx.atoms.get_atom_by_name('C1_4')
200 atomid = a.atomid
201 same_atom = self.shx.atoms[atomid]
202 self.assertEqual(str(a), str(same_atom))
203
204 def test_len(self):
205 self.assertEqual(148, len(self.shx.atoms))
206
207 def test_get_atom_by_name_none_for_gt(self):
208 self.assertIsNone(self.shx.atoms.get_atom_by_name('>'))
209
210 def test_get_atom_by_name_none_for_lt(self):
211 self.assertIsNone(self.shx.atoms.get_atom_by_name('<'))
212
213 def test_get_atom_by_name_not_found(self):
214 self.assertIsNone(self.shx.atoms.get_atom_by_name('ZZZZZ'))
215
216 def test_get_atom_by_id_not_found(self):
217 self.assertIsNone(self.shx.atoms.get_atom_by_id(99999))
218
219 def test_nameslist(self):
220 names = self.shx.atoms.nameslist
221 self.assertIsInstance(names, tuple)
222 self.assertIn('O1_4', names)
223 self.assertIn('GA1_0', names)
224
225 def test_distance_not_found(self):
226 d = self.shx.atoms.distance('NONEXISTENT1', 'NONEXISTENT2')
227 self.assertEqual(0.0, d)
228
229 def test_has_atom_false(self):
230 self.assertFalse(self.shx.atoms.has_atom('XXXXXXX'))
231
232
233 # =============================================================================
234 # Tests for shelxfile/atoms/pairs.py
235 # =============================================================================
236
237 class TestAtomPair(TestCase):
238 def test_repr_both_atoms(self):
239 p = AtomPair('C1', 'C2')
240 self.assertEqual('C1 C2', repr(p))
241
242 def test_repr_missing_atom(self):
243 p = AtomPair(None, 'C2')
244 self.assertEqual('', repr(p))
245
246 def test_len_both(self):
247 p = AtomPair('C1', 'C2')
248 self.assertEqual(2, len(p))
249
250 def test_len_none(self):
251 p = AtomPair(None, 'C2')
252 self.assertEqual(0, len(p))
253
254 def test_len_both_none(self):
255 p = AtomPair(None, None)
256 self.assertEqual(0, len(p))
257
258
259 # =============================================================================
260 # Tests for shelxfile/shelx/shelx.py
261 # =============================================================================
262
263 class TestShelxfileOperations(TestCase):
264 """Tests for Shelxfile methods with uncovered lines."""
265
266 def setUp(self):
267 self.shx = Shelxfile()
268 self.shx.read_file('tests/resources/p21c.res')
269
270 def test_verbose_and_debug_raises(self):
271 with self.assertRaises(ValueError):
272 Shelxfile(verbose=True, debug=True)
273
274 def test_add_atom(self):
275 count_before = len(self.shx.atoms)
276 self.shx.add_atom(name='C99', coordinates=[0.1, 0.2, 0.3], element='C',
277 uvals=[0.04, 0.0, 0.0, 0.0, 0.0, 0.0])
278 # The atom list should be repopulated on next access:
279 # Since add_atom uses _append_card at position 0, let's check it was added
280 found = self.shx.atoms.get_atom_by_name('C99')
281 self.assertIsNotNone(found)
282 self.assertEqual('C99', found.name)
283 self.assertGreater(len(self.shx.atoms), count_before)
284
285 def test_add_atom_with_part(self):
286 self.shx.add_atom(name='N1', coordinates=[0.5, 0.5, 0.5], element='N', part=1, sof=21.0,
287 uvals=[0.04, 0.0, 0.0, 0.0, 0.0, 0.0])
288 found = self.shx.atoms.get_atom_by_name('N1')
289 self.assertIsNotNone(found)
290
291 def test_frac_to_cart(self):
292 result = self.shx.frac_to_cart([0.1, 0.2, 0.3])
293 self.assertEqual(3, len(result))
294
295 def test_repr(self):
296 r = repr(self.shx)
297 self.assertIn('TITL', r)
298 self.assertIn('CELL', r)
299
300 def test_sum_formula(self):
301 sf = self.shx.sum_formula
302 self.assertIn('C', sf)
303 self.assertIsNotNone(self.shx.formula_weight)
304
305 def test_sum_formula_exact(self):
306 sf = self.shx.sum_formula_exact
307 self.assertIn('C', sf)
308 self.assertIn('H', sf)
309
310 def test_sum_formula_exact_as_dict(self):
311 d = self.shx.sum_formula_exact_as_dict()
312 self.assertIsInstance(d, dict)
313 self.assertIn('C', d)
314
315 def test_insert_anis_default(self):
316 shx = Shelxfile()
317 shx.read_file('tests/resources/p21c.res')
318 shx.insert_anis()
319 # Check that ANIS was inserted
320 found = any('ANIS' in str(x) for x in shx._reslist)
321 self.assertTrue(found)
322
323 def test_insert_anis_with_atoms(self):
324 shx = Shelxfile()
325 shx.read_file('tests/resources/p21c.res')
326 shx.insert_anis(atoms='C1 C2')
327 found = any('ANIS C1 C2' in str(x) for x in shx._reslist)
328 self.assertTrue(found)
329
330 def test_insert_anis_with_residue(self):
331 shx = Shelxfile()
332 shx.read_file('tests/resources/p21c.res')
333 shx.insert_anis(atoms='$C', residue='CCF3')
334 found = any('ANIS_CCF3 $C' in str(x) for x in shx._reslist)
335 self.assertTrue(found)
336
337 def test_add_line(self):
338 shx = Shelxfile()
339 shx.read_file('tests/resources/p21c.res')
340 old_len = len(shx._reslist)
341 shx.add_line(5, 'REM Test Line')
342 self.assertEqual(old_len + 1, len(shx._reslist))
343 self.assertEqual('REM Test Line', shx._reslist[6])
344
345 def test_replace_line(self):
346 shx = Shelxfile()
347 shx.read_file('tests/resources/p21c.res')
348 old_plan = shx.plan
349 shx.replace_line(old_plan, 'PLAN 99')
350 idx = shx.index_of('PLAN 99')
351 self.assertGreater(idx, 0)
352
353 def test_index_of(self):
354 idx = self.shx.index_of(self.shx.plan)
355 self.assertGreater(idx, 0)
356
357 def test_weight_converged_true(self):
358 self.assertTrue(self.shx._weight_converged([0.0, 0.0]))
359
360 def test_weight_converged_false(self):
361 self.assertFalse(self.shx._weight_converged([0.1, 0.0]))
362
363 def test_update_weight(self):
364 shx = Shelxfile()
365 shx.read_file('tests/resources/p21c.res')
366 # No suggested weight, should just return
367 shx.wght_suggested = None
368 shx.update_weight() # Should not raise
369
370 def test_is_atom_coordinates_unrealistic(self):
371 self.assertTrue(Shelxfile._coordinates_are_unrealistic(['O1', '3', '5.0', '0.5', '0.5']))
372 self.assertFalse(Shelxfile._coordinates_are_unrealistic(['O1', '3', '0.5', '0.5', '0.5']))
373
374 def test_write_shelx_file_no_reslist(self):
375 shx = Shelxfile()
376 # No file loaded, _reslist is empty
377 result = shx.write_shelx_file('nonexistent.ins')
378 self.assertIsNone(result)
379
380 def test_write_shelx_file(self):
381 with tempfile.NamedTemporaryFile(suffix='.ins', delete=False) as f:
382 tmppath = Path(f.name)
383 try:
384 self.shx.write_shelx_file(str(tmppath))
385 self.assertTrue(tmppath.exists())
386 content = tmppath.read_text()
387 self.assertIn('TITL', content)
388 self.assertIn('CELL', content)
389 finally:
390 tmppath.unlink(missing_ok=True)
391
392 def test_is_atom_blank_line(self):
393 self.assertFalse(Shelxfile.is_atom(''))
394
395 def test_is_atom_with_exclamation(self):
396 self.assertFalse(Shelxfile.is_atom('O1 3 0.1 0.2 0.3 !'))
397
398
399 class TestShelxfileResiduals(TestCase):
400
401 def setUp(self):
402 self.shx = Shelxfile()
403 self.shx.read_file('tests/resources/p21c.res')
404
405 def test_r1(self):
406 self.assertEqual(0.04, self.shx.R1)
407
408 def test_wr2(self):
409 self.assertEqual(0.1005, self.shx.wr2)
410
411 def test_goof(self):
412 self.assertEqual(1.016, self.shx.goof)
413
414 def test_space_group(self):
415 self.assertEqual('P2(1)/c', self.shx.space_group)
416
417 def test_wavelength(self):
418 self.assertEqual(0.71073, self.shx.wavelength)
419
420 def test_temp(self):
421 self.assertAlmostEqual(-173.18, self.shx.temp, places=2)
422
423 def test_temp_in_kelvin(self):
424 self.assertAlmostEqual(99.97, self.shx.temp_in_kelvin, places=2)
425
426 def test_data(self):
427 self.assertEqual(10786, self.shx.data)
428
429 def test_parameters(self):
430 self.assertEqual(945, self.shx.parameters)
431
432 def test_dat_to_param(self):
433 self.assertAlmostEqual(11.4138, self.shx.dat_to_param, places=3)
434
435 def test_num_restraints(self):
436 self.assertEqual(1842, self.shx.num_restraints)
437
438 def test_highest_peak(self):
439 self.assertAlmostEqual(0.407, self.shx.highest_peak, places=3)
440
441 def test_deepest_hole(self):
442 self.assertAlmostEqual(-0.691, self.shx.deepest_hole, places=3)
443
444
445 # =============================================================================
446 # Tests for shelxfile/shelx/cards.py
447 # =============================================================================
448
449 class TestRestraintsClass(TestCase):
450 def test_empty_restraints(self):
451 r = Restraints()
452 self.assertEqual('No Restraints in file.', repr(r))
453
454 def test_restraints_getitem(self):
455 shx = Shelxfile()
456 shx.read_file('tests/resources/p21c.res')
457 r = shx.restraints[0]
458 self.assertIsNotNone(r)
459
460 def test_restraints_iter(self):
461 shx = Shelxfile()
462 shx.read_file('tests/resources/p21c.res')
463 count = sum(1 for _ in shx.restraints)
464 self.assertGreater(count, 0)
465
466
467 class TestDAMP(TestCase):
468 def test_damp_with_limse(self):
469 shx = Shelxfile()
470 d = DAMP(shx, 'DAMP 0.5 20'.split())
471 self.assertEqual(0.5, d.damp)
472 self.assertEqual(20, d.limse)
473 self.assertEqual('DAMP 0.5 20', repr(d))
474
475 def test_damp_without_limse(self):
476 shx = Shelxfile()
477 d = DAMP(shx, 'DAMP 0.7'.split())
478 self.assertEqual(0.7, d.damp)
479 self.assertEqual(0, d.limse)
480 self.assertEqual('DAMP 0.7', repr(d))
481
482
483 class TestHKLF(TestCase):
484 def test_hklf_basic(self):
485 shx = Shelxfile()
486 h = HKLF(shx, 'HKLF 4'.split())
487 self.assertEqual(4, h.n)
488 self.assertEqual(1, h.s)
489 self.assertEqual([1, 0, 0, 0, 1, 0, 0, 0, 1], h.matrix)
490
491 def test_hklf_repr(self):
492 shx = Shelxfile()
493 h = HKLF(shx, 'HKLF 4'.split())
494 self.assertIn('HKLF 4', repr(h))
495
496
497 class TestSUMP(TestCase):
498 def test_sump_basic(self):
499 shx = Shelxfile()
500 shx.read_file('tests/resources/p21c.res')
501 s = SUMP(shx, 'SUMP 1 0.001 1 2 1 3'.split())
502 self.assertEqual(1.0, s.c)
503 self.assertEqual(0.001, s.sigma)
504 self.assertEqual([1.0, 2], s[0])
505 self.assertEqual([1.0, 3], s[1])
506
507
508 class TestBLOC(TestCase):
509 def test_bloc(self):
510 shx = Shelxfile()
511 b = BLOC(shx, 'BLOC 1 5 C1 C2'.split())
512 self.assertEqual(1, b.n1)
513 self.assertEqual(5, b.n2)
514 self.assertEqual(['C1', 'C2'], b.atoms)
515
516
517 class TestBOND(TestCase):
518 def test_bond(self):
519 shx = Shelxfile()
520 b = BOND(shx, 'BOND C1 C2 C3'.split())
521 self.assertEqual(['C1', 'C2', 'C3'], b.atoms)
522
523
524 class TestBIND(TestCase):
525 def test_bind(self):
526 shx = Shelxfile()
527 b = BIND(shx, 'BIND C1 C2'.split())
528 self.assertEqual(['C1', 'C2'], b.atoms)
529
530
531 class TestREM(TestCase):
532 def test_rem(self):
533 shx = Shelxfile()
534 r = REM(shx, 'REM This is a remark'.split())
535 self.assertEqual('REM This is a remark', str(r))
536
537
538 class TestFMAP(TestCase):
539 def test_fmap_all_params(self):
540 shx = Shelxfile()
541 f = FMAP(shx, 'FMAP 2 1 53'.split())
542 self.assertEqual(2, f.code)
543 self.assertEqual(1, f.axis)
544 self.assertEqual(53, f.nl)
545
546
547 class TestMOVE(TestCase):
548 def test_move(self):
549 shx = Shelxfile()
550 m = MOVE(shx, 'MOVE 0.1 0.2 0.3 1'.split())
551 self.assertEqual([0.1, 0.2, 0.3], m.dxdydz)
552 self.assertEqual(1, m.sign)
553
554
555 class TestMERG(TestCase):
556 def test_merg(self):
557 shx = Shelxfile()
558 m = MERG(shx, 'MERG 3'.split())
559 self.assertEqual(3, m.n)
560
561
562 class TestSIZE(TestCase):
563 def test_size_all_params(self):
564 shx = Shelxfile()
565 shx.read_file('tests/resources/p21c.res')
566 self.assertIsNotNone(shx.size)
567 self.assertEqual(0.12, shx.size.dx)
568 self.assertEqual(0.23, shx.size.dy)
569 self.assertEqual(0.33, shx.size.dz)
570 self.assertEqual(0.33, shx.size.max)
571 self.assertEqual(0.23, shx.size.mid)
572 self.assertEqual(0.12, shx.size.min)
573
574 def test_size_as_text(self):
575 shx = Shelxfile()
576 s = SIZE(shx, 'SIZE 0.1 0.2 0.3'.split())
577 self.assertEqual('SIZE 0.1 0.2 0.3', s._as_text())
578
579
580 class TestHTAB(TestCase):
581 def test_htab_dh(self):
582 shx = Shelxfile()
583 h = HTAB(shx, 'HTAB 2.5'.split())
584 self.assertEqual(2.5, h.dh)
585
586 def test_htab_atoms(self):
587 shx = Shelxfile()
588 h = HTAB(shx, 'HTAB O1 N1'.split())
589 self.assertEqual('O1', h.donor)
590 self.assertEqual('N1', h.acceptor)
591
592
593 class TestGRID(TestCase):
594 def test_grid(self):
595 shx = Shelxfile()
596 g = GRID(shx, 'GRID 1.0 2.0 3.0 4.0 5.0 6.0'.split())
597 self.assertEqual(1.0, g.sl)
598 self.assertEqual(2.0, g.sa)
599 self.assertEqual(3.0, g.sd)
600 self.assertEqual(4.0, g.dl)
601 self.assertEqual(5.0, g.da)
602 self.assertEqual(6.0, g.dd)
603
604
605 class TestSHEL(TestCase):
606 def test_shel(self):
607 shx = Shelxfile()
608 s = SHEL(shx, 'SHEL 99.0 0.5'.split())
609 self.assertEqual(99.0, s.lowres)
610 self.assertEqual(0.5, s.highres)
611
612
613 class TestSTIR(TestCase):
614 def test_stir(self):
615 shx = Shelxfile()
616 s = STIR(shx, 'STIR 0.8 0.02'.split())
617 self.assertEqual(0.8, s.sres)
618 self.assertEqual(0.02, s.step)
619
620
621 class TestTWIN(TestCase):
622 def test_twin(self):
623 shx = Shelxfile()
624 t = TWIN(shx, 'TWIN -1 0 0 0 -1 0 0 0 -1 2'.split())
625 self.assertEqual([-1, 0, 0, 0, -1, 0, 0, 0, -1], t.matrix)
626 self.assertEqual(2, t.n_value)
627 self.assertEqual(1, t.allowed_N)
628
629
630 class TestSWAT(TestCase):
631 def test_swat(self):
632 shx = Shelxfile()
633 s = SWAT(shx, 'SWAT 0.5 3.0'.split())
634 self.assertEqual(0.5, s.g)
635 self.assertEqual(3.0, s.U)
636
637
638 class TestWIGL(TestCase):
639 def test_wigl(self):
640 shx = Shelxfile()
641 w = WIGL(shx, 'WIGL 0.3 0.4'.split())
642 self.assertEqual(0.3, w.d)
643 self.assertEqual(0.4, w.dU)
644
645
646 class TestWPDB(TestCase):
647 def test_wpdb(self):
648 shx = Shelxfile()
649 w = WPDB(shx, 'WPDB 2'.split())
650 self.assertEqual(2, w.n)
651
652
653 class TestPRIG(TestCase):
654 def test_prig(self):
655 shx = Shelxfile()
656 p = PRIG(shx, 'PRIG 5'.split())
657 self.assertEqual(5, p.p)
658
659
660 class TestCONF(TestCase):
661 def test_conf(self):
662 shx = Shelxfile()
663 c = CONF(shx, 'CONF'.split())
664 self.assertEqual('CONF', str(c))
665
666
667 class TestXNPD(TestCase):
668 def test_xnpd_default(self):
669 shx = Shelxfile()
670 x = XNPD(shx, 'XNPD'.split())
671 self.assertEqual(-0.001, x.Umin)
672
673 def test_xnpd_custom(self):
674 shx = Shelxfile()
675 x = XNPD(shx, 'XNPD -0.005'.split())
676 self.assertEqual(-0.005, x.Umin)
677
678
679 class TestFVAR(TestCase):
680 def test_fvar_str(self):
681 f = FVAR(1, 0.5)
682 self.assertEqual('0.5', str(f))
683
684 def test_fvar_repr(self):
685 f = FVAR(2, 0.753)
686 self.assertEqual('0.753', repr(f))
687
688
689 class TestFVARs(TestCase):
690 def setUp(self):
691 self.shx = Shelxfile()
692 self.shx.read_file('tests/resources/p21c.res')
693
694 def test_len(self):
695 self.assertGreater(len(self.shx.fvars), 0)
696
697 def test_getitem(self):
698 # SHELXL counts fvars from 1
699 val = self.shx.fvars[1]
700 self.assertIsNotNone(val)
701
702 def test_str(self):
703 s = str(self.shx.fvars)
704 self.assertIn('FVAR', s)
705
706 def test_repr(self):
707 r = repr(self.shx.fvars)
708 self.assertIsNotNone(r)
709
710 def test_fvars_used(self):
711 result = self.shx.fvars.fvars_used()
712 self.assertIsInstance(result, dict)
713
714
715 class TestACTAClass(TestCase):
716 def test_acta_with_twotheta(self):
717 shx = Shelxfile()
718 shx.read_file('tests/resources/p21c.res')
719 a = ACTA(shx, 'ACTA 45'.split())
720 self.assertEqual('ACTA 45', repr(a))
721 self.assertEqual('ACTA 45', str(a))
722
723 def test_acta_without_twotheta(self):
724 shx = Shelxfile()
725 shx.read_file('tests/resources/p21c.res')
726 a = ACTA(shx, 'ACTA'.split())
727 self.assertEqual('ACTA', repr(a))
728
729
730 class TestWGHTClass(TestCase):
731 def setUp(self):
732 self.shx = Shelxfile()
733 self.shx.read_file('tests/resources/p21c.res')
734
735 def test_wght_a(self):
736 self.assertEqual(0.049, self.shx.wght.a)
737
738 def test_wght_b(self):
739 self.assertEqual(0.0, self.shx.wght.b)
740
741
742 class TestDEFS(TestCase):
743 def test_defs(self):
744 shx = Shelxfile()
745 shx.read_file('tests/resources/p21c.res')
746 self.assertIsNotNone(shx.defs)
747 self.assertEqual(0.0234, shx.defs.sd)
748 self.assertIsNotNone(shx.defs.all)
749
750
751 class TestRestraintTypes(TestCase):
752 """Tests for various restraint card types."""
753
754 def setUp(self):
755 self.shx = Shelxfile()
756 self.shx.read_file('tests/resources/p21c.res')
757
758 def test_same(self):
759 s = SAME(self.shx, 'SAME 0.03 0.05 C1 C2 C3'.split())
760 self.assertEqual(0.03, s.s1)
761 self.assertEqual(0.05, s.s2)
762 self.assertEqual(['C1', 'C2', 'C3'], s.atoms)
763
764 def test_rigu(self):
765 r = RIGU(self.shx, 'RIGU 0.005 0.006 C1 C2'.split())
766 self.assertEqual(0.005, r.s1)
767 self.assertEqual(0.006, r.s2)
768
769 def test_simu(self):
770 s = SIMU(self.shx, 'SIMU 0.05 0.09 3.0 C1 C2'.split())
771 self.assertEqual(0.05, s.s)
772 self.assertEqual(0.09, s.st)
773 self.assertEqual(3.0, s.dmax)
774
775 def test_delu(self):
776 d = DELU(self.shx, 'DELU 0.02 0.03 C1 C2'.split())
777 self.assertEqual(0.02, d.s1)
778 self.assertEqual(0.03, d.s2)
779
780 def test_isor(self):
781 i = ISOR(self.shx, 'ISOR 0.15 0.25 C1 C2'.split())
782 self.assertEqual(0.15, i.s)
783 self.assertEqual(0.25, i.st)
784
785 def test_flat(self):
786 f = FLAT(self.shx, 'FLAT 0.2 C1 C2 C3 C4'.split())
787 self.assertEqual(0.2, f.s)
788 self.assertEqual(['C1', 'C2', 'C3', 'C4'], f.atoms)
789
790 def test_bump(self):
791 b = BUMP(self.shx, 'BUMP 0.03'.split())
792 self.assertEqual(0.03, b.s)
793
794 def test_chiv(self):
795 c = CHIV(self.shx, 'CHIV 0 0.2 C1'.split())
796 self.assertIsNotNone(c)
797
798 def test_eadp(self):
799 e = EADP(self.shx, 'EADP C1 C2'.split())
800 self.assertEqual(['C1', 'C2'], e.atoms)
801
802 def test_exyz(self):
803 e = EXYZ(self.shx, 'EXYZ C1 C2'.split())
804 self.assertEqual(['C1', 'C2'], e.atoms)
805
806
807 class TestRTAB(TestCase):
808 def test_rtab(self):
809 shx = Shelxfile()
810 shx.read_file('tests/resources/p21c.res')
811 r = RTAB(shx, 'RTAB omeg C1 C2 C3 C4'.split())
812 self.assertEqual('omeg', r.code)
813 self.assertEqual(['C1', 'C2', 'C3', 'C4'], r.atoms)
814
815
816 class TestLSCycles(TestCase):
817 def test_ls_cycles(self):
818 shx = Shelxfile()
819 shx.read_file('tests/resources/p21c.res')
820 self.assertIsNotNone(shx.cycles)
821 self.assertIsNotNone(shx.cycles.number)
822
823
824 class TestCELLCard(TestCase):
825 def test_cell(self):
826 shx = Shelxfile()
827 shx.read_file('tests/resources/p21c.res')
828 c = shx.cell
829 self.assertEqual(10.5086, c.a)
830 self.assertEqual(20.9035, c.b)
831 self.assertEqual(20.5072, c.c)
832 self.assertEqual(90.0, c.alpha)
833 self.assertEqual(94.13, c.beta)
834 self.assertEqual(90.0, c.gamma)
835 self.assertAlmostEqual(4493.0474, c.volume, places=4)
836
837 def test_cell_iter(self):
838 shx = Shelxfile()
839 shx.read_file('tests/resources/p21c.res')
840 cell_list = list(shx.cell)
841 self.assertEqual(6, len(cell_list))
842 self.assertEqual(10.5086, cell_list[0])
843
844
845 class TestZERR(TestCase):
846 def test_zerr(self):
847 shx = Shelxfile()
848 shx.read_file('tests/resources/p21c.res')
849 self.assertIsNotNone(shx.zerr)
850 self.assertEqual(4, shx.Z)
851
852
853 class TestLATT(TestCase):
854 def test_latt_centric(self):
855 shx = Shelxfile()
856 shx.read_file('tests/resources/p21c.res')
857 self.assertTrue(shx.latt.centric)
858
859
860 class TestSFACTable(TestCase):
861 def test_sfac_elements(self):
862 shx = Shelxfile()
863 shx.read_file('tests/resources/p21c.res')
864 self.assertEqual('SFAC C H O F Al Ga', str(shx.sfac_table))
865 elements = shx.sfac_table.elements_list
866 self.assertEqual(['C', 'H', 'O', 'F', 'AL', 'GA'], elements)
867
868
869 class TestUNIT(TestCase):
870 def test_unit(self):
871 shx = Shelxfile()
872 shx.read_file('tests/resources/p21c.res')
873 self.assertEqual('UNIT 1 2 3 4 5 6', str(shx.unit))
874 self.assertEqual([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shx.unit.values)
875
876
877 class TestHFIX(TestCase):
878 def test_hfix(self):
879 shx = Shelxfile()
880 h = HFIX(shx, 'HFIX 23 C1 C2'.split())
881 self.assertEqual([23], h.params)
882 self.assertEqual(['C1', 'C2'], h.atoms)
883 self.assertIn('HFIX', repr(h))
884
885
886 class TestMORE(TestCase):
887 def test_more(self):
888 shx = Shelxfile()
889 m = MORE(shx, 'MORE 2'.split())
890 self.assertEqual(2, m.m)
891
892
893 class TestFREE(TestCase):
894 def test_free(self):
895 shx = Shelxfile()
896 shx.read_file('tests/resources/p21c.res')
897 f = FREE(shx, 'FREE C1 C2'.split())
898 self.assertEqual('C1', f.atom1)
899 self.assertEqual('C2', f.atom2)
900
901
902 class TestCommandBase(TestCase):
903 """Test Command base class methods."""
904
905 def test_set(self):
906 shx = Shelxfile()
907 shx.read_file('tests/resources/p21c.res')
908 shx.plan.set('PLAN 50')
909 self.assertEqual('PLAN 50', repr(shx.plan))
910
911 def test_split(self):
912 shx = Shelxfile()
913 shx.read_file('tests/resources/p21c.res')
914 parts = shx.plan.split()
915 self.assertEqual(['PLAN', '20'], parts)
916
917 def test_iter(self):
918 shx = Shelxfile()
919 shx.read_file('tests/resources/p21c.res')
920 parts = list(shx.plan)
921 self.assertIn('PLAN', parts)
922
923
924 class TestRestraintBase(TestCase):
925 """Test Restraint base class methods."""
926
927 def test_iter(self):
928 shx = Shelxfile()
929 shx.read_file('tests/resources/p21c.res')
930 r = shx.restraints[0]
931 parts = list(r)
932 self.assertGreater(len(parts), 0)
933
934 def test_split(self):
935 shx = Shelxfile()
936 shx.read_file('tests/resources/p21c.res')
937 r = shx.restraints[0]
938 parts = r.split()
939 self.assertIsInstance(parts, list)
940
941
942 # =============================================================================
943 # Tests for shelxfile/misc/misc.py
944 # =============================================================================
945
946 class TestSplitFvarAndParameter(TestCase):
947 def test_positive(self):
948 self.assertEqual((3, 0.5), split_fvar_and_parameter(30.5))
949
950 def test_negative(self):
951 self.assertEqual((-3, -0.5), split_fvar_and_parameter(-30.5))
952
953 def test_eleven(self):
954 self.assertEqual((1, 1.0), split_fvar_and_parameter(11.0))
955
956 def test_negative_eleven(self):
957 self.assertEqual((-1, -1.0), split_fvar_and_parameter(-11.0))
958
959
960 class TestFlatten(TestCase):
961 def test_flatten_nested(self):
962 self.assertEqual([1, 2, 3, 4], flatten([[1, 2], [3, [4]]]))
963
964 def test_flatten_empty(self):
965 self.assertEqual([], flatten([]))
966
967 def test_flatten_no_nesting(self):
968 self.assertEqual([1, 2, 3], flatten([1, 2, 3]))
969
970
971 class TestRemoveFile(TestCase):
972 def test_remove_existing_file(self):
973 with tempfile.NamedTemporaryFile(delete=False) as f:
974 path = f.name
975 self.assertTrue(remove_file(path))
976
977 def test_remove_nonexistent_file(self):
978 result = remove_file(str(Path(tempfile.gettempdir()) / 'nonexistent_file_xyz123.txt'))
979 self.assertFalse(result)
980
981
982 class TestWrapLine(TestCase):
983 def test_short_line(self):
984 self.assertEqual('SHORT LINE', wrap_line('SHORT LINE'))
985
986 def test_long_line(self):
987 line = 'A' * 100
988 wrapped = wrap_line(line)
989 self.assertIn(' =\n', wrapped)
990
991
992 class TestMultilineTest(TestCase):
993 def test_multiline_true(self):
994 self.assertTrue(multiline_test('C1 1 0.1 0.2 0.3 11.0 0.05 ='))
995
996 def test_multiline_false(self):
997 self.assertFalse(multiline_test('C1 1 0.1 0.2 0.3 11.0 0.05'))
998
999 def test_multiline_rem_not_dsr(self):
1000 self.assertFalse(multiline_test('REM this has = sign'))
1001
1002
1003 # =============================================================================
1004 # Tests for edge cases and integration
1005 # =============================================================================
1006
1007 class TestReadString(TestCase):
1008 def test_read_string(self):
1009 shx = Shelxfile()
1010 content = Path('tests/resources/p21c.res').read_text(encoding='latin1')
1011 shx.read_string(content)
1012 self.assertEqual(148, len(shx.atoms))
1013 self.assertIsNotNone(shx.cell)
1014
1015
1016 class TestSymmCards(TestCase):
1017 def test_symmcards(self):
1018 shx = Shelxfile()
1019 shx.read_file('tests/resources/p21c.res')
1020 self.assertGreater(len(shx.symmcards), 0)
1021 s = str(shx.symmcards)
1022 self.assertIn('1 0 0', s)
1023
1024
1025 class TestDifferentFiles(TestCase):
1026 """Test parsing different res files for broader coverage."""
1027
1028 def test_I43d_file(self):
1029 shx = Shelxfile()
1030 shx.read_file('tests/resources/I-43d.res')
1031 self.assertGreater(len(shx.atoms), 0)
1032
1033 def test_sad_final_file(self):
1034 shx = Shelxfile()
1035 shx.read_file('tests/resources/sad-final.res')
1036 self.assertGreater(len(shx.atoms), 0)
1037
1038 def test_2240189_file(self):
1039 shx = Shelxfile()
1040 shx.read_file('tests/resources/2240189.res')
1041 self.assertGreater(len(shx.atoms), 0)
1042
1043
1044 if __name__ == '__main__':
1045 unittest.main()