def test_make_commits_transaction_once(self): """ L{ZStormResourceManager.make} commits schema changes only once across all stores, after all patch and delete statements have been executed. """ database2 = { "name": "test2", "uri": "sqlite:///%s" % self.makeFile(), "schema": self.databases[0]["schema"] } self.databases.append(database2) other_store = Store(create_database(database2["uri"])) for store in [self.store, other_store]: store.execute("CREATE TABLE patch " "(version INTEGER NOT NULL PRIMARY KEY)") store.execute("CREATE TABLE test (foo TEXT)") store.execute("INSERT INTO test (foo) VALUES ('data')") store.commit() with CaptureTracer() as tracer: zstorm = self.resource.make([]) self.assertEqual(["COMMIT", "COMMIT"], tracer.queries[-2:]) store1 = zstorm.get("test") store2 = zstorm.get("test2") self.assertEqual([], list(store1.execute("SELECT foo FROM test"))) self.assertEqual([], list(store2.execute("SELECT foo FROM test")))
def test_capture_as_context_manager(self): """{CaptureTracer}s can be used as context managers.""" conn = StubConnection() with CaptureTracer() as tracer: self.assertEqual([tracer], get_tracers()) tracer.connection_raw_execute(conn, "cursor", "select", []) self.assertEqual([], get_tracers()) self.assertEqual(["select"], tracer.queries)
def test_capture_multiple(self): """L{CaptureTracer}s can be used as nested context managers.""" conn = StubConnection() def trace(statement): for tracer in get_tracers(): tracer.connection_raw_execute(conn, "cursor", statement, []) with CaptureTracer() as tracer1: trace("one") with CaptureTracer() as tracer2: trace("two") trace("three") self.assertEqual([], get_tracers()) self.assertEqual(["one", "two", "three"], tracer1.queries) self.assertEqual(["two"], tracer2.queries)
def test_capture_with_exception(self): """ L{CaptureTracer}s re-raise any error when used as context managers. """ errors = [] try: with CaptureTracer(): raise RuntimeError("boom") except RuntimeError, error: errors.append(error)
def test_capture_with_exception(self): """ L{CaptureTracer}s re-raise any error when used as context managers. """ errors = [] try: with CaptureTracer(): raise RuntimeError("boom") except RuntimeError as error: errors.append(error) [error] = errors self.assertEqual("boom", str(error)) self.assertEqual([], get_tracers())
def test_no_schema_clean(self): """ A particular database may have no schema associated. If it's committed during tests, it will just be skipped when cleaning up tables. """ self.databases[0]["schema"] = None zstorm = self.resource.make([]) store = zstorm.get("test") store.commit() with CaptureTracer() as tracer: self.resource.clean(zstorm) self.assertEqual([], tracer.queries)
def test_capture(self): """ Using the L{CaptureTracer} fixture starts capturing queries and stops removes the tracer upon cleanup. """ tracer = self.useFixture(CaptureTracer()) self.assertEqual([tracer], get_tracers()) conn = StubConnection() conn.param_mark = '%s' var = MockVariable(u"var") tracer.connection_raw_execute(conn, "cursor", "select %s", [var]) self.assertEqual(["select 'var'"], tracer.queries) def check(): self.assertEqual([], get_tracers()) self.addCleanup(check)
def test_use_schema_stamp(self): """ If a schema stamp directory is set, then it's used to decide whether to upgrade the schema or not. In case the patch directory hasn't been changed since the last known upgrade, no schema upgrade is run. """ self.resource.schema_stamp_dir = self.makeFile() self.resource.make([]) # Simulate a second test run that initializes the zstorm resource # from scratch, using the same schema stamp directory resource2 = ZStormResourceManager(self.databases) resource2.schema_stamp_dir = self.resource.schema_stamp_dir with CaptureTracer() as tracer: resource2.make([]) self.assertEqual([], tracer.queries)