11"""
2- Tests of neo.rawio.examplerawio
2+ Tests of neo.rawio.phyrawio
33
4- Note for dev:
5- if you write a new RawIO class your need to put some file
6- to be tested at g-node portal, Ask neuralensemble list for that.
7- The file need to be small.
8-
9- Then you have to copy/paste/renamed the TestExampleRawIO
10- class and a full test will be done to test if the new coded IO
11- is compliant with the RawIO API.
12-
13- If you have problems, do not hesitate to ask help github (prefered)
14- of neuralensemble list.
15-
16- Note that same mechanism is used a neo.io API so files are tested
17- several time with neo.rawio (numpy buffer) and neo.io (neo object tree).
18- See neo.test.iotest.*
19-
20-
21- Author: Samuel Garcia
4+ Author: Regimantas Jurkus
225
236"""
247
@@ -28,6 +11,12 @@ class and a full test will be done to test if the new coded IO
2811
2912from neo .test .rawiotest .common_rawio_test import BaseTestRawIO
3013
14+ import csv
15+ import tempfile
16+ from pathlib import Path
17+ from collections import OrderedDict
18+ import sys
19+
3120
3221class TestPhyRawIO (BaseTestRawIO , unittest .TestCase ):
3322 rawioclass = PhyRawIO
@@ -43,6 +32,57 @@ class TestPhyRawIO(BaseTestRawIO, unittest.TestCase):
4332 ]
4433 entities_to_test = ['phy_example_0' ]
4534
35+ def test_csv_tsv_parser_with_csv (self ):
36+ csv_tempfile = Path (tempfile .gettempdir ()).joinpath ('test.csv' )
37+ with open (csv_tempfile , 'w' ) as csv_file :
38+ csv_writer = csv .writer (csv_file , delimiter = ',' )
39+ csv_writer .writerow (['Header 1' , 'Header 2' ])
40+ csv_writer .writerow (['Value 1' , 'Value 2' ])
41+
42+ # the parser in PhyRawIO runs csv.DictReader to parse the file
43+ # csv.DictReader for python version 3.6+ returns list of OrderedDict
44+ if (3 , 6 ) <= sys .version_info < (3 , 8 ):
45+ target = [OrderedDict ({'Header 1' : 'Value 1' ,
46+ 'Header 2' : 'Value 2' })]
47+
48+ # csv.DictReader for python version 3.8+ returns list of dict
49+ elif sys .version_info >= (3 , 8 ):
50+ target = [{'Header 1' : 'Value 1' , 'Header 2' : 'Value 2' }]
51+
52+ list_of_dict = PhyRawIO ._parse_tsv_or_csv_to_list_of_dict (csv_tempfile )
53+
54+ self .assertEqual (target , list_of_dict )
55+
56+ def test_csv_tsv_parser_with_tsv (self ):
57+ tsv_tempfile = Path (tempfile .gettempdir ()).joinpath ('test.tsv' )
58+ with open (tsv_tempfile , 'w' ) as tsv_file :
59+ tsv_writer = csv .writer (tsv_file , delimiter = '\t ' )
60+ tsv_writer .writerow (['Header 1' , 'Header 2' ])
61+ tsv_writer .writerow (['Value 1' , 'Value 2' ])
62+
63+ # the parser in PhyRawIO runs csv.DictReader to parse the file
64+ # csv.DictReader for python version 3.6+ returns list of OrderedDict
65+ if (3 , 6 ) <= sys .version_info < (3 , 8 ):
66+ target = [OrderedDict ({'Header 1' : 'Value 1' ,
67+ 'Header 2' : 'Value 2' })]
68+
69+ # csv.DictReader for python version 3.8+ returns list of dict
70+ elif sys .version_info >= (3 , 8 ):
71+ target = [{'Header 1' : 'Value 1' , 'Header 2' : 'Value 2' }]
72+
73+ list_of_dict = PhyRawIO ._parse_tsv_or_csv_to_list_of_dict (tsv_tempfile )
74+
75+ self .assertEqual (target , list_of_dict )
76+
77+ def test_csv_tsv_parser_error_raising (self ):
78+ txt_tempfile = Path (tempfile .gettempdir ()).joinpath ('test.txt' )
79+ with open (txt_tempfile , 'w' ) as txt_file :
80+ txt_file .write ('This is a test' )
81+
82+ self .assertRaises (ValueError ,
83+ PhyRawIO ._parse_tsv_or_csv_to_list_of_dict ,
84+ txt_tempfile )
85+
4686
4787if __name__ == "__main__" :
4888 unittest .main ()
0 commit comments