99from linkml_runtime .loaders import yaml_loader
1010from linkml_runtime .utils .formatutils import remove_empty_items , is_empty
1111from linkml_runtime .utils .schemaview import SchemaView
12- from linkml_runtime .dumpers import csv_dumper
13- from linkml_runtime .loaders import csv_loader
12+ from linkml_runtime .dumpers import csv_dumper , tsv_dumper
13+ from linkml_runtime .loaders import csv_loader , tsv_loader
1414from linkml_runtime .utils .yamlutils import as_json_object
15- from tests .test_loaders_dumpers .models .books_normalized import Shop , Book , GenreEnum , BookSeries
15+ from tests .test_loaders_dumpers .models .books_normalized import Author , Review , Shop , Book , GenreEnum , BookSeries
1616
1717
1818ROOT = os .path .abspath (os .path .dirname (__file__ ))
@@ -30,25 +30,27 @@ def _json(obj) -> str:
3030 return json .dumps (obj , indent = ' ' , sort_keys = True )
3131
3232
33- class CSVGenTestCase (unittest .TestCase ):
33+ class CsvAndTsvGenTestCase (unittest .TestCase ):
3434
3535 def test_object_model (self ):
3636 book = Book (id = 'B1' , genres = ['fantasy' ], creator = {})
37- print (book .genres )
38- print (type (book .genres [0 ]))
3937 logging .debug (as_json_obj (book .genres [0 ]))
4038 assert str (book .genres [0 ]) == 'fantasy'
4139 assert book .genres [0 ].code .text == 'fantasy'
4240 processed = remove_empty_items (book .genres )
43- print (f'PR={ processed } ' )
4441 assert processed [0 ] == 'fantasy'
45- series = BookSeries (id = 'S1' )
42+ series = BookSeries (id = 'S1' , creator = Author ( name = "Q. Writer" ), reviews = [ Review ( rating = 5 )] )
4643 series .books .append (book )
4744 schemaview = SchemaView (SCHEMA )
4845 shop = Shop ()
49- shop .all_book_series .append (book )
50- #csvstr = csv_dumper.dumps(shop, index_slot='all_book_series', schemaview=schemaview)
51- #logging.debug(csvstr)
46+ shop .all_book_series .append (series )
47+
48+ csvstr = csv_dumper .dumps (shop , index_slot = 'all_book_series' , schemaview = schemaview )
49+ assert "," in csvstr
50+ assert "\t " not in csvstr
51+
52+ tsvstr = tsv_dumper .dumps (shop , index_slot = 'all_book_series' , schemaview = schemaview )
53+ assert "\t " in tsvstr
5254
5355 def test_csvgen_roundtrip (self ):
5456 schemaview = SchemaView (SCHEMA )
@@ -60,6 +62,13 @@ def test_csvgen_roundtrip(self):
6062 logging .debug (f'COMPARE 2: { data } ' )
6163 assert roundtrip == data
6264
65+ def test_tsvgen_roundtrip (self ):
66+ schemaview = SchemaView (SCHEMA )
67+ data = yaml_loader .load (DATA , target_class = Shop )
68+ tsv_dumper .dump (data , to_file = OUTPUT , index_slot = 'all_book_series' , schemaview = schemaview )
69+ roundtrip = tsv_loader .load (OUTPUT , target_class = Shop , index_slot = 'all_book_series' , schemaview = schemaview )
70+ assert roundtrip == data
71+
6372 def test_csvgen_unroundtrippable (self ):
6473 schemaview = SchemaView (SCHEMA )
6574 #schema = YAMLGenerator(SCHEMA).schema
@@ -84,6 +93,13 @@ def test_csvgen_unroundtrippable(self):
8493 logging .debug (json_dumper .dumps (roundtrip ))
8594 assert roundtrip == data
8695
96+ def test_tsvgen_unroundtrippable (self ):
97+ schemaview = SchemaView (SCHEMA )
98+ data = yaml_loader .load (DATA2 , target_class = Shop )
99+ assert str (data .all_book_series [0 ].genres [0 ]) == 'fantasy'
100+ tsv_dumper .dump (data , to_file = OUTPUT2 , index_slot = 'all_book_series' , schemaview = schemaview )
101+ roundtrip = tsv_loader .load (OUTPUT2 , target_class = Shop , index_slot = 'all_book_series' , schemaview = schemaview )
102+ assert roundtrip == data
87103
88104
89105
0 commit comments