<commit_before>package main

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/joshsoftware/curem/config"
)

func TestGetContactsHandler(t *testing.T) {
	ts := httptest.NewServer(r)
	defer ts.Close()
	c, err := NewContact(
		"Encom Inc.",
		"Flynn",
		"<EMAIL>",
		"",
		"",
		"USA",
	)
	if err != nil {
		t.Errorf("%s", err)
	}
	x := []contact{*c}
	y, err := json.Marshal(x)
	resp, err := http.Get(ts.URL + "/contacts")
	if err != nil {
		t.Errorf("%s", err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("%s", err)
	}
	bodystring := strings.TrimSpace(string(body))
	if bodystring != string(y) {
		t.Errorf("expected %s, but got %s", string(y), bodystring)
	}
	err = config.ContactsCollection.DropCollection()
	if err != nil {
		t.Errorf("%s", err)
	}
}

func TestGetContactHandler(t *testing.T) {
	ts := httptest.NewServer(r)
	defer ts.Close()
	c, err := NewContact(
		"Encom Inc.",
		"Flynn",
		"<EMAIL>",<commit_msg>Add test for postContactsHandler<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/joshsoftware/curem/config"
)

func TestGetContactsHandler(t *testing.T) {
	ts := httptest.NewServer(r)
	defer ts.Close()
	c, err := NewContact(
		"Encom Inc.",
		"Flynn",
		"<EMAIL>",
		"",
		"",
		"USA",
	)
	if err != nil {
		t.Errorf("%s", err)
	}
	x := []contact{*c}
	y, err := json.Marshal(x)
	resp, err := http.Get(ts.URL + "/contacts")
	if err != nil {
		t.Errorf("%s", err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("%s", err)
	}
	bodystring := strings.TrimSpace(string(body))
	if bodystring != string(y) {
		t.Errorf("expected %s, but got %s", string(y), bodystring)
	}
	err = config.ContactsCollection.DropCollection()
	if err != nil {
		t.Errorf("%s", err)
	}
}

func TestPostContactsHandler(t *testing.T) {
	ts := httptest.NewServer(r)
	defer ts.Close()
	var b bytes.Buffer
	b.Write([]byte(`{"person":"<NAME>","email":"<EMAIL>"}`))
	resp, err := http.Post(ts.URL+"/contacts", "encoding/json", &b)
	if err != nil {
		t.Errorf("%s", err)
	}
	defer resp.Body.Close()
	if resp.StatusCode == 201 {
		x, err := GetAllContacts()
		if err != nil {
			t.Errorf("%s", err)
		}
		if len(x) == 1 {
			if x[0].Person != "<NAME>" {
				t.Errorf("expected person to be <NAME>, but got %s", x[0].Person)
			}
			if x[0].Email != "<EMAIL>" {
				t.Errorf("expected email to be <EMAIL>, but got %s", x[0].Email)
			}
		} else {
			t.Errorf("expected 1 contact, but got %d contacts", len(x))
		}
		err = config.ContactsCollection.DropCollection()
		if err != nil {
			t.Errorf("%s", err)
		}

	} else {
		t.Errorf("expected response code to be 201 but got %d", resp.StatusCode)
	}
}

func TestGetContactHandler(t *testing.T) {
	ts := httptest.NewServer(r)
	defer ts.Close()
	c, err := NewContact(
		"Encom Inc.",
		"Flynn",
		"<EMAIL>",
{'fraction_non_alphanumeric': 0.2008704385671242, 'fraction_numerical': 0.004017408771342484, 'mean_word_length': 3.618238021638331, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 13, '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': '6916709', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1225, 'n_words': 265}