func TestAttributesName(t *testing.T) { need_connection() p := new(Personne) attr := gouda.M(p).AttributesNames() names := []string{"Nom", "Id", "Age"} sort.StringArray(attr).Sort() sort.StringArray(names).Sort() if len(names) != len(attr) { t.Error("Attributes Names, found size mismatch : " + fmt.Sprint(len(attr)) + " for " + fmt.Sprint(len(names))) } if !reflect.DeepEqual(names, attr) { t.Error("Can't find Attributes Names, found : " + fmt.Sprint(attr)) } var pp Personne attr = gouda.M(pp).AttributesNames() sort.StringArray(attr).Sort() if len(names) != len(attr) { t.Error("Attributes Names, found size mismatch : " + fmt.Sprint(len(attr)) + " for " + fmt.Sprint(len(names))) } if !reflect.DeepEqual(names, attr) { t.Error("Can't find Attributes Names, found : " + fmt.Sprint(attr)) } }
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, "/") }
func TestTableName(t *testing.T) { need_connection() var c Car Cars := gouda.M(c) if Cars.TableName() != "cars" { t.Error("Wrong TableName") } }
func TestAttributes(t *testing.T) { need_connection() p := new(Personne) attr := gouda.M(p).Attributes() types := map[string]reflect.Type{"Nom": &reflect.StringType{}, "Id": &reflect.IntType{}} //,"Age":&reflect.FloatType{}} if !compare(attr, types) { t.Error("Can't find Attributes found : " + fmt.Sprint(attr) + "\n") } var pp Personne attr = gouda.M(pp).Attributes() if !compare(attr, types) { t.Error("Can't find Attributes found : " + fmt.Sprint(attr) + "\n") } }
func TestModelFetch(t *testing.T) { need_connection() p := new(Personne) z := gouda.M(p).First().(Personne) if z.Id != 1 { t.Error("wrong personne found : Id : " + fmt.Sprint(z.Id)) } z = gouda.M(p).Last().(Personne) if z.Id != 2 { t.Error("wrong personne found : Id : " + fmt.Sprint(z.Id)) } tab := gouda.M(p).All() if len(tab) != 2 { t.Error("Wrong Fetched Size (" + fmt.Sprint(len(tab)) + ")") } }
func create_person(ctx *web.Context) { var p Personne Personnes := gouda.M(p) p.Id = 0 p.Nom = ctx.Request.Params["nom"][0] p.Age, _ = strconv.Atoi(ctx.Request.Params["age"][0]) Personnes.Save(p) ctx.Redirect(302, "/") }
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)) }
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 }
func TestModelAssociations(t *testing.T) { var p Personne var c Car need_connection() Personnes := gouda.M(p) Cars := gouda.M(c) Cars.BelongsToKey(Personnes, "owner", "Owner_id") Personnes.HasManyKey(Cars, "cars", "Owner_id") car := Cars.First().(Car) p = Cars.GetAssociated("owner", car).(Personne) if p.Nom != "toto" || p.Id != 1 { t.Error("Not Found toto") } cars := Personnes.GetAssociated("cars", p).([]interface{}) if len(cars) != 2 { t.Error("Associated cars not found") } if cars[0].(Car).Id != 1 { t.Error("Associated car not found (first one)") } }
func main() { conn := need_connection() var p Personne var c Car Personnes := gouda.M(p) Cars := gouda.M(c) Cars.BelongsToKey(Personnes, "owner", "Owner_id") Personnes.HasManyKey(Cars, "cars", "Owner_id") //web.Get("/(.*)", hello) web.Get("/new_person/", new_person) web.Get("/persons/", persons) web.Get("/", persons) web.Get("/person/(.*)", person_detail) web.Get("/edit_person/(.*)", person_edit) web.Get("/delete_person/(.*)", person_delete) web.Post("/person", create_person) web.Post("/update_person", update_person) web.Run("0.0.0.0:9999") conn.Close() }
func TestModelDelete(t *testing.T) { var p Personne need_connection() Personnes := gouda.M(p) Personnes.Delete(Personnes.First()) if len(Personnes.All()) != 3 { t.Error("Not deleted ! counting :" + fmt.Sprint(len(Personnes.All()))) } gouda.Delete(Personnes.First()) if len(Personnes.All()) != 2 { t.Error("Not deleted ! counting :" + fmt.Sprint(len(Personnes.All()))) } }
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))) } }
func persons() string { var p Personne Personnes := gouda.M(p) coll := Personnes.Order("id", "ASC").All() ret := "<h1>Personnes</h1>" ret += "<table>" ret += "<tr><th>Id</th><th>Nom</th><th>Age</th><th>Action</th></tr>" for _, r := range coll { p = r.(Personne) ret += "<tr><td><a href=\"/person/" + fmt.Sprint(p.Id) + "\">" + fmt.Sprint(p.Id) + "</a></td><td>" + p.Nom + "</td><td>" + fmt.Sprint(p.Age) + "</td><td><a href=\"/delete_person/" + fmt.Sprint(p.Id) + "\">suppr</a></td></tr>" } ret += "</table><a href=\"/new_person/\">Ajouter Person</a>" return ret }
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"}))) } }
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 }
func TestModelRelationRefresh(t *testing.T) { var p Personne need_connection() Personnes := gouda.M(p) toto := Personnes.First().(Personne) toto = Personnes.Refresh(toto).(Personne) if toto.Id != 1 { t.Error("Refresh Failed, " + fmt.Sprint(toto)) } toto = Personnes.Refresh(&toto).(Personne) if toto.Id != 1 { t.Error("Refresh Failed, " + fmt.Sprint(toto)) } toto = gouda.Refresh(&toto).(Personne) if toto.Id != 1 { t.Error("Refresh Failed, " + fmt.Sprint(toto)) } }
func TestModelRelationFetchOrder(t *testing.T) { var p Personne need_connection() Personnes := gouda.M(p) totos := Personnes.Order("nom", "asc").All() if len(totos) != 2 { t.Error("Wrong Fetched Size, fetched :" + fmt.Sprint(len(totos))) } i := 0 if totos[i].(Personne).Nom != "titi" { t.Error("Not Found titi " + fmt.Sprint(totos[i].(Personne))) } i++ if totos[i].(Personne).Nom != "toto" { t.Error("Not Found toto " + fmt.Sprint(totos[i].(Personne))) } }
func TestModelInsertOrUpdate(t *testing.T) { var p Personne need_connection() gouda.Save(&Personne{Id: 3, Nom: "test", Age: 12}) Personnes := gouda.M(p) p.Id = 0 p.Nom = "tata" p.Age = 7 Personnes.Save(&p) if p.Id != 4 { t.Error("Id not setted, found " + fmt.Sprint(p.Id)) } p.Nom = "plop" Personnes.Save(&p) if p.Id != 4 { t.Error("Save should have updated not inserted!") } p = gouda.Refresh(&p).(Personne) if p.Nom != "plop" { t.Error("Not Updated !") } }