Skip to content

Commit

Permalink
fix: map now() to current_timestamp
Browse files Browse the repository at this point in the history
Fixes #497
  • Loading branch information
olavloite committed Dec 5, 2024
1 parent 3944cea commit 5f84b93
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
3 changes: 3 additions & 0 deletions google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ def get_from_hint_text(self, _, text):
"""
return text

def visit_now_func(self, func, **kwargs):
return "current_timestamp"

def visit_empty_set_expr(self, type_, **kw):
"""Return an empty set expression of the given type.
Expand Down
17 changes: 11 additions & 6 deletions test/mockserver_tests/mock_server_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Client,
ResultSet,
PingingPool,
TypeCode,
)
from google.cloud.spanner_v1.database import Database
from google.cloud.spanner_v1.instance import Instance
Expand All @@ -36,6 +37,12 @@ def add_result(sql: str, result: ResultSet):


def add_select1_result():
add_single_result("select 1", "c", TypeCode.INT64, [("1",)])


def add_single_result(
sql: str, column_name: str, type_code: spanner_type.TypeCode, row
):
result = result_set.ResultSet(
dict(
metadata=result_set.ResultSetMetadata(
Expand All @@ -45,10 +52,8 @@ def add_select1_result():
fields=[
spanner_type.StructType.Field(
dict(
name="c",
type=spanner_type.Type(
dict(code=spanner_type.TypeCode.INT64)
),
name=column_name,
type=spanner_type.Type(dict(code=type_code)),
)
)
]
Expand All @@ -58,8 +63,8 @@ def add_select1_result():
),
)
)
result.rows.extend(["1"])
MockServerTestBase.spanner_service.mock_spanner.add_result("select 1", result)
result.rows.extend(row)
MockServerTestBase.spanner_service.mock_spanner.add_result(sql, result)


class MockServerTestBase(fixtures.TestBase):
Expand Down
33 changes: 32 additions & 1 deletion test/mockserver_tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,32 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
from google.cloud.spanner_admin_database_v1 import UpdateDatabaseDdlRequest
from sqlalchemy import create_engine, select, MetaData, Table, Column, Integer, String
from sqlalchemy import (
create_engine,
select,
MetaData,
Table,
Column,
Integer,
String,
func,
)
from sqlalchemy.testing import eq_, is_instance_of
from google.cloud.spanner_v1 import (
FixedSizePool,
BatchCreateSessionsRequest,
ExecuteSqlRequest,
ResultSet,
PingingPool,
TypeCode,
)
from test.mockserver_tests.mock_server_test_base import (
MockServerTestBase,
add_select1_result,
add_result,
add_single_result,
)


Expand Down Expand Up @@ -59,6 +71,25 @@ def test_sqlalchemy_select1(self):
results = connection.execute(select(1)).fetchall()
self.verify_select1(results)

def test_sqlalchemy_select_now(self):
now = datetime.datetime.now(datetime.UTC)
iso_now = now.isoformat().replace("+00:00", "Z")
add_single_result(
"SELECT current_timestamp AS now_1",
"now_1",
TypeCode.TIMESTAMP,
[(iso_now,)],
)
engine = create_engine(
"spanner:///projects/p/instances/i/databases/d",
connect_args={"client": self.client, "pool": PingingPool(size=10)},
)
with engine.connect().execution_options(
isolation_level="AUTOCOMMIT"
) as connection:
spanner_now = connection.execute(select(func.now())).fetchone()[0]
eq_(spanner_now.timestamp(), now.timestamp())

def test_create_table(self):
add_result(
"""SELECT true
Expand Down

0 comments on commit 5f84b93

Please sign in to comment.