func formTestMartini() *martini.Martini {
	config := configuration.ReadTestConfigInRecursive()
	mainDb := db.NewMainDb(config.Main.Database.ConnString, config.Main.Database.Name)
	m := web.NewSessionAuthorisationHandler(mainDb)
	m.Use(render.Renderer(render.Options{
		Directory:  "templates/auth",
		Extensions: []string{".tmpl", ".html"},
		Charset:    "UTF-8",
	}))
	router := martini.NewRouter()

	router.Get("/", func(r render.Render) {
		r.HTML(200, "login", nil)
	})
	router.Group("/test", func(r martini.Router) {
		r.Get("/:id", func(ren render.Render, prms martini.Params) {
			id := prms["id"]
			ren.JSON(200, map[string]interface{}{"id": id})
		})
		r.Get("/new", func(ren render.Render) {
			ren.JSON(200, nil)
		})
		r.Get("/update/:id", func(ren render.Render, prms martini.Params) {
			id := prms["id"]
			ren.JSON(200, map[string]interface{}{"id": id})
		})
		r.Get("/delete/:id", func(ren render.Render, prms martini.Params) {
			id := prms["id"]
			ren.JSON(200, map[string]interface{}{"id": id})
		})
	})
	m.Action(router.Handle)
	return m
}
func TestWum(t *testing.T) {
	conf := configuration.ReadTestConfigInRecursive()
	db := db.NewMainDb(conf.Main.Database.ConnString, conf.Main.Database.Name)
	wum := NewWatchManager(db, "test")
	go wum.WatchUnreadMessages()

	for i, el := range []string{"one", "two", "three"} {
		wum.AddConfiguration(el, "test", "key", 2)
		if len(wum.Configs) != i+1 {
			t.Error("bad configs count")
		}
		time.Sleep(10)
	}
}
func TestRequest(t *testing.T) {
	conf := c.ReadTestConfigInRecursive()
	tconf := conf.Taxis["fake"]
	inf := initInfinity(tconf.Api, "fake")
	mrkps := inf.Markups()
	log.Printf("mrkps before session: %v", mrkps)

	inf.LoginResponse.SessionID = "foo"
	new_mrkps := inf.Markups()
	log.Printf("mrkps after session: %v", mrkps)

	if !testEq(mrkps, new_mrkps) {
		t.Error("session must refresh")
	}
}
func TestAutoAnswersStoring(t *testing.T) {
	log.Printf("start tes....")
	config := configuration.ReadTestConfigInRecursive()
	store := NewMainDb(config.Main.Database.ConnString, config.Main.Database.Name)
	store.Messages.MessagesCollection.RemoveAll(bson.M{})

	store.Messages.StoreMessage("from", "to", "test", "testMessageId")
	store.Messages.StoreMessage("from", "to", "test1", "testMessageId1")
	messages, _ := store.Messages.GetMessagesForAutoAnswer("to", 0)
	log.Printf("messages: %+v", messages)
	if len(messages) != 2 {
		t.Errorf("I must see 2 messages to auto answer 0 but %v", len(messages))
	}

	messages, _ = store.Messages.GetMessagesForAutoAnswer("to", 1)
	log.Printf("messages1: %+v", messages)
	if len(messages) != 0 {
		t.Errorf("I must see 0 messages to auto answer 1 but %v", len(messages))
	}
	time.Sleep(time.Minute)

	messages, _ = store.Messages.GetMessagesForAutoAnswer("to", 0)
	log.Printf("messages: %+v", messages)
	if len(messages) != 2 {
		t.Errorf("I must see 2 messages to auto answer 0 after sleep but %v", len(messages))
	}

	messages, _ = store.Messages.GetMessagesForAutoAnswer("to", 1)
	log.Printf("messages1after sleep: %+v", messages)
	if len(messages) != 2 {
		t.Errorf("I must see 2 messages to auto answer 1 after sleep but %v", len(messages))
	}

	store.Messages.SetMessagesAutoAnswered("from", "to", 0)
	messages, _ = store.Messages.GetMessagesForAutoAnswer("to", 0)
	log.Printf("messages after auto aswer 0: %+v", messages)
	if len(messages) != 0 {
		t.Errorf("I must see 2 messages to auto answer 0 after sleep and set auto answered but %v", len(messages))
	}
	store.Messages.SetMessagesAutoAnswered("from", "to", 1)
	messages, _ = store.Messages.GetMessagesForAutoAnswer("to", 1)
	log.Printf("messages1after sleep and answer 0: %+v", messages)
	if len(messages) != 0 {
		t.Errorf("I must see 2 messages to auto answer 1 after sleep and set auto answered but %v", len(messages))
	}

}
func main() {
	config := configuration.ReadTestConfigInRecursive()
	mainDb := db.NewMainDb(config.Main.Database.ConnString, config.Main.Database.Name)
	web.TestRun(mainDb)
}
package voting

import (
	"testing"
	"msngr/configuration"
	"log"
	"gopkg.in/mgo.v2/bson"
	"msngr/test"
	"msngr/utils"
)

var config = configuration.ReadTestConfigInRecursive()

func clear(vdh *VotingDataHandler) {
	vdh.Companies.RemoveAll(bson.M{})
}

func PrepObj() *VotingDataHandler {
	vdh, err := NewVotingHandler(config.Main.Database.ConnString, config.Main.Database.Name)
	if err != nil {
		log.Panic(err)
	}
	clear(vdh)
	return vdh
}

func TestVoteCompany(t *testing.T) {
	vdh := PrepObj()
	err := vdh.ConsiderCompany("test company", "NSK", "test", "", "test_user", "test_role")
	if err != nil {
		t.Errorf("first add cmp %v", err)
func TestTaxiInfinityFail(t *testing.T) {
	conf := c.ReadTestConfigInRecursive()
	db := d.NewMainDb(conf.Main.Database.ConnString, conf.Main.Database.Name)
	taxi_conf := conf.Taxis["fake"]

	t.Logf("taxi api configuration for %+v:\n%v", taxi_conf.Name, taxi_conf.Api)
	external := i.GetTestInfAPI(taxi_conf.Api)

	external_api := external.(taxi.TaxiInterface)
	external_address_supplier := external.(taxi.AddressSupplier)

	apiMixin := taxi.ExternalApiMixin{API: external_api}

	carsCache := taxi.NewCarsCache(external_api)
	notifier := n.NewNotifier(conf.Main.CallbackAddr, taxi_conf.Chat.Key, db)

	address_handler, address_supplier := GetAddressInstruments(conf, taxi_conf.Name, external_address_supplier)
	configStore := d.NewConfigurationStorage(conf.Main.ConfigDatabase)
	botContext := taxi.FormTaxiBotContext(&apiMixin, db, configStore, taxi_conf, address_handler, carsCache)
	controller := m.FormBotController(botContext, db)

	t.Logf("Was create bot context: %+v\n", botContext)
	http.HandleFunc(fmt.Sprintf("/taxi/%v", taxi_conf.Name), controller)

	go func() {
		taxiContext := taxi.TaxiContext{API: external_api, DataBase: db, Cars: carsCache, Notifier: notifier}
		t.Logf("Will start order watcher for [%v]", botContext.Name)
		taxi.TaxiOrderWatch(&taxiContext, botContext)
	}()

	http.HandleFunc(fmt.Sprintf("/taxi/%v/streets", taxi_conf.Name), func(w http.ResponseWriter, r *http.Request) {
		geo.StreetsSearchController(w, r, address_supplier)
	})

	go func() {
		server_address := fmt.Sprintf(":%v", conf.Main.Port)
		t.Logf("\nStart listen and serving at: %v\n", server_address)
		server := &http.Server{
			Addr: server_address,
		}
		server.ListenAndServe()
	}()

	u.After(external_api.IsConnected, func() {
		inf_api := external.(*i.InfinityAPI)
		for i, _ := range inf_api.ConnStrings {
			inf_api.ConnStrings[i] += "w"
		}
	})

	_, err := u.GET(taxi_conf.DictUrl, &map[string]string{"q": "лесос"})
	if err != nil {
		t.Error("Error at getting street %v", err)
	}

	out_res, err := tu.POST(fmt.Sprintf("http://localhost:%v/taxi/%v", conf.Main.Port, taxi_conf.Name), &structs.InPkg{
		Message: &structs.InMessage{
			ID: u.GenStringId(),
			Commands: &[]structs.InCommand{
				structs.InCommand{Action: "information", Title: "information"},
			},
		},
		From:     "test user",
		UserData: &structs.InUserData{Name: "TEST"},
	})

	if err != nil {
		t.Errorf("Error at unmarshal result info %v", err)
	}

	if out_res == nil {
		t.Error("Out info result is nil!")
	} else {
		if out_res.Message.Type != "chat" {
			t.Errorf("Out message type != chat, but == %v", out_res.Message.Type)
		}
		ch, _ := configStore.GetChatConfig(taxi_conf.Chat.CompanyId)

		if out_res.Message.Body != ch.Information {
			t.Errorf("Out message body != info in config, but == %v", out_res.Message.Body)
		}
	}

}