func TestPhones(t *testing.T) {
	ph, err := NewProfileDbHandler(config.Main.PGDatabase.ConnString)
	tu.CheckErr(t, err, "init")
	deleteAll(ph)
	phone_value := "79811064022"
	phone_value2 := "79138973664"
	p := &Profile{Name: "test phone", ShortDescription: "test phone", TextDescription: "test phone", UserName: "******"}
	p.AllowedPhones = append(p.AllowedPhones, ProfileAllowedPhone{Value: phone_value})

	new_p, err := ph.InsertNewProfile(p)
	tu.CheckErr(t, err, "insert profile with phone")
	if len(new_p.AllowedPhones) != 1 {
		t.Errorf("Except one phone but %v", len(new_p.AllowedPhones))
	}
	if new_p.AllowedPhones[0].Value != phone_value {
		t.Errorf("Except phone number %v but %v", phone_value, new_p.AllowedPhones[0].Value)
	}
	new_p.AllowedPhones = append(new_p.AllowedPhones, ProfileAllowedPhone{Value: phone_value2})
	ph.UpdateProfile(new_p)

	new_p, err = ph.GetProfile("test_phone")
	tu.CheckErr(t, err, "get profile with two phones")

	if len(new_p.AllowedPhones) != 2 {
		t.Errorf("Except two phones but %v", len(new_p.AllowedPhones))
	}
	if new_p.AllowedPhones[1].Value != phone_value2 {
		t.Errorf("Except phone number %v but %v", phone_value2, new_p.AllowedPhones[1].Value)
	}
}
func TestUserVotes(t *testing.T) {
	vdh := PrepObj()
	vdh.ConsiderCompany("abc", "NSK", "abc", "foo", "u1", "")
	vdh.ConsiderCompany("abc1", "NSK", "abc", "foo", "u1", "")
	vdh.ConsiderCompany("abc2", "NSK", "abc", "foo", "u1", "")

	vdh.ConsiderCompany("abc2", "NSK", "abc", "foo", "u2", "")
	vdh.ConsiderCompany("abc3", "NSK", "abc", "foo", "u2", "")
	vdh.ConsiderCompany("abc4", "NSK", "abc", "foo", "u2", "")
	vdh.ConsiderCompany("abc5", "NSK", "abc", "foo", "u2", "")

	cmps, err := vdh.GetUserVotes("u1")
	test.CheckErr(t, err, "get votes error")
	test.CheckCount(cmps, 3, t, "for u1 must be 3 votes")

	vdh.ConsiderCompany("abc2", "NSK", "abc", "foo", "u2", "")
	vdh.ConsiderCompany("abc3", "NSK", "abc", "foo", "u2", "")
	vdh.ConsiderCompany("abc4", "NSK", "abc", "foo", "u2", "")
	vdh.ConsiderCompany("abc5", "NSK", "abc", "foo", "u2", "")

	cmps, err = vdh.GetUserVotes("u2")
	test.CheckErr(t, err, "get votes error")
	test.CheckCount(cmps, 4, t, "for u2 must be 4 votes")

}
func TestProfilesUpdateGroups(t *testing.T) {
	t.SkipNow()
	ph, err := NewProfileDbHandler(config.Main.PGDatabase.ConnString)
	tu.CheckErr(t, err, "init")
	deleteAll(ph)

	profile1PtrAfterInsert, _ := ph.InsertNewProfile(&profileWithGroup)
	if profile1PtrAfterInsert == nil {
		t.Error("After insert result  must be not nil")
	}
	check_groups(t, profile1PtrAfterInsert, 1)
	if !profile1PtrAfterInsert.Equal(&profileWithGroup) {
		t.Error("After insert profile and Before are not equals")
	}

	//add equal group and at result it must be one group
	profileWithGroup.Groups = append(profileWithGroup.Groups, group)
	err = ph.UpdateProfile(&profileWithGroup)
	tu.CheckErr(t, err, "add group update")
	savedProfile2AfterUpdate, err := ph.GetProfile(profileWithGroup.UserName)
	tu.CheckErr(t, err, "get updated group profile")
	check_groups(t, savedProfile2AfterUpdate, 1)

	//add another group
	profileWithGroup.Groups = append(profileWithGroup.Groups, ProfileGroup{Name: "test_Gr_2", Description: "foooooo"})
	err = ph.UpdateProfile(&profileWithGroup)
	tu.CheckErr(t, err, "add group update")
	savedProfile3AfterUpdate, err := ph.GetProfile(profileWithGroup.UserName)
	tu.CheckErr(t, err, "get updated group profile")
	check_groups(t, savedProfile3AfterUpdate, 2)
}
func TestProfilesCounts(t *testing.T) {
	t.SkipNow()
	ph, err := NewProfileDbHandler(config.Main.PGDatabase.ConnString)
	tu.CheckErr(t, err, "init")
	deleteAll(ph)
	testProfilesCount(t, ph, 0)
	ph.InsertNewProfile(&profile1)
	testProfilesCount(t, ph, 1)

	ph.DeleteProfile(profile1.UserName)
	testProfilesCount(t, ph, 0)
	//
	ph.InsertNewProfile(&profile1)
	ph.InsertNewProfile(&profile2)
	ph.InsertNewProfile(&profile3)

	testProfilesCount(t, ph, 3)

	profile1 = Profile{UserName: "******", Name: "testProfile1", ImageURL: "http://www.foo.bar", ShortDescription: "test", TextDescription: "ffffffffffffffffffffffffffffff"}
	profile1.Contacts = append(profile1.Contacts, ProfileContact{})
	profile1.Groups = append(profile1.Groups, ProfileGroup{Name: "fooo"})

	ph.UpdateProfile(&profile1)
	testProfilesCount(t, ph, 3)
}
func TestLastCompanyOfUser(t *testing.T){
	vdh := PrepObj()
	vdh.ConsiderCompany("abc", "NSK", "abc", "foo", "u1", "")
	vdh.ConsiderCompany("abc1", "NSK", "abc", "foo", "u1", "")
	vdh.ConsiderCompany("abc2", "NSK", "abc2", "foo2", "u1", "")

	lc, err := vdh.GetLastVote("u1")
	test.CheckErr(t, err ,"last vote err")
	if lc.Name != "abc2" || lc.City != "NSK" || lc.Service != "abc2"{
		t.Errorf("Return not last consider company")
	}
}
func TestElements(t *testing.T) {
	t.SkipNow()
	ph, err := NewProfileDbHandler(config.Main.PGDatabase.ConnString)
	tu.CheckErr(t, err, "init")
	deleteAll(ph)

	inserted_profile, _ := ph.InsertNewProfile(&profile1)
	if !inserted_profile.Equal(&profile1) {
		t.Error("after insert without contacts and groups ptrs must be equals")
	}

	inserted_group, err := ph.AddGroupToProfile(inserted_profile.UserName, &ProfileGroup{Name: "test_group", Description: "test_group_description"})
	tu.CheckErr(t, err, "insert group")
	if inserted_group.Id == 0 {
		t.Error("after insert group must be have id")
	}

	inserted_contact, err := ph.AddContactToProfile(inserted_profile.UserName, &contact)
	tu.CheckErr(t, err, "insert contact")
	if inserted_contact.ContactId == 0 {
		t.Error("after insert contact must have id")
	}

}
func TestAutocomplete(t *testing.T) {
	vdh := PrepObj()
	vdh.ConsiderCompany("abc", "NSK", "abc", "", "", "")
	vdh.ConsiderCompany("aabc", "NSK_", "abc cba", "", "", "")
	vdh.ConsiderCompany("aabbc", "NS_K", "abc qwe", "", "", "")
	vdh.ConsiderCompany("aabbcc", "_NSK", "qwe abc ", "", "", "")

	res, err := vdh.TextFoundByCompanyField("a", "name")
	test.CheckErr(t, err, "found by company field")
	test.CheckCount(res, 4, t, "by 'a' and 'name' must be all")

	res, err = vdh.TextFoundByCompanyField("ab", "name")
	test.CheckErr(t, err, "found by company field")
	test.CheckCount(res, 4, t, "by 'ab' and 'name' must be all")

	res, err = vdh.TextFoundByCompanyField("ab", "service")
	test.CheckErr(t, err, "found by company field")
	test.CheckCount(res, 4, t, "by 'ab' and 'service' must be all")

	res, err = vdh.TextFoundByCompanyField("cc", "name")
	test.CheckErr(t, err, "found by company field")
	test.CheckCount(res, 1, t, "by 'c' and 'name' must be all")

	res, err = vdh.TextFoundByCompanyField("bb", "name")
	test.CheckErr(t, err, "found by company field")
	test.CheckCount(res, 2, t, "by 'bb' and 'name' must be all")
	if !utils.InS("aabbc", res) {
		t.Errorf("interested aabbc not in result: %+v", res)
	}

	res, err = vdh.TextFoundByCompanyField("_", "city")
	test.CheckErr(t, err, "found by company field")
	test.CheckCount(res, 3, t, "by '_' and 'city' must be all")
	if !utils.InS("NS_K", res) || !utils.InS("NSK_", res) || !utils.InS("_NSK", res) {
		t.Errorf("interested names not in result: %+v", res)
	}
}
func TestProfilesUpdateFields(t *testing.T) {
	t.SkipNow()
	ph, err := NewProfileDbHandler(config.Main.PGDatabase.ConnString)
	tu.CheckErr(t, err, "init")
	deleteAll(ph)
	spe, _ := ph.InsertNewProfile(&profileEnabled)
	if !spe.Enable {
		t.Error("spe not enabled")
	}
	sppe, _ := ph.InsertNewProfile(&profilePublic)
	if !sppe.Public {
		t.Error("sppe not public")
	}
	spe.ShortDescription = "ttrtt"
	err = ph.UpdateProfile(spe)
	tu.CheckErr(t, err, "short descr changed")
	supe, err := ph.GetProfile(spe.UserName)
	tu.CheckErr(t, err, "short upd descr changed")
	if supe.ShortDescription != spe.ShortDescription {
		t.Error("short descr not changed")
	}

	sppe.TextDescription = "ttttttt"
	err = ph.UpdateProfile(sppe)
	tu.CheckErr(t, err, "long descr changed")
	susppe, err := ph.GetProfile(sppe.UserName)
	tu.CheckErr(t, err, "short upd descr changed")
	if susppe.TextDescription != sppe.TextDescription {
		t.Error("long descr not changed")
	}

	sppe.Public = false
	err = ph.UpdateProfile(sppe)
	tu.CheckErr(t, err, "public changed")
	susppe, err = ph.GetProfile(sppe.UserName)
	tu.CheckErr(t, err, "public upd changed")
	if susppe.Public != sppe.Public {
		t.Error("public not changed")
	}

	sppe.Enable = true
	err = ph.UpdateProfile(sppe)
	tu.CheckErr(t, err, "Enable changed")
	susppe, err = ph.GetProfile(sppe.UserName)
	tu.CheckErr(t, err, "Enable upd changed")
	if susppe.Enable != sppe.Enable {
		t.Error("Enable not changed")
	}

	sppe.ImageURL = "fooo"
	err = ph.UpdateProfile(sppe)
	tu.CheckErr(t, err, "ImageURL changed")
	susppe, err = ph.GetProfile(sppe.UserName)
	tu.CheckErr(t, err, "ImageURL upd changed")
	if susppe.ImageURL != sppe.ImageURL {
		t.Error("ImageURL not changed")
	}

	sppe.Name = "aaaaaaaaaa"
	err = ph.UpdateProfile(sppe)
	tu.CheckErr(t, err, "Name changed")
	susppe, err = ph.GetProfile(sppe.UserName)
	tu.CheckErr(t, err, "Name upd changed")
	if susppe.Name != sppe.Name {
		t.Error("Name not changed")
	}
}
func TestProfilesUpdateContacts(t *testing.T) {
	t.SkipNow()
	ph, err := NewProfileDbHandler(config.Main.PGDatabase.ConnString)
	tu.CheckErr(t, err, "init")
	deleteAll(ph)

	pwc, _ := ph.InsertNewProfile(&profileWithContacts)
	if pwc == nil {
		t.Error("After insert result  must be not nil")
	}
	if !check_contacts(t, pwc) {
		t.Error("Contacts or links not have id")
	}

	profileWithContacts.Contacts[0].Links[0].Description = "another_description"
	err = ph.UpdateProfile(&profileWithContacts)
	tu.CheckErr(t, err, "update link description")
	saved, err := ph.GetProfile(profileWithContacts.UserName)
	tu.CheckErr(t, err, "get saved update link description")
	if saved.Contacts[0].Links[0].Description != profileWithContacts.Contacts[0].Links[0].Description {
		t.Error("links description are not changed!", saved.Contacts[0].Links[0].Description, profileWithContacts.Contacts[0].Links[0].Description)
	}
	if len(saved.Contacts) != len(profileWithContacts.Contacts) {
		t.Error("added not added contact")
	}

	profileWithContacts.Contacts[0].Description = "another_description"
	err = ph.UpdateProfile(&profileWithContacts)
	tu.CheckErr(t, err, "update description")
	saved, err = ph.GetProfile(profileWithContacts.UserName)
	tu.CheckErr(t, err, "get saved update description")
	if saved.Contacts[0].Description != profileWithContacts.Contacts[0].Description {
		t.Error("description are not changed!", saved.Contacts[0].Description, profileWithContacts.Contacts[0].Description)
	}
	if len(saved.Contacts) != len(profileWithContacts.Contacts) {
		t.Error("added not added contact")
	}

	profileWithContacts.Contacts[0].Lat = 123.456
	profileWithContacts.Contacts[0].Lon = 456.123
	err = ph.UpdateProfile(&profileWithContacts)
	tu.CheckErr(t, err, "update geo")
	saved, err = ph.GetProfile(profileWithContacts.UserName)
	tu.CheckErr(t, err, "get saved update geo")
	if saved.Contacts[0].Lat != profileWithContacts.Contacts[0].Lat || saved.Contacts[0].Lon != profileWithContacts.Contacts[0].Lon {
		t.Error("geo are not changed!")
	}
	if len(saved.Contacts) != len(profileWithContacts.Contacts) {
		t.Error("added not added contact")
	}

	profileWithContacts.Contacts[0].OrderNumber = 100500
	err = ph.UpdateProfile(&profileWithContacts)
	tu.CheckErr(t, err, "update OrderNumber")
	saved, err = ph.GetProfile(profileWithContacts.UserName)
	tu.CheckErr(t, err, "get saved update OrderNumber")
	if saved.Contacts[0].OrderNumber != profileWithContacts.Contacts[0].OrderNumber {
		t.Error("OrderNumber are not changed!")
	}
	if len(saved.Contacts) != len(profileWithContacts.Contacts) {
		t.Error("added not added contact")
	}
}