|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | + |
| 20 | +import Foundation |
| 21 | +import Testing |
| 22 | + |
| 23 | +@testable import SparkConnect |
| 24 | + |
| 25 | +/// A test utility |
| 26 | +struct SQLHelper { |
| 27 | + public static func withDatabase(_ spark: SparkSession, _ dbNames: String...) -> ( |
| 28 | + () async throws -> Void |
| 29 | + ) async throws -> Void { |
| 30 | + func body(_ f: () async throws -> Void) async throws { |
| 31 | + try await ErrorUtils.tryWithSafeFinally( |
| 32 | + f, |
| 33 | + { |
| 34 | + for name in dbNames { |
| 35 | + _ = try await spark.sql("DROP DATABASE IF EXISTS \(name) CASCADE").count() |
| 36 | + } |
| 37 | + }) |
| 38 | + } |
| 39 | + return body |
| 40 | + } |
| 41 | + |
| 42 | + public static func withTable(_ spark: SparkSession, _ tableNames: String...) -> ( |
| 43 | + () async throws -> Void |
| 44 | + ) async throws -> Void { |
| 45 | + func body(_ f: () async throws -> Void) async throws { |
| 46 | + try await ErrorUtils.tryWithSafeFinally( |
| 47 | + f, |
| 48 | + { |
| 49 | + for name in tableNames { |
| 50 | + _ = try await spark.sql("DROP TABLE IF EXISTS \(name)").count() |
| 51 | + } |
| 52 | + }) |
| 53 | + } |
| 54 | + return body |
| 55 | + } |
| 56 | +} |
0 commit comments