gss-bi-udfs 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tests/test_utils.py ADDED
@@ -0,0 +1,119 @@
1
+ import os
2
+ import unittest
3
+ from types import SimpleNamespace
4
+ from unittest.mock import MagicMock, patch
5
+
6
+ from pyspark.sql.types import (
7
+ BooleanType,
8
+ DateType,
9
+ DecimalType,
10
+ DoubleType,
11
+ FloatType,
12
+ IntegerType,
13
+ LongType,
14
+ StringType,
15
+ TimestampType,
16
+ )
17
+
18
+ from gss_bi_udfs import utils
19
+
20
+
21
+ class TestUtils(unittest.TestCase):
22
+ def test_get_env_uses_default_and_env_var(self):
23
+ with patch.dict(os.environ, {}, clear=True):
24
+ self.assertEqual(utils.get_env(), "dev")
25
+ self.assertEqual(utils.get_env(default="qa"), "qa")
26
+
27
+ with patch.dict(os.environ, {"ENV": "prod"}, clear=True):
28
+ self.assertEqual(utils.get_env(), "prod")
29
+
30
+ def test_get_env_catalog(self):
31
+ with patch.dict(os.environ, {"ENV": "pro"}, clear=True):
32
+ self.assertEqual(utils.get_env_catalog("fi_comunes"), "fi_comunes")
33
+ with patch.dict(os.environ, {"ENV": "dev"}, clear=True):
34
+ self.assertEqual(utils.get_env_catalog("fi_comunes"), "fi_comunes_dev")
35
+
36
+ def test_get_env_table_path(self):
37
+ with patch("gss_bi_udfs.utils.get_env_catalog", return_value="cat_dev"):
38
+ self.assertEqual(
39
+ utils.get_env_table_path("cat", "silver.dim_cliente"),
40
+ "cat_dev.silver.dim_cliente",
41
+ )
42
+
43
+ @patch("gss_bi_udfs.utils.get_env_catalog", return_value="cat_dev")
44
+ def test_get_schema_root_location(self, _mock_catalog):
45
+ spark = MagicMock()
46
+ df = MagicMock()
47
+ df.filter.return_value = df
48
+ df.select.return_value = df
49
+ df.collect.return_value = [["s3://bucket/root"]]
50
+ spark.sql.return_value = df
51
+
52
+ out = utils.get_schema_root_location(spark, "cat", "silver")
53
+
54
+ self.assertEqual(out, "s3://bucket/root")
55
+ spark.sql.assert_called_once_with("DESCRIBE SCHEMA EXTENDED cat_dev.silver")
56
+
57
+ def test_get_table_info_validations(self):
58
+ spark = MagicMock()
59
+ with self.assertRaises(ValueError):
60
+ utils.get_table_info(spark, full_table_name="solo.dos")
61
+ with self.assertRaises(ValueError):
62
+ utils.get_table_info(spark)
63
+
64
+ @patch("gss_bi_udfs.utils.get_schema_root_location", return_value="s3://bucket/root/silver")
65
+ @patch("gss_bi_udfs.utils.get_env_catalog", return_value="cat_dev")
66
+ def test_get_table_info_when_table_does_not_exist(self, _mock_env_catalog, _mock_root):
67
+ spark = MagicMock()
68
+ spark.catalog.tableExists.return_value = False
69
+
70
+ info = utils.get_table_info(spark, full_table_name="cat.silver.dim_cliente")
71
+
72
+ self.assertEqual(info["catalog"], "cat_dev")
73
+ self.assertEqual(info["schema"], "silver")
74
+ self.assertEqual(info["table"], "dim_cliente")
75
+ self.assertEqual(info["full_table_name"], "cat_dev.silver.dim_cliente")
76
+ self.assertEqual(info["path"], "s3://bucket/root/silver/dim_cliente")
77
+ self.assertFalse(info["exists"])
78
+
79
+ @patch("gss_bi_udfs.utils.get_schema_root_location", return_value="s3://bucket/root/silver")
80
+ @patch("gss_bi_udfs.utils.get_env_catalog", return_value="cat_dev")
81
+ def test_get_table_info_when_table_exists(self, _mock_env_catalog, _mock_root):
82
+ spark = MagicMock()
83
+ spark.catalog.tableExists.return_value = True
84
+ desc_df = MagicMock()
85
+ desc_df.filter.return_value = desc_df
86
+ desc_df.collect.return_value = [
87
+ SimpleNamespace(col_name="Location", data_type="s3://bucket/real/location"),
88
+ SimpleNamespace(col_name="Provider", data_type="delta"),
89
+ SimpleNamespace(col_name="Type", data_type="MANAGED"),
90
+ ]
91
+ spark.sql.return_value = desc_df
92
+
93
+ info = utils.get_table_info(spark, full_table_name="cat.silver.dim_cliente")
94
+
95
+ self.assertTrue(info["exists"])
96
+ self.assertEqual(info["path"], "s3://bucket/real/location")
97
+ self.assertEqual(info["provider"], "delta")
98
+ self.assertEqual(info["table_type"], "MANAGED")
99
+
100
+ def test_get_default_value_by_type_returns_column(self):
101
+ dtypes = [
102
+ IntegerType(),
103
+ LongType(),
104
+ DecimalType(10, 2),
105
+ DoubleType(),
106
+ FloatType(),
107
+ DateType(),
108
+ TimestampType(),
109
+ BooleanType(),
110
+ StringType(),
111
+ ]
112
+ for dtype in dtypes:
113
+ with self.subTest(dtype=dtype):
114
+ out = utils.get_default_value_by_type(dtype)
115
+ self.assertEqual(out.__class__.__name__, "Column")
116
+
117
+
118
+ if __name__ == "__main__":
119
+ unittest.main()
workspace/main.py ADDED
File without changes
workspace/prueba.py ADDED
@@ -0,0 +1,10 @@
1
+ from pyspark.sql import SparkSession
2
+
3
+ spark = SparkSession.getActiveSession()
4
+ if spark is None:
5
+ spark = SparkSession.builder.appName("MiApp").getOrCreate()
6
+
7
+ df = spark.range(1000 * 1000)
8
+ print(df.count())
9
+
10
+ spark.stop()
@@ -0,0 +1,20 @@
1
+ class Calculadora:
2
+ """Clase calculadora con operaciones básicas"""
3
+
4
+ def sumar(self, a, b):
5
+ """Suma dos números"""
6
+ return a + b
7
+
8
+ def restar(self, a, b):
9
+ """Resta dos números"""
10
+ return a - b
11
+
12
+ def multiplicar(self, a, b):
13
+ """Multiplica dos números"""
14
+ return a * b
15
+
16
+ def dividir(self, a, b):
17
+ """Divide dos números"""
18
+ if b == 0:
19
+ raise ValueError("No se puede dividir entre cero")
20
+ return a / b
@@ -0,0 +1,14 @@
1
+ import pytest
2
+ import sys
3
+ sys.path.append("/workspace")
4
+
5
+ from .prueba_calculadora import sumar, restar
6
+
7
+ def test_sumar():
8
+ assert sumar(2, 3) == 5
9
+
10
+ def test_restar():
11
+ assert restar(5, 3) == 2
12
+
13
+ def test_sumar_negativos():
14
+ assert sumar(-1, -2) == -3
@@ -1,4 +0,0 @@
1
- gss_bi_udfs-0.1.0.dist-info/METADATA,sha256=ChQSSxNCSDO4zK3mupeVxs9X0rXov2NWzib4n0HAoaM,339
2
- gss_bi_udfs-0.1.0.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
3
- gss_bi_udfs-0.1.0.dist-info/top_level.txt,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
4
- gss_bi_udfs-0.1.0.dist-info/RECORD,,
@@ -1 +0,0 @@
1
-