11import os
2+ import pandas as pd
23import pytest
34
5+ from pandas .testing import assert_frame_equal
46from pathlib import Path
57
68from pins import constructors as c
7- from pins .tests .conftest import PATH_TO_EXAMPLE_BOARD
9+ from pins .tests .conftest import (
10+ PATH_TO_EXAMPLE_BOARD ,
11+ PATH_TO_EXAMPLE_VERSION ,
12+ EXAMPLE_REL_PATH ,
13+ )
814from pins .tests .helpers import rm_env
915
1016
11- # adapted from https://stackoverflow.com/a/34333710
17+ @pytest .fixture
18+ def df_csv ():
19+ return pd .read_csv (PATH_TO_EXAMPLE_VERSION / "df_csv.csv" , index_col = 0 )
1220
1321
1422def check_dir_writable (p_dir ):
1523 assert p_dir .parent .exists ()
1624 assert os .access (p_dir .parent .absolute (), os .W_OK )
1725
1826
27+ def check_cache_file_path (p_file , p_cache ):
28+ assert str (p_file .relative_to (p_cache )).count ("/" ) == 2
29+
30+
1931# End-to-end constructor tests
2032
2133# there are two facets of boards: reading and writing.
2234# copied from test_compat
23- def test_constructor_board_url (tmp_cache , http_example_board_path ):
35+ def test_constructor_board_url_data (tmp_cache , http_example_board_path , df_csv ):
36+ board = c .board_urls (
37+ http_example_board_path ,
38+ # could derive from example version path
39+ pin_paths = {"df_csv" : "df_csv/20220214T163720Z-9bfad/" },
40+ )
41+
42+ df = board .pin_read ("df_csv" )
43+
44+ # check data ----
45+ assert_frame_equal (df , df_csv )
46+
47+
48+ @pytest .mark .xfail
49+ def test_constructor_board_url_cache (tmp_cache , http_example_board_path , df_csv ):
50+ # TODO: downloading a pin does not put files in the same directory, since
51+ # in this case we are hashing on the full url.
52+
2453 board = c .board_urls (
25- http_example_board_path , pin_paths = {"df_csv" : "df_csv/20220214T163718Z-eceac/" }
54+ http_example_board_path ,
55+ # could derive from example version path
56+ pin_paths = {"df_csv" : "df_csv/20220214T163718Z-eceac/" },
2657 )
2758
2859 board .pin_read ("df_csv" )
2960
30- # check cache
31- # check data
61+ # check cache ----
62+ http_dirs = list (tmp_cache .glob ("http_*" ))
63+
64+ assert len (http_dirs ) == 1
3265
66+ parent = http_dirs [0 ]
67+ res = list (parent .rglob ("**/*.csv" ))
68+ assert len (res ) == 1
3369
34- def test_constructor_board_github (tmp_cache , http_example_board_path ):
35- board = c .board_github ("machow" , "pins-python" , PATH_TO_EXAMPLE_BOARD ) # noqa
70+ # has form: <pin>/<version>/<file>
71+ check_cache_file_path (res [0 ], parent )
72+
73+
74+ def test_constructor_board_github (tmp_cache , http_example_board_path , df_csv ):
75+ board = c .board_github ("machow" , "pins-python" , EXAMPLE_REL_PATH ) # noqa
76+
77+ df = board .pin_read ("df_csv" )
78+ assert_frame_equal (df , df_csv )
79+
80+ cache_options = list (tmp_cache .glob ("github_*" ))
81+ assert len (cache_options ) == 1
82+ cache_dir = cache_options [0 ]
83+
84+ res = list (cache_dir .rglob ("**/*.csv" ))
85+ assert len (res ) == 1
86+
87+ check_cache_file_path (res [0 ], cache_dir )
3688
3789
3890@pytest .fixture (scope = "session" )
@@ -44,7 +96,7 @@ def board(backend):
4496 backend .teardown_board (board )
4597
4698
47- def test_constructor_board (board ):
99+ def test_constructor_board (board , df_csv , tmp_cache ):
48100 prot = board .fs .protocol
49101
50102 fs_name = prot if isinstance (prot , str ) else prot [0 ]
@@ -58,9 +110,20 @@ def test_constructor_board(board):
58110 con_name = fs_name
59111
60112 board = getattr (c , f"board_{ con_name } " )(board .board )
113+ df = board .pin_read ("df_csv" )
61114
62- # check cache
63115 # check data
116+ assert_frame_equal (df , df_csv )
117+
118+ # check cache
119+ options = list (tmp_cache .glob ("s3_*" ))
120+ assert len (options ) == 1
121+
122+ cache_dir = options [0 ]
123+ res = list (cache_dir .rglob ("**/*.csv" ))
124+ assert len (res ) == 1
125+
126+ check_cache_file_path (res [0 ], cache_dir )
64127
65128
66129# Board particulars ===========================================================
0 commit comments