11"""Unit tests for the one.alf.path module."""
22import unittest
33import tempfile
4+ from datetime import datetime
45from types import GeneratorType
56from uuid import uuid4
67from pathlib import Path , PurePath , PureWindowsPath , PurePosixPath
@@ -496,6 +497,67 @@ def test_with_extension(self):
496497 self .assertRaises (ValueError , self .alfpath .with_extension , '' )
497498 self .assertRaises (ALFInvalid , self .alfpath .with_stem ('foo' ).with_extension , 'ext' )
498499
500+ def test_with_lab (self ):
501+ """Test for PureALFPath.with_lab method."""
502+ # Test with lab
503+ expected = ALFPath (* self .alfpath .parts [:- 8 ], 'newlab' , * self .alfpath .parts [- 7 :])
504+ self .assertEqual (expected , self .alfpath .with_lab ('newlab' ))
505+ # Test without lab
506+ alfpath = ALFPath (* self .alfpath .parts [:- 8 ], * self .alfpath .parts [- 6 :])
507+ self .assertEqual (expected , alfpath .with_lab ('newlab' ))
508+ # Test strict
509+ self .assertEqual (expected , self .alfpath .with_lab ('newlab' , strict = True ))
510+ self .assertRaises (ALFInvalid , alfpath .with_lab , 'newlab' , strict = True )
511+ # Test validation
512+ self .assertRaises (ValueError , self .alfpath .with_lab , '' )
513+ self .assertRaises (ValueError , self .alfpath .with_lab , None )
514+ self .assertRaises (ValueError , self .alfpath .with_lab , '#s!@#' )
515+ self .assertRaises (ALFInvalid , self .alfpath .relative_to_session ().with_lab , 'lab' )
516+
517+ def test_with_subject (self ):
518+ """Test for PureALFPath.with_subject method."""
519+ # Test with subject
520+ expected = ALFPath (* self .alfpath .parts [:- 6 ], 'foo' , * self .alfpath .parts [- 5 :])
521+ self .assertEqual (expected , self .alfpath .with_subject ('foo' ))
522+ # Test without lab (should not depend on Subjects folder)
523+ alfpath = ALFPath (* self .alfpath .parts [:- 8 ], * self .alfpath .parts [- 6 :])
524+ expected = ALFPath (* alfpath .parts [:- 6 ], 'foo' , * alfpath .parts [- 5 :])
525+ self .assertEqual (expected , alfpath .with_subject ('foo' ))
526+ # Test validation
527+ self .assertRaises (ValueError , self .alfpath .with_subject , '' )
528+ self .assertRaises (ValueError , self .alfpath .with_subject , None )
529+ self .assertRaises (ValueError , self .alfpath .with_subject , '#s!@#' )
530+ self .assertRaises (ALFInvalid , self .alfpath .relative_to_session ().with_subject , 'subject' )
531+
532+ def test_with_date (self ):
533+ """Test for PureALFPath.with_date method."""
534+ # Test with date
535+ expected = ALFPath (* self .alfpath .parts [:- 5 ], '2020-01-02' , * self .alfpath .parts [- 4 :])
536+ self .assertEqual (expected , self .alfpath .with_date ('2020-01-02' ))
537+ # Test with datetime object
538+ date = datetime .fromisoformat ('2020-01-02T00:00:00' )
539+ self .assertEqual (expected , self .alfpath .with_date (date ))
540+ # Test validation
541+ self .assertRaises (ValueError , self .alfpath .with_date , '' )
542+ self .assertRaises (ValueError , self .alfpath .with_date , None )
543+ self .assertRaises (ValueError , self .alfpath .with_date , '6/1/2020' )
544+ self .assertRaises (ALFInvalid , self .alfpath .relative_to_session ().with_date , '2020-01-02' )
545+
546+ def test_with_sequence (self ):
547+ """Test for PureALFPath.with_sequence method."""
548+ # Test with number
549+ expected = ALFPath (* self .alfpath .parts [:- 4 ], '002' , * self .alfpath .parts [- 3 :])
550+ self .assertEqual (expected , self .alfpath .with_sequence (2 ))
551+ self .assertEqual (expected , self .alfpath .with_sequence ('002' ))
552+ # Test with zero
553+ self .assertEqual ('000' , self .alfpath .with_sequence (0 ).parts [- 4 ])
554+ # Test validation
555+ self .assertRaises (ValueError , self .alfpath .with_sequence , '' )
556+ self .assertRaises (ValueError , self .alfpath .with_sequence , None )
557+ self .assertRaises (ValueError , self .alfpath .with_sequence , 'foo' )
558+ self .assertRaises (ValueError , self .alfpath .with_sequence , 1e4 )
559+ self .assertRaises (ALFInvalid , self .alfpath .relative_to_session ().with_sequence , 2 )
560+
499561 def test_parts_properties (self ):
500562 """Test the PureALFPath ALF dataset part properties."""
501563 # Namespace
@@ -521,6 +583,18 @@ def test_parts_properties(self):
521583 alfpath = self .alfpath .with_name ('_ns_obj.attr_times_bpod.foo.bar.ext' )
522584 expected = ('ns' , 'obj' , 'attr_times' , 'bpod' , 'foo.bar' , 'ext' )
523585 self .assertEqual (expected , alfpath .dataset_name_parts )
586+ # Lab
587+ self .assertEqual ('labname' , self .alfpath .lab )
588+ self .assertEqual ('' , self .alfpath .relative_to_session ().lab )
589+ # Subject
590+ self .assertEqual ('subject' , self .alfpath .subject )
591+ self .assertEqual ('' , self .alfpath .relative_to_session ().subject )
592+ # Date
593+ self .assertEqual ('1900-01-01' , self .alfpath .date )
594+ self .assertEqual ('' , self .alfpath .relative_to_session ().date )
595+ # Number
596+ self .assertEqual ('001' , self .alfpath .sequence )
597+ self .assertEqual ('' , self .alfpath .relative_to_session ().sequence )
524598 # session_parts
525599 self .assertEqual (('labname' , 'subject' , '1900-01-01' , '001' ), self .alfpath .session_parts )
526600 alfpath = ALFPath (* self .alfpath .parts [5 :])
0 commit comments