2727"""
2828
2929from contextlib import contextmanager
30+ import importlib
3031import pathlib
3132import tempfile
3233
4344
4445
4546@contextmanager
46- def create_catalog ():
47+ def create_catalog (catalog_name_in_pyiceberg_config = None ):
4748 # the catalog stores the full path of data files, so the catalog needs to be
4849 # created dynamically, and not saved in pandas/tests/io/data as other formats
49- with tempfile .TemporaryDirectory ("pandas-iceberg.tmp" ) as catalog_path :
50+ with tempfile .TemporaryDirectory ("- pandas-iceberg.tmp" ) as catalog_path :
5051 uri = f"sqlite:///{ catalog_path } /catalog.sqlite"
52+ warehouse = f"file://{ catalog_path } "
5153 catalog = pyiceberg_catalog .load_catalog (
52- "default" ,
54+ catalog_name_in_pyiceberg_config or "default" ,
5355 type = "sql" ,
5456 uri = uri ,
55- warehouse = f"file:// { catalog_path } " ,
57+ warehouse = warehouse ,
5658 )
57- catalog .create_namespace ("default " )
59+ catalog .create_namespace ("ns " )
5860
5961 df = pq .read_table (
6062 pathlib .Path (__file__ ).parent / "data" / "parquet" / "simple.parquet"
6163 )
62- table = catalog .create_table ("default.simple " , schema = df .schema )
64+ table = catalog .create_table ("ns.my_table " , schema = df .schema )
6365 table .append (df )
6466
65- yield uri
67+ if catalog_name_in_pyiceberg_config is not None :
68+ config_path = pathlib .Path .home () / ".pyiceberg.yaml"
69+ with open (config_path , "w" ) as f :
70+ f .write (f"""\
71+ catalog:
72+ { catalog_name_in_pyiceberg_config } :
73+ type: sql
74+ uri: { uri }
75+ warehouse: { warehouse } """ )
76+ importlib .reload (pyiceberg_catalog ) # needed to reload the config file
77+
78+ try :
79+ yield uri
80+ finally :
81+ if catalog_name_in_pyiceberg_config is not None :
82+ config_path .unlink ()
6683
6784
6885class TestIceberg :
@@ -75,31 +92,25 @@ def test_read(self):
7592 )
7693 with create_catalog () as catalog_uri :
7794 result = read_iceberg (
78- "default.simple " ,
95+ "ns.my_table " ,
7996 catalog_properties = {"uri" : catalog_uri },
8097 )
8198 tm .assert_frame_equal (result , expected )
8299
83- def test_read_by_catalog_name (self ):
84- config_path = pathlib .Path .home () / ".pyiceberg.yaml"
85- with create_catalog () as catalog_uri :
86- with open (config_path , "w" ) as f :
87- f .write (f"""\
88- catalog:
89- pandas_tests_catalog:
90- uri: { catalog_uri } """ )
91- expected = pd .DataFrame (
92- {
93- "A" : [1 , 2 , 3 ],
94- "B" : ["foo" , "foo" , "foo" ],
95- }
96- )
100+ @pytest .mark .parametrize ("catalog_name" , ["default" , "pandas_tests" ])
101+ def test_read_by_catalog_name (self , catalog_name ):
102+ expected = pd .DataFrame (
103+ {
104+ "A" : [1 , 2 , 3 ],
105+ "B" : ["foo" , "foo" , "foo" ],
106+ }
107+ )
108+ with create_catalog (catalog_name_in_pyiceberg_config = catalog_name ):
97109 result = read_iceberg (
98- "default.simple " ,
99- catalog_name = "pandas_tests_catalog" ,
110+ "ns.my_table " ,
111+ catalog_name = catalog_name ,
100112 )
101113 tm .assert_frame_equal (result , expected )
102- # config_path.unlink()
103114
104115 def test_read_with_row_filter (self ):
105116 expected = pd .DataFrame (
@@ -110,7 +121,7 @@ def test_read_with_row_filter(self):
110121 )
111122 with create_catalog () as catalog_uri :
112123 result = read_iceberg (
113- "default.simple " ,
124+ "ns.my_table " ,
114125 catalog_properties = {"uri" : catalog_uri },
115126 row_filter = "A > 1" ,
116127 )
@@ -124,7 +135,7 @@ def test_read_with_case_sensitive(self):
124135 )
125136 with create_catalog () as catalog_uri :
126137 result = read_iceberg (
127- "default.simple " ,
138+ "ns.my_table " ,
128139 catalog_properties = {"uri" : catalog_uri },
129140 selected_fields = ["a" ],
130141 case_sensitive = False ,
@@ -134,7 +145,7 @@ def test_read_with_case_sensitive(self):
134145 with create_catalog () as catalog_uri :
135146 with pytest .raises (ValueError , match = "^Could not find column" ):
136147 read_iceberg (
137- "default.simple " ,
148+ "ns.my_table " ,
138149 catalog_properties = {"uri" : catalog_uri },
139150 selected_fields = ["a" ],
140151 case_sensitive = True ,
@@ -149,7 +160,7 @@ def test_read_with_limit(self):
149160 )
150161 with create_catalog () as catalog_uri :
151162 result = read_iceberg (
152- "default.simple " ,
163+ "ns.my_table " ,
153164 catalog_properties = {"uri" : catalog_uri },
154165 limit = 2 ,
155166 )
0 commit comments