Skip to content

Commit 5640a04

Browse files
committed
Improve test coverage
1 parent ea8790b commit 5640a04

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

tests/test_constraints.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def test_simple_constraints2(self, generationSpec1):
6060
rowCount = testDataDF.count()
6161
assert rowCount == 100
6262

63+
def test_sql_expr_requires_non_empty_expression(self):
64+
with pytest.raises(ValueError, match="Expression must be a valid non-empty SQL string"):
65+
SqlExpr("")
66+
6367
def test_multiple_constraints(self, generationSpec1):
6468
testDataSpec = generationSpec1.withConstraints([SqlExpr("id < 100"), SqlExpr("id > 0")])
6569

tests/test_quick_tests.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -465,37 +465,55 @@ def test_empty_range(self):
465465
assert empty_range.isEmpty()
466466

467467
def test_nrange_legacy_min_and_minvalue_conflict(self):
468-
"""Ensure conflicting legacy 'min' and 'minValue' arguments raise a clear error."""
469468
with pytest.raises(ValueError, match="Only one of 'minValue' and legacy 'min' may be specified"):
470469
NRange(minValue=0.0, min=1.0)
471470

472471
def test_nrange_legacy_min_must_be_numeric(self):
473-
"""Ensure legacy 'min' argument must be numeric."""
474472
with pytest.raises(ValueError, match=r"Legacy 'min' argument must be an integer or float\."):
475473
NRange(min="not-a-number")
476474

475+
def test_nrange_legacy_max_and_maxvalue_conflict(self):
476+
with pytest.raises(ValueError, match="Only one of 'maxValue' and legacy 'max' may be specified"):
477+
NRange(maxValue=10.0, max=11.0)
478+
479+
def test_nrange_legacy_max_must_be_numeric(self):
480+
with pytest.raises(ValueError, match=r"Legacy 'max' argument must be an integer or float\."):
481+
NRange(max="not-a-number")
482+
477483
def test_nrange_unexpected_kwargs_error_message(self):
478-
"""Ensure unexpected keyword arguments produce a helpful error."""
479484
with pytest.raises(ValueError, match=r"Unexpected keyword arguments for NRange: .*"):
480485
NRange(foo=1)
481486

482487
def test_nrange_maxvalue_and_until_conflict(self):
483-
"""Ensure conflicting 'maxValue' and 'until' arguments raise a clear error."""
484488
with pytest.raises(ValueError, match="Only one of 'maxValue' or 'until' may be specified."):
485489
NRange(maxValue=10, until=20)
486490

487491
def test_nrange_discrete_range_requires_min_max_step(self):
488-
"""Ensure getDiscreteRange validates required attributes."""
489492
rng = NRange(minValue=0.0, maxValue=10.0)
490493
with pytest.raises(ValueError, match="Range must have 'minValue', 'maxValue', and 'step' defined\\."):
491494
_ = rng.getDiscreteRange()
492495

493496
def test_nrange_discrete_range_step_must_be_non_zero(self):
494-
"""Ensure getDiscreteRange validates non-zero step."""
495497
rng = NRange(minValue=0.0, maxValue=10.0, step=0)
496498
with pytest.raises(ValueError, match="Parameter 'step' must be non-zero when computing discrete range\\."):
497499
_ = rng.getDiscreteRange()
498500

501+
def test_nrange_adjust_for_byte_type_maxvalue_out_of_range(self):
502+
rng = NRange(maxValue=300) # above allowed ByteType max of 256
503+
with pytest.raises(
504+
ValueError,
505+
match=r"`maxValue` must be within the valid range \(0 - 256\) for ByteType\.",
506+
):
507+
rng.adjustForColumnDatatype(ByteType())
508+
509+
def test_nrange_get_continuous_range_requires_min_and_max(self):
510+
rng = NRange(minValue=None, maxValue=10.0)
511+
with pytest.raises(
512+
ValueError,
513+
match=r"Range must have 'minValue' and 'maxValue' defined\.",
514+
):
515+
_ = rng.getContinuousRange()
516+
499517
def test_reversed_ranges(self):
500518
testDataSpec = (
501519
dg.DataGenerator(sparkSession=spark, name="ranged_data", rows=100000, partitions=4)
@@ -561,8 +579,6 @@ def test_date_time_ranges(self):
561579
rowCount = rangedDF.count()
562580
assert rowCount == 100000
563581

564-
# TODO: add additional validation statement
565-
566582
@pytest.mark.parametrize("asHtml", [True, False])
567583
def test_script_table(self, asHtml):
568584
testDataSpec = (

tests/test_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from datetime import timedelta
33

44
import pytest
5+
from pyspark.sql import Column
6+
from pyspark.sql.functions import current_date
57

68
from dbldatagen import (
79
ensure,
@@ -18,6 +20,8 @@
1820
system_time_millis,
1921
)
2022

23+
from dbldatagen.utils import ensure_column
24+
2125
spark = SparkSingleton.getLocalInstance("unit tests")
2226

2327

@@ -225,3 +229,8 @@ def test_topological_sort_cycle_error_message(self):
225229
deps = [("a", {"b"}), ("b", {"a"})]
226230
with pytest.raises(ValueError, match="cyclic or missing dependency detected"):
227231
topologicalSort(deps)
232+
233+
@pytest.mark.parametrize("col", ["col1", current_date()])
234+
def test_ensure_column(self, col):
235+
ensured = ensure_column(col)
236+
assert isinstance(ensured, Column)

0 commit comments

Comments
 (0)