<commit_before>package config

import (
	"errors"
	"testing"
)

func TestLoadFromFile(t *testing.T) {
	var conf Configuration
	err := conf.LoadFromFile("testdata/config.yml")
	if err != nil {
		t.Fatalf("LoadFromFile() failed: %s", err.Error())
	}

	err = conf.LoadFromFile("testdata/config.yml-NONEXISTING")
	if err == nil {
		t.Fatalf("LoadFromFile() did not fail for nonexisting file")
	}

	err = conf.LoadFromFile("testdata/malformed-config.yml")
	if err == nil {
		t.Fatalf("LoadFromFile() did not fail for malformed input")
	}
}

func TestSetDefaults(t *testing.T) {
	var conf Configuration
	conf.SetDefaults()
	if conf.Bind != ":4934" {
		t.Fatalf("Wrong default")
	}

	if !conf.HTTP.TLS {
		t.Fatalf("Configuration should use TLS as default.")
	}

	if conf.DataDir == "" {
		t.Fatalf("No default datadir")
	}
}

type writer struct {
	saves int
	err   error
}

func (w *writer) Save(data interface{}) error {
	w.saves++

	return w.err
}

func (w *writer) Delete(data interface{}) error {
	return w.err
}

func TestSaveChecks(t *testing.T) {
	w := &writer{}

	var conf Configuration
	conf.LoadFromFile("testdata/checks.yml")

	err := conf.SaveChecks(w)
	if err != nil {
		t.Errorf("SaveChecks() returned an error: %s", err.Error())
	}

	if w.saves != 1 {
		t.Errorf("Something went wrong, %d checks saved", w.saves)
	}

	w.err = errors.New("error")
	err = conf.SaveChecks(w)
	if err == nil {
		t.Errorf("SaveChecks() did not return an error")
	}
}

func TestSaveHosts(t *testing.T) {
	w := &writer{}

	var conf Configuration
	conf.LoadFromFile("testdata/hosts.yml")

	err := conf.SaveHosts(w)
	if err != nil {
		t.Errorf("SaveChecks() returned an error: %s", err.Error())
	}

	if w.saves != 1 {
		t.Errorf("Something went wrong, %d hosts saved", w.saves)
	}

	w.err = errors.New("error")
	err = conf.SaveHosts(w)
	if err == nil {
		t.Errorf("SaveHosts() did not return an error")
	}
}<commit_msg>Added a few tests.<commit_after>package config

import (
	"errors"
	"testing"
)

func TestLoadFromFile(t *testing.T) {
	var conf Configuration
	err := conf.LoadFromFile("testdata/config.yml")
	if err != nil {
		t.Fatalf("LoadFromFile() failed: %s", err.Error())
	}

	err = conf.LoadFromFile("testdata/config.yml-NONEXISTING")
	if err == nil {
		t.Fatalf("LoadFromFile() did not fail for nonexisting file")
	}

	err = conf.LoadFromFile("testdata/malformed-config.yml")
	if err == nil {
		t.Fatalf("LoadFromFile() did not fail for malformed input")
	}
}

func TestSetDefaults(t *testing.T) {
	var conf Configuration
	conf.SetDefaults()
	if conf.Bind != ":4934" {
		t.Fatalf("Wrong default")
	}

	if !conf.HTTP.TLS {
		t.Fatalf("Configuration should use TLS as default.")
	}

	if conf.DataDir == "" {
		t.Fatalf("No default datadir")
	}
}

type writer struct {
	saves int
	err   error
}

func (w *writer) Save(data interface{}) error {
	w.saves++

	return w.err
}

func (w *writer) Delete(data interface{}) error {
	return w.err
}

func TestSaveChecks(t *testing.T) {
	w := &writer{}

	var conf Configuration
	conf.SetDefaults()
	conf.LoadFromFile("testdata/checks.yml")

	err := conf.SaveChecks(w)
	if err != nil {
		t.Errorf("SaveChecks() returned an error: %s", err.Error())
	}

	if w.saves != 1 {
		t.Errorf("Something went wrong, %d checks saved", w.saves)
	}

	w.err = errors.New("error")
	err = conf.SaveChecks(w)
	if err == nil {
		t.Errorf("SaveChecks() did not return an error")
	}
}

func TestSaveHosts(t *testing.T) {
	w := &writer{}

	var conf Configuration
	conf.SetDefaults()
	conf.LoadFromFile("testdata/hosts.yml")

	err := conf.SaveHosts(w)
	if err != nil {
		t.Errorf("SaveHosts() returned an error: %s", err.Error())
	}

	if w.saves != 1 {
		t.Errorf("Something went wrong, %d hosts saved", w.saves)
	}

	w.err = errors.New("error")
	err = conf.SaveHosts(w)
	if err == nil {
		t.Errorf("SaveHosts() did not return an error")
	}
}

func TestSaveContactGroups(t *testing.T) {
	w := &writer{}

	var conf Configuration
	conf.SetDefaults()
	conf.LoadFromFile("testdata/contactgroups.yml")

	err := conf.SaveContactGroups(w)
	if err != nil {
		t.Errorf("SaveContactGroups() returned an error: %s", err.Error())
	}

	if w.saves != 1 {
		t.Errorf("Something went wrong, %d contactgroups saved", w.saves)
	}

	w.err = errors.New("error")
	err = conf.SaveContactGroups(w)
	if err == nil {
		t.Errorf("SaveContactGroups() did not return an error")
	}
}

func TestSaveContacts(t *testing.T) {
	w := &writer{}

	var conf Configuration
	conf.SetDefaults()
	conf.LoadFromFile("testdata/contacts.yml")

	err := conf.SaveContacts(w)
	if err != nil {
		t.Errorf("SaveContacts() returned an error: %s", err.Error())
	}

	if w.saves != 1 {
		t.Errorf("Something went wrong, %d contacts saved", w.saves)
	}

	w.err = errors.New("error")
	err = conf.SaveContacts(w)
	if err == nil {
		t.Errorf("SaveContacts() did not return an error")
	}
}
{'fraction_non_alphanumeric': 0.15939632003307835, 'fraction_numerical': 0.002894356005788712, 'mean_word_length': 3.862311557788945, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5777965', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1799, 'n_words': 451}