1313from app .main import app
1414
1515from dlt .sources .rest_api import rest_api_source
16- from dlt .common .destination .dataset import SupportsReadableDataset
1716
1817from tests .utils import EXPECTED_TABLES_COUNTS_JANUARY_2017
1918
@@ -31,9 +30,7 @@ def send(self, prepared_request, **kwargs):
3130 )
3231
3332
34- def row_counts (
35- dataset : SupportsReadableDataset , tables : list [str ] = None
36- ) -> dict [str , int ]:
33+ def row_counts (dataset : dlt .Dataset , tables : list [str ] = None ) -> dict [str , int ]:
3734 counts = dataset .row_counts (table_names = tables ).arrow ().to_pydict ()
3835 return {t : c for t , c in zip (counts ["table_name" ], counts ["row_count" ])}
3936
@@ -135,3 +132,113 @@ def test_live_jaffle_shop():
135132 "orders" : 3303 , # 3303 orders in january 2017
136133 "orders__items" : 5072 ,
137134 }
135+
136+
137+ def test_expected_columns_schema ():
138+ """
139+ This tests extracts the full dataset with the dlt rest_api source
140+ """
141+ source = rest_api_source (
142+ {
143+ "client" : {
144+ "base_url" : "http://localhost:8000/api/v1" ,
145+ "paginator" : {
146+ "type" : "header_link" ,
147+ },
148+ "session" : FastAPISession (app ),
149+ },
150+ "resources" : [
151+ "customers" ,
152+ "products" ,
153+ "stores" ,
154+ "supplies" ,
155+ # orders includes items
156+ # we only load orders for january 2017
157+ {
158+ "name" : "orders" ,
159+ "endpoint" : {
160+ "path" : "orders" ,
161+ "params" : {
162+ "page_size" : 1000 ,
163+ "start_date" : "2017-01-01" ,
164+ "end_date" : "2017-01-31" ,
165+ },
166+ },
167+ },
168+ ],
169+ },
170+ )
171+ pipeline = dlt .pipeline (
172+ pipeline_name = "rest_api_example" ,
173+ destination = duckdb (credentials = "test.db" ),
174+ dataset_name = "rest_api_data" ,
175+ dev_mode = True ,
176+ )
177+ pipeline .run (source )
178+
179+ expected_table_columns = {
180+ "customers" : {
181+ "id" : {"name" : "id" , "data_type" : "text" , "nullable" : True },
182+ "name" : {"name" : "name" , "data_type" : "text" , "nullable" : True },
183+ },
184+ "products" : {
185+ "sku" : {"name" : "sku" , "data_type" : "text" , "nullable" : True },
186+ "name" : {"name" : "name" , "data_type" : "text" , "nullable" : True },
187+ "type" : {"name" : "type" , "data_type" : "text" , "nullable" : True },
188+ "price" : {"name" : "price" , "data_type" : "double" , "nullable" : True },
189+ "description" : {
190+ "name" : "description" ,
191+ "data_type" : "text" ,
192+ "nullable" : True ,
193+ },
194+ },
195+ "stores" : {
196+ "id" : {"name" : "id" , "data_type" : "text" , "nullable" : True },
197+ "name" : {"name" : "name" , "data_type" : "text" , "nullable" : True },
198+ "opened_at" : {
199+ "name" : "opened_at" ,
200+ "data_type" : "timestamp" ,
201+ "nullable" : True ,
202+ },
203+ "tax_rate" : {"name" : "tax_rate" , "data_type" : "double" , "nullable" : True },
204+ },
205+ "supplies" : {
206+ "id" : {"name" : "id" , "data_type" : "text" , "nullable" : True },
207+ "name" : {"name" : "name" , "data_type" : "text" , "nullable" : True },
208+ "cost" : {"name" : "cost" , "data_type" : "double" , "nullable" : True },
209+ "perishable" : {"name" : "perishable" , "data_type" : "bool" , "nullable" : True },
210+ "sku" : {"name" : "sku" , "data_type" : "text" , "nullable" : True },
211+ },
212+ "orders" : {
213+ "id" : {"name" : "id" , "data_type" : "text" , "nullable" : True },
214+ "customer_id" : {
215+ "name" : "customer_id" ,
216+ "data_type" : "text" ,
217+ "nullable" : True ,
218+ },
219+ "store_id" : {"name" : "store_id" , "data_type" : "text" , "nullable" : True },
220+ "ordered_at" : {
221+ "name" : "ordered_at" ,
222+ "data_type" : "timestamp" ,
223+ "nullable" : True ,
224+ },
225+ "subtotal" : {"name" : "subtotal" , "data_type" : "double" , "nullable" : True },
226+ "tax_paid" : {"name" : "tax_paid" , "data_type" : "double" , "nullable" : True },
227+ "order_total" : {
228+ "name" : "order_total" ,
229+ "data_type" : "double" ,
230+ "nullable" : True ,
231+ },
232+ },
233+ "orders__items" : {
234+ "id" : {"name" : "id" , "data_type" : "text" , "nullable" : True },
235+ "order_id" : {"name" : "order_id" , "data_type" : "text" , "nullable" : True },
236+ "sku" : {"name" : "sku" , "data_type" : "text" , "nullable" : True },
237+ },
238+ }
239+
240+ for expected_table_name , expected_columns in expected_table_columns .items ():
241+ table = pipeline .default_schema .tables [expected_table_name ]
242+ table_columns = table ["columns" ]
243+ for expected_col_name , expected_col in expected_columns .items ():
244+ assert table_columns [expected_col_name ] == expected_col
0 commit comments