<commit_before>package controllers_test

import (
	"github.com/yang-f/beauty/db"
	"github.com/yang-f/beauty/router"
	"github.com/yang-f/beauty/settings"
	"net/http"
	"net/http/httptest"
	"testing"
)

func init() {
	db.Query("drop database beauty_test")
	db.Query("create database beauty_test")
	settings.Local["mysql_database"] = "beauty_test"
	db.Query(`create table if not exists user
        (
            user_id int primary key not null  auto_increment,
            user_name varchar(64),
            user_pass varchar(64),
            user_mobile varchar(32),
            user_type enum('user', 'admin', 'test') not null,
            add_time timestamp not null default CURRENT_TIMESTAMP
        )`)
	db.Query(`insert into user (user_name, user_pass, user_mobile, add_time) values('test', 'test', '12345678901', '2017-01-12 12:15:38')`)
}

func TestConfig(t *testing.T) {
	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatal(err)
	}
	rr := httptest.NewRecorder()

	router := router.NewRouter()
	router.ServeHTTP(rr, req)
	if status := rr.Code; status != http.StatusOK {
		t.Errorf("handler returned wrong status code: got %v want %v",
			status, http.StatusOK)
	}
	expected := `{"description":"this is json"}`
	if rr.Body.String() != expected {
		t.Errorf("handler returned unexpected body: got %v want %v",
			rr.Body.String(), expected)
	}

}

func Benchmark_config(b *testing.B) {
	for i := 0; i < b.N; i++ {
		req, err := http.NewRequest("GET", "/", nil)
		if err != nil {
			b.Fatal(err)
		}
		rr := httptest.NewRecorder()

		router := router.NewRouter()
		router.ServeHTTP(rr, req)<commit_msg>controller test<commit_after>package controllers_test

import (
	"github.com/yang-f/beauty/db"
	"github.com/yang-f/beauty/router"
	"github.com/yang-f/beauty/settings"
	"net/http"
	"net/http/httptest"
	"testing"
)

func init() {
	db.Query("drop database beauty_test")
	db.Query("create database beauty_test")
	settings.Local["mysql_database"] = "beauty_test"
	db.Query(`create table if not exists user
        (
            user_id int primary key not null  auto_increment,
            user_name varchar(64),
            user_pass varchar(64),
            user_mobile varchar(32),
            user_type enum('user', 'admin', 'test') not null,
            add_time timestamp not null default CURRENT_TIMESTAMP
        )`)
	db.Query(`insert into user (user_name, user_pass, user_mobile, add_time) values('test', 'test', '<PASSWORD>', '2017-01-12 12:15:38')`)
}

type tester struct {
	Pattern  string
	Status   int
	Expected string
	Method   string
}

func TestConfig(t *testing.T) {
	var tests = []tester{
		{"/", 200, `{"description":"this is json"}`, "GET"},
		{"/demo1", 403, `{"status":403,"description":"token not found.","code":"AUTH_FAILED"}`, "GET"},
		{"/demo2", 200, `{"description":"this is json"}`, "GET"},
		{"/demo3", 403, `{"status":403,"description":"token not found.","code":"AUTH_FAILED"}`, "GET"},
	}

	for _, test := range tests {
		req, err := http.NewRequest(test.Method, test.Pattern, nil)
		if err != nil {
			t.Fatal(err)
		}
		rr := httptest.NewRecorder()

		router := router.NewRouter()
		router.ServeHTTP(rr, req)
		if status := rr.Code; status != test.Status {
			t.Errorf("handler returned wrong status code: got %v want %v",
				status, http.StatusOK)
		}
		if rr.Body.String() != test.Expected {
			t.Errorf("handler returned unexpected body: got %v want %v",
				rr.Body.String(), test.Expected)
		}
	}
}

func Benchmark_config(b *testing.B) {
	for i := 0; i < b.N; i++ {
		req, err := http.NewRequest("GET", "/", nil)
		if err != nil {
			b.Fatal(err)
		}
		rr := httptest.NewRecorder()

		router := router.NewRouter()
		router.ServeHTTP(rr, req)
{'fraction_non_alphanumeric': 0.1647757968665586, 'fraction_numerical': 0.019989195029713667, 'mean_word_length': 3.515853658536585, 'pattern_counts': {'":': 9, '<': 6, '<?xml version=': 0, '>': 4, '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': '3602909', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1300, 'n_words': 308}