def test_run_task(self): self.test_create_queue() # Create a couple of queues path = self._client.queue_path('[PROJECT]', '[LOCATION]', "test_queue2") self._client.pause_queue(path) # Don't run any tasks while testing payload = "Hello World!" task = { 'app_engine_http_request': { # Specify the type of request. 'http_method': 'POST', 'relative_uri': '/example_task_handler', 'body': payload.encode() } } response = self._client.create_task(path, task) self.assertTrue(response.name.startswith(path)) class FakeResponse: status = 200 with sleuth.fake("server._make_task_request", return_value=FakeResponse()): self._client.run_task(response.name) # Should return NOT_FOUND self.assertRaises( Unknown, self._client.run_task, "%s/tasks/1119129292929292929" % path, # Not a valid task )
def test_create_table(self): with sleuth.fake("pyspannerdb.fetch.fetch", return_value=FakeOperationOK()) as fetch: with self.connection.cursor() as cursor: cursor.execute("CREATE TABLE bananas") self.assertEqual(fetch.calls[0].kwargs["method"], "PATCH") self.assertEqual(fetch.calls[0].args[0], self.target_url) statements = json.loads( fetch.calls[0].kwargs["payload"])["statements"] self.assertEqual(statements[0], "CREATE TABLE bananas")
def test_readonly_transaction(self): self.connection.autocommit(False) # Disable auto-commit with sleuth.fake("pyspannerdb.fetch.fetch", return_value=FakeOperationOK()) as fetch: with self.connection.cursor() as cursor: cursor.execute("START TRANSACTION READONLY") self.assertTrue(fetch.called) data = json.loads(fetch.calls[0].kwargs["payload"]) self.assertEqual("SELECT 1", data["sql"]) self.assertEqual("readOnly", data["transaction"]["begin"].keys()[0])
def test_multi_delete(self): sql = "DELETE FROM test WHERE field1 IN (%s, %s)" with sleuth.fake("pyspannerdb.fetch.fetch", return_value=FakeInsertOK()) as fetch: with self.connection.cursor() as cursor: cursor.execute(sql, [1, 2]) self.assertTrue(fetch.called) data = json.loads(fetch.calls[1].kwargs["payload"]) self.assertEqual(1, len(data["mutations"])) m0 = data["mutations"][0] self.assertTrue("delete" in m0) delete = m0["delete"] self.assertEqual("test", delete["table"]) self.assertEqual([str(1), str(2)], delete["keySet"])
def test_basic_update(self): sql = "UPDATE test SET field1 = %s, field2 = %s" with sleuth.fake("pyspannerdb.fetch.fetch", return_value=FakeInsertOK()) as fetch: with self.connection.cursor() as cursor: cursor.execute(sql, [1, 2]) self.assertTrue(fetch.called) data = json.loads(fetch.calls[1].kwargs["payload"]) self.assertEqual(1, len(data["mutations"])) m0 = data["mutations"][0] self.assertTrue("update" in m0) update = m0["update"] self.assertEqual("test", update["table"]) self.assertEqual([[str(1), str(2)]], update["values"]) self.assertEqual(["field1", "field2"], update["columns"])
def test_ddl_transaction_commit(self): self.connection.autocommit(False) # Disable auto-commit with sleuth.fake("pyspannerdb.fetch.fetch", return_value=FakeOperationOK()) as fetch: with self.connection.cursor() as cursor: cursor.execute("CREATE TABLE bananas") cursor.execute("DROP TABLE bananas") # Nothing should've been submitted so far self.assertFalse(fetch.call_count) self.connection.commit() # Should commit DDL updates before any mutations (the actual commit call) # so should be fetch.calls[0] self.assertEqual(fetch.calls[0].args[0], self.target_url) statements = json.loads( fetch.calls[0].kwargs["payload"])["statements"] # Two statements should've been submitted self.assertEqual(statements[0], "CREATE TABLE bananas") self.assertEqual(statements[1], "DROP TABLE bananas")
def test_insert_returns_id(self): self.connection._pk_lookup["test"] = "id" with sleuth.fake("pyspannerdb.fetch.fetch", return_value=FakeInsertOK()) as fetch: with self.connection.cursor() as cursor: cursor.execute("INSERT INTO test (field) VALUES (%s)", [1]) self.assertTrue(fetch.called) data = json.loads(fetch.calls[1].kwargs["payload"]) self.assertEqual(1, len(data["mutations"])) m0 = data["mutations"][0] self.assertTrue("insert" in m0) insert = m0["insert"] self.assertEqual("test", insert["table"]) self.assertEqual( [[str(cursor.lastrowid), str(1)]], insert["values"]) self.assertEqual(["id", "field"], insert["columns"]) self.assertIsNotNone(cursor.lastrowid)