Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: bytes field type #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions spanner_orm/admin/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

_string_pattern = re.compile(r"^STRING\([0-9]+\)+$")
_string_array_pattern = re.compile(r"^ARRAY<STRING\([0-9]+\)>+$")
_bytes_pattern = re.compile(r"^BYTES\([0-9]+\)+$")


class ColumnSchema(schema.InformationSchema):
Expand Down Expand Up @@ -51,6 +52,8 @@ def field_type(self) -> Type[field.FieldType]:
return field.String
elif bool(_string_array_pattern.match(self.spanner_type)):
return field.StringArray
elif bool(_bytes_pattern.match(self.spanner_type)):
return field.Bytes

raise error.SpannerError(
"No corresponding Type for {}".format(self.spanner_type)
Expand Down
18 changes: 18 additions & 0 deletions spanner_orm/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,23 @@ def validate_type(value) -> None:
raise error.ValidationError("{} is not of type str".format(value))


class Bytes(FieldType):
"""Represents a bytes type."""

@staticmethod
def ddl(size="MAX") -> str:
return "BYTES({size})".format(size=size)

@staticmethod
def grpc_type() -> type_pb2.Type:
return type_pb2.Type(code=type_pb2.BYTES)

@staticmethod
def validate_type(value) -> None:
if not isinstance(value, bytes):
raise error.ValidationError("{} is not of type bytes".format(value))


class Date(FieldType):
"""Represents a date type."""

Expand Down Expand Up @@ -333,6 +350,7 @@ def validate_type(value: Any) -> None:
Integer,
Float,
String,
Bytes,
Date,
Timestamp,
StringArray,
Expand Down
2 changes: 2 additions & 0 deletions spanner_orm/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class UnittestModel(model.Model):
field.Timestamp, nullable=True, allow_commit_timestamp=True
)
date = field.Field(field.Date, nullable=True)
bytes_ = field.Field(field.Bytes, nullable=True)
bytes_2 = field.Field(field.Bytes, nullable=True, size=2048)
bool_array = field.Field(field.BoolArray, nullable=True)
int_array = field.Field(field.IntegerArray, nullable=True)
float_array = field.Field(field.FloatArray, nullable=True)
Expand Down
4 changes: 4 additions & 0 deletions spanner_orm/tests/update_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ def test_create_table(self, get_model):
" timestamp TIMESTAMP NOT NULL,"
" timestamp_2 TIMESTAMP OPTIONS (allow_commit_timestamp=true),"
" date DATE,"
" bytes_ BYTES(MAX),"
" bytes_2 BYTES(2048),"
" bool_array ARRAY<BOOL>,"
" int_array ARRAY<INT64>,"
" float_array ARRAY<FLOAT64>,"
" date_array ARRAY<DATE>,"
" string_array ARRAY<STRING(MAX)>,"
" string_array_2 ARRAY<STRING(50)>) PRIMARY KEY (int_, float_, string)"
)

self.assertEqual(test_update.ddl(), test_model_ddl)

@mock.patch("spanner_orm.admin.metadata.SpannerMetadata.model")
Expand Down Expand Up @@ -180,6 +183,7 @@ def test_create_table_no_model(self, get_model):
" string_array ARRAY<STRING(MAX)>,"
" string_array_2 ARRAY<STRING(50)>) PRIMARY KEY (int_, float_, string)"
)

self.assertEqual(test_update.ddl(), test_model_ddl)

@mock.patch("spanner_orm.admin.metadata.SpannerMetadata.model")
Expand Down