|
| 1 | +from django.db import connection |
| 2 | +from django.db.migrations.state import ProjectState |
| 3 | +from migrations.test_base import OperationTestBase |
| 4 | + |
| 5 | +from django_mongodb.migrations import RunMQL |
| 6 | + |
| 7 | + |
| 8 | +def create_collection(schema_editor, database): # noqa: ARG001 |
| 9 | + database.create_collection("test_runmql") |
| 10 | + |
| 11 | + |
| 12 | +def drop_collection(schema_editor, database): # noqa: ARG001 |
| 13 | + database.drop_collection("test_runmql") |
| 14 | + |
| 15 | + |
| 16 | +class RunMQLTests(OperationTestBase): |
| 17 | + available_apps = ["migrations_"] |
| 18 | + |
| 19 | + def test_basic(self): |
| 20 | + project_state = ProjectState() |
| 21 | + operation = RunMQL(create_collection, reverse_code=drop_collection) |
| 22 | + self.assertEqual(operation.describe(), "Raw MQL operation") |
| 23 | + # Test the state alteration does nothing |
| 24 | + new_state = project_state.clone() |
| 25 | + operation.state_forwards("test_runmql", new_state) |
| 26 | + self.assertEqual(new_state, project_state) |
| 27 | + # Test the database alteration |
| 28 | + self.assertTableNotExists("test_runmql") |
| 29 | + with connection.schema_editor() as editor: |
| 30 | + operation.database_forwards("test_runmql", editor, project_state, new_state) |
| 31 | + self.assertTableExists("test_runmql") |
| 32 | + # Now test reversal |
| 33 | + self.assertTrue(operation.reversible) |
| 34 | + with connection.schema_editor() as editor: |
| 35 | + operation.database_backwards("test_runmql", editor, project_state, new_state) |
| 36 | + self.assertTableNotExists("test_runmql") |
| 37 | + # Test deconstruction |
| 38 | + definition = operation.deconstruct() |
| 39 | + self.assertEqual(definition[0], "RunMQL") |
| 40 | + self.assertEqual(definition[1], []) |
| 41 | + self.assertEqual(sorted(definition[2]), ["code", "reverse_code"]) |
| 42 | + # Also test reversal fails, with an operation identical to above but |
| 43 | + # without reverse_code set. |
| 44 | + no_reverse_operation = RunMQL(create_collection) |
| 45 | + self.assertFalse(no_reverse_operation.reversible) |
| 46 | + with connection.schema_editor() as editor: |
| 47 | + no_reverse_operation.database_forwards("test_runmql", editor, project_state, new_state) |
| 48 | + with self.assertRaises(NotImplementedError): |
| 49 | + no_reverse_operation.database_backwards( |
| 50 | + "test_runmql", editor, new_state, project_state |
| 51 | + ) |
| 52 | + self.assertTableExists("test_runmql") |
| 53 | + |
| 54 | + def test_run_msql_no_reverse(self): |
| 55 | + project_state = ProjectState() |
| 56 | + new_state = project_state.clone() |
| 57 | + operation = RunMQL(create_collection) |
| 58 | + self.assertTableNotExists("test_runmql") |
| 59 | + with connection.schema_editor() as editor: |
| 60 | + operation.database_forwards("test_runmql", editor, project_state, new_state) |
| 61 | + self.assertTableExists("test_runmql") |
| 62 | + # And deconstruction |
| 63 | + definition = operation.deconstruct() |
| 64 | + self.assertEqual(definition[0], "RunMQL") |
| 65 | + self.assertEqual(definition[1], []) |
| 66 | + self.assertEqual(sorted(definition[2]), ["code"]) |
| 67 | + |
| 68 | + def test_elidable(self): |
| 69 | + operation = RunMQL(create_collection) |
| 70 | + self.assertIs(operation.reduce(operation, []), False) |
| 71 | + elidable_operation = RunMQL(create_collection, elidable=True) |
| 72 | + self.assertEqual(elidable_operation.reduce(operation, []), [operation]) |
| 73 | + |
| 74 | + def test_run_mql_invalid_code(self): |
| 75 | + with self.assertRaisesMessage(ValueError, "RunMQL must be supplied with a callable"): |
| 76 | + RunMQL("print 'ahahaha'") |
0 commit comments