| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2021-10-18 19:44:29 +00:00
										 |  |  | import sure  # noqa # pylint: disable=unused-import | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | from moto import mock_athena | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_athena | 
					
						
							|  |  |  | def test_create_work_group(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     client = boto3.client("athena", region_name="us-east-1") | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response = client.create_work_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Name="athena_workgroup", | 
					
						
							|  |  |  |         Description="Test work group", | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  |         Configuration={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "ResultConfiguration": { | 
					
						
							|  |  |  |                 "OutputLocation": "s3://bucket-name/prefix/", | 
					
						
							|  |  |  |                 "EncryptionConfiguration": { | 
					
						
							|  |  |  |                     "EncryptionOption": "SSE_KMS", | 
					
						
							|  |  |  |                     "KmsKey": "aws:arn:kms:1233456789:us-east-1:key/number-1", | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  |                 }, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         Tags=[], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         # The second time should throw an error | 
					
						
							|  |  |  |         response = client.create_work_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             Name="athena_workgroup", | 
					
						
							|  |  |  |             Description="duplicate", | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  |             Configuration={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 "ResultConfiguration": { | 
					
						
							|  |  |  |                     "OutputLocation": "s3://bucket-name/prefix/", | 
					
						
							|  |  |  |                     "EncryptionConfiguration": { | 
					
						
							|  |  |  |                         "EncryptionOption": "SSE_KMS", | 
					
						
							|  |  |  |                         "KmsKey": "aws:arn:kms:1233456789:us-east-1:key/number-1", | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  |                     }, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  |             }, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     except ClientError as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         err.response["Error"]["Code"].should.equal("InvalidRequestException") | 
					
						
							|  |  |  |         err.response["Error"]["Message"].should.equal("WorkGroup already exists") | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         raise RuntimeError("Should have raised ResourceNotFoundException") | 
					
						
							| 
									
										
										
										
											2019-10-22 14:37:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Then test the work group appears in the work group list | 
					
						
							|  |  |  |     response = client.list_work_groups() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     response["WorkGroups"].should.have.length_of(1) | 
					
						
							|  |  |  |     work_group = response["WorkGroups"][0] | 
					
						
							|  |  |  |     work_group["Name"].should.equal("athena_workgroup") | 
					
						
							|  |  |  |     work_group["Description"].should.equal("Test work group") | 
					
						
							|  |  |  |     work_group["State"].should.equal("ENABLED") | 
					
						
							| 
									
										
										
										
											2020-05-16 15:00:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_athena | 
					
						
							|  |  |  | def test_create_and_get_workgroup(): | 
					
						
							|  |  |  |     client = boto3.client("athena", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     create_basic_workgroup(client=client, name="athena_workgroup") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-16 15:03:26 +01:00
										 |  |  |     work_group = client.get_work_group(WorkGroup="athena_workgroup")["WorkGroup"] | 
					
						
							|  |  |  |     del work_group["CreationTime"]  # Were not testing creationtime atm | 
					
						
							|  |  |  |     work_group.should.equal( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "Name": "athena_workgroup", | 
					
						
							|  |  |  |             "State": "ENABLED", | 
					
						
							|  |  |  |             "Configuration": { | 
					
						
							|  |  |  |                 "ResultConfiguration": {"OutputLocation": "s3://bucket-name/prefix/"} | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             "Description": "Test work group", | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-05-16 15:00:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_athena | 
					
						
							|  |  |  | def test_start_query_execution(): | 
					
						
							|  |  |  |     client = boto3.client("athena", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     create_basic_workgroup(client=client, name="athena_workgroup") | 
					
						
							| 
									
										
										
										
											2020-05-16 15:03:26 +01:00
										 |  |  |     response = client.start_query_execution( | 
					
						
							|  |  |  |         QueryString="query1", | 
					
						
							|  |  |  |         QueryExecutionContext={"Database": "string"}, | 
					
						
							|  |  |  |         ResultConfiguration={"OutputLocation": "string"}, | 
					
						
							|  |  |  |         WorkGroup="athena_workgroup", | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert "QueryExecutionId" in response | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sec_response = client.start_query_execution( | 
					
						
							|  |  |  |         QueryString="query2", | 
					
						
							|  |  |  |         QueryExecutionContext={"Database": "string"}, | 
					
						
							|  |  |  |         ResultConfiguration={"OutputLocation": "string"}, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert "QueryExecutionId" in sec_response | 
					
						
							| 
									
										
										
										
											2020-05-16 15:00:06 +01:00
										 |  |  |     response["QueryExecutionId"].shouldnt.equal(sec_response["QueryExecutionId"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_athena | 
					
						
							|  |  |  | def test_start_query_validate_workgroup(): | 
					
						
							|  |  |  |     client = boto3.client("athena", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as err: | 
					
						
							| 
									
										
										
										
											2020-05-16 15:03:26 +01:00
										 |  |  |         client.start_query_execution( | 
					
						
							|  |  |  |             QueryString="query1", | 
					
						
							|  |  |  |             QueryExecutionContext={"Database": "string"}, | 
					
						
							|  |  |  |             ResultConfiguration={"OutputLocation": "string"}, | 
					
						
							|  |  |  |             WorkGroup="unknown_workgroup", | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     err.value.response["Error"]["Code"].should.equal("InvalidRequestException") | 
					
						
							|  |  |  |     err.value.response["Error"]["Message"].should.equal("WorkGroup does not exist") | 
					
						
							| 
									
										
										
										
											2020-05-16 15:00:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_athena | 
					
						
							|  |  |  | def test_get_query_execution(): | 
					
						
							|  |  |  |     client = boto3.client("athena", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     query = "SELECT stuff" | 
					
						
							|  |  |  |     location = "s3://bucket-name/prefix/" | 
					
						
							|  |  |  |     database = "database" | 
					
						
							|  |  |  |     # Start Query | 
					
						
							| 
									
										
										
										
											2020-05-16 15:03:26 +01:00
										 |  |  |     exex_id = client.start_query_execution( | 
					
						
							|  |  |  |         QueryString=query, | 
					
						
							|  |  |  |         QueryExecutionContext={"Database": database}, | 
					
						
							|  |  |  |         ResultConfiguration={"OutputLocation": location}, | 
					
						
							|  |  |  |     )["QueryExecutionId"] | 
					
						
							| 
									
										
										
										
											2020-05-16 15:00:06 +01:00
										 |  |  |     # | 
					
						
							|  |  |  |     details = client.get_query_execution(QueryExecutionId=exex_id)["QueryExecution"] | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     details["QueryExecutionId"].should.equal(exex_id) | 
					
						
							|  |  |  |     details["Query"].should.equal(query) | 
					
						
							|  |  |  |     details["StatementType"].should.equal("DDL") | 
					
						
							|  |  |  |     details["ResultConfiguration"]["OutputLocation"].should.equal(location) | 
					
						
							|  |  |  |     details["QueryExecutionContext"]["Database"].should.equal(database) | 
					
						
							|  |  |  |     details["Status"]["State"].should.equal("QUEUED") | 
					
						
							| 
									
										
										
										
											2020-05-16 15:03:26 +01:00
										 |  |  |     details["Statistics"].should.equal( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "EngineExecutionTimeInMillis": 0, | 
					
						
							|  |  |  |             "DataScannedInBytes": 0, | 
					
						
							|  |  |  |             "TotalExecutionTimeInMillis": 0, | 
					
						
							|  |  |  |             "QueryQueueTimeInMillis": 0, | 
					
						
							|  |  |  |             "QueryPlanningTimeInMillis": 0, | 
					
						
							|  |  |  |             "ServiceProcessingTimeInMillis": 0, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-05-16 15:00:06 +01:00
										 |  |  |     assert "WorkGroup" not in details | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_athena | 
					
						
							|  |  |  | def test_stop_query_execution(): | 
					
						
							|  |  |  |     client = boto3.client("athena", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     query = "SELECT stuff" | 
					
						
							|  |  |  |     location = "s3://bucket-name/prefix/" | 
					
						
							|  |  |  |     database = "database" | 
					
						
							|  |  |  |     # Start Query | 
					
						
							| 
									
										
										
										
											2020-05-16 15:03:26 +01:00
										 |  |  |     exex_id = client.start_query_execution( | 
					
						
							|  |  |  |         QueryString=query, | 
					
						
							|  |  |  |         QueryExecutionContext={"Database": database}, | 
					
						
							|  |  |  |         ResultConfiguration={"OutputLocation": location}, | 
					
						
							|  |  |  |     )["QueryExecutionId"] | 
					
						
							| 
									
										
										
										
											2020-05-16 15:00:06 +01:00
										 |  |  |     # Stop Query | 
					
						
							|  |  |  |     client.stop_query_execution(QueryExecutionId=exex_id) | 
					
						
							|  |  |  |     # Verify status | 
					
						
							|  |  |  |     details = client.get_query_execution(QueryExecutionId=exex_id)["QueryExecution"] | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     details["QueryExecutionId"].should.equal(exex_id) | 
					
						
							|  |  |  |     details["Status"]["State"].should.equal("CANCELLED") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-12 01:27:29 +09:00
										 |  |  | @mock_athena | 
					
						
							|  |  |  | def test_create_named_query(): | 
					
						
							|  |  |  |     client = boto3.client("athena", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # craete named query | 
					
						
							|  |  |  |     res = client.create_named_query( | 
					
						
							| 
									
										
										
										
											2020-11-11 15:55:37 +00:00
										 |  |  |         Name="query-name", Database="target_db", QueryString="SELECT * FROM table1", | 
					
						
							| 
									
										
										
										
											2020-06-12 01:27:29 +09:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert "NamedQueryId" in res | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_athena | 
					
						
							|  |  |  | def test_get_named_query(): | 
					
						
							|  |  |  |     client = boto3.client("athena", region_name="us-east-1") | 
					
						
							|  |  |  |     query_name = "query-name" | 
					
						
							|  |  |  |     database = "target_db" | 
					
						
							|  |  |  |     query_string = "SELECT * FROM tbl1" | 
					
						
							|  |  |  |     description = "description of this query" | 
					
						
							|  |  |  |     # craete named query | 
					
						
							|  |  |  |     res_create = client.create_named_query( | 
					
						
							|  |  |  |         Name=query_name, | 
					
						
							|  |  |  |         Database=database, | 
					
						
							|  |  |  |         QueryString=query_string, | 
					
						
							|  |  |  |         Description=description, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     query_id = res_create["NamedQueryId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # get named query | 
					
						
							|  |  |  |     res_get = client.get_named_query(NamedQueryId=query_id)["NamedQuery"] | 
					
						
							|  |  |  |     res_get["Name"].should.equal(query_name) | 
					
						
							|  |  |  |     res_get["Description"].should.equal(description) | 
					
						
							|  |  |  |     res_get["Database"].should.equal(database) | 
					
						
							|  |  |  |     res_get["QueryString"].should.equal(query_string) | 
					
						
							|  |  |  |     res_get["NamedQueryId"].should.equal(query_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-16 15:00:06 +01:00
										 |  |  | def create_basic_workgroup(client, name): | 
					
						
							|  |  |  |     client.create_work_group( | 
					
						
							|  |  |  |         Name=name, | 
					
						
							|  |  |  |         Description="Test work group", | 
					
						
							|  |  |  |         Configuration={ | 
					
						
							| 
									
										
										
										
											2020-11-11 15:55:37 +00:00
										 |  |  |             "ResultConfiguration": {"OutputLocation": "s3://bucket-name/prefix/",} | 
					
						
							| 
									
										
										
										
											2020-05-16 15:03:26 +01:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-05-16 15:00:06 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2022-02-14 20:11:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_athena | 
					
						
							|  |  |  | def test_create_data_catalog(): | 
					
						
							|  |  |  |     client = boto3.client("athena", region_name="us-east-1") | 
					
						
							|  |  |  |     response = client.create_data_catalog( | 
					
						
							|  |  |  |         Name="athena_datacatalog", | 
					
						
							|  |  |  |         Type="GLUE", | 
					
						
							|  |  |  |         Description="Test data catalog", | 
					
						
							|  |  |  |         Parameters={"catalog-id": "AWS Test account ID"}, | 
					
						
							|  |  |  |         Tags=[], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         # The second time should throw an error | 
					
						
							|  |  |  |         response = client.create_data_catalog( | 
					
						
							|  |  |  |             Name="athena_datacatalog", | 
					
						
							|  |  |  |             Type="GLUE", | 
					
						
							|  |  |  |             Description="Test data catalog", | 
					
						
							|  |  |  |             Parameters={"catalog-id": "AWS Test account ID"}, | 
					
						
							|  |  |  |             Tags=[], | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     except ClientError as err: | 
					
						
							|  |  |  |         err.response["Error"]["Code"].should.equal("InvalidRequestException") | 
					
						
							|  |  |  |         err.response["Error"]["Message"].should.equal("DataCatalog already exists") | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise RuntimeError("Should have raised ResourceNotFoundException") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Then test the work group appears in the work group list | 
					
						
							|  |  |  |     response = client.list_data_catalogs() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response["DataCatalogsSummary"].should.have.length_of(1) | 
					
						
							|  |  |  |     data_catalog = response["DataCatalogsSummary"][0] | 
					
						
							|  |  |  |     data_catalog["CatalogName"].should.equal("athena_datacatalog") | 
					
						
							|  |  |  |     data_catalog["Type"].should.equal("GLUE") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_athena | 
					
						
							|  |  |  | def test_create_and_get_data_catalog(): | 
					
						
							|  |  |  |     client = boto3.client("athena", region_name="us-east-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     client.create_data_catalog( | 
					
						
							|  |  |  |         Name="athena_datacatalog", | 
					
						
							|  |  |  |         Type="GLUE", | 
					
						
							|  |  |  |         Description="Test data catalog", | 
					
						
							|  |  |  |         Parameters={"catalog-id": "AWS Test account ID"}, | 
					
						
							|  |  |  |         Tags=[], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data_catalog = client.get_data_catalog(Name="athena_datacatalog") | 
					
						
							|  |  |  |     data_catalog["DataCatalog"].should.equal( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "Name": "athena_datacatalog", | 
					
						
							|  |  |  |             "Description": "Test data catalog", | 
					
						
							|  |  |  |             "Type": "GLUE", | 
					
						
							|  |  |  |             "Parameters": {"catalog-id": "AWS Test account ID"}, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) |