1+ import json
2+
3+ import yaml
4+
15from linkml_runtime .dumpers import yaml_dumper , json_dumper , csv_dumper
26from tests .test_loaders_dumpers .loaderdumpertestcase import LoaderDumperTestCase
37from tests .test_loaders_dumpers .models .books_normalized_pydantic import Book , BookSeries , Author
8+ from linkml_runtime .utils .formatutils import remove_empty_items
49
510
611class PydanticDumpersTestCase (LoaderDumperTestCase ):
@@ -10,22 +15,29 @@ class PydanticDumpersTestCase(LoaderDumperTestCase):
1015 def setUpClass (cls ) -> None :
1116 """ Generate a small sample Books instance for testing purposes """
1217 LoaderDumperTestCase .setUpClass ()
13- b1 = Book (name = 'The Fellowship of the Ring' , id = "S001.1" , price = "5.99" , summary = "Hobbits" )
18+ b1 = Book (name = 'Fellowship of the Ring' , id = "S001.1" , price = "5.99" , summary = "Hobbits" )
1419 b2 = Book (name = 'The Two Towers' , id = "S001.2" , price = "5.99" , summary = "More hobbits" )
15- b3 = Book (name = 'The Return of the King' , id = "S001.3" , price = "5 .99" , summary = "Yet more hobbits" )
16- jrr = Author (name = 'JRR Tolkien ' , from_country = 'England' )
17- cls .bookseries = BookSeries (name = 'The Lord of the Rings' , id = "S001" , genres = ["fantasy" ], creator = jrr , books = [b1 , b2 , b3 ])
20+ b3 = Book (name = 'Return of the King' , id = "S001.3" , price = "6 .99" , summary = "Yet more hobbits" )
21+ jrr = Author (name = 'JRR Tolkein ' , from_country = 'England' )
22+ cls .bookseries = BookSeries (name = 'Lord of the Rings' , id = "S001" , genres = ["fantasy" ], creator = jrr , books = [b1 , b2 , b3 ])
1823
1924 def test_yaml_dumper (self ):
2025 """ Test the yaml emitter """
2126 self .dump_test ('book_series_lotr.yaml' , lambda out_fname : yaml_dumper .dump (self .bookseries , out_fname ))
2227 self .dumps_test ('book_series_lotr.yaml' , lambda : yaml_dumper .dumps (self .bookseries ))
2328
24- #TODO: test the contents of generated file
29+ # test contents of yaml file with cleaned dict made from bookseries instance in setup
30+ with open (self .env .input_path ('book_series_lotr.yaml' ), 'r' ) as f :
31+ data = yaml .safe_load (f )
32+ self .assertEqual (data , remove_empty_items (self .bookseries .dict ()))
33+
2534
2635 def test_json_dumper (self ):
2736 """ Test the json emitter """
2837 self .dump_test ('book_series_lotr.json' , lambda out_fname : json_dumper .dump (self .bookseries , out_fname ))
2938 self .dumps_test ('book_series_lotr.json' , lambda : json_dumper .dumps (self .bookseries ))
3039
31- #TODO: test the contents of generated file
40+ # test contents of json file with cleaned dict made from bookseries instance in setup
41+ with open (self .env .input_path ('book_series_lotr.json' ), 'r' ) as f :
42+ data = json .load (f )
43+ self .assertEqual (data , remove_empty_items (self .bookseries .dict ()))
0 commit comments