File: test_timestreamquery.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (200 lines) | stat: -rw-r--r-- 6,656 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""Unit tests for timestreamquery-supported APIs."""

import boto3
import pytest
import requests
from botocore.exceptions import ClientError

from moto import mock_aws, settings
from tests import DEFAULT_ACCOUNT_ID

# See our Development Tips on writing tests for hints on how to write good tests:
# http://docs.getmoto.org/en/latest/docs/contributing/development_tips/tests.html


@mock_aws
def test_create_scheduled_query():
    target_config = {
        "TimestreamConfiguration": {
            "DatabaseName": "mydb",
            "TableName": "mytab",
            "TimeColumn": "tc",
            "DimensionMappings": [],
        }
    }

    client = boto3.client("timestream-query", region_name="us-east-2")
    arn = client.create_scheduled_query(
        Name="myquery",
        QueryString="SELECT *",
        ScheduleConfiguration={"ScheduleExpression": "* * * * * 1"},
        NotificationConfiguration={"SnsConfiguration": {"TopicArn": "arn:some:topic"}},
        TargetConfiguration=target_config,
        ScheduledQueryExecutionRoleArn="some role",
        ErrorReportConfiguration=get_error_config(),
        KmsKeyId="arn:kms:key",
    )["Arn"]

    assert (
        arn
        == f"arn:aws:timestream:us-east-2:{DEFAULT_ACCOUNT_ID}:scheduled-query/myquery"
    )

    query = client.describe_scheduled_query(ScheduledQueryArn=arn)["ScheduledQuery"]
    assert query["Arn"] == arn
    assert query["Name"] == "myquery"
    assert query["QueryString"] == "SELECT *"
    assert query["CreationTime"]
    assert query["State"] == "ENABLED"
    assert query["ScheduleConfiguration"] == {"ScheduleExpression": "* * * * * 1"}
    assert query["NotificationConfiguration"] == {
        "SnsConfiguration": {"TopicArn": "arn:some:topic"}
    }
    assert query["TargetConfiguration"] == target_config
    assert query["ScheduledQueryExecutionRoleArn"] == "some role"
    assert query["ErrorReportConfiguration"] == get_error_config()
    assert query["KmsKeyId"] == "arn:kms:key"


@mock_aws
def test_delete_scheduled_query():
    client = boto3.client("timestream-query", region_name="us-east-2")

    arn = client.create_scheduled_query(
        Name="myquery",
        QueryString="SELECT *",
        ScheduleConfiguration={"ScheduleExpression": "* * * * * 1"},
        NotificationConfiguration={"SnsConfiguration": {"TopicArn": "arn:some:topic"}},
        ScheduledQueryExecutionRoleArn="some role",
        ErrorReportConfiguration=get_error_config(),
    )["Arn"]
    client.delete_scheduled_query(ScheduledQueryArn=arn)

    with pytest.raises(ClientError) as exc:
        client.describe_scheduled_query(ScheduledQueryArn=arn)
    err = exc.value.response["Error"]
    assert err["Code"] == "ResourceNotFoundException"
    assert err["Message"] == f"The resource with arn {arn} does not exist."


@mock_aws
def test_update_scheduled_query():
    client = boto3.client("timestream-query", region_name="eu-west-1")
    arn = client.create_scheduled_query(
        Name="myquery",
        QueryString="SELECT *",
        ScheduleConfiguration={"ScheduleExpression": "* * * * * 1"},
        NotificationConfiguration={"SnsConfiguration": {"TopicArn": "arn:some:topic"}},
        ScheduledQueryExecutionRoleArn="some role",
        ErrorReportConfiguration=get_error_config(),
    )["Arn"]

    client.update_scheduled_query(
        ScheduledQueryArn=arn,
        State="DISABLED",
    )

    query = client.describe_scheduled_query(ScheduledQueryArn=arn)["ScheduledQuery"]
    assert query["State"] == "DISABLED"


@mock_aws
def test_query_default_results():
    client = boto3.client("timestream-query", region_name="us-east-1")
    resp = client.query(QueryString="SELECT *")

    assert resp["QueryId"]
    assert resp["Rows"] == []
    assert resp["ColumnInfo"] == []


@mock_aws
def test_query__configured_results():
    base_url = (
        "localhost:5000" if settings.TEST_SERVER_MODE else "motoapi.amazonaws.com"
    )

    first_result = {
        "QueryId": "some_id",
        "Rows": [{"Data": [{"ScalarValue": "1"}]}],
        "ColumnInfo": [
            {"Name": "c", "Type": {"ScalarType": "VARCHAR"}},
        ],
        "QueryStatus": {
            "ProgressPercentage": 50,
            "CumulativeBytesScanned": 5,
            "CumulativeBytesMetered": 5,
        },
    }
    second_result = {
        "QueryId": "some_id",
        "Rows": [{"Data": [{"ScalarValue": "1"}]}, {"Data": [{"ScalarValue": "2"}]}],
        "ColumnInfo": [
            {"Name": "c", "Type": {"ScalarType": "VARCHAR"}},
            {"Name": "c", "Type": {"ScalarType": "VARCHAR"}},
        ],
        "QueryStatus": {
            "ProgressPercentage": 100,
            "CumulativeBytesScanned": 10,
            "CumulativeBytesMetered": 10,
        },
    }
    third_result = {
        "Rows": [{"Data": [{"ScalarValue": "5"}]}],
        "ColumnInfo": [{"Name": "c", "Type": {"ScalarType": "VARCHAR"}}],
        "QueryStatus": {
            "ProgressPercentage": 100,
            "CumulativeBytesScanned": 10,
            "CumulativeBytesMetered": 10,
        },
    }

    result = {
        "results": {
            "SELECT *": [first_result, second_result],
            None: [third_result],
        }
    }
    requests.post(
        f"http://{base_url}/moto-api/static/timestream/query-results",
        json=result,
    )

    client = boto3.client("timestream-query", region_name="us-east-1")

    # Unknown QUERY returns third result
    resp = client.query(QueryString="SELECT unknown")

    assert resp["Rows"] == third_result["Rows"]
    assert resp["ColumnInfo"] == third_result["ColumnInfo"]

    # Known QUERY returns first result
    resp = client.query(QueryString="SELECT *")

    assert resp["QueryId"] == "some_id"
    assert resp["Rows"] == first_result["Rows"]
    assert resp["ColumnInfo"] == first_result["ColumnInfo"]

    # Querying the same thing returns the second result
    resp = client.query(QueryString="SELECT *")

    assert resp["QueryId"] == "some_id"
    assert resp["Rows"] == second_result["Rows"]
    assert resp["ColumnInfo"] == second_result["ColumnInfo"]

    # Unknown QUERY returns third result again
    resp = client.query(QueryString="SELECT unknown")

    assert resp["Rows"] == third_result["Rows"]
    assert resp["ColumnInfo"] == third_result["ColumnInfo"]

    # Known QUERY returns nothing - we've already returned all possible results
    resp = client.query(QueryString="SELECT *")

    assert resp["QueryId"]
    assert resp["Rows"] == []
    assert resp["ColumnInfo"] == []


def get_error_config():
    return {"S3Configuration": {"BucketName": "mybucket", "ObjectKeyPrefix": "prefix"}}