RATapi 0.0.0.dev0__cp39-cp39-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. RATapi/__init__.py +8 -0
  2. RATapi/classlist.py +387 -0
  3. RATapi/controls.py +143 -0
  4. RATapi/eventManager.dll +0 -0
  5. RATapi/events.py +70 -0
  6. RATapi/examples/__init__.py +0 -0
  7. RATapi/examples/absorption/__init__.py +0 -0
  8. RATapi/examples/absorption/absorption.py +148 -0
  9. RATapi/examples/absorption/volume_thiol_bilayer.py +138 -0
  10. RATapi/examples/data/D2O_spin_down.dat +100 -0
  11. RATapi/examples/data/D2O_spin_up.dat +101 -0
  12. RATapi/examples/data/DSPC_D2O.dat +82 -0
  13. RATapi/examples/data/DSPC_SMW.dat +82 -0
  14. RATapi/examples/data/H2O_spin_down.dat +102 -0
  15. RATapi/examples/data/H2O_spin_up.dat +102 -0
  16. RATapi/examples/data/__init__.py +0 -0
  17. RATapi/examples/data/c_PLP0016596.dat +146 -0
  18. RATapi/examples/data/c_PLP0016601.dat +97 -0
  19. RATapi/examples/data/c_PLP0016607.dat +104 -0
  20. RATapi/examples/domains/__init__.py +0 -0
  21. RATapi/examples/domains/alloy_domains.py +26 -0
  22. RATapi/examples/domains/domains_XY_model.py +70 -0
  23. RATapi/examples/domains/domains_custom_XY.py +69 -0
  24. RATapi/examples/domains/domains_custom_layers.py +46 -0
  25. RATapi/examples/domains/domains_standard_layers.py +75 -0
  26. RATapi/examples/languages/__init__.py +0 -0
  27. RATapi/examples/languages/custom_bilayer.py +66 -0
  28. RATapi/examples/languages/run_custom_file_languages.py +41 -0
  29. RATapi/examples/languages/setup_problem.py +130 -0
  30. RATapi/examples/non_polarised/DSPC_custom_XY.py +140 -0
  31. RATapi/examples/non_polarised/DSPC_custom_layers.py +121 -0
  32. RATapi/examples/non_polarised/DSPC_standard_layers.py +358 -0
  33. RATapi/examples/non_polarised/__init__.py +0 -0
  34. RATapi/examples/non_polarised/custom_XY_DSPC.py +137 -0
  35. RATapi/examples/non_polarised/custom_bilayer_DSPC.py +85 -0
  36. RATapi/inputs.py +471 -0
  37. RATapi/models.py +288 -0
  38. RATapi/outputs.py +218 -0
  39. RATapi/project.py +659 -0
  40. RATapi/rat_core.cp39-win_amd64.pyd +0 -0
  41. RATapi/run.py +35 -0
  42. RATapi/utils/__init__.py +0 -0
  43. RATapi/utils/custom_errors.py +42 -0
  44. RATapi/utils/enums.py +101 -0
  45. RATapi/utils/plotting.py +211 -0
  46. RATapi/wrappers.py +115 -0
  47. RATapi-0.0.0.dev0.dist-info/METADATA +57 -0
  48. RATapi-0.0.0.dev0.dist-info/RECORD +64 -0
  49. RATapi-0.0.0.dev0.dist-info/WHEEL +5 -0
  50. RATapi-0.0.0.dev0.dist-info/top_level.txt +2 -0
  51. tests/__init__.py +0 -0
  52. tests/conftest.py +4728 -0
  53. tests/test_classlist.py +882 -0
  54. tests/test_controls.py +863 -0
  55. tests/test_custom_errors.py +49 -0
  56. tests/test_events.py +61 -0
  57. tests/test_inputs.py +908 -0
  58. tests/test_models.py +308 -0
  59. tests/test_outputs.py +31 -0
  60. tests/test_plotting.py +167 -0
  61. tests/test_project.py +1387 -0
  62. tests/test_run.py +570 -0
  63. tests/test_wrappers.py +57 -0
  64. tests/utils.py +171 -0
@@ -0,0 +1,882 @@
1
+ """Test the ClassList."""
2
+
3
+ import re
4
+ import warnings
5
+ from collections.abc import Iterable, Sequence
6
+ from typing import Any, Union
7
+
8
+ import pytest
9
+
10
+ from RATapi.classlist import ClassList
11
+ from tests.utils import InputAttributes, SubInputAttributes
12
+
13
+
14
+ @pytest.fixture
15
+ def one_name_class_list():
16
+ """A ClassList of InputAttributes, containing one element with a name defined."""
17
+ return ClassList([InputAttributes(name="Alice")])
18
+
19
+
20
+ @pytest.fixture
21
+ def two_name_class_list():
22
+ """A ClassList of InputAttributes, containing two elements with names defined."""
23
+ return ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob")])
24
+
25
+
26
+ @pytest.fixture
27
+ def two_name_class_list_table():
28
+ """The table representation of the ClassList defined in the "two_name_class_list" fixture."""
29
+ return (
30
+ "+-------+-------+\n"
31
+ "| index | name |\n"
32
+ "+-------+-------+\n"
33
+ "| 0 | Alice |\n"
34
+ "| 1 | Bob |\n"
35
+ "+-------+-------+"
36
+ )
37
+
38
+
39
+ @pytest.fixture
40
+ def three_name_class_list():
41
+ """A ClassList of InputAttributes, containing three elements with names defined."""
42
+ return ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob"), InputAttributes(name="Eve")])
43
+
44
+
45
+ class TestInitialisation:
46
+ @pytest.mark.parametrize(
47
+ "input_object",
48
+ [
49
+ (InputAttributes()),
50
+ (InputAttributes(name="Alice")),
51
+ (InputAttributes(surname="Morgan")),
52
+ "Alice",
53
+ ],
54
+ )
55
+ def test_input_object(self, input_object: Any) -> None:
56
+ """For an input of an object, the ClassList should contain a one-element list containing the object and
57
+ _class_handle should be set to the type of the object.
58
+ """
59
+ class_list = ClassList(input_object)
60
+ assert class_list.data == [input_object]
61
+ assert isinstance(input_object, class_list._class_handle)
62
+
63
+ @pytest.mark.parametrize(
64
+ "input_sequence",
65
+ [
66
+ ([InputAttributes()]),
67
+ ([InputAttributes(name="Alice")]),
68
+ ([InputAttributes(surname="Morgan")]),
69
+ ([InputAttributes(name="Alice"), InputAttributes(name="Bob")]),
70
+ (InputAttributes(),),
71
+ (InputAttributes(name="Alice"),),
72
+ (InputAttributes(surname="Morgan"),),
73
+ (InputAttributes(name="Alice"), InputAttributes(name="Bob")),
74
+ ],
75
+ )
76
+ def test_input_sequence(self, input_sequence: Sequence[object]) -> None:
77
+ """For an input of a sequence, the ClassList should be a list equal to the input sequence, and _class_handle
78
+ should be set to the type of the objects within.
79
+ """
80
+ class_list = ClassList(input_sequence)
81
+ assert class_list.data == list(input_sequence)
82
+ for element in input_sequence:
83
+ assert isinstance(element, class_list._class_handle)
84
+
85
+ @pytest.mark.parametrize(
86
+ "input_sequence",
87
+ [
88
+ ([InputAttributes(name="Alice"), SubInputAttributes(name="Bob")]),
89
+ ([SubInputAttributes(name="Alice"), InputAttributes(name="Bob")]),
90
+ ],
91
+ )
92
+ def test_input_sequence_subclass(self, input_sequence: Sequence[object]) -> None:
93
+ """For an input of a sequence containing objects of a class and its subclasses, the ClassList should be a list
94
+ equal to the input sequence, and _class_handle should be set to the type of the parent class.
95
+ """
96
+ class_list = ClassList(input_sequence)
97
+ assert class_list.data == list(input_sequence)
98
+ for element in input_sequence:
99
+ assert isinstance(element, class_list._class_handle)
100
+
101
+ @pytest.mark.parametrize("empty_input", [([]), (())])
102
+ def test_empty_input(self, empty_input: Sequence[object]) -> None:
103
+ """If we initialise a ClassList with an empty input (list or tuple), the ClassList should be an empty list, and
104
+ _class_handle should be unset.
105
+ """
106
+ class_list = ClassList(empty_input)
107
+ assert class_list.data == []
108
+ assert not hasattr(class_list, "_class_handle")
109
+
110
+ @pytest.mark.parametrize(
111
+ "input_list",
112
+ [
113
+ ([InputAttributes(name="Alice"), dict(name="Bob")]),
114
+ ],
115
+ )
116
+ def test_different_classes(self, input_list: Sequence[object]) -> None:
117
+ """If we initialise a ClassList with an input containing multiple classes, we should raise a ValueError."""
118
+ with pytest.raises(
119
+ ValueError,
120
+ match=f"Input list contains elements of type other than '{type(input_list[0]).__name__}'",
121
+ ):
122
+ ClassList(input_list)
123
+
124
+ @pytest.mark.parametrize(
125
+ "input_list, name_field",
126
+ [
127
+ ([InputAttributes(name="Alice"), InputAttributes(name="Alice")], "name"),
128
+ ([InputAttributes(id="Alice"), InputAttributes(id="Alice")], "id"),
129
+ ],
130
+ )
131
+ def test_identical_name_fields(self, input_list: Sequence[object], name_field: str) -> None:
132
+ """If we initialise a ClassList with input objects with identical values of the name_field,
133
+ we should raise a ValueError.
134
+ """
135
+ with pytest.raises(
136
+ ValueError,
137
+ match=f"Input list contains objects with the same value of the {name_field} attribute",
138
+ ):
139
+ ClassList(input_list, name_field=name_field)
140
+
141
+
142
+ def test_str_table(two_name_class_list: ClassList, two_name_class_list_table: str) -> None:
143
+ """For classes with the __dict__ attribute, we should be able to print the ClassList like a table."""
144
+ assert str(two_name_class_list) == two_name_class_list_table
145
+
146
+
147
+ def test_str_empty_table() -> None:
148
+ """For empty classes with the __dict__ attribute, we should be able to print the contents of the ClassList as a
149
+ list.
150
+ """
151
+ empty_attributes = InputAttributes()
152
+ assert str(ClassList(empty_attributes)) == str([empty_attributes])
153
+
154
+
155
+ @pytest.mark.parametrize(
156
+ "input_list",
157
+ [
158
+ (["Alice", "Bob"]),
159
+ ],
160
+ )
161
+ def test_str_list(input_list: list[str]) -> None:
162
+ """For classes without the __dict__ attribute, we should be able to print the ClassList as a list."""
163
+ class_list = ClassList(input_list)
164
+ assert str(class_list) == str(input_list)
165
+
166
+
167
+ def test_str_empty_classlist() -> None:
168
+ """For empty ClassLists, we should be able to print the ClassList as an empty list."""
169
+ assert str(ClassList()) == str([])
170
+
171
+
172
+ @pytest.mark.parametrize(
173
+ ["new_item", "expected_classlist"],
174
+ [
175
+ (InputAttributes(name="Eve"), ClassList([InputAttributes(name="Eve"), InputAttributes(name="Bob")])),
176
+ (
177
+ InputAttributes(name="John", surname="Luther"),
178
+ ClassList([InputAttributes(name="John", surname="Luther"), InputAttributes(name="Bob")]),
179
+ ),
180
+ ],
181
+ )
182
+ def test_setitem(two_name_class_list: ClassList, new_item: InputAttributes, expected_classlist: ClassList) -> None:
183
+ """We should be able to set values in an element of a ClassList using a new object."""
184
+ class_list = two_name_class_list
185
+ class_list[0] = new_item
186
+ assert class_list == expected_classlist
187
+
188
+
189
+ @pytest.mark.parametrize(
190
+ "new_item",
191
+ [
192
+ (InputAttributes(name="Bob")),
193
+ ],
194
+ )
195
+ def test_setitem_same_name_field(two_name_class_list: ClassList, new_item: InputAttributes) -> None:
196
+ """If we set the name_field of an object in the ClassList to one already defined, we should raise a ValueError."""
197
+ with pytest.raises(ValueError, match="Input list contains objects with the same value of the name attribute"):
198
+ two_name_class_list[0] = new_item
199
+
200
+
201
+ @pytest.mark.parametrize(
202
+ "new_values",
203
+ [
204
+ "Bob",
205
+ ],
206
+ )
207
+ def test_setitem_different_classes(two_name_class_list: ClassList, new_values: dict[str, Any]) -> None:
208
+ """If we set the name_field of an object in the ClassList to one already defined, we should raise a ValueError."""
209
+ with pytest.raises(ValueError, match="Input list contains elements of type other than 'InputAttributes'"):
210
+ two_name_class_list[0] = new_values
211
+
212
+
213
+ def test_delitem(two_name_class_list: ClassList, one_name_class_list: ClassList) -> None:
214
+ """We should be able to delete elements from a ClassList with the del operator."""
215
+ class_list = two_name_class_list
216
+ del class_list[1]
217
+ assert class_list == one_name_class_list
218
+
219
+
220
+ def test_delitem_not_present(two_name_class_list: ClassList) -> None:
221
+ """If we use the del operator to delete an index out of range, we should raise an IndexError."""
222
+ class_list = two_name_class_list
223
+ with pytest.raises(IndexError, match=re.escape("list assignment index out of range")):
224
+ del class_list[2]
225
+
226
+
227
+ @pytest.mark.parametrize(
228
+ "added_list",
229
+ [
230
+ (ClassList(InputAttributes(name="Eve"))),
231
+ ([InputAttributes(name="Eve")]),
232
+ (InputAttributes(name="Eve"),),
233
+ (InputAttributes(name="Eve")),
234
+ ],
235
+ )
236
+ def test_iadd(two_name_class_list: ClassList, added_list: Iterable, three_name_class_list: ClassList) -> None:
237
+ """We should be able to use the "+=" operator to add iterables to a ClassList. Individual objects should be wrapped
238
+ in a list before being added.
239
+ """
240
+ class_list = two_name_class_list
241
+ class_list += added_list
242
+ assert class_list == three_name_class_list
243
+
244
+
245
+ @pytest.mark.parametrize(
246
+ "added_list",
247
+ [
248
+ (ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob")])),
249
+ ([InputAttributes(name="Alice"), InputAttributes(name="Bob")]),
250
+ (InputAttributes(name="Alice"), InputAttributes(name="Bob")),
251
+ ],
252
+ )
253
+ def test_iadd_empty_classlist(added_list: Sequence, two_name_class_list: ClassList) -> None:
254
+ """We should be able to use the "+=" operator to add iterables to an empty ClassList, whilst also setting
255
+ _class_handle.
256
+ """
257
+ class_list = ClassList()
258
+ class_list += added_list
259
+ assert class_list == two_name_class_list
260
+ assert isinstance(added_list[-1], class_list._class_handle)
261
+
262
+
263
+ def test_mul(two_name_class_list: ClassList) -> None:
264
+ """If we use the "*" operator on a ClassList, we should raise a TypeError."""
265
+ n = 2
266
+ with pytest.raises(
267
+ TypeError,
268
+ match=re.escape(
269
+ f"unsupported operand type(s) for *: "
270
+ f"'{two_name_class_list.__class__.__name__}' and "
271
+ f"'{n.__class__.__name__}'",
272
+ ),
273
+ ):
274
+ two_name_class_list * n
275
+
276
+
277
+ def test_rmul(two_name_class_list: ClassList) -> None:
278
+ """If we use the "*" operator on a ClassList, we should raise a TypeError."""
279
+ n = 2
280
+ with pytest.raises(
281
+ TypeError,
282
+ match=re.escape(
283
+ f"unsupported operand type(s) for *: "
284
+ f"'{n.__class__.__name__}' and "
285
+ f"'{two_name_class_list.__class__.__name__}'",
286
+ ),
287
+ ):
288
+ n * two_name_class_list
289
+
290
+
291
+ def test_imul(two_name_class_list: ClassList) -> None:
292
+ """If we use the "*=" operator on a ClassList, we should raise a TypeError."""
293
+ n = 2
294
+ with pytest.raises(
295
+ TypeError,
296
+ match=re.escape(
297
+ f"unsupported operand type(s) for *=: "
298
+ f"'{two_name_class_list.__class__.__name__}' and "
299
+ f"'{n.__class__.__name__}'",
300
+ ),
301
+ ):
302
+ two_name_class_list *= n
303
+
304
+
305
+ @pytest.mark.parametrize(
306
+ "new_object",
307
+ [
308
+ (InputAttributes(name="Eve")),
309
+ ],
310
+ )
311
+ def test_append_object(two_name_class_list: ClassList, new_object: object, three_name_class_list: ClassList) -> None:
312
+ """We should be able to append to a ClassList using a new object."""
313
+ class_list = two_name_class_list
314
+ class_list.append(new_object)
315
+ assert class_list == three_name_class_list
316
+
317
+
318
+ @pytest.mark.parametrize(
319
+ "new_values",
320
+ [
321
+ ({"name": "Eve"}),
322
+ ],
323
+ )
324
+ def test_append_kwargs(
325
+ two_name_class_list: ClassList,
326
+ new_values: dict[str, Any],
327
+ three_name_class_list: ClassList,
328
+ ) -> None:
329
+ """We should be able to append to a ClassList using keyword arguments."""
330
+ class_list = two_name_class_list
331
+ class_list.append(**new_values)
332
+ assert class_list == three_name_class_list
333
+
334
+
335
+ @pytest.mark.parametrize(
336
+ ["new_object", "new_values"],
337
+ [
338
+ (InputAttributes(name="Eve"), {"name": "John"}),
339
+ ],
340
+ )
341
+ def test_append_object_and_kwargs(
342
+ two_name_class_list: ClassList,
343
+ new_object: object,
344
+ new_values: dict[str, Any],
345
+ three_name_class_list: ClassList,
346
+ ) -> None:
347
+ """If we append to a ClassList using a new object and keyword arguments, we raise a warning, and append the object,
348
+ discarding the keyword arguments.
349
+ """
350
+ class_list = two_name_class_list
351
+ with pytest.warns(SyntaxWarning):
352
+ warnings.warn(
353
+ "ClassList.append() called with both an object and keyword arguments. "
354
+ "The keyword arguments will be ignored.",
355
+ SyntaxWarning,
356
+ stacklevel=2,
357
+ )
358
+ class_list.append(new_object, **new_values)
359
+ assert class_list == three_name_class_list
360
+
361
+
362
+ @pytest.mark.parametrize(
363
+ "new_object",
364
+ [
365
+ (InputAttributes(name="Alice")),
366
+ ],
367
+ )
368
+ def test_append_object_empty_classlist(new_object: object, one_name_class_list: ClassList) -> None:
369
+ """We should be able to append to an empty ClassList using a new object, whilst also setting _class_handle."""
370
+ class_list = ClassList()
371
+ class_list.append(new_object)
372
+ assert class_list == one_name_class_list
373
+ assert isinstance(new_object, class_list._class_handle)
374
+
375
+
376
+ @pytest.mark.parametrize(
377
+ "new_values",
378
+ [
379
+ ({"name": "Alice"}),
380
+ ],
381
+ )
382
+ def test_append_kwargs_empty_classlist(new_values: dict[str, Any]) -> None:
383
+ """If we append to an empty ClassList using keyword arguments we should raise a TypeError."""
384
+ class_list = ClassList()
385
+ with pytest.raises(
386
+ TypeError,
387
+ match=re.escape(
388
+ "ClassList.append() called with keyword arguments for a ClassList "
389
+ "without a class defined. Call ClassList.append() with an object to "
390
+ "define the class.",
391
+ ),
392
+ ):
393
+ class_list.append(**new_values)
394
+
395
+
396
+ @pytest.mark.parametrize(
397
+ "new_object",
398
+ [
399
+ (InputAttributes(name="Alice")),
400
+ ],
401
+ )
402
+ def test_append_object_same_name_field(two_name_class_list: ClassList, new_object: object) -> None:
403
+ """If we append an object with an already-specified name_field value to a ClassList we should raise a ValueError."""
404
+ with pytest.raises(
405
+ ValueError,
406
+ match=f"Input list contains objects with the same value of the " f"{two_name_class_list.name_field} attribute",
407
+ ):
408
+ two_name_class_list.append(new_object)
409
+
410
+
411
+ @pytest.mark.parametrize(
412
+ "new_values",
413
+ [
414
+ ({"name": "Alice"}),
415
+ ],
416
+ )
417
+ def test_append_kwargs_same_name_field(two_name_class_list: ClassList, new_values: dict[str, Any]) -> None:
418
+ """If we append an object with an already-specified name_field value to a ClassList we should raise a ValueError."""
419
+ with pytest.raises(
420
+ ValueError,
421
+ match=f"Input arguments contain the {two_name_class_list.name_field} "
422
+ f"'{new_values[two_name_class_list.name_field]}', "
423
+ f"which is already specified in the ClassList",
424
+ ):
425
+ two_name_class_list.append(**new_values)
426
+
427
+
428
+ @pytest.mark.parametrize(
429
+ "new_object",
430
+ [
431
+ (InputAttributes(name="Eve")),
432
+ ],
433
+ )
434
+ def test_insert_object(two_name_class_list: ClassList, new_object: object) -> None:
435
+ """We should be able to insert an object within a ClassList using a new object."""
436
+ two_name_class_list.insert(1, new_object)
437
+ assert two_name_class_list == ClassList(
438
+ [InputAttributes(name="Alice"), InputAttributes(name="Eve"), InputAttributes(name="Bob")],
439
+ )
440
+
441
+
442
+ @pytest.mark.parametrize(
443
+ "new_values",
444
+ [
445
+ ({"name": "Eve"}),
446
+ ],
447
+ )
448
+ def test_insert_kwargs(two_name_class_list: ClassList, new_values: dict[str, Any]) -> None:
449
+ """We should be able to insert an object within a ClassList using keyword arguments."""
450
+ two_name_class_list.insert(1, **new_values)
451
+ assert two_name_class_list == ClassList(
452
+ [InputAttributes(name="Alice"), InputAttributes(name="Eve"), InputAttributes(name="Bob")],
453
+ )
454
+
455
+
456
+ @pytest.mark.parametrize(
457
+ ["new_object", "new_values"],
458
+ [
459
+ (InputAttributes(name="Eve"), {"name": "John"}),
460
+ ],
461
+ )
462
+ def test_insert_object_and_kwargs(
463
+ two_name_class_list: ClassList,
464
+ new_object: object,
465
+ new_values: dict[str, Any],
466
+ three_name_class_list: ClassList,
467
+ ) -> None:
468
+ """If call insert() on a ClassList using a new object and keyword arguments, we raise a warning, and append the
469
+ object, discarding the keyword arguments.
470
+ """
471
+ class_list = two_name_class_list
472
+ with pytest.warns(SyntaxWarning):
473
+ warnings.warn(
474
+ "ClassList.insert() called with both an object and keyword arguments. "
475
+ "The keyword arguments will be ignored.",
476
+ SyntaxWarning,
477
+ stacklevel=2,
478
+ )
479
+ class_list.insert(1, new_object, **new_values)
480
+ assert class_list == ClassList(
481
+ [InputAttributes(name="Alice"), InputAttributes(name="Eve"), InputAttributes(name="Bob")],
482
+ )
483
+
484
+
485
+ @pytest.mark.parametrize(
486
+ "new_object",
487
+ [
488
+ (InputAttributes(name="Alice")),
489
+ ],
490
+ )
491
+ def test_insert_object_empty_classlist(new_object: object, one_name_class_list: ClassList) -> None:
492
+ """We should be able to insert a new object into an empty ClassList, whilst also setting _class_handle."""
493
+ class_list = ClassList()
494
+ class_list.insert(0, new_object)
495
+ assert class_list == one_name_class_list
496
+ assert isinstance(new_object, class_list._class_handle)
497
+
498
+
499
+ @pytest.mark.parametrize(
500
+ "new_values",
501
+ [
502
+ ({"name": "Alice"}),
503
+ ],
504
+ )
505
+ def test_insert_kwargs_empty_classlist(new_values: dict[str, Any]) -> None:
506
+ """If we append to an empty ClassList using keyword arguments we should raise a TypeError."""
507
+ class_list = ClassList()
508
+ with pytest.raises(
509
+ TypeError,
510
+ match=re.escape(
511
+ "ClassList.insert() called with keyword arguments for a ClassList "
512
+ "without a class defined. Call ClassList.insert() with an object to "
513
+ "define the class.",
514
+ ),
515
+ ):
516
+ class_list.insert(0, **new_values)
517
+
518
+
519
+ @pytest.mark.parametrize(
520
+ "new_object",
521
+ [
522
+ (InputAttributes(name="Alice")),
523
+ ],
524
+ )
525
+ def test_insert_object_same_name(two_name_class_list: ClassList, new_object: object) -> None:
526
+ """If we insert an object with an already-specified name_field value to a ClassList we should raise a ValueError."""
527
+ with pytest.raises(
528
+ ValueError,
529
+ match=f"Input list contains objects with the same value of the " f"{two_name_class_list.name_field} attribute",
530
+ ):
531
+ two_name_class_list.insert(1, new_object)
532
+
533
+
534
+ @pytest.mark.parametrize(
535
+ "new_values",
536
+ [
537
+ ({"name": "Alice"}),
538
+ ],
539
+ )
540
+ def test_insert_kwargs_same_name(two_name_class_list: ClassList, new_values: dict[str, Any]) -> None:
541
+ """If we insert an object with an already-specified name_field value to a ClassList we should raise a ValueError."""
542
+ with pytest.raises(
543
+ ValueError,
544
+ match=f"Input arguments contain the {two_name_class_list.name_field} "
545
+ f"'{new_values[two_name_class_list.name_field]}', "
546
+ f"which is already specified in the ClassList",
547
+ ):
548
+ two_name_class_list.insert(1, **new_values)
549
+
550
+
551
+ @pytest.mark.parametrize(
552
+ "remove_value",
553
+ [
554
+ "Bob",
555
+ (InputAttributes(name="Bob")),
556
+ ],
557
+ )
558
+ def test_remove(two_name_class_list: ClassList, remove_value: Union[object, str]) -> None:
559
+ """We should be able to remove an object either by the value of the name_field or by specifying the object
560
+ itself.
561
+ """
562
+ two_name_class_list.remove(remove_value)
563
+ assert two_name_class_list == ClassList([InputAttributes(name="Alice")])
564
+
565
+
566
+ @pytest.mark.parametrize(
567
+ "remove_value",
568
+ [
569
+ "Eve",
570
+ (InputAttributes(name="Eve")),
571
+ ],
572
+ )
573
+ def test_remove_not_present(two_name_class_list: ClassList, remove_value: Union[object, str]) -> None:
574
+ """If we remove an object not included in the ClassList we should raise a ValueError."""
575
+ with pytest.raises(ValueError, match=re.escape("list.remove(x): x not in list")):
576
+ two_name_class_list.remove(remove_value)
577
+
578
+
579
+ @pytest.mark.parametrize(
580
+ ["count_value", "expected_count"],
581
+ [
582
+ ("Bob", 1),
583
+ (InputAttributes(name="Bob"), 1),
584
+ ("Eve", 0),
585
+ (InputAttributes(name="Eve"), 0),
586
+ ],
587
+ )
588
+ def test_count(two_name_class_list: ClassList, count_value: Union[object, str], expected_count: int) -> None:
589
+ """We should be able to determine the number of times an object is in the ClassList using either the object itself
590
+ or its name_field value.
591
+ """
592
+ assert two_name_class_list.count(count_value) == expected_count
593
+
594
+
595
+ @pytest.mark.parametrize(
596
+ ["index_value", "expected_index"],
597
+ [
598
+ ("Bob", 1),
599
+ (InputAttributes(name="Bob"), 1),
600
+ ],
601
+ )
602
+ def test_index(two_name_class_list: ClassList, index_value: Union[object, str], expected_index: int) -> None:
603
+ """We should be able to find the index of an object in the ClassList either by its name_field value or by
604
+ specifying the object itself.
605
+ """
606
+ assert two_name_class_list.index(index_value) == expected_index
607
+
608
+
609
+ @pytest.mark.parametrize(
610
+ ["index_value", "offset", "expected_index"],
611
+ [
612
+ ("Bob", 1, 2),
613
+ (InputAttributes(name="Bob"), -3, -2),
614
+ ],
615
+ )
616
+ def test_index_offset(
617
+ two_name_class_list: ClassList,
618
+ index_value: Union[object, str],
619
+ offset: int,
620
+ expected_index: int,
621
+ ) -> None:
622
+ """We should be able to find the index of an object in the ClassList either by its name_field value or by
623
+ specifying the object itself. When using an offset, the value of the index should be shifted accordingly.
624
+ """
625
+ assert two_name_class_list.index(index_value, offset) == expected_index
626
+
627
+
628
+ @pytest.mark.parametrize(
629
+ "index_value",
630
+ [
631
+ "Eve",
632
+ (InputAttributes(name="Eve")),
633
+ ],
634
+ )
635
+ def test_index_not_present(two_name_class_list: ClassList, index_value: Union[object, str]) -> None:
636
+ """If we try to find the index of an object not included in the ClassList we should raise a ValueError."""
637
+ # with pytest.raises(ValueError, match=f"'{index_value}' is not in list") as e:
638
+ with pytest.raises(ValueError):
639
+ two_name_class_list.index(index_value)
640
+
641
+
642
+ @pytest.mark.parametrize(
643
+ "extended_list",
644
+ [
645
+ (ClassList(InputAttributes(name="Eve"))),
646
+ ([InputAttributes(name="Eve")]),
647
+ (InputAttributes(name="Eve"),),
648
+ (InputAttributes(name="Eve")),
649
+ ],
650
+ )
651
+ def test_extend(two_name_class_list: ClassList, extended_list: Sequence, three_name_class_list: ClassList) -> None:
652
+ """We should be able to extend a ClassList using another ClassList or a sequence. Individual objects should be
653
+ wrapped in a list before being added.
654
+ """
655
+ class_list = two_name_class_list
656
+ class_list.extend(extended_list)
657
+ assert class_list == three_name_class_list
658
+
659
+
660
+ @pytest.mark.parametrize(
661
+ "extended_list",
662
+ [
663
+ (ClassList(InputAttributes(name="Alice"))),
664
+ ([InputAttributes(name="Alice")]),
665
+ (InputAttributes(name="Alice"),),
666
+ ],
667
+ )
668
+ def test_extend_empty_classlist(extended_list: Sequence, one_name_class_list: ClassList) -> None:
669
+ """We should be able to extend a ClassList using another ClassList or a sequence"""
670
+ class_list = ClassList()
671
+ class_list.extend(extended_list)
672
+ assert class_list == one_name_class_list
673
+ assert isinstance(extended_list[-1], class_list._class_handle)
674
+
675
+
676
+ @pytest.mark.parametrize(
677
+ ["new_values", "expected_classlist"],
678
+ [
679
+ ({"name": "Eve"}, ClassList([InputAttributes(name="Eve"), InputAttributes(name="Bob")])),
680
+ (
681
+ {"name": "John", "surname": "Luther"},
682
+ ClassList([InputAttributes(name="John", surname="Luther"), InputAttributes(name="Bob")]),
683
+ ),
684
+ ],
685
+ )
686
+ def test_set_fields(two_name_class_list: ClassList, new_values: dict[str, Any], expected_classlist: ClassList) -> None:
687
+ """We should be able to set field values in an element of a ClassList using keyword arguments."""
688
+ class_list = two_name_class_list
689
+ class_list.set_fields(0, **new_values)
690
+ assert class_list == expected_classlist
691
+
692
+
693
+ @pytest.mark.parametrize(
694
+ "new_values",
695
+ [
696
+ ({"name": "Bob"}),
697
+ ],
698
+ )
699
+ def test_set_fields_same_name_field(two_name_class_list: ClassList, new_values: dict[str, Any]) -> None:
700
+ """If we set the name_field of an object in the ClassList to one already defined, we should raise a ValueError."""
701
+ with pytest.raises(
702
+ ValueError,
703
+ match=f"Input arguments contain the {two_name_class_list.name_field} "
704
+ f"'{new_values[two_name_class_list.name_field]}', "
705
+ f"which is already specified in the ClassList",
706
+ ):
707
+ two_name_class_list.set_fields(0, **new_values)
708
+
709
+
710
+ @pytest.mark.parametrize(
711
+ ["class_list", "expected_names"],
712
+ [
713
+ (ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob")]), ["Alice", "Bob"]),
714
+ (ClassList([InputAttributes(id="Alice"), InputAttributes(id="Bob")], name_field="id"), ["Alice", "Bob"]),
715
+ (ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob")], name_field="id"), []),
716
+ (ClassList([InputAttributes(surname="Morgan"), InputAttributes(surname="Terwilliger")]), []),
717
+ (
718
+ ClassList([InputAttributes(name="Alice", surname="Morgan"), InputAttributes(surname="Terwilliger")]),
719
+ ["Alice"],
720
+ ),
721
+ (
722
+ ClassList(
723
+ [InputAttributes(name="Alice", surname="Morgan"), InputAttributes(surname="Terwilliger")],
724
+ name_field="surname",
725
+ ),
726
+ ["Morgan", "Terwilliger"],
727
+ ),
728
+ (ClassList(InputAttributes()), []),
729
+ ],
730
+ )
731
+ def test_get_names(class_list: ClassList, expected_names: list[str]) -> None:
732
+ """We should get a list of the values of the name_field attribute from each object with it defined in the
733
+ ClassList.
734
+ """
735
+ assert class_list.get_names() == expected_names
736
+
737
+
738
+ @pytest.mark.parametrize(
739
+ ["class_list", "expected_matches"],
740
+ [
741
+ (ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob")]), [(0, "name")]),
742
+ (ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob", id="Alice")]), [(0, "name"), (1, "id")]),
743
+ (ClassList([InputAttributes(surname="Morgan"), InputAttributes(surname="Terwilliger")]), []),
744
+ (ClassList(InputAttributes()), []),
745
+ ],
746
+ )
747
+ def test_get_all_matches(class_list: ClassList, expected_matches: list[tuple]) -> None:
748
+ """We should get a list of (index, field) tuples matching the given value in the ClassList."""
749
+ assert class_list.get_all_matches("Alice") == expected_matches
750
+
751
+
752
+ @pytest.mark.parametrize(
753
+ "input_dict",
754
+ [
755
+ ({"name": "Eve"}),
756
+ ({"surname": "Polastri"}),
757
+ ],
758
+ )
759
+ def test__validate_name_field(two_name_class_list: ClassList, input_dict: dict[str, Any]) -> None:
760
+ """We should not raise an error if the input values do not contain a name_field value defined in an object in the
761
+ ClassList.
762
+ """
763
+ assert two_name_class_list._validate_name_field(input_dict) is None
764
+
765
+
766
+ @pytest.mark.parametrize(
767
+ "input_dict",
768
+ [
769
+ ({"name": "Alice"}),
770
+ ],
771
+ )
772
+ def test__validate_name_field_not_unique(two_name_class_list: ClassList, input_dict: dict[str, Any]) -> None:
773
+ """We should raise a ValueError if we input values containing a name_field defined in an object in the ClassList."""
774
+ with pytest.raises(
775
+ ValueError,
776
+ match=f"Input arguments contain the {two_name_class_list.name_field} "
777
+ f"'{input_dict[two_name_class_list.name_field]}', "
778
+ f"which is already specified in the ClassList",
779
+ ):
780
+ two_name_class_list._validate_name_field(input_dict)
781
+
782
+
783
+ @pytest.mark.parametrize(
784
+ "input_list",
785
+ [
786
+ ([InputAttributes(name="Alice"), InputAttributes(name="Bob")]),
787
+ ([InputAttributes(surname="Morgan"), InputAttributes(surname="Terwilliger")]),
788
+ ([InputAttributes(name="Alice", surname="Morgan"), InputAttributes(surname="Terwilliger")]),
789
+ ([InputAttributes()]),
790
+ ([]),
791
+ ],
792
+ )
793
+ def test__check_unique_name_fields(two_name_class_list: ClassList, input_list: Iterable) -> None:
794
+ """We should not raise an error if an input list contains objects with different name_field values, or if the
795
+ name_field is not defined.
796
+ """
797
+ assert two_name_class_list._check_unique_name_fields(input_list) is None
798
+
799
+
800
+ @pytest.mark.parametrize(
801
+ "input_list",
802
+ [
803
+ ([InputAttributes(name="Alice"), InputAttributes(name="Alice")]),
804
+ ],
805
+ )
806
+ def test__check_unique_name_fields_not_unique(two_name_class_list: ClassList, input_list: Iterable) -> None:
807
+ """We should raise a ValueError if an input list contains multiple objects with matching name_field values
808
+ defined.
809
+ """
810
+ with pytest.raises(
811
+ ValueError,
812
+ match=f"Input list contains objects with the same value of the " f"{two_name_class_list.name_field} attribute",
813
+ ):
814
+ two_name_class_list._check_unique_name_fields(input_list)
815
+
816
+
817
+ @pytest.mark.parametrize(
818
+ "input_list",
819
+ [
820
+ ([InputAttributes(name="Alice"), InputAttributes(name="Bob")]),
821
+ ],
822
+ )
823
+ def test__check_classes(input_list: Iterable) -> None:
824
+ """We should not raise an error all objects in the ClassList are of the same type."""
825
+ class_list = ClassList([InputAttributes()])
826
+ assert class_list._check_classes(input_list) is None
827
+
828
+
829
+ @pytest.mark.parametrize(
830
+ "input_list",
831
+ [
832
+ ([InputAttributes(name="Alice"), dict(name="Bob")]),
833
+ ],
834
+ )
835
+ def test__check_classes_different_classes(input_list: Iterable) -> None:
836
+ """We should raise a ValueError if an input list contains objects of different types."""
837
+ class_list = ClassList([InputAttributes()])
838
+ with pytest.raises(
839
+ ValueError,
840
+ match=(f"Input list contains elements of type other " f"than '{class_list._class_handle.__name__}'"),
841
+ ):
842
+ class_list._check_classes(input_list)
843
+
844
+
845
+ @pytest.mark.parametrize(
846
+ ["value", "expected_output"],
847
+ [
848
+ ("Alice", InputAttributes(name="Alice")),
849
+ ("Eve", "Eve"),
850
+ ],
851
+ )
852
+ def test__get_item_from_name_field(
853
+ two_name_class_list: ClassList,
854
+ value: str,
855
+ expected_output: Union[object, str],
856
+ ) -> None:
857
+ """When we input the name_field value of an object defined in the ClassList, we should return the object.
858
+ If the value is not the name_field of an object defined in the ClassList, we should return the value.
859
+ """
860
+ assert two_name_class_list._get_item_from_name_field(value) == expected_output
861
+
862
+
863
+ @pytest.mark.parametrize(
864
+ ["input_list", "expected_type"],
865
+ [
866
+ ([InputAttributes(name="Alice")], InputAttributes),
867
+ ([InputAttributes(name="Alice"), SubInputAttributes(name="Bob")], InputAttributes),
868
+ ([SubInputAttributes(name="Alice"), InputAttributes(name="Bob")], InputAttributes),
869
+ ([SubInputAttributes(name="Alice"), SubInputAttributes(name="Bob")], SubInputAttributes),
870
+ (
871
+ [SubInputAttributes(name="Alice"), SubInputAttributes(name="Bob"), InputAttributes(name="Eve")],
872
+ InputAttributes,
873
+ ),
874
+ ([InputAttributes(name="Alice"), dict(name="Bob")], InputAttributes),
875
+ ([dict(name="Alice"), InputAttributes(name="Bob")], dict),
876
+ ],
877
+ )
878
+ def test_determine_class_handle(input_list: ClassList, expected_type: type) -> None:
879
+ """The _class_handle for the ClassList should be the type that satisfies the condition "isinstance(element, type)"
880
+ for all elements in the ClassList.
881
+ """
882
+ assert ClassList._determine_class_handle(input_list) == expected_type