Skip to content

Commit a8547aa

Browse files
authored
chore(tests): reduce tests complexity (#25)
1 parent f2d3179 commit a8547aa

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

deidentify.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ func (d *Deidentifier) deidentifyValue(value string, dataType DataType, columnNa
299299
}
300300

301301
var result string
302-
var err error
303302

304303
switch dataType {
305304
case TypeName:
@@ -318,10 +317,6 @@ func (d *Deidentifier) deidentifyValue(value string, dataType DataType, columnNa
318317
result = d.generateGeneric(value)
319318
}
320319

321-
if err != nil {
322-
return "", err
323-
}
324-
325320
// Store mapping for consistency
326321
d.setMapping(columnName, value, result)
327322
return result, nil
@@ -364,7 +359,7 @@ func (d *Deidentifier) generateCreditCard(original string) string {
364359

365360
// Generate 15 digits (4000 + 11 more digits)
366361
cardNumber := "4000"
367-
for i := 0; i < 11; i++ {
362+
for i := range 11 {
368363
digit := d.hashToIndex(hash[i*2:i*2+2], 10)
369364
cardNumber += strconv.Itoa(digit)
370365
}

deidentify_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,22 @@ func TestSlices(t *testing.T) {
505505
}
506506

507507
// Check result dimensions
508+
checkSlicesDimensions(t, result, data)
509+
510+
// Test that non-empty values are deidentified
511+
checkNonEmptyValuesDeidentified(t, result, data)
512+
513+
// Test that empty values remain empty
514+
checkEmptyValuesRemainEmpty(t, result)
515+
516+
// Test deterministic behavior
517+
checkDeterministicBehavior(t, d, data, columnTypes, columnNames, result)
518+
519+
// Test with different column names (should produce different results)
520+
checkDifferentColumnNames(t, data, columnTypes, result)
521+
}
522+
523+
func checkSlicesDimensions(t *testing.T, result, data [][]string) {
508524
if len(result) != len(data) {
509525
t.Errorf("Expected %d rows, got %d", len(data), len(result))
510526
}
@@ -514,21 +530,24 @@ func TestSlices(t *testing.T) {
514530
t.Errorf("Row %d: expected %d columns, got %d", i, len(data[i]), len(row))
515531
}
516532
}
533+
}
517534

518-
// Test that non-empty values are deidentified
535+
func checkNonEmptyValuesDeidentified(t *testing.T, result, data [][]string) {
519536
if result[0][0] == data[0][0] && data[0][0] != "" {
520537
t.Error("Name should be deidentified")
521538
}
522539
if result[0][1] == data[0][1] && data[0][1] != "" {
523540
t.Error("Email should be deidentified")
524541
}
542+
}
525543

526-
// Test that empty values remain empty
544+
func checkEmptyValuesRemainEmpty(t *testing.T, result [][]string) {
527545
if result[3][0] != "" || result[3][1] != "" {
528546
t.Error("Empty values should remain empty")
529547
}
548+
}
530549

531-
// Test deterministic behavior
550+
func checkDeterministicBehavior(t *testing.T, d *Deidentifier, data [][]string, columnTypes []DataType, columnNames []string, result [][]string) {
532551
result2, err := d.Slices(data, columnTypes, columnNames)
533552
if err != nil {
534553
t.Fatalf("Second Slices failed: %v", err)
@@ -537,8 +556,9 @@ func TestSlices(t *testing.T) {
537556
if result[0][0] != result2[0][0] {
538557
t.Error("Deidentification should be deterministic")
539558
}
559+
}
540560

541-
// Test with different column names (should produce different results)
561+
func checkDifferentColumnNames(t *testing.T, data [][]string, columnTypes []DataType, result [][]string) {
542562
// Create a fresh deidentifier to ensure clean mapping table
543563
d2 := NewDeidentifier("test-secret-key-3")
544564
differentColumnNames := []string{"customer_name", "customer_email", "customer_phone", "customer_ssn"}

0 commit comments

Comments
 (0)