11import csv
22import gzip
33import io
4- import sys
4+ import tempfile
5+ from pathlib import Path
6+ from unittest .mock import MagicMock
57
68import petl
79import pytest
10+ from petl .util .base import TableWrapper
811
912from parsons .targetsmart .targetsmart_api import TargetSmartAPI
1013
1114
1215@pytest .fixture
13- def intable ():
16+ def intable () -> TableWrapper :
1417 return petl .wrap (
1518 [
1619 [
@@ -29,7 +32,7 @@ def intable():
2932
3033
3134@pytest .fixture
32- def raw_outtable (intable ) :
35+ def raw_outtable (intable : TableWrapper ) -> tuple :
3336 return (
3437 intable .addrownumbers (field = "ts__input_row" )
3538 .addrownumbers (field = "ts__row" )
@@ -42,53 +45,114 @@ def raw_outtable(intable):
4245
4346
4447@pytest .fixture
45- def prep_intable (intable ) :
48+ def prep_intable (intable : TableWrapper ) -> TableWrapper :
4649 return intable .addrownumbers (field = "matchback_id" )
4750
4851
4952@pytest .fixture
50- def raw_outcsv (raw_outtable ) :
53+ def raw_outcsv (raw_outtable : tuple ) -> str :
5154 buf = io .StringIO ()
5255 writer = csv .writer (buf )
5356 writer .writerows (list (raw_outtable ))
5457 return buf .getvalue ()
5558
5659
5760@pytest .fixture
58- def raw_outgz (raw_outcsv ) :
61+ def raw_outgz (raw_outcsv : str ) -> bytes :
5962 buf = io .BytesIO ()
6063 with gzip .GzipFile (fileobj = buf , mode = "w" ) as gz :
6164 gz .write (raw_outcsv .encode ("utf8" ))
6265 return buf .getvalue ()
6366
6467
6568@pytest .fixture
66- def final_outtable (prep_intable , raw_outtable ) :
69+ def final_outtable (prep_intable : TableWrapper , raw_outtable : tuple ) -> TableWrapper :
6770 return petl .leftjoin (prep_intable , raw_outtable , key = "matchback_id" ).cutout ("matchback_id" )
6871
6972
70- @pytest .fixture
71- def submit_filename ():
72- return "parsons_test.csv"
73-
74-
75- @pytest .mark .skipif (sys .platform == "win32" , reason = "need to fix this test on windows" )
76- def test_smartmatch (
77- intable ,
78- submit_filename ,
79- raw_outgz ,
80- raw_outcsv ,
81- raw_outtable ,
82- final_outtable ,
83- requests_mock ,
84- ):
85- ts = TargetSmartAPI ("mockkey" )
73+ def smartmatch_requests_mock (requests_mock : MagicMock , raw_outgz : bytes ):
8674 resp1 = {"url" : "https://mock_smartmatch_upload_endpoint" , "error" : None }
87- poll_resp = {"url" : "https://mock_smartmatch_download_endpoint" , "error" : None }
8875 requests_mock .get ("https://api.targetsmart.com/service/smartmatch" , json = resp1 )
8976 requests_mock .put (resp1 ["url" ])
77+ poll_resp = {"url" : "https://mock_smartmatch_download_endpoint" , "error" : None }
9078 requests_mock .get ("https://api.targetsmart.com/service/smartmatch/poll" , json = poll_resp )
9179 requests_mock .get (poll_resp ["url" ], content = raw_outgz )
80+ return requests_mock
81+
82+
83+ def test_smartmatch_returned_petl (
84+ intable : TableWrapper ,
85+ raw_outgz : bytes ,
86+ final_outtable : TableWrapper ,
87+ requests_mock : MagicMock ,
88+ ):
89+ ts = TargetSmartAPI ("mockkey" )
90+ smartmatch_requests_mock (requests_mock , raw_outgz )
9291
9392 results = ts .smartmatch (intable ).to_petl ()
9493 assert list (final_outtable ) == list (results )
94+
95+
96+ def test_smartmatch_output_csv_exists (
97+ intable : TableWrapper ,
98+ raw_outgz : bytes ,
99+ requests_mock : MagicMock ,
100+ ):
101+ ts = TargetSmartAPI ("mockkey" )
102+ smartmatch_requests_mock (requests_mock , raw_outgz )
103+
104+ temp_dir = tempfile .mkdtemp ()
105+ ts .smartmatch (intable , tmp_location = temp_dir )
106+ assert sorted (Path (temp_dir ).glob ("smartmatch_output*.csv" )) != []
107+
108+
109+ def test_smartmatch_keep_smartmatch_input_csv (
110+ intable : TableWrapper ,
111+ raw_outgz : bytes ,
112+ requests_mock : MagicMock ,
113+ ):
114+ ts = TargetSmartAPI ("mockkey" )
115+ smartmatch_requests_mock (requests_mock , raw_outgz )
116+
117+ temp_dir = tempfile .mkdtemp ()
118+ ts .smartmatch (intable , tmp_location = temp_dir , keep_smartmatch_input_file = True )
119+ assert sorted (Path (temp_dir ).glob ("smartmatch_input*.csv" )) != []
120+
121+
122+ def test_smartmatch_keep_smartmatch_input_csv_false (
123+ intable : TableWrapper ,
124+ raw_outgz : bytes ,
125+ requests_mock : MagicMock ,
126+ ):
127+ ts = TargetSmartAPI ("mockkey" )
128+ smartmatch_requests_mock (requests_mock , raw_outgz )
129+
130+ temp_dir = tempfile .mkdtemp ()
131+ ts .smartmatch (intable , tmp_location = temp_dir , keep_smartmatch_input_file = False )
132+ assert sorted (Path (temp_dir ).glob ("smartmatch_input*.csv" )) == []
133+
134+
135+ def test_smartmatch_keep_smartmatch_output_gz (
136+ intable : TableWrapper ,
137+ raw_outgz : bytes ,
138+ requests_mock : MagicMock ,
139+ ):
140+ ts = TargetSmartAPI ("mockkey" )
141+ smartmatch_requests_mock (requests_mock , raw_outgz )
142+
143+ temp_dir = tempfile .mkdtemp ()
144+ ts .smartmatch (intable , tmp_location = temp_dir , keep_smartmatch_output_gz_file = True )
145+ assert sorted (Path (temp_dir ).glob ("smartmatch_output*.csv.gz" )) != []
146+
147+
148+ def test_smartmatch_keep_smartmatch_output_gz_false (
149+ intable : TableWrapper ,
150+ raw_outgz : bytes ,
151+ requests_mock : MagicMock ,
152+ ):
153+ ts = TargetSmartAPI ("mockkey" )
154+ smartmatch_requests_mock (requests_mock , raw_outgz )
155+
156+ temp_dir = tempfile .mkdtemp ()
157+ ts .smartmatch (intable , tmp_location = temp_dir , keep_smartmatch_output_gz_file = False )
158+ assert sorted (Path (temp_dir ).glob ("smartmatch_output*.csv.gz" )) == []
0 commit comments