Esempio n. 1
0
func TestModelRelationFetch(t *testing.T) {
	var p Personne
	need_connection()
	Personnes := gouda.M(p)
	toto := Personnes.Where(gouda.F("nom").Eq("toto")).First().(Personne)
	if toto.Nom != "toto" || toto.Id != 1 {
		t.Error("Not Found toto")
	}
	toto = Personnes.Where(gouda.F("nom").Eq("toto")).Last().(Personne)
	if toto.Nom != "toto" || toto.Id != 1 {
		t.Error("Not Found toto")
	}

	totos := Personnes.Where(gouda.F("nom").Eq("toto")).All()

	if len(totos) != 1 {
		t.Fatal("Wrong Fetched Size, fetched :" + fmt.Sprint(len(totos)))
	}
	toto = totos[0].(Personne)
	if toto.Nom != "toto" || toto.Id != 1 {
		t.Error("Not Found toto")
	}
	p = Personnes.Where(gouda.F("id").Gt(1)).First().(Personne)
	if p.Nom != "titi" || p.Id != 2 {
		t.Error("Not Found titi")
	}

	p = Personnes.Where(gouda.F("id").Lt(2)).First().(Personne)
	if p.Nom != "toto" || p.Id != 1 {
		t.Error("Not Found toto")
	}

	p = Personnes.Where(gouda.F("id").GtEq(1)).First().(Personne)
	if p.Nom != "toto" || p.Id != 1 {
		t.Error("Not Found toto")
	}

	p = Personnes.Order("id", "DESC").Where(gouda.F("id").LtEq(2)).First().(Personne)
	if p.Nom != "titi" || p.Id != 2 {
		t.Error("Not Found titi")
	}

	p = Personnes.Where(gouda.F("id").NEq(1)).First().(Personne)
	if p.Nom != "titi" || p.Id != 2 {
		t.Error("Not Found titi")
	}
	totos = Personnes.Where(gouda.F("id").Eq(1).Or(gouda.F("age").IsNull())).All()
	if len(totos) != 2 {
		t.Fatal("Wrong Fetched Size, fetched :" + fmt.Sprint(len(totos)))
	}
}
Esempio n. 2
0
func update_person(ctx *web.Context) {
	var p Personne
	Personnes := gouda.M(p)
	i, _ := strconv.Atoi(ctx.Request.Params["id"][0])
	p = Personnes.Where(gouda.F("id").Eq(i)).First().(Personne)
	p.Nom = ctx.Request.Params["nom"][0]
	p.Age, _ = strconv.Atoi(ctx.Request.Params["age"][0])
	Personnes.Save(p)
	ctx.Redirect(302, "/person/"+fmt.Sprint(p.Id))
}
Esempio n. 3
0
func person_edit(val string) string {
	var p Personne
	Personnes := gouda.M(p)
	i, _ := strconv.Atoi(val)
	p = Personnes.Where(gouda.F("id").Eq(i)).First().(Personne)
	ret := "<div><form action=\"/update_person\" method=\"post\">"
	ret += "<p> Nom : <input type=\"text\" name=\"nom\" value=\"" + p.Nom + "\"></p>"
	ret += "<p> Age : <input type=\"text\" name=\"age\" value=\"" + fmt.Sprint(p.Age) + "\"></p>"
	ret += "<input type=\"hidden\" name=\"id\" value=\"" + fmt.Sprint(p.Id) + "\"><input type=\"submit\" /></form></div>"
	return ret
}
Esempio n. 4
0
func person_delete(ctx *web.Context, val string) {
	var p Personne
	var c Car
	Personnes := gouda.M(p)
	Cars := gouda.M(c)
	i, _ := strconv.Atoi(val)
	p = Personnes.Where(gouda.F("id").Eq(i)).First().(Personne)
	cars := Personnes.GetAssociated("cars", p).([]interface{})
	for _, c := range cars {
		Cars.Delete(c.(Car))
	}
	Personnes.Delete(p)
	ctx.Redirect(302, "/")
}
Esempio n. 5
0
func TestModelRelationFetchCount(t *testing.T) {
	var p Personne
	need_connection()
	Personnes := gouda.M(p)
	if Personnes.Count() != 2 {
		t.Error("Counting Personne failed, counted : " + fmt.Sprint(Personnes.Count()))
	}

	if Personnes.Where(gouda.F("nom").Eq("toto")).Count() != 1 {
		t.Error("Counting toto failed, counted : " + fmt.Sprint(Personnes.Count()))
	}

	if Personnes.Count([]string{"age"}) != 1 {
		t.Error("Counting toto age failed, counted : " + fmt.Sprint(Personnes.Count([]string{"age"})))
	}

}
Esempio n. 6
0
func person_detail(val string) string {
	var p Personne
	Personnes := gouda.M(p)
	//Cars:=gouda.M(c)
	i, _ := strconv.Atoi(val)
	p = Personnes.Where(gouda.F("id").Eq(i)).First().(Personne)
	ret := "<div>"
	ret += "<p> Nom : " + p.Nom + "</p>"
	ret += "<p> Age : " + fmt.Sprint(p.Age) + "</p>"
	ret += "</div>"
	cars := Personnes.GetAssociated("cars", p).([]interface{})
	ret += "<ul>"
	for _, c := range cars {
		ret += "<li>" + c.(Car).Plate + "</li>"
	}
	ret += "</ul>"
	ret += "<a href=\"/edit_person/" + fmt.Sprint(p.Id) + "\"/>Editer</a>"
	return ret
}