forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_connect_basic.py
More file actions
634 lines (544 loc) · 23.2 KB
/
test_connect_basic.py
File metadata and controls
634 lines (544 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import Any
import unittest
import shutil
import tempfile
import grpc # type: ignore
from pyspark.testing.sqlutils import have_pandas, SQLTestUtils
if have_pandas:
import pandas
from pyspark.sql import SparkSession, Row
from pyspark.sql.types import StructType, StructField, LongType, StringType
if have_pandas:
from pyspark.sql.connect.session import SparkSession as RemoteSparkSession
from pyspark.sql.connect.client import ChannelBuilder
from pyspark.sql.connect.function_builder import udf
from pyspark.sql.connect.functions import lit, col
from pyspark.sql.dataframe import DataFrame
from pyspark.sql.connect.dataframe import DataFrame as CDataFrame
from pyspark.testing.connectutils import should_test_connect, connect_requirement_message
from pyspark.testing.pandasutils import PandasOnSparkTestCase
from pyspark.testing.utils import ReusedPySparkTestCase
@unittest.skipIf(not should_test_connect, connect_requirement_message)
class SparkConnectSQLTestCase(PandasOnSparkTestCase, ReusedPySparkTestCase, SQLTestUtils):
"""Parent test fixture class for all Spark Connect related
test cases."""
if have_pandas:
connect: RemoteSparkSession
tbl_name: str
tbl_name_empty: str
df_text: "DataFrame"
@classmethod
def setUpClass(cls: Any):
ReusedPySparkTestCase.setUpClass()
cls.tempdir = tempfile.NamedTemporaryFile(delete=False)
cls.hive_available = True
# Create the new Spark Session
cls.spark = SparkSession(cls.sc)
cls.testData = [Row(key=i, value=str(i)) for i in range(100)]
cls.testDataStr = [Row(key=str(i)) for i in range(100)]
cls.df = cls.sc.parallelize(cls.testData).toDF()
cls.df_text = cls.sc.parallelize(cls.testDataStr).toDF()
cls.tbl_name = "test_connect_basic_table_1"
cls.tbl_name_empty = "test_connect_basic_table_empty"
# Cleanup test data
cls.spark_connect_clean_up_test_data()
# Load test data
cls.spark_connect_load_test_data()
@classmethod
def tearDownClass(cls: Any) -> None:
cls.spark_connect_clean_up_test_data()
ReusedPySparkTestCase.tearDownClass()
@classmethod
def spark_connect_load_test_data(cls: Any):
# Setup Remote Spark Session
cls.connect = RemoteSparkSession.builder.remote().getOrCreate()
df = cls.spark.createDataFrame([(x, f"{x}") for x in range(100)], ["id", "name"])
# Since we might create multiple Spark sessions, we need to create global temporary view
# that is specifically maintained in the "global_temp" schema.
df.write.saveAsTable(cls.tbl_name)
empty_table_schema = StructType(
[
StructField("firstname", StringType(), True),
StructField("middlename", StringType(), True),
StructField("lastname", StringType(), True),
]
)
emptyRDD = cls.spark.sparkContext.emptyRDD()
empty_df = cls.spark.createDataFrame(emptyRDD, empty_table_schema)
empty_df.write.saveAsTable(cls.tbl_name_empty)
@classmethod
def spark_connect_clean_up_test_data(cls: Any) -> None:
cls.spark.sql("DROP TABLE IF EXISTS {}".format(cls.tbl_name))
cls.spark.sql("DROP TABLE IF EXISTS {}".format(cls.tbl_name_empty))
class SparkConnectTests(SparkConnectSQLTestCase):
def test_simple_read(self):
df = self.connect.read.table(self.tbl_name)
data = df.limit(10).toPandas()
# Check that the limit is applied
self.assertEqual(len(data.index), 10)
def test_columns(self):
# SPARK-41036: test `columns` API for python client.
columns = self.connect.read.table(self.tbl_name).columns
self.assertEqual(["id", "name"], columns)
def test_collect(self):
df = self.connect.read.table(self.tbl_name)
data = df.limit(10).collect()
self.assertEqual(len(data), 10)
# Check Row has schema column names.
self.assertTrue("name" in data[0])
self.assertTrue("id" in data[0])
def test_simple_udf(self):
def conv_udf(x) -> str:
return "Martin"
u = udf(conv_udf)
df = self.connect.read.table(self.tbl_name)
result = df.select(u(df.id)).toPandas()
self.assertIsNotNone(result)
def test_with_local_data(self):
"""SPARK-41114: Test creating a dataframe using local data"""
pdf = pandas.DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]})
df = self.connect.createDataFrame(pdf)
rows = df.filter(df.a == lit(3)).collect()
self.assertTrue(len(rows) == 1)
self.assertEqual(rows[0][0], 3)
self.assertEqual(rows[0][1], "c")
def test_simple_explain_string(self):
df = self.connect.read.table(self.tbl_name).limit(10)
result = df.explain()
self.assertGreater(len(result), 0)
def test_schema(self):
schema = self.connect.read.table(self.tbl_name).schema
self.assertEqual(
StructType(
[StructField("id", LongType(), True), StructField("name", StringType(), True)]
),
schema,
)
# test FloatType, DoubleType, DecimalType, StringType, BooleanType, NullType
query = """
SELECT * FROM VALUES
(float(1.0), double(1.0), 1.0, "1", true, NULL),
(float(2.0), double(2.0), 2.0, "2", false, NULL),
(float(3.0), double(3.0), NULL, "3", false, NULL)
AS tab(a, b, c, d, e, f)
"""
self.assertEqual(
self.spark.sql(query).schema,
self.connect.sql(query).schema,
)
# test TimestampType, DateType
query = """
SELECT * FROM VALUES
(TIMESTAMP('2019-04-12 15:50:00'), DATE('2022-02-22')),
(TIMESTAMP('2019-04-12 15:50:00'), NULL),
(NULL, DATE('2022-02-22'))
AS tab(a, b)
"""
self.assertEqual(
self.spark.sql(query).schema,
self.connect.sql(query).schema,
)
# test MapType
query = """
SELECT * FROM VALUES
(MAP('a', 'ab'), MAP('a', 'ab'), MAP(1, 2, 3, 4)),
(MAP('x', 'yz'), MAP('x', NULL), NULL),
(MAP('c', 'de'), NULL, MAP(-1, NULL, -3, -4))
AS tab(a, b, c)
"""
self.assertEqual(
self.spark.sql(query).schema,
self.connect.sql(query).schema,
)
# test ArrayType
query = """
SELECT * FROM VALUES
(ARRAY('a', 'ab'), ARRAY(1, 2, 3), ARRAY(1, NULL, 3)),
(ARRAY('x', NULL), NULL, ARRAY(1, 3)),
(NULL, ARRAY(-1, -2, -3), Array())
AS tab(a, b, c)
"""
self.assertEqual(
self.spark.sql(query).schema,
self.connect.sql(query).schema,
)
# test StructType
query = """
SELECT STRUCT(a, b, c, d), STRUCT(e, f, g), STRUCT(STRUCT(a, b), STRUCT(h)) FROM VALUES
(float(1.0), double(1.0), 1.0, "1", true, NULL, ARRAY(1, NULL, 3), MAP(1, 2, 3, 4)),
(float(2.0), double(2.0), 2.0, "2", false, NULL, ARRAY(1, 3), MAP(1, NULL, 3, 4)),
(float(3.0), double(3.0), NULL, "3", false, NULL, ARRAY(NULL), NULL)
AS tab(a, b, c, d, e, f, g, h)
"""
# compare the __repr__() to ignore the metadata for now
# the metadata is not supported in Connect for now
self.assertEqual(
self.spark.sql(query).schema.__repr__(),
self.connect.sql(query).schema.__repr__(),
)
def test_print_schema(self):
# SPARK-41216: Test print schema
tree_str = self.connect.sql("SELECT 1 AS X, 2 AS Y")._tree_string()
# root
# |-- X: integer (nullable = false)
# |-- Y: integer (nullable = false)
expected = "root\n |-- X: integer (nullable = false)\n |-- Y: integer (nullable = false)\n"
self.assertEqual(tree_str, expected)
def test_is_local(self):
# SPARK-41216: Test is local
self.assertTrue(self.connect.sql("SHOW DATABASES").isLocal)
self.assertFalse(self.connect.read.table(self.tbl_name).isLocal)
def test_is_streaming(self):
# SPARK-41216: Test is streaming
self.assertFalse(self.connect.read.table(self.tbl_name).isStreaming)
self.assertFalse(self.connect.sql("SELECT 1 AS X LIMIT 0").isStreaming)
def test_input_files(self):
# SPARK-41216: Test input files
tmpPath = tempfile.mkdtemp()
shutil.rmtree(tmpPath)
try:
self.df_text.write.text(tmpPath)
input_files_list1 = (
self.spark.read.format("text").schema("id STRING").load(path=tmpPath).inputFiles()
)
input_files_list2 = (
self.connect.read.format("text").schema("id STRING").load(path=tmpPath).inputFiles()
)
self.assertTrue(len(input_files_list1) > 0)
self.assertEqual(len(input_files_list1), len(input_files_list2))
for file_path in input_files_list2:
self.assertTrue(file_path in input_files_list1)
finally:
shutil.rmtree(tmpPath)
def test_simple_binary_expressions(self):
"""Test complex expression"""
df = self.connect.read.table(self.tbl_name)
pd = df.select(df.id).where(df.id % lit(30) == lit(0)).sort(df.id.asc()).toPandas()
self.assertEqual(len(pd.index), 4)
res = pandas.DataFrame(data={"id": [0, 30, 60, 90]})
self.assert_(pd.equals(res), f"{pd.to_string()} != {res.to_string()}")
def test_limit_offset(self):
df = self.connect.read.table(self.tbl_name)
pd = df.limit(10).offset(1).toPandas()
self.assertEqual(9, len(pd.index))
pd2 = df.offset(98).limit(10).toPandas()
self.assertEqual(2, len(pd2.index))
def test_sql(self):
pdf = self.connect.sql("SELECT 1").toPandas()
self.assertEqual(1, len(pdf.index))
def test_head(self):
# SPARK-41002: test `head` API in Python Client
df = self.connect.read.table(self.tbl_name)
self.assertIsNotNone(len(df.head()))
self.assertIsNotNone(len(df.head(1)))
self.assertIsNotNone(len(df.head(5)))
df2 = self.connect.read.table(self.tbl_name_empty)
self.assertIsNone(df2.head())
def test_first(self):
# SPARK-41002: test `first` API in Python Client
df = self.connect.read.table(self.tbl_name)
self.assertIsNotNone(len(df.first()))
df2 = self.connect.read.table(self.tbl_name_empty)
self.assertIsNone(df2.first())
def test_take(self) -> None:
# SPARK-41002: test `take` API in Python Client
df = self.connect.read.table(self.tbl_name)
self.assertEqual(5, len(df.take(5)))
df2 = self.connect.read.table(self.tbl_name_empty)
self.assertEqual(0, len(df2.take(5)))
def test_drop(self):
# SPARK-41169: test drop
query = """
SELECT * FROM VALUES
(false, 1, NULL), (false, NULL, 2), (NULL, 3, 3)
AS tab(a, b, c)
"""
cdf = self.connect.sql(query)
sdf = self.spark.sql(query)
self.assert_eq(
cdf.drop("a").toPandas(),
sdf.drop("a").toPandas(),
)
self.assert_eq(
cdf.drop("a", "b").toPandas(),
sdf.drop("a", "b").toPandas(),
)
self.assert_eq(
cdf.drop("a", "x").toPandas(),
sdf.drop("a", "x").toPandas(),
)
self.assert_eq(
cdf.drop(cdf.a, cdf.x).toPandas(),
sdf.drop("a", "x").toPandas(),
)
def test_subquery_alias(self) -> None:
# SPARK-40938: test subquery alias.
plan_text = (
self.connect.read.table(self.tbl_name).alias("special_alias").explain(extended=True)
)
self.assertTrue("special_alias" in plan_text)
def test_range(self):
self.assert_eq(
self.connect.range(start=0, end=10).toPandas(),
self.spark.range(start=0, end=10).toPandas(),
)
self.assert_eq(
self.connect.range(start=0, end=10, step=3).toPandas(),
self.spark.range(start=0, end=10, step=3).toPandas(),
)
self.assert_eq(
self.connect.range(start=0, end=10, step=3, numPartitions=2).toPandas(),
self.spark.range(start=0, end=10, step=3, numPartitions=2).toPandas(),
)
def test_create_global_temp_view(self):
# SPARK-41127: test global temp view creation.
with self.tempView("view_1"):
self.connect.sql("SELECT 1 AS X LIMIT 0").createGlobalTempView("view_1")
self.connect.sql("SELECT 2 AS X LIMIT 1").createOrReplaceGlobalTempView("view_1")
self.assertTrue(self.spark.catalog.tableExists("global_temp.view_1"))
# Test when creating a view which is alreayd exists but
self.assertTrue(self.spark.catalog.tableExists("global_temp.view_1"))
with self.assertRaises(grpc.RpcError):
self.connect.sql("SELECT 1 AS X LIMIT 0").createGlobalTempView("view_1")
def test_to_pandas(self):
# SPARK-41005: Test to pandas
query = """
SELECT * FROM VALUES
(false, 1, NULL),
(false, NULL, float(2.0)),
(NULL, 3, float(3.0))
AS tab(a, b, c)
"""
self.assert_eq(
self.connect.sql(query).toPandas(),
self.spark.sql(query).toPandas(),
)
query = """
SELECT * FROM VALUES
(1, 1, NULL),
(2, NULL, float(2.0)),
(3, 3, float(3.0))
AS tab(a, b, c)
"""
self.assert_eq(
self.connect.sql(query).toPandas(),
self.spark.sql(query).toPandas(),
)
query = """
SELECT * FROM VALUES
(double(1.0), 1, "1"),
(NULL, NULL, NULL),
(double(2.0), 3, "3")
AS tab(a, b, c)
"""
self.assert_eq(
self.connect.sql(query).toPandas(),
self.spark.sql(query).toPandas(),
)
query = """
SELECT * FROM VALUES
(float(1.0), double(1.0), 1, "1"),
(float(2.0), double(2.0), 2, "2"),
(float(3.0), double(3.0), 3, "3")
AS tab(a, b, c, d)
"""
self.assert_eq(
self.connect.sql(query).toPandas(),
self.spark.sql(query).toPandas(),
)
def test_select_expr(self):
# SPARK-41201: test selectExpr API.
self.assert_eq(
self.connect.read.table(self.tbl_name).selectExpr("id * 2").toPandas(),
self.spark.read.table(self.tbl_name).selectExpr("id * 2").toPandas(),
)
self.assert_eq(
self.connect.read.table(self.tbl_name)
.selectExpr(["id * 2", "cast(name as long) as name"])
.toPandas(),
self.spark.read.table(self.tbl_name)
.selectExpr(["id * 2", "cast(name as long) as name"])
.toPandas(),
)
self.assert_eq(
self.connect.read.table(self.tbl_name)
.selectExpr("id * 2", "cast(name as long) as name")
.toPandas(),
self.spark.read.table(self.tbl_name)
.selectExpr("id * 2", "cast(name as long) as name")
.toPandas(),
)
def test_fill_na(self):
# SPARK-41128: Test fill na
query = """
SELECT * FROM VALUES
(false, 1, NULL), (false, NULL, 2.0), (NULL, 3, 3.0)
AS tab(a, b, c)
"""
# +-----+----+----+
# | a| b| c|
# +-----+----+----+
# |false| 1|null|
# |false|null| 2.0|
# | null| 3| 3.0|
# +-----+----+----+
self.assert_eq(
self.connect.sql(query).fillna(True).toPandas(),
self.spark.sql(query).fillna(True).toPandas(),
)
self.assert_eq(
self.connect.sql(query).fillna(2).toPandas(),
self.spark.sql(query).fillna(2).toPandas(),
)
self.assert_eq(
self.connect.sql(query).fillna(2, ["a", "b"]).toPandas(),
self.spark.sql(query).fillna(2, ["a", "b"]).toPandas(),
)
self.assert_eq(
self.connect.sql(query).na.fill({"a": True, "b": 2}).toPandas(),
self.spark.sql(query).na.fill({"a": True, "b": 2}).toPandas(),
)
def test_empty_dataset(self):
# SPARK-41005: Test arrow based collection with empty dataset.
self.assertTrue(
self.connect.sql("SELECT 1 AS X LIMIT 0")
.toPandas()
.equals(self.spark.sql("SELECT 1 AS X LIMIT 0").toPandas())
)
pdf = self.connect.sql("SELECT 1 AS X LIMIT 0").toPandas()
self.assertEqual(0, len(pdf)) # empty dataset
self.assertEqual(1, len(pdf.columns)) # one column
self.assertEqual("X", pdf.columns[0])
def test_is_empty(self):
# SPARK-41212: Test is empty
self.assertFalse(self.connect.sql("SELECT 1 AS X").isEmpty())
self.assertTrue(self.connect.sql("SELECT 1 AS X LIMIT 0").isEmpty())
def test_session(self):
self.assertEqual(self.connect, self.connect.sql("SELECT 1").sparkSession())
def test_show(self):
# SPARK-41111: Test the show method
show_str = self.connect.sql("SELECT 1 AS X, 2 AS Y")._show_string()
# +---+---+
# | X| Y|
# +---+---+
# | 1| 2|
# +---+---+
expected = "+---+---+\n| X| Y|\n+---+---+\n| 1| 2|\n+---+---+\n"
self.assertEqual(show_str, expected)
def test_repr(self):
# SPARK-41213: Test the __repr__ method
query = """SELECT * FROM VALUES (1L, NULL), (3L, "Z") AS tab(a, b)"""
self.assertEqual(
self.connect.sql(query).__repr__(),
self.spark.sql(query).__repr__(),
)
def test_explain_string(self):
# SPARK-41122: test explain API.
plan_str = self.connect.sql("SELECT 1").explain(extended=True)
self.assertTrue("Parsed Logical Plan" in plan_str)
self.assertTrue("Analyzed Logical Plan" in plan_str)
self.assertTrue("Optimized Logical Plan" in plan_str)
self.assertTrue("Physical Plan" in plan_str)
with self.assertRaises(ValueError) as context:
self.connect.sql("SELECT 1").explain(mode="unknown")
self.assertTrue("unknown" in str(context.exception))
def test_simple_datasource_read(self) -> None:
writeDf = self.df_text
tmpPath = tempfile.mkdtemp()
shutil.rmtree(tmpPath)
writeDf.write.text(tmpPath)
readDf = self.connect.read.format("text").schema("id STRING").load(path=tmpPath)
expectResult = writeDf.collect()
pandasResult = readDf.toPandas()
if pandasResult is None:
self.assertTrue(False, "Empty pandas dataframe")
else:
actualResult = pandasResult.values.tolist()
self.assertEqual(len(expectResult), len(actualResult))
def test_simple_transform(self) -> None:
"""SPARK-41203: Support DF.transform"""
def transform_df(input_df: CDataFrame) -> CDataFrame:
return input_df.select((col("id") + lit(10)).alias("id"))
df = self.connect.range(1, 100)
result_left = df.transform(transform_df).collect()
result_right = self.connect.range(11, 110).collect()
self.assertEqual(result_right, result_left)
# Check assertion.
with self.assertRaises(AssertionError):
df.transform(lambda x: 2) # type: ignore
def test_alias(self) -> None:
"""Testing supported and unsupported alias"""
col0 = (
self.connect.range(1, 10)
.select(col("id").alias("name", metadata={"max": 99}))
.schema.names[0]
)
self.assertEqual("name", col0)
with self.assertRaises(grpc.RpcError) as exc:
self.connect.range(1, 10).select(col("id").alias("this", "is", "not")).collect()
self.assertIn("(this, is, not)", str(exc.exception))
def test_agg_with_two_agg_exprs(self):
# SPARK-41230: test dataframe.agg()
self.assert_eq(
self.connect.read.table(self.tbl_name).agg({"name": "min", "id": "max"}).toPandas(),
self.spark.read.table(self.tbl_name).agg({"name": "min", "id": "max"}).toPandas(),
)
class ChannelBuilderTests(ReusedPySparkTestCase):
def test_invalid_connection_strings(self):
invalid = [
"scc://host:12",
"http://host",
"sc:/host:1234/path",
"sc://host/path",
"sc://host/;parm1;param2",
]
for i in invalid:
self.assertRaises(AttributeError, ChannelBuilder, i)
def test_sensible_defaults(self):
chan = ChannelBuilder("sc://host")
self.assertFalse(chan.secure, "Default URL is not secure")
chan = ChannelBuilder("sc://host/;token=abcs")
self.assertTrue(chan.secure, "specifying a token must set the channel to secure")
chan = ChannelBuilder("sc://host/;use_ssl=abcs")
self.assertFalse(chan.secure, "Garbage in, false out")
def test_valid_channel_creation(self):
chan = ChannelBuilder("sc://host").toChannel()
self.assertIsInstance(chan, grpc.Channel)
# Sets up a channel without tokens because ssl is not used.
chan = ChannelBuilder("sc://host/;use_ssl=true;token=abc").toChannel()
self.assertIsInstance(chan, grpc.Channel)
chan = ChannelBuilder("sc://host/;use_ssl=true").toChannel()
self.assertIsInstance(chan, grpc.Channel)
def test_channel_properties(self):
chan = ChannelBuilder("sc://host/;use_ssl=true;token=abc;param1=120%2021")
self.assertEqual("host:15002", chan.endpoint)
self.assertEqual(True, chan.secure)
self.assertEqual("120 21", chan.get("param1"))
def test_metadata(self):
chan = ChannelBuilder("sc://host/;use_ssl=true;token=abc;param1=120%2021;x-my-header=abcd")
md = chan.metadata()
self.assertEqual([("param1", "120 21"), ("x-my-header", "abcd")], md)
if __name__ == "__main__":
from pyspark.sql.tests.connect.test_connect_basic import * # noqa: F401
try:
import xmlrunner # type: ignore
testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2)
except ImportError:
testRunner = None
unittest.main(testRunner=testRunner, verbosity=2)