Skip to content

Commit

Permalink
support empty transactionId (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Aug 10, 2020
1 parent f53a677 commit 3651f0c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
13 changes: 13 additions & 0 deletions local_data_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pydantic import BaseModel
from pydantic import Field as Field_
from pydantic import validator

TYPE_HINT_TO_CONVERTER: Dict[str, Callable[[Any], Any]] = {
'DECIMAL': Decimal,
Expand Down Expand Up @@ -61,6 +62,12 @@ class ExecuteStatementRequests(BaseModel):
schema_: Optional[str] = Field_(None, alias='schema')
transactionId: Optional[str]

@validator('transactionId', pre=True)
def validate_transaction_id(cls, v: Any) -> Any:
if not v:
return None
return v


class ColumnMetadata(BaseModel):
arrayBaseColumnType: Optional[int]
Expand Down Expand Up @@ -133,6 +140,12 @@ class BatchExecuteStatementRequests(BaseModel):
schema_: Optional[str] = Field_(None, alias='schema')
transactionId: Optional[str]

@validator('transactionId', pre=True)
def validate_transaction_id(cls, v: Any) -> Any:
if not v:
return None
return v


class UpdateResult(BaseModel):
generatedFields: List[Field]
Expand Down
49 changes: 49 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ def test_execute_statement_with_transaction(
}


def test_execute_statement_with_empty_transaction_id(
mocked_mysql, mocked_connection, mocked_connection_pool, mocked_cursor
):
mocked_cursor.description = 1, 1, 1, 1, 1, 1, 1
mocked_cursor.fetchall.side_effect = [((1, 'abc'),)]
mocked_connection_pool['2'] = mocked_connection

response = client.post(
"/Execute",
json={
'resourceArn': 'abc',
'secretArn': '1',
'sql': 'select * from users',
'transactionId': '',
},
)
assert response.status_code == 200
response_json = response.json()
assert response_json == {
'numberOfRecordsUpdated': 0,
'records': [[{'longValue': 1}, {'stringValue': 'abc'}]],
}


def test_batch_execute_statement(mocked_mysql, mocked_cursor):
mocked_cursor.description = ''
mocked_cursor.rowcount = 1
Expand Down Expand Up @@ -233,3 +257,28 @@ def test_batch_execute_statement_with_transaction(
assert response.status_code == 200
response_json = response.json()
assert response_json == {'updateResults': [{'generatedFields': [{'longValue': 1}]}]}


def test_batch_execute_statement_with_empty_transaction_id(
mocked_mysql, mocked_connection, mocked_connection_pool, mocked_cursor
):
mocked_cursor.description = ''
mocked_cursor.rowcount = 1
mocked_cursor.lastrowid = 1

mocked_connection_pool['2'] = mocked_connection

response = client.post(
"/BatchExecute",
json={
'resourceArn': 'abc',
'secretArn': '1',
'sql': "insert into users (name) values (:name)",
'transactionId': '',
'parameterSets': [[{'name': 'name', 'value': {'stringValue': 'abc'}}]],
},
)

assert response.status_code == 200
response_json = response.json()
assert response_json == {'updateResults': [{'generatedFields': [{'longValue': 1}]}]}

0 comments on commit 3651f0c

Please sign in to comment.