Working on fake data for tests

This commit is contained in:
Andrea Fazzi 2020-01-10 12:07:11 +01:00
parent b9fadab25b
commit c07bb4ded9
7 changed files with 8593 additions and 8507 deletions

View file

@ -15,6 +15,7 @@ import (
"git.andreafazzi.eu/andrea/oef/i18n"
"git.andreafazzi.eu/andrea/oef/orm"
"git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm"
"github.com/jinzhu/inflection"
)
@ -117,6 +118,28 @@ func (c *Client) ReadAll(model interface{}) error {
return nil
}
func (c *Client) Read(model orm.IDer) (interface{}, error) {
var response renderer.JsonResponse
data, err := c.SendRequest("GET", fmt.Sprintf("/api/%s/%d?format=json", pluralizedModelName(model), model.GetID()), nil)
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &response); err != nil {
return nil, err
}
if string(response.Error) != "" {
return nil, errors.New(string(response.Error))
}
if err := json.Unmarshal(response.Result, &model); err != nil {
return nil, err
}
return data, nil
}
func (c *Client) Create(model interface{}) (uint, error) {
var response renderer.JsonResponse
data, err := json.Marshal(model)
@ -203,6 +226,11 @@ func (c *Client) DeleteAllFunc(model interface{}, testFn func(interface{}) bool)
return nil
}
func (c *Client) Exists(model orm.IDer) bool {
_, err := c.Read(model)
return err != gorm.ErrRecordNotFound
}
// FIXME: refactor this (it's duplicated across the code)
func pluralizedModelName(value interface{}) string {
return inflection.Plural(strings.ToLower(orm.ModelName(value)))

View file

@ -27,28 +27,6 @@ var (
store = sessions.NewCookieStore([]byte(config.Config.Keys.CookieStoreKey))
categories = []string{"Junior", "Senior"}
regions = []string{
"Abruzzo",
"Basilicata",
"Calabria",
"Campania",
"Emilia-Romagna",
"Friuli-Venezia Giulia",
"Lazio",
"Liguria",
"Lombardia",
"Marche",
"Molise",
"Piemonte",
"Puglia",
"Sardegna",
"Sicilia",
"Toscana",
"Trentino-Alto Adige",
"Umbria",
"Valle d'Aosta",
"Veneto",
}
)
func init() {
@ -79,15 +57,6 @@ func CreateCategories() {
}
}
func CreateRegions() {
for _, name := range regions {
var region Region
if err := currDB.FirstOrCreate(&region, Region{Name: name}).Error; err != nil {
panic(err)
}
}
}
func Use(db *gorm.DB) {
currDB = db
}

View file

@ -7,12 +7,68 @@ import (
"github.com/jinzhu/gorm"
)
var (
regions = []string{
"Abruzzo",
"Basilicata",
"Calabria",
"Campania",
"Emilia-Romagna",
"Friuli-Venezia Giulia",
"Lazio",
"Liguria",
"Lombardia",
"Marche",
"Molise",
"Piemonte",
"Puglia",
"Sardegna",
"Sicilia",
"Toscana",
"Trentino-Alto Adige",
"Umbria",
"Valle d'Aosta",
"Veneto",
}
regionsMap = map[string]uint{
"Abruzzo": 1,
"Basilicata": 2,
"Calabria": 3,
"Campania": 4,
"Emilia-Romagna": 5,
"Friuli-Venezia Giulia": 6,
"Lazio": 7,
"Liguria": 8,
"Lombardia": 9,
"Marche": 10,
"Molise": 11,
"Piemonte": 12,
"Puglia": 13,
"Sardegna": 14,
"Sicilia": 15,
"Toscana": 16,
"Trentino-Alto Adige": 17,
"Umbria": 18,
"Valle d'Aosta": 19,
"Veneto": 20,
}
)
type Region struct {
gorm.Model
Name string
}
func CreateRegions() {
for _, name := range regions {
var region Region
if err := currDB.FirstOrCreate(&region, Region{Name: name}).Error; err != nil {
panic(err)
}
}
}
func (model *Region) GetID() uint { return model.ID }
func (model *Region) String() string {

View file

@ -12,6 +12,8 @@ import (
"github.com/jinzhu/gorm"
)
type RegionID uint
type School struct {
gorm.Model
@ -31,7 +33,7 @@ type School struct {
ContestResponsibleFirstname string
UserID uint
RegionID uint `schema:"region_id"`
RegionID RegionID `schema:"region_id"`
Region *Region
User *User
@ -41,6 +43,11 @@ type School struct {
AllRegions []*Region `gorm:"-"`
}
func (id *RegionID) UnmarshalCSV(csv string) error {
*id = RegionID(regionsMap[strings.Title(strings.ToLower(csv))])
return nil
}
func (model *School) GetID() uint { return model.ID }
func (model *School) String() string {
@ -190,7 +197,7 @@ func (model *School) Update(args map[string]string, w http.ResponseWriter, r *ht
}
school.SelectedRegion = make(map[uint]string)
school.SelectedRegion[school.RegionID] = "selected"
school.SelectedRegion[uint(school.RegionID)] = "selected"
return school, nil
} else {

View file

@ -3,13 +3,16 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"git.andreafazzi.eu/andrea/oef/client"
"git.andreafazzi.eu/andrea/oef/config"
"git.andreafazzi.eu/andrea/oef/orm"
"github.com/BurntSushi/toml"
"github.com/gocarina/gocsv"
)
type importer struct {
@ -21,7 +24,8 @@ func newImporter(c *client.Client) *importer {
return &importer{
c,
map[string]func(*client.Client, string) error{
"contest": importContest,
"contests": importContest,
"schools": importSchools,
},
}
}
@ -34,6 +38,28 @@ func (i *importer) runImport(importer, filename string) error {
return f(i.client, filename)
}
func importSchools(client *client.Client, filename string) error {
var schools []*orm.School
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
if err := gocsv.Unmarshal(strings.NewReader(string(data)), &schools); err != nil {
panic(err)
}
for _, s := range schools {
id, err := client.Create(s)
if err != nil {
panic(err)
}
log.Println("Create school with ID", id)
}
return nil
}
func importContest(client *client.Client, filename string) error {
log.Println("Reading contest data from toml file...")
var contest *orm.Contest

File diff suppressed because it is too large Load diff

View file

@ -52,9 +52,9 @@
<dd class="col-sm-9">{{.Data.EmailSentDate|prettyDate}} alle ore {{.Data.EmailSentDate|prettyTime}}</dd>
{{end}}
<dt class="col-sm-3">Username</dt>
<dd class="col-sm-9">{{.Data.User.Username}}</dd>
<dd class="col-sm-9"><span class="text-monospace">{{.Data.User.Username}}</span></dd>
<dt class="col-sm-3">Password</dt>
<dd class="col-sm-9">{{.Data.User.Password}}</dd>
<dd class="col-sm-9"><span class="text-monospace">{{.Data.User.Password}}</span></dd>
{{if $isAdmin}}
{{if $creatorUser:=.Data.CreatedBy}}