Exemplo n.º 1
0
func TestGenerateSchema(t *testing.T) {
	s := &schema.Schema{}
	err := json.Unmarshal([]byte(testdata.JSON1), s)
	common.TestEquals(t, nil, err)

	// replace "~" with "`"
	result := strings.Replace(`
// Account represents a registered User
type Account struct {
	CreatedAt              time.Time ~json:"createdAt,omitempty"~              // Profile's creation time
	EmailAddress           string    ~json:"emailAddress"~                     // Email Address of the Account
	EmailStatusConstant    string    ~json:"emailStatusConstant,omitempty"~    // Status of the Account's Email
	ID                     int64     ~json:"id,omitempty,string"~              // The unique identifier for a Account's Profile
	Password               string    ~json:"password"~                         // Salted Password of the Account
	PasswordStatusConstant string    ~json:"passwordStatusConstant,omitempty"~ // Status of the Account's Password
	ProfileID              int64     ~json:"profileId,omitempty,string"~       // The unique identifier for a Account's Profile
	Salt                   string    ~json:"salt,omitempty"~                   // Salt used to hash Password of the Account
	StatusConstant         string    ~json:"statusConstant,omitempty"~         // Status of the Account
	URL                    string    ~json:"url,omitempty"~                    // Salted Password of the Account
	URLName                string    ~json:"urlName,omitempty"~                // Salted Password of the Account
}`, "~", "`", -1)

	code, err := GenerateSchema(s.Definitions["Account"])
	common.TestEquals(t, nil, err)
	common.TestEquals(t, result, string(code))
}
Exemplo n.º 2
0
func TestGenerateValidators(t *testing.T) {
	s := &schema.Schema{}
	err := json.Unmarshal([]byte(testdata.JSON1), s)
	common.TestEquals(t, nil, err)

	result := `
// Validate validates the Account struct
func (a *Account) Validate() error {
	return govalidator.NewMulti(govalidator.Date(a.CreatedAt),
		govalidator.MaxLength(a.Salt, 255),
		govalidator.Min(float64(a.ID), 1.000000),
		govalidator.Min(float64(a.ProfileID), 1.000000),
		govalidator.MinLength(a.Password, 6),
		govalidator.MinLength(a.URL, 6),
		govalidator.MinLength(a.URLName, 6),
		govalidator.OneOf(a.EmailStatusConstant, []string{
			AccountEmailStatusConstant.Verified,
			AccountEmailStatusConstant.NotVerified,
		}),
		govalidator.OneOf(a.PasswordStatusConstant, []string{
			AccountPasswordStatusConstant.Valid,
			AccountPasswordStatusConstant.NeedsReset,
			AccountPasswordStatusConstant.Generated,
		}),
		govalidator.OneOf(a.StatusConstant, []string{
			AccountStatusConstant.Registered,
			AccountStatusConstant.Unregistered,
			AccountStatusConstant.NeedsManualVerification,
		})).Validate()
}`

	code, err := validators.Generate(s.Definitions["Account"])
	common.TestEquals(t, nil, err)
	common.TestEquals(t, result, string(code))
}
Exemplo n.º 3
0
func TestDatabase(t *testing.T) {
	s := &schema.Schema{}
	err := json.Unmarshal([]byte(testdata.TestDataFull), s)
	common.TestEquals(t, nil, err)

	s = s.Resolve(s)
	moduleName := strings.ToLower(s.Title)
	settings := GenerateSettings(GeneratorName, moduleName, s)

	index := 0
	for _, def := range s.Definitions {

		// schema should have our generator
		if !def.Generators.Has(GeneratorName) {
			continue
		}

		settingsDef := SetDefaultSettings(GeneratorName, settings, def)
		settingsDef.Set("tableName", stringext.ToFieldName(def.Title))

		sts, err := DefineDatabase(settingsDef, def)
		if err != nil {
			t.Fatal(err.Error())
		}

		common.TestEquals(t, expectedDatabases[index], string(sts))
		index++
	}
}
Exemplo n.º 4
0
func TestGenerateModel(t *testing.T) {
	var s schema.Schema
	err := json.Unmarshal([]byte(testdata.JSON1), &s)
	common.TestEquals(t, nil, err)

	_, err = GenerateModel(&s)
	common.TestEquals(t, nil, err)
}
Exemplo n.º 5
0
func TestConstructors(t *testing.T) {
	var s schema.Schema
	if err := json.Unmarshal([]byte(testdata.JSON1), &s); err != nil {
		t.Fatal(err.Error())
	}

	a, err := Generate(&s)
	common.TestEquals(t, nil, err)
	common.TestEquals(t, expected, string(a))
}
Exemplo n.º 6
0
func TestConstants(t *testing.T) {
	s := &schema.Schema{}
	if err := json.Unmarshal([]byte(testdata.JSON1), s); err != nil {
		t.Fatal(err.Error())
	}

	s = s.Resolve(nil)

	a, err := Generate(s.Definitions["Account"])
	common.TestEquals(t, nil, err)
	common.TestEquals(t, expected, string(a))
}
Exemplo n.º 7
0
func TestFunctions(t *testing.T) {
	s := &schema.Schema{}
	err := json.Unmarshal([]byte(testdata.TestDataFull), s)

	s = s.Resolve(s)

	sts, err := (&Generator{}).Generate(common.NewContext(), s)
	common.TestEquals(t, nil, err)

	for i, s := range sts {
		common.TestEquals(t, expecteds[i], string(s.Content))
	}
}
Exemplo n.º 8
0
func TestClients(t *testing.T) {
	s := &schema.Schema{}
	if err := json.Unmarshal([]byte(testdata.JSON1), s); err != nil {
		t.Fatal(err.Error())
	}

	s = s.Resolve(s)

	sts, err := (&Generator{}).Generate(common.NewContext(), s)
	common.TestEquals(t, nil, err)

	for i, s := range sts {
		common.TestEquals(t, expecteds[i], string(s.Content))
	}
}