Files
hireme/pkg/crawler/snowflake/snowflake_test.go
T

98 lines
2.5 KiB
Go

package snowflake
import (
"bytes"
"context"
"io"
"net/http"
"strings"
"testing"
)
type roundTripFunc func(req *http.Request) *http.Response
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
func TestSnowflakeCrawler_DiscoverAndFetch(t *testing.T) {
mockClient := &http.Client{
Transport: roundTripFunc(func(r *http.Request) *http.Response {
if strings.HasSuffix(r.URL.Path, "/jobs/4192301") {
body := `{
"id": 4192301,
"title": "Principal Software Engineer - Core Database Engine",
"content": "<p>Snowflake is the Data Cloud.</p>",
"location": {
"name": "Bellevue, WA"
}
}`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(body)),
Header: make(http.Header),
}
}
if strings.HasSuffix(r.URL.Path, "/jobs") {
body := `{
"jobs": [
{
"id": 4192301,
"title": "Principal Software Engineer - Core Database Engine",
"absolute_url": "https://boards.greenhouse.io/snowflake/jobs/4192301",
"location": {
"name": "Bellevue, WA"
}
}
]
}`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(body)),
Header: make(http.Header),
}
}
return &http.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(bytes.NewBufferString("not found")),
Header: make(http.Header),
}
}),
}
c := NewSnowflakeCrawlerWithClient(mockClient)
if !c.CanHandle("snowflake") {
t.Errorf("CanHandle should support snowflake")
}
discovered, err := c.DiscoverJobs(context.Background(), "https://boards-api.greenhouse.io/v1/boards/snowflake/jobs")
if err != nil {
t.Fatalf("DiscoverJobs failed: %v", err)
}
if len(discovered) != 1 {
t.Fatalf("expected 1 discovered job, got %d", len(discovered))
}
if discovered[0].CanonicalID != "snowflake:4192301" {
t.Errorf("unexpected canonical ID: %s", discovered[0].CanonicalID)
}
extracted, err := c.FetchJobDetail(context.Background(), "https://boards.greenhouse.io/snowflake/jobs/4192301")
if err != nil {
t.Fatalf("FetchJobDetail failed: %v", err)
}
if extracted.Title != "Principal Software Engineer - Core Database Engine" {
t.Errorf("unexpected title: %s", extracted.Title)
}
if extracted.Company != "Snowflake" {
t.Errorf("unexpected company: %s", extracted.Company)
}
if extracted.Location != "Bellevue, WA" {
t.Errorf("unexpected location: %s", extracted.Location)
}
}