@@ -53,7 +53,7 @@ func TestParseRange(t *testing.T) {
5353
5454// TestProcessCSV_MissingColumns tests that processCSV returns an error when required columns are missing
5555func TestProcessCSV_MissingColumns (t * testing.T ) {
56- _ , err := processCSV ("testdata/missing-columns.csv" , nil , false )
56+ _ , err := processCSV ("testdata/missing-columns.csv" , nil , "" , false )
5757 if err == nil {
5858 t .Error ("processCSV() expected error for missing columns, got nil" )
5959 }
@@ -65,7 +65,7 @@ func TestProcessCSV_MissingColumns(t *testing.T) {
6565
6666// TestProcessCSV_InvalidURL tests that processCSV skips URLs that don't start with www.
6767func TestProcessCSV_InvalidURL (t * testing.T ) {
68- records , err := processCSV ("testdata/invalid-url.csv" , nil , false )
68+ records , err := processCSV ("testdata/invalid-url.csv" , nil , "" , false )
6969 if err != nil {
7070 t .Errorf ("processCSV() unexpected error: %v" , err )
7171 }
@@ -96,7 +96,7 @@ func TestProcessCSV_ValidFiltering(t *testing.T) {
9696
9797 for _ , tt := range tests {
9898 t .Run (tt .name , func (t * testing.T ) {
99- records , err := processCSV (tt .file , nil , false )
99+ records , err := processCSV (tt .file , nil , "" , false )
100100 if err != nil {
101101 t .Fatalf ("processCSV() unexpected error: %v" , err )
102102 }
@@ -115,7 +115,7 @@ func TestProcessCSV_ValidFiltering(t *testing.T) {
115115
116116// TestProcessCSV_EmptyFile tests that processCSV handles empty CSV files
117117func TestProcessCSV_EmptyFile (t * testing.T ) {
118- records , err := processCSV ("testdata/empty.csv" , nil , false )
118+ records , err := processCSV ("testdata/empty.csv" , nil , "" , false )
119119 if err != nil {
120120 t .Fatalf ("processCSV() unexpected error: %v" , err )
121121 }
@@ -126,7 +126,7 @@ func TestProcessCSV_EmptyFile(t *testing.T) {
126126
127127// TestProcessCSV_FileNotFound tests that processCSV returns an error for non-existent files
128128func TestProcessCSV_FileNotFound (t * testing.T ) {
129- _ , err := processCSV ("testdata/nonexistent.csv" , nil , false )
129+ _ , err := processCSV ("testdata/nonexistent.csv" , nil , "" , false )
130130 if err == nil {
131131 t .Error ("processCSV() expected error for non-existent file, got nil" )
132132 }
@@ -311,7 +311,7 @@ func splitLines(s string) []string {
311311
312312// TestProcessCSV_OnlyPageviewsFiltered tests that only Pageviews rows are included
313313func TestProcessCSV_OnlyPageviewsFiltered (t * testing.T ) {
314- records , err := processCSV ("testdata/more-data.csv" , nil , false )
314+ records , err := processCSV ("testdata/more-data.csv" , nil , "" , false )
315315 if err != nil {
316316 t .Fatalf ("processCSV() unexpected error: %v" , err )
317317 }
@@ -352,7 +352,7 @@ func TestProcessCSV_URLValidation(t *testing.T) {
352352 t .Fatalf ("Failed to create test file: %v" , err )
353353 }
354354
355- records , err := processCSV (tmpFile , nil , false )
355+ records , err := processCSV (tmpFile , nil , "" , false )
356356 if err != nil {
357357 t .Errorf ("processCSV() unexpected error for URL %q: %v" , tt .url , err )
358358 }
@@ -403,7 +403,7 @@ func TestIntegration_EndToEnd(t *testing.T) {
403403 outputPath := filepath .Join (tmpDir , "result.csv" )
404404
405405 // Process the valid-with-filtering.csv file
406- records , err := processCSV ("testdata/valid-with-filtering.csv" , nil , false )
406+ records , err := processCSV ("testdata/valid-with-filtering.csv" , nil , "" , false )
407407 if err != nil {
408408 t .Fatalf ("processCSV() unexpected error: %v" , err )
409409 }
@@ -458,7 +458,7 @@ www.example.com/page4,Pageviews,400
458458
459459 // Test with ignore list
460460 ignoreURLs := []string {"www.example.com/page2" , "www.example.com/page4" }
461- records , err := processCSV (tmpFile , ignoreURLs , false )
461+ records , err := processCSV (tmpFile , ignoreURLs , "" , false )
462462 if err != nil {
463463 t .Fatalf ("processCSV() unexpected error: %v" , err )
464464 }
@@ -492,6 +492,103 @@ www.example.com/page4,Pageviews,400
492492 }
493493}
494494
495+ // TestProcessCSV_ContainsFilter tests that URLs are filtered by substring
496+ func TestProcessCSV_ContainsFilter (t * testing.T ) {
497+ tmpDir := t .TempDir ()
498+ tmpFile := filepath .Join (tmpDir , "test.csv" )
499+
500+ // Create test CSV with various URLs
501+ content := `Page,Measure Names,Measure Values
502+ www.example.com/manual/page1,Pageviews,100
503+ www.example.com/blog/post1,Pageviews,200
504+ www.example.com/manual/page2,Pageviews,150
505+ www.example.com/docs/guide,Pageviews,300
506+ www.example.com/manual/tutorial,Pageviews,250
507+ `
508+ if err := os .WriteFile (tmpFile , []byte (content ), 0644 ); err != nil {
509+ t .Fatalf ("Failed to create test file: %v" , err )
510+ }
511+
512+ tests := []struct {
513+ name string
514+ containsFilter string
515+ expectedCount int
516+ expectedURLs []string
517+ }{
518+ {
519+ name : "no filter" ,
520+ containsFilter : "" ,
521+ expectedCount : 5 ,
522+ expectedURLs : []string {"www.example.com/manual/page1" , "www.example.com/blog/post1" , "www.example.com/manual/page2" , "www.example.com/docs/guide" , "www.example.com/manual/tutorial" },
523+ },
524+ {
525+ name : "filter for /manual/" ,
526+ containsFilter : "/manual/" ,
527+ expectedCount : 3 ,
528+ expectedURLs : []string {"www.example.com/manual/page1" , "www.example.com/manual/page2" , "www.example.com/manual/tutorial" },
529+ },
530+ {
531+ name : "filter for /blog/" ,
532+ containsFilter : "/blog/" ,
533+ expectedCount : 1 ,
534+ expectedURLs : []string {"www.example.com/blog/post1" },
535+ },
536+ {
537+ name : "filter for /docs/" ,
538+ containsFilter : "/docs/" ,
539+ expectedCount : 1 ,
540+ expectedURLs : []string {"www.example.com/docs/guide" },
541+ },
542+ {
543+ name : "filter with no matches" ,
544+ containsFilter : "/nonexistent/" ,
545+ expectedCount : 0 ,
546+ expectedURLs : []string {},
547+ },
548+ }
549+
550+ for _ , tt := range tests {
551+ t .Run (tt .name , func (t * testing.T ) {
552+ records , err := processCSV (tmpFile , nil , tt .containsFilter , false )
553+ if err != nil {
554+ t .Fatalf ("processCSV() unexpected error: %v" , err )
555+ }
556+
557+ if len (records ) != tt .expectedCount {
558+ t .Errorf ("processCSV() got %d records, want %d" , len (records ), tt .expectedCount )
559+ }
560+
561+ // Verify all expected URLs are present
562+ for _ , expectedURL := range tt .expectedURLs {
563+ found := false
564+ for _ , record := range records {
565+ if record .Page == expectedURL {
566+ found = true
567+ break
568+ }
569+ }
570+ if ! found {
571+ t .Errorf ("Expected URL %q not found in results" , expectedURL )
572+ }
573+ }
574+
575+ // Verify no unexpected URLs are present
576+ for _ , record := range records {
577+ found := false
578+ for _ , expectedURL := range tt .expectedURLs {
579+ if record .Page == expectedURL {
580+ found = true
581+ break
582+ }
583+ }
584+ if ! found {
585+ t .Errorf ("Unexpected URL %q found in results" , record .Page )
586+ }
587+ }
588+ })
589+ }
590+ }
591+
495592// TestWriteOutput_ShowPageviews tests that pageviews column is added when enabled
496593func TestWriteOutput_ShowPageviews (t * testing.T ) {
497594 tmpDir := t .TempDir ()
0 commit comments