|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | + |
| 4 | +def _read_csv(file_path: Path) -> str: |
| 5 | + return f"read_csv_auto('{str(file_path)}',all_varchar=true,delim=',',quote='\"',escape='\"')" |
| 6 | + |
| 7 | + |
| 8 | +def count_rows( |
| 9 | + conn, file_path: Path, expected: int, comparison_rule: str = "greater_than" |
| 10 | +): |
| 11 | + """ |
| 12 | + Counts the number of rows in the CSV and compares against an expected value. |
| 13 | +
|
| 14 | + Args: |
| 15 | + conn: duckdb connection |
| 16 | + file_path: path to the CSV file |
| 17 | + expected: the expected row count |
| 18 | + comparison_rule: how to compare actual vs expected |
| 19 | + """ |
| 20 | + result = conn.execute(f"SELECT COUNT(*) FROM {_read_csv(file_path)}").fetchone() |
| 21 | + actual = result[0] |
| 22 | + |
| 23 | + comparison_rules = { |
| 24 | + "equals_to": actual == expected, |
| 25 | + "not_equal_to": actual != expected, |
| 26 | + "greater_than": actual > expected, |
| 27 | + "greater_than_or_equal_to": actual >= expected, |
| 28 | + "less_than": actual < expected, |
| 29 | + "less_than_or_equal_to": actual <= expected, |
| 30 | + } |
| 31 | + |
| 32 | + if comparison_rule not in comparison_rules: |
| 33 | + raise ValueError( |
| 34 | + f"Invalid comparison_rule: '{comparison_rule}'. Must be one of {list(comparison_rules.keys())}." |
| 35 | + ) |
| 36 | + |
| 37 | + passed = comparison_rules[comparison_rule] |
| 38 | + message = f"there were {actual} rows found" |
| 39 | + details = { |
| 40 | + "actual": actual, |
| 41 | + "expected": expected, |
| 42 | + } |
| 43 | + |
| 44 | + return passed, message, details |
| 45 | + |
| 46 | + |
| 47 | +def check_unique(conn, file_path: Path, field: str): |
| 48 | + """ |
| 49 | + Checks that all values in a given field are unique. |
| 50 | +
|
| 51 | + Args: |
| 52 | + conn: duckdb connection |
| 53 | + file_path: path to the CSV file |
| 54 | + field: the column name to check for uniqueness |
| 55 | + """ |
| 56 | + result = conn.execute( |
| 57 | + f'SELECT "{field}", COUNT(*) as cnt FROM {_read_csv(file_path)} GROUP BY "{field}" HAVING cnt > 1' |
| 58 | + ).fetchall() |
| 59 | + |
| 60 | + duplicates = [{"value": row[0], "count": row[1]} for row in result] |
| 61 | + |
| 62 | + if len(duplicates) == 0: |
| 63 | + passed = True |
| 64 | + message = f"all values in '{field}' are unique" |
| 65 | + else: |
| 66 | + passed = False |
| 67 | + message = f"there were {len(duplicates)} duplicate values in '{field}'" |
| 68 | + |
| 69 | + details = { |
| 70 | + "field": field, |
| 71 | + "duplicates": duplicates, |
| 72 | + } |
| 73 | + |
| 74 | + return passed, message, details |
| 75 | + |
| 76 | + |
| 77 | +def check_no_shared_values(conn, file_path: Path, field_1: str, field_2: str): |
| 78 | + """ |
| 79 | + Checks that no value appears in both field_1 and field_2. |
| 80 | +
|
| 81 | + Args: |
| 82 | + conn: duckdb connection |
| 83 | + file_path: path to the CSV file |
| 84 | + field_1: the first column name |
| 85 | + field_2: the second column name |
| 86 | + """ |
| 87 | + result = conn.execute( |
| 88 | + f""" |
| 89 | + SELECT DISTINCT a."{field_1}" as value |
| 90 | + FROM {_read_csv(file_path)} a |
| 91 | + WHERE a."{field_1}" IN (SELECT "{field_2}" FROM {_read_csv(file_path)}) |
| 92 | + AND a."{field_1}" IS NOT NULL AND a."{field_1}" != '' |
| 93 | + """ |
| 94 | + ).fetchall() |
| 95 | + |
| 96 | + shared_values = [row[0] for row in result] |
| 97 | + |
| 98 | + if len(shared_values) == 0: |
| 99 | + passed = True |
| 100 | + message = f"no shared values between '{field_1}' and '{field_2}'" |
| 101 | + else: |
| 102 | + passed = False |
| 103 | + message = f"there were {len(shared_values)} shared values between '{field_1}' and '{field_2}'" |
| 104 | + |
| 105 | + details = { |
| 106 | + "field_1": field_1, |
| 107 | + "field_2": field_2, |
| 108 | + "shared_values": shared_values, |
| 109 | + } |
| 110 | + |
| 111 | + return passed, message, details |
| 112 | + |
| 113 | + |
| 114 | +def check_no_overlapping_ranges(conn, file_path: Path, min_field: str, max_field: str): |
| 115 | + """ |
| 116 | + Checks that no ranges overlap between rows. |
| 117 | +
|
| 118 | + Two ranges [a_min, a_max] and [b_min, b_max] overlap if: |
| 119 | + a_min <= b_max AND a_max >= b_min |
| 120 | +
|
| 121 | + Args: |
| 122 | + conn: duckdb connection |
| 123 | + file_path: path to the CSV file |
| 124 | + min_field: the column name for the range minimum |
| 125 | + max_field: the column name for the range maximum |
| 126 | + """ |
| 127 | + result = conn.execute( |
| 128 | + f""" |
| 129 | + SELECT |
| 130 | + a."{min_field}" as a_min, |
| 131 | + a."{max_field}" as a_max, |
| 132 | + b."{min_field}" as b_min, |
| 133 | + b."{max_field}" as b_max |
| 134 | + FROM {_read_csv(file_path)} a |
| 135 | + JOIN {_read_csv(file_path)} b |
| 136 | + ON CAST(a."{min_field}" AS BIGINT) < CAST(b."{min_field}" AS BIGINT) |
| 137 | + WHERE CAST(a."{min_field}" AS BIGINT) <= CAST(b."{max_field}" AS BIGINT) |
| 138 | + AND CAST(a."{max_field}" AS BIGINT) >= CAST(b."{min_field}" AS BIGINT) |
| 139 | + """ |
| 140 | + ).fetchall() |
| 141 | + |
| 142 | + overlaps = [ |
| 143 | + {"range_1": [row[0], row[1]], "range_2": [row[2], row[3]]} for row in result |
| 144 | + ] |
| 145 | + |
| 146 | + if len(overlaps) == 0: |
| 147 | + passed = True |
| 148 | + message = f"no overlapping ranges found between '{min_field}' and '{max_field}'" |
| 149 | + else: |
| 150 | + passed = False |
| 151 | + message = f"there were {len(overlaps)} overlapping ranges found" |
| 152 | + |
| 153 | + details = { |
| 154 | + "min_field": min_field, |
| 155 | + "max_field": max_field, |
| 156 | + "overlaps": overlaps, |
| 157 | + } |
| 158 | + |
| 159 | + return passed, message, details |
0 commit comments