<commit_before>package watch

import (
	"github.com/foomo/petze/config"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

type validationCheck struct {
	errorType ErrorType
	length    int
	message   string
}

func TestValidateDurationError(t *testing.T) {
	ctx := &CheckContext{
		duration: 200 * time.Millisecond,
		check:    config.Check{Duration: 100 * time.Millisecond},
	}

	errs := ValidateDuration(ctx)
	if len(errs) != 1 || errs[0].Type != ErrorTypeServerTooSlow {
		t.Fail()
	}
}

func TestValidateContentTypeError(t *testing.T) {
	resp := httptest.NewRecorder()
	resp.Header().Set("Content-Type", "application/xml")
	ctx := &CheckContext{
		response: resp.Result(),
		check:    config.Check{ContentType: "application/json"},
	}

	errs := ValidateContentType(ctx)
	if len(errs) != 1 || errs[0].Type != ErrorTypeUnexpectedContentType {
		t.Fail()
	}
}

func TestValidateGoQueryBadResponseBody(t *testing.T) {
	resp := httptest.NewRecorder()

	ctx := &CheckContext{
		response: resp.Result(),
		check: config.Check{
			Goquery: map[string]config.Expect{},
		},
	}

	errs := ValidateGoQuery(ctx)
	if len(errs) != 1 || errs[0].Type != ErrorTypeGoQuery {
		t.Fail()
	}
}





var validateJsonPathTests = []struct {
	in  *CheckContext
	out validationCheck
}{
	{&CheckContext{
		response: createResponse("{\"hello\":\"world\"}", "application/json"),
		check: config.Check{Data: map[string]config.Expect{"$.hello": {Equals: "world"}}},
	}, validationCheck{"", 0, "failed valid jquery path"}},
}

func createResponse(data, contentType string) *http.Response {
	resp := httptest.NewRecorder()
	resp.HeaderMap.Set("Content-Type", contentType)
	resp.Body.Write([]byte(data))
	return resp.Result()
}

func TestValidateJsonPath(t *testing.T) {
	for _, test := range validateJsonPathTests {
		errs := ValidateJsonPath(test.in)
		if len(errs) != test.out.length || errs[0].Type != test.out.errorType {
			t.Error(test.out.message)
		}
	}
}<commit_msg>Add tests for validator service<commit_after>package watch

import (
	"github.com/foomo/petze/config"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

type validationCheck struct {
	errorType ErrorType
	length    int
	message   string
}

func TestValidateDurationError(t *testing.T) {
	ctx := &CheckContext{
		duration: 200 * time.Millisecond,
		check:    config.Check{Duration: 100 * time.Millisecond},
	}

	errs := ValidateDuration(ctx)
	if len(errs) != 1 || errs[0].Type != ErrorTypeServerTooSlow {
		t.Fail()
	}
}

func TestValidateContentTypeError(t *testing.T) {
	resp := httptest.NewRecorder()
	resp.Header().Set("Content-Type", "application/xml")
	ctx := &CheckContext{
		response: resp.Result(),
		check:    config.Check{ContentType: "application/json"},
	}

	errs := ValidateContentType(ctx)
	if len(errs) != 1 || errs[0].Type != ErrorTypeUnexpectedContentType {
		t.Fail()
	}
}

func TestValidateGoQueryBadResponseBody(t *testing.T) {
	resp := httptest.NewRecorder()

	ctx := &CheckContext{
		response: resp.Result(),
		check: config.Check{
			Goquery: map[string]config.Expect{},
		},
	}

	errs := ValidateGoQuery(ctx)
	if len(errs) != 1 || errs[0].Type != ErrorTypeGoQuery {
		t.Fail()
	}
}

var validateJsonPathTests = []struct {
	in  *CheckContext
	out validationCheck
}{
	{&CheckContext{
		response: createResponse(`{"hello":"world"}`, "application/json"),
		check:    config.Check{Data: map[string]config.Expect{"$.hello+": {Equals: "world"}}},
	}, validationCheck{"", 0, "failed valid jquery path"}},
	{&CheckContext{
		response: createResponse(`{"hello":"world"}`, "application/json"),
		check:    config.Check{Data: map[string]config.Expect{"$.nonexist+": {Equals: "world"}}},
	}, validationCheck{ErrorJsonPath, 1, "failed non-existing selector"}},
	{&CheckContext{
		response: createResponse(`{"hello": ["one","two"]}`, "application/json"),
		check:    config.Check{Data: map[string]config.Expect{"$.hello+": {Min: &[]int64{3}[0]}}},
	}, validationCheck{ErrorJsonPath, 1, "failed failed minimum selection"}},
}

func createResponse(data, contentType string) *http.Response {
	resp := httptest.NewRecorder()
	resp.HeaderMap.Set("Content-Type", contentType)
	resp.Body.Write([]byte(data))
	return resp.Result()
}

func TestValidateJsonPath(t *testing.T) {
	for _, test := range validateJsonPathTests {
		errs := ValidateJsonPath(test.in)
		if len(errs) != test.out.length || (len(errs) > 0 && errs[0].Type != test.out.errorType) {
			t.Error(test.out.message)
		}
	}
}
{'fraction_non_alphanumeric': 0.1695182350292661, 'fraction_numerical': 0.007879333633498424, 'mean_word_length': 4.916111850865513, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4298125', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1556, 'n_words': 294}