Esempio n. 1
0
File: home.go Progetto: keep94/vsafe
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	session := common.GetUserSession(r)
	sortBy := r.Form.Get("sort")
	id, _ := strconv.ParseInt(r.Form.Get("id"), 10, 64)
	entries, err := vsafedb.Entries(h.Store, session.Key().Id, r.Form.Get("q"))
	if err != nil {
		http_util.ReportError(w, "Error reading database", err)
		return
	}
	switch sortBy {
	case "newest":
		vsafedb.Reverse(entries)
	default:
		vsafedb.SortByTitle(entries)
	}
	http_util.WriteTemplate(
		w,
		kTemplate,
		&view{
			Values:  http_util.Values{r.Form},
			Name:    session.User.Name,
			Entries: entries,
			Url:     r.URL,
			Id:      id})
}
Esempio n. 2
0
func TestEntries(t *testing.T) {
	yahoo, _ := url.Parse("http://www.yahoo.com")
	google, _ := url.Parse("http://www.google.com")
	var store FakeStore
	entry1 := vsafe.Entry{Title: " First", Url: yahoo, Desc: "the SeconD   oNe"}
	entry2 := vsafe.Entry{Title: "aGAiN  sEcond", Url: google, Desc: "a desc"}
	entry3 := vsafe.Entry{Title: "third again", Desc: "foo bar"}
	vsafedb.AddEntry(&store, nil, kKey, &entry1)
	vsafedb.AddEntry(&store, nil, kKey, &entry2)
	vsafedb.AddEntry(&store, nil, kKey, &entry3)
	entries, err := vsafedb.Entries(store, kKey.Id, "")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 3 {
		t.Errorf("Expected 3 entries, got %v", len(entries))
	}
	if entries[0].Title != entry1.Title || entries[1].Title != entry2.Title || entries[2].Title != entry3.Title {
		t.Error("Returned 3 entries in wrong order")
	}
	entries, err = vsafedb.Entries(store, kKey.Id, "  first")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 1 {
		t.Errorf("Expected 1 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, "second  ")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 2 {
		t.Errorf("Expected 2 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, "google")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 1 {
		t.Errorf("Expected 1 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, "biz")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 0 {
		t.Errorf("Expected 0 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, " eCond  one ")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 1 {
		t.Errorf("Expected 1 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, " Gain   SEco ")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 1 {
		t.Errorf("Expected 1 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, " hain   SEco ")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 0 {
		t.Errorf("Expected 0 entries, got %v", len(entries))
	}
}