| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,5 @@ | |||
| 1 | + [](https://github.com/google/python-spanner-orm/actions/workflows/test.yaml) | ||
| 2 | + | ||
| 1 | 3 | # Google Cloud Spanner ORM | |
| 2 | 4 | ||
| 3 | 5 | This is a lightweight ORM written in Python and built on top of Cloud Spanner. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,9 +55,8 @@ def execute(self) -> None: | |||
| 55 | 55 | self.validate() | |
| 56 | 56 | api.spanner_admin_api().update_schema(self.ddl()) | |
| 57 | 57 | ||
| 58 | - @abc.abstractmethod | ||
| 59 | 58 | def validate(self) -> None: | |
| 60 | - raise NotImplementedError | ||
| 59 | + pass # TODO(dseomn): Remove this method. | ||
| 61 | 60 | ||
| 62 | 61 | ||
| 63 | 62 | class CreateTable(SchemaUpdate): | |
@@ -137,25 +136,6 @@ def __init__(self, table_name: str): | |||
| 137 | 136 | def ddl(self) -> str: | |
| 138 | 137 | return 'DROP TABLE {}'.format(self._table) | |
| 139 | 138 | ||
| 140 | - def validate(self) -> None: | ||
| 141 | - existing_model = metadata.SpannerMetadata.model(self._table) | ||
| 142 | - if not existing_model: | ||
| 143 | - raise error.SpannerError('Table {} does not exist'.format(self._table)) | ||
| 144 | - | ||
| 145 | - # Model indexes include the primary index | ||
| 146 | - if len(existing_model.indexes) > 1: | ||
| 147 | - raise error.SpannerError('Table {} has a secondary index'.format( | ||
| 148 | - self._table)) | ||
| 149 | - | ||
| 150 | - self._validate_not_interleaved(existing_model) | ||
| 151 | - | ||
| 152 | - def _validate_not_interleaved(self, | ||
| 153 | - existing_model: Type[model.Model]) -> None: | ||
| 154 | - for model_ in metadata.SpannerMetadata.models().values(): | ||
| 155 | - if model_.interleaved == existing_model: | ||
| 156 | - raise error.SpannerError('Table {} has interleaved table {}'.format( | ||
| 157 | - self._table, model_.table)) | ||
| 158 | - | ||
| 159 | 139 | ||
| 160 | 140 | class AddColumn(SchemaUpdate): | |
| 161 | 141 | """Update for adding a column to an existing table. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,16 +15,23 @@ | |||
| 15 | 15 | import datetime | |
| 16 | 16 | import logging | |
| 17 | 17 | import os | |
| 18 | + import textwrap | ||
| 19 | + from typing import Iterable, Type | ||
| 18 | 20 | import unittest | |
| 19 | 21 | ||
| 22 | + from absl.testing import absltest | ||
| 23 | + from absl.testing import parameterized | ||
| 20 | 24 | import spanner_orm | |
| 25 | + from spanner_orm.admin import metadata | ||
| 21 | 26 | from spanner_orm.tests import models | |
| 22 | 27 | from spanner_orm.testlib.spanner_emulator import testlib as spanner_emulator_testlib | |
| 23 | 28 | ||
| 24 | 29 | from google.api_core import exceptions as google_api_exceptions | |
| 25 | 30 | ||
| 26 | 31 | ||
| 27 | 32 | class MigrationsEmulatorTest(spanner_emulator_testlib.TestCase): | |
| 33 | + """Basic tests using generic migrations.""" | ||
| 34 | + | ||
| 28 | 35 | TEST_MIGRATIONS_DIR = os.path.join( | |
| 29 | 36 | os.path.dirname(os.path.abspath(__file__)), | |
| 30 | 37 | 'migrations_for_emulator_test', | |
@@ -73,6 +80,162 @@ def test_key(self): | |||
| 73 | 80 | }).save() | |
| 74 | 81 | ||
| 75 | 82 | ||
| 83 | + class SpecificMigrationsEmulatorTest( | ||
| 84 | + parameterized.TestCase, | ||
| 85 | + spanner_emulator_testlib.TestCase, | ||
| 86 | + ): | ||
| 87 | + """Tests of specific migrations.""" | ||
| 88 | + | ||
| 89 | + def setUp(self): | ||
| 90 | + super().setUp() | ||
| 91 | + self._migrations_dir = self.create_tempdir() | ||
| 92 | + self._migration_index = None | ||
| 93 | + | ||
| 94 | + def _append_migrations(self, *migrations: str) -> None: | ||
| 95 | + """Appends migrations to the sequence of migrations in self._migrations_dir. | ||
| 96 | + | ||
| 97 | + Args: | ||
| 98 | + *migrations: Each string is the python code to define a single upgrade() | ||
| 99 | + function. Leading indentation is stripped and migration boilerplate is | ||
| 100 | + added. | ||
| 101 | + """ | ||
| 102 | + for migration in migrations: | ||
| 103 | + if self._migration_index is None: | ||
| 104 | + prev_migration_id = None | ||
| 105 | + self._migration_index = 0 | ||
| 106 | + else: | ||
| 107 | + prev_migration_id = str(self._migration_index) | ||
| 108 | + self._migration_index += 1 | ||
| 109 | + migration_id = str(self._migration_index) | ||
| 110 | + self._migrations_dir.create_file( | ||
| 111 | + f'migration_{migration_id}.py', | ||
| 112 | + '\n'.join(( | ||
| 113 | + 'import spanner_orm', | ||
| 114 | + f'migration_id = {migration_id!r}', | ||
| 115 | + f'prev_migration_id = {prev_migration_id!r}', | ||
| 116 | + textwrap.dedent(migration), | ||
| 117 | + 'def downgrade(): raise NotImplementedError()', | ||
| 118 | + )), | ||
| 119 | + ) | ||
| 120 | + | ||
| 121 | + def test_drop_interleaved_table(self): | ||
| 122 | + self._append_migrations( | ||
| 123 | + """ | ||
| 124 | + class _Parent(spanner_orm.Model): | ||
| 125 | + __table__ = 'Parent' | ||
| 126 | + parent_key = spanner_orm.Field( | ||
| 127 | + spanner_orm.String, primary_key=True) | ||
| 128 | + | ||
| 129 | + def upgrade(): | ||
| 130 | + return spanner_orm.CreateTable(_Parent) | ||
| 131 | + """, | ||
| 132 | + """ | ||
| 133 | + class _Parent(spanner_orm.Model): | ||
| 134 | + __table__ = 'Parent' | ||
| 135 | + parent_key = spanner_orm.Field( | ||
| 136 | + spanner_orm.String, primary_key=True) | ||
| 137 | + | ||
| 138 | + class _Child(spanner_orm.Model): | ||
| 139 | + __table__ = 'Child' | ||
| 140 | + __interleaved__ = _Parent | ||
| 141 | + parent_key = spanner_orm.Field( | ||
| 142 | + spanner_orm.String, primary_key=True) | ||
| 143 | + child_key = spanner_orm.Field( | ||
| 144 | + spanner_orm.String, primary_key=True) | ||
| 145 | + | ||
| 146 | + def upgrade(): | ||
| 147 | + return spanner_orm.CreateTable(_Child) | ||
| 148 | + """, | ||
| 149 | + """ | ||
| 150 | + def upgrade(): | ||
| 151 | + return spanner_orm.DropTable('Child') | ||
| 152 | + """, | ||
| 153 | + ) | ||
| 154 | + self.run_orm_migrations(self._migrations_dir) | ||
| 155 | + self.assertCountEqual( | ||
| 156 | + ('Parent',), | ||
| 157 | + metadata.SpannerMetadata.tables().keys() - {'spanner_orm_migrations'}, | ||
| 158 | + ) | ||
| 159 | + | ||
| 160 | + @parameterized.named_parameters( | ||
| 161 | + dict( | ||
| 162 | + testcase_name='does_not_exist', | ||
| 163 | + create_migrations=(), | ||
| 164 | + error_class=google_api_exceptions.NotFound, | ||
| 165 | + ), | ||
| 166 | + dict( | ||
| 167 | + testcase_name='has_secondary_index', | ||
| 168 | + create_migrations=( | ||
| 169 | + """ | ||
| 170 | + class _TableToDrop(spanner_orm.Model): | ||
| 171 | + __table__ = 'TableToDrop' | ||
| 172 | + key = spanner_orm.Field( | ||
| 173 | + spanner_orm.String, primary_key=True) | ||
| 174 | + value = spanner_orm.Field(spanner_orm.String) | ||
| 175 | + | ||
| 176 | + def upgrade(): | ||
| 177 | + return spanner_orm.CreateTable(_TableToDrop) | ||
| 178 | + """, | ||
| 179 | + """ | ||
| 180 | + def upgrade(): | ||
| 181 | + return spanner_orm.CreateIndex( | ||
| 182 | + table_name='TableToDrop', | ||
| 183 | + index_name='value_index', | ||
| 184 | + columns=['value'], | ||
| 185 | + ) | ||
| 186 | + """, | ||
| 187 | + ), | ||
| 188 | + error_class=google_api_exceptions.FailedPrecondition, | ||
| 189 | + ), | ||
| 190 | + dict( | ||
| 191 | + testcase_name='has_interleaved_child', | ||
| 192 | + create_migrations=( | ||
| 193 | + """ | ||
| 194 | + class _TableToDrop(spanner_orm.Model): | ||
| 195 | + __table__ = 'TableToDrop' | ||
| 196 | + parent_key = spanner_orm.Field( | ||
| 197 | + spanner_orm.String, primary_key=True) | ||
| 198 | + | ||
| 199 | + def upgrade(): | ||
| 200 | + return spanner_orm.CreateTable(_TableToDrop) | ||
| 201 | + """, | ||
| 202 | + """ | ||
| 203 | + class _TableToDrop(spanner_orm.Model): | ||
| 204 | + __table__ = 'TableToDrop' | ||
| 205 | + parent_key = spanner_orm.Field( | ||
| 206 | + spanner_orm.String, primary_key=True) | ||
| 207 | + | ||
| 208 | + class _Child(spanner_orm.Model): | ||
| 209 | + __table__ = 'Child' | ||
| 210 | + __interleaved__ = _TableToDrop | ||
| 211 | + parent_key = spanner_orm.Field( | ||
| 212 | + spanner_orm.String, primary_key=True) | ||
| 213 | + child_key = spanner_orm.Field( | ||
| 214 | + spanner_orm.String, primary_key=True) | ||
| 215 | + | ||
| 216 | + def upgrade(): | ||
| 217 | + return spanner_orm.CreateTable(_Child) | ||
| 218 | + """, | ||
| 219 | + ), | ||
| 220 | + error_class=google_api_exceptions.FailedPrecondition, | ||
| 221 | + ), | ||
| 222 | + ) | ||
| 223 | + def test_drop_table_error( | ||
| 224 | + self, | ||
| 225 | + *, | ||
| 226 | + create_migrations: Iterable[str], | ||
| 227 | + error_class: Type[Exception], | ||
| 228 | + ): | ||
| 229 | + self._append_migrations(*create_migrations) | ||
| 230 | + self.run_orm_migrations(self._migrations_dir) | ||
| 231 | + self._append_migrations(""" | ||
| 232 | + def upgrade(): | ||
| 233 | + return spanner_orm.DropTable('TableToDrop') | ||
| 234 | + """) | ||
| 235 | + with self.assertRaises(error_class): | ||
| 236 | + self.run_orm_migrations(self._migrations_dir) | ||
| 237 | + | ||
| 238 | + | ||
| 76 | 239 | if __name__ == '__main__': | |
| 77 | 240 | logging.basicConfig() | |
| 78 | - unittest.main() | ||
| 241 | + absltest.main() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments