Skip to content

Commit

Permalink
Merge pull request #82 from tminaorg/unit-tests
Browse files Browse the repository at this point in the history
Unit tests
  • Loading branch information
aleksasiriski authored Sep 26, 2023
2 parents 93861ee + 00aefb0 commit bcc5352
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 308 deletions.
34 changes: 34 additions & 0 deletions src/engines/_engines_test/structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package _engines_test

import (
"github.com/tminaorg/brzaguza/src/config"
"github.com/tminaorg/brzaguza/src/engines"
)

type TestCaseHasAnyResults struct {
Query string
Options engines.Options
}

type TestCaseContainsResults struct {
Query string
ResultURL []string
Options engines.Options
}

type TestCaseRankedResults struct {
Query string
ResultURL []string
Options engines.Options
}

func NewConfig(engineName engines.Name) *config.Config {
config.EnabledEngines = append(config.EnabledEngines, engineName)
return &config.Config{
Engines: map[string]config.Engine{
engineName.ToLower(): {
Enabled: true,
},
},
}
}
59 changes: 59 additions & 0 deletions src/engines/_engines_test/tester.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package _engines_test

import (
"strings"
"testing"

"github.com/tminaorg/brzaguza/src/config"
"github.com/tminaorg/brzaguza/src/search"
)

func CheckTestCases(tchar []TestCaseHasAnyResults, tccr []TestCaseContainsResults,
tcrr []TestCaseRankedResults, t *testing.T, conf *config.Config) {

// TestCaseHasAnyResults
for _, tc := range tchar {
if results := search.PerformSearch(tc.Query, tc.Options, conf); len(results) == 0 {
defer t.Errorf("Got no results for %v", tc.Query)
}
}

// TestCaseContainsResults
for _, tc := range tccr {
results := search.PerformSearch(tc.Query, tc.Options, conf)
if len(results) == 0 {
defer t.Errorf("Got no results for %v", tc.Query)
} else {
for _, rURL := range tc.ResultURL {
found := false

for _, r := range results {
if strings.Contains(r.URL, rURL) {
found = true
break
}
}

if !found {
defer t.Errorf("Couldn't find %v (%v)", rURL, tc.Query)
}
}
}
}

// TestCaseRankedResults
for _, tc := range tcrr {
results := search.PerformSearch(tc.Query, tc.Options, conf)
if len(results) == 0 {
defer t.Errorf("Got no results for %v", tc.Query)
} else if len(results) < len(tc.ResultURL) {
defer t.Errorf("Number of results is less than test case URLs.")
} else {
for i, rURL := range tc.ResultURL {
if !strings.Contains(results[i].URL, rURL) {
defer t.Errorf("Wrong result on position %v: %v (%v)", i, rURL, tc.Query)
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/engines/bing/bing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func Search(ctx context.Context, query string, relay *bucket.Relay, options engi
return err
}

// Removes Telemetry. Acknowladge this overwrite user-side.
options.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"

var col *colly.Collector
var pagesCol *colly.Collector
var retError error
Expand Down
52 changes: 27 additions & 25 deletions src/engines/bing/bing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,46 @@ package bing_test
import (
"testing"

"github.com/tminaorg/brzaguza/src/config"
"github.com/tminaorg/brzaguza/src/engines"
"github.com/tminaorg/brzaguza/src/search"
"github.com/tminaorg/brzaguza/src/engines/_engines_test"
)

type TestCase struct {
query string
options engines.Options
}

func TestSearch(t *testing.T) {
engineName := engines.Bing

// testing config
conf := config.Config{
Engines: map[string]config.Engine{
engineName.ToLower(): {
Enabled: true,
},
conf := _engines_test.NewConfig(engineName)

// test cases
tchar := [...]_engines_test.TestCaseHasAnyResults{{
Query: "ping",
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}
}}

// enabled engines names
config.EnabledEngines = append(config.EnabledEngines, engineName)
//tccr := [...]_engines_test.TestCaseContainsResults{}
//tcrr := [...]_engines_test.TestCaseRankedResults{}

// test cases
testCases := [...]TestCase{{
query: "wikipedia",
options: engines.Options{
//bing has hard telemetry links, solved by UA overwrite
tccr := [...]_engines_test.TestCaseContainsResults{{
Query: "facebook",
ResultURL: []string{"facebook.com"},
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}}

tcrr := [...]_engines_test.TestCaseRankedResults{{
Query: "wikipedia",
ResultURL: []string{"wikipedia."},
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}}

// running tests
for _, tc := range testCases {
if results := search.PerformSearch(tc.query, tc.options, &conf); len(results) == 0 {
t.Errorf("Got no results for %v", tc.query)
}
}
_engines_test.CheckTestCases(tchar[:], tccr[:], tcrr[:], t, conf)
}
48 changes: 23 additions & 25 deletions src/engines/brave/brave_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,42 @@ package brave_test
import (
"testing"

"github.com/tminaorg/brzaguza/src/config"
"github.com/tminaorg/brzaguza/src/engines"
"github.com/tminaorg/brzaguza/src/search"
"github.com/tminaorg/brzaguza/src/engines/_engines_test"
)

type TestCase struct {
query string
options engines.Options
}

func TestSearch(t *testing.T) {
engineName := engines.Brave

// testing config
conf := config.Config{
Engines: map[string]config.Engine{
engineName.ToLower(): {
Enabled: true,
},
conf := _engines_test.NewConfig(engineName)

// test cases
tchar := [...]_engines_test.TestCaseHasAnyResults{{
Query: "ping",
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}
}}

// enabled engines names
config.EnabledEngines = append(config.EnabledEngines, engineName)
tccr := [...]_engines_test.TestCaseContainsResults{{
Query: "facebook",
ResultURL: []string{"facebook.com"},
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}}

// test cases
testCases := [...]TestCase{{
query: "wikipedia",
options: engines.Options{
tcrr := [...]_engines_test.TestCaseRankedResults{{
Query: "wikipedia",
ResultURL: []string{"wikipedia."},
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}}

// running tests
for _, tc := range testCases {
if results := search.PerformSearch(tc.query, tc.options, &conf); len(results) == 0 {
t.Errorf("Got no results for %v", tc.query)
}
}
_engines_test.CheckTestCases(tchar[:], tccr[:], tcrr[:], t, conf)
}
48 changes: 23 additions & 25 deletions src/engines/duckduckgo/duckduckgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,42 @@ package duckduckgo_test
import (
"testing"

"github.com/tminaorg/brzaguza/src/config"
"github.com/tminaorg/brzaguza/src/engines"
"github.com/tminaorg/brzaguza/src/search"
"github.com/tminaorg/brzaguza/src/engines/_engines_test"
)

type TestCase struct {
query string
options engines.Options
}

func TestSearch(t *testing.T) {
engineName := engines.DuckDuckGo

// testing config
conf := config.Config{
Engines: map[string]config.Engine{
engineName.ToLower(): {
Enabled: true,
},
conf := _engines_test.NewConfig(engineName)

// test cases
tchar := [...]_engines_test.TestCaseHasAnyResults{{
Query: "ping",
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}
}}

// enabled engines names
config.EnabledEngines = append(config.EnabledEngines, engineName)
tccr := [...]_engines_test.TestCaseContainsResults{{
Query: "facebook",
ResultURL: []string{"facebook.com"},
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}}

// test cases
testCases := [...]TestCase{{
query: "wikipedia",
options: engines.Options{
tcrr := [...]_engines_test.TestCaseRankedResults{{
Query: "wikipedia",
ResultURL: []string{"wikipedia."},
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}}

// running tests
for _, tc := range testCases {
if results := search.PerformSearch(tc.query, tc.options, &conf); len(results) == 0 {
t.Errorf("Got no results for %v", tc.query)
}
}
_engines_test.CheckTestCases(tchar[:], tccr[:], tcrr[:], t, conf)
}
48 changes: 23 additions & 25 deletions src/engines/etools/etools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,42 @@ package etools_test
import (
"testing"

"github.com/tminaorg/brzaguza/src/config"
"github.com/tminaorg/brzaguza/src/engines"
"github.com/tminaorg/brzaguza/src/search"
"github.com/tminaorg/brzaguza/src/engines/_engines_test"
)

type TestCase struct {
query string
options engines.Options
}

func TestSearch(t *testing.T) {
engineName := engines.Etools

// testing config
conf := config.Config{
Engines: map[string]config.Engine{
engineName.ToLower(): {
Enabled: true,
},
conf := _engines_test.NewConfig(engineName)

// test cases
tchar := [...]_engines_test.TestCaseHasAnyResults{{
Query: "ping",
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}
}}

// enabled engines names
config.EnabledEngines = append(config.EnabledEngines, engineName)
tccr := [...]_engines_test.TestCaseContainsResults{{
Query: "facebook",
ResultURL: []string{"facebook.com"},
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}}

// test cases
testCases := [...]TestCase{{
query: "wikipedia",
options: engines.Options{
tcrr := [...]_engines_test.TestCaseRankedResults{{
Query: "wikipedia",
ResultURL: []string{"wikipedia."},
Options: engines.Options{
MaxPages: 1,
VisitPages: false,
},
}}

// running tests
for _, tc := range testCases {
if results := search.PerformSearch(tc.query, tc.options, &conf); len(results) == 0 {
t.Errorf("Got no results for %v", tc.query)
}
}
_engines_test.CheckTestCases(tchar[:], tccr[:], tcrr[:], t, conf)
}
Loading

0 comments on commit bcc5352

Please sign in to comment.