<commit_before>package rainforest

import (
	"bytes"
	"fmt"
	"net/http"
	"reflect"
	"strconv"
	"strings"
	"testing"
	"time"
)

func TestGetGenerators(t *testing.T) {
	setup()
	defer cleanup()

	const reqMethod = "GET"

	mux.HandleFunc("/generators", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != reqMethod {
			t.Errorf("Request method = %v, want %v", r.Method, reqMethod)
		}
		fmt.Fprint(w, `[{"id":1337,"created_at":"2016-11-24T14:56:19Z","name":"foo","description":`+
			`"bar","generator_type":"tabular","single_use":false,"columns":[{"id":30225,"created_at":"2016-11-24T14:56:19Z","name":"username"}`+
			`,{"id":30226,"created_at":"2016-11-24T14:56:19Z","name":"password"}],"row_count":42}]`)
	})

	out, _ := client.GetGenerators()

	want := []Generator{{
		ID:           1337,
		Name:         "foo",
		CreationDate: time.Date(2016, time.November, 24, 14, 56, 19, 0, time.UTC),
		Description:  "bar",
		Type:         "tabular",
		SingleUse:    false,
		RowCount:     42,
		Columns: []GeneratorColumn{
			{
				ID:           30225,
				Name:         "username",
				CreationDate: time.Date(2016, time.November, 24, 14, 56, 19, 0, time.UTC),
			},
			{
				ID:           30226,
				Name:         "password",
				CreationDate: time.Date(2016, time.November, 24, 14, 56, 19, 0, time.UTC),
			},
		},
	}}

	if !reflect.DeepEqual(out, want) {
		t.Errorf("Response out = %v, want %v", out, want)
	}
}

func TestDeleteGenerator(t *testing.T) {
	setup()
	defer cleanup()

	const reqMethod = "DELETE"
	const genID = 123

	mux.HandleFunc("/generators/", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != reqMethod {
			t.Errorf("Request method = %v, want %v", r.Method, reqMethod)
		}
		if !strings.Contains(r.URL.String(), strconv.Itoa(genID)) {
			t.Errorf("Expected genID %v in URL, got %v", genID, r.URL.String())
		}

		// we don't care about reply
		fmt.Fprint(w, `{"foo": "bar"}`)
	})

	err := client.DeleteGenerator(genID)
	if err != nil {
		t.Errorf("Got error: %v", err.Error())
	}
}

func TestCreateTabularVar(t *testing.T) {
	setup()
	defer cleanup()

	const reqMethod = "POST"

	mux.HandleFunc("/generators", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != reqMethod {
			t.Errorf("Request method = %v, want %v", r.Method, reqMethod)
		}

		buf := new(bytes.Buffer)
		buf.ReadFrom(r.Body)
		s := strings.TrimSpace(buf.String())
		if wantedBody := `{"name":"foo","description":"bar","single_use":false,"columns":["baz","wut"]}`; s != wantedBody {
			t.Errorf("Request body = %v, want %v", s, wantedBody)
		}
		fmt.Fprint(w, `{"id":1337,"created_at":"2016-11-24T14:56:19Z","name":"foo","description":`+
			`"bar","generator_type":"tabular","single_use":false,"columns":[{"id":30225,"created_at":"2016-11-24T14:56:19Z","name":"baz"}`+
			`,{"id":30226,"created_at":"2016-11-24T14:56:19Z","name":"wut"}],"row_count":0}`)
	})

	newGenerator, _ := client.CreateTabularVar("foo", "bar", []string{"baz", "wut"}, false)
	want := &Generator{<commit_msg>fix unit test in rainforest tabular vars<commit_after>package rainforest

import (
	"bytes"
	"fmt"
	"net/http"
	"reflect"
	"strconv"
	"strings"
	"testing"
	"time"
)

func TestGetGenerators(t *testing.T) {
	setup()
	defer cleanup()

	const reqMethod = "GET"

	mux.HandleFunc("/generators", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != reqMethod {
			t.Errorf("Request method = %v, want %v", r.Method, reqMethod)
		}
		fmt.Fprint(w, `[{"id":1337,"created_at":"2016-11-24T14:56:19Z","name":"foo","description":`+
			`"bar","generator_type":"tabular","single_use":false,"columns":[{"id":30225,"created_at":"2016-11-24T14:56:19Z","name":"username"}`+
			`,{"id":30226,"created_at":"2016-11-24T14:56:19Z","name":"password"}],"row_count":42}]`)
	})

	out, _ := client.GetGenerators()

	want := []Generator{{
		ID:           1337,
		Name:         "foo",
		CreationDate: time.Date(2016, time.November, 24, 14, 56, 19, 0, time.UTC),
		Description:  "bar",
		Type:         "tabular",
		SingleUse:    false,
		RowCount:     42,
		Columns: []GeneratorColumn{
			{
				ID:           30225,
				Name:         "username",
				CreationDate: time.Date(2016, time.November, 24, 14, 56, 19, 0, time.UTC),
			},
			{
				ID:           30226,
				Name:         "password",
				CreationDate: time.Date(2016, time.November, 24, 14, 56, 19, 0, time.UTC),
			},
		},
	}}

	if !reflect.DeepEqual(out, want) {
		t.Errorf("Response out = %v, want %v", out, want)
	}
}

func TestDeleteGenerator(t *testing.T) {
	setup()
	defer cleanup()

	const reqMethod = "DELETE"
	const genID = 123

	mux.HandleFunc("/generators/", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != reqMethod {
			t.Errorf("Request method = %v, want %v", r.Method, reqMethod)
		}
		if !strings.Contains(r.URL.String(), strconv.Itoa(genID)) {
			t.Errorf("Expected genID %v in URL, got %v", genID, r.URL.String())
		}

		// we don't care about reply
		fmt.Fprint(w, `{"foo": "bar"}`)
	})

	err := client.DeleteGenerator(genID)
	if err != nil {
		t.Errorf("Got error: %v", err.Error())
	}
}

func TestCreateTabularVar(t *testing.T) {
	setup()
	defer cleanup()

	const reqMethod = "POST"

	mux.HandleFunc("/generators", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != reqMethod {
			t.Errorf("Request method = %v, want %v", r.Method, reqMethod)
		}

		buf := new(bytes.Buffer)
		buf.ReadFrom(r.Body)
		s := strings.TrimSpace(buf.String())
		if wantedBody := `{"name":"foo","description":"bar","columns":["baz","wut"]}`; s != wantedBody {
			t.Errorf("Request body = %v, want %v", s, wantedBody)
		}
		fmt.Fprint(w, `{"id":1337,"created_at":"2016-11-24T14:56:19Z","name":"foo","description":`+
			`"bar","generator_type":"tabular","single_use":false,"columns":[{"id":30225,"created_at":"2016-11-24T14:56:19Z","name":"baz"}`+
			`,{"id":30226,"created_at":"2016-11-24T14:56:19Z","name":"wut"}],"row_count":0}`)
	})

	newGenerator, _ := client.CreateTabularVar("foo", "bar", []string{"baz", "wut"}, false)
	want := &Generator{
{'fraction_non_alphanumeric': 0.2107190412782956, 'fraction_numerical': 0.05758988015978695, 'mean_word_length': 4.149100257069409, 'pattern_counts': {'":': 65, '<': 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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5084451', 'n_tokens_mistral': 2672, 'n_tokens_neox': 2254, 'n_words': 427}