<commit_before>package e2e

import (
	"bytes"
	"fmt"
	"net"
	"net/http"
	"net/http/httptest"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/jinzhu/gorm"
	"github.com/oinume/lekcije/server/config"
	interfaces_http "github.com/oinume/lekcije/server/interfaces/http"
	"github.com/oinume/lekcije/server/logger"
	"github.com/oinume/lekcije/server/model"
	"github.com/sclevine/agouti"
	"go.uber.org/zap/zapcore"
)

var server *httptest.Server
var client = http.DefaultClient
var db *gorm.DB
var helper = model.NewTestHelper()

func TestMain(m *testing.M) {
	config.MustProcessDefault()
	db = helper.DB()
	if err := os.Setenv("MYSQL_DATABASE", "lekcije_test"); err != nil {
		// TODO: Not use panic
		panic(err)
	}

	var accessLogBuffer, appLogBuffer bytes.Buffer
	logger.InitializeAccessLogger(&accessLogBuffer)
	appLogLevel := zapcore.InfoLevel
	if level := os.Getenv("LOG_LEVEL"); level != "" {
		appLogLevel = logger.NewLevel(level)
	}
	logger.InitializeAppLogger(&appLogBuffer, appLogLevel)

	helper.TruncateAllTables(helper.DB())

	port := config.DefaultVars.HTTPPort
	routes := interfaces_http.CreateMux(nil) // TODO: grpc-gateway
	port += 1
	server = newTestServer(routes, port)
	fmt.Printf("Test HTTP server created: port=%d, url=%s\n", port, server.URL)
	defer server.Close()

	client.Timeout = 5 * time.Second
	os.Chdir("../")
	status := m.Run()
	defer os.Exit(status)
}

// newTestServer returns a new test Server with fixed port number.
func newTestServer(handler http.Handler, port int) *httptest.Server {
	listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))<commit_msg>Fix test failure<commit_after>package e2e

import (
	"bytes"
	"fmt"
	"net"
	"net/http"
	"net/http/httptest"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/jinzhu/gorm"
	"github.com/oinume/lekcije/server/config"
	interfaces_http "github.com/oinume/lekcije/server/interfaces/http"
	"github.com/oinume/lekcije/server/logger"
	"github.com/oinume/lekcije/server/model"
	"github.com/sclevine/agouti"
	"go.uber.org/zap/zapcore"
)

var server *httptest.Server
var client = http.DefaultClient
var db *gorm.DB
var helper = model.NewTestHelper()

func TestMain(m *testing.M) {
	config.MustProcessDefault()
	db = helper.DB()
	if err := os.Setenv("MYSQL_DATABASE", "lekcije_test"); err != nil {
		// TODO: Not use panic
		panic(err)
	}

	var accessLogBuffer, appLogBuffer bytes.Buffer
	logger.InitializeAccessLogger(&accessLogBuffer)
	appLogLevel := zapcore.InfoLevel
	if level := os.Getenv("LOG_LEVEL"); level != "" {
		appLogLevel = logger.NewLevel(level)
	}
	logger.InitializeAppLogger(&appLogBuffer, appLogLevel)

	helper.TruncateAllTables(helper.DB())

	port := config.DefaultVars.HTTPPort
	s := interfaces_http.NewServer()
	routes := interfaces_http.CreateMux(s, nil) // TODO: grpc-gateway
	port += 1
	server = newTestServer(routes, port)
	fmt.Printf("Test HTTP server created: port=%d, url=%s\n", port, server.URL)
	defer server.Close()

	client.Timeout = 5 * time.Second
	os.Chdir("../")
	status := m.Run()
	defer os.Exit(status)
}

// newTestServer returns a new test Server with fixed port number.
func newTestServer(handler http.Handler, port int) *httptest.Server {
	listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
{'fraction_non_alphanumeric': 0.14956736711990112, 'fraction_numerical': 0.005562422744128554, 'mean_word_length': 4.74955595026643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '57701', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1221, 'n_words': 234}