2022-10-24 17:29:03 -07:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import json
from unittest . mock import MagicMock , patch , mock_open
from botocore . exceptions import ClientError
import pytest
import query
def test_query_table ( scenario_data , monkeypatch , capsys , input_mocker ) :
2023-10-18 10:35:05 -07:00
execs = [ " INSERT INTO " , " SELECT title " , " SELECT * " ]
2022-10-24 17:29:03 -07:00
movie = {
2023-10-18 10:35:05 -07:00
" title " : " test-title " ,
" year " : 1984 ,
" info " : { " release_date " : " 1984-10-31T00:00:00Z " , " plot " : " test-plot " } ,
}
2022-10-24 17:29:03 -07:00
def verify_execute ( stmt , parameters ) :
stmt_start = execs . pop ( 0 )
assert stmt . query_string . startswith ( stmt_start )
mm_movie = MagicMock (
2023-10-18 10:35:05 -07:00
title = movie [ " title " ] ,
year = movie [ " year " ] ,
release_date = movie [ " info " ] [ " release_date " ] ,
plot = movie [ " info " ] [ " plot " ] ,
)
2022-10-24 17:29:03 -07:00
return MagicMock ( all = lambda : [ mm_movie ] , one = lambda : mm_movie )
input_mocker . mock_answers ( [ 1 ] )
2023-10-18 10:35:05 -07:00
scenario_data . scenario . ks_wrapper . table_name = " test-table "
test_movie_file = " test/resources/test_movies.json "
monkeypatch . setattr ( query , " SSLContext " , lambda x : MagicMock ( ) )
monkeypatch . setattr ( query , " SigV4AuthProvider " , lambda x : MagicMock ( ) )
monkeypatch . setattr ( query , " ExecutionProfile " , lambda * * kw : MagicMock ( ) )
2022-10-24 17:29:03 -07:00
session = MagicMock ( execute = verify_execute )
monkeypatch . setattr (
2023-10-18 10:35:05 -07:00
query , " Cluster " , lambda x , * * kw : MagicMock ( connect = lambda x : session )
)
2022-10-24 17:29:03 -07:00
2023-10-18 10:35:05 -07:00
with query . QueryManager ( " test-cert-path " , MagicMock ( ) , " test-ks " ) as qm :
with patch (
" builtins.open " , mock_open ( read_data = json . dumps ( [ movie ] ) )
) as mock_file :
2023-09-27 10:28:34 -07:00
scenario_data . scenario . query_table ( qm , test_movie_file )
2023-10-18 10:35:05 -07:00
mock_file . assert_called_with ( test_movie_file , " r " )
2022-10-24 17:29:03 -07:00
capt = capsys . readouterr ( )
2023-10-18 10:35:05 -07:00
assert movie [ " title " ] in capt . out
assert (
f " Released: { movie [ ' info ' ] [ ' release_date ' ] . partition ( ' T ' ) [ 0 ] } "
in capt . out
)
2022-10-24 17:29:03 -07:00
assert f " Plot: { movie [ ' info ' ] [ ' plot ' ] } " in capt . out