// 18.4.2. Create Node with properties func TestGetOrCreateNode(t *testing.T) { db := connectTest(t) defer cleanup(t, db) label, err := randutil.String(12, randutil.Alphabet) if err != nil { t.Fatal(err) } key, err := randutil.String(12, randutil.Alphabet) if err != nil { t.Fatal(err) } value, err := randutil.String(12, randutil.Alphabet) if err != nil { t.Fatal(err) } p0 := Props{key: value, "foo": "bar"} p1 := Props{key: value} p2 := Props{"foo": "bar"} // // Create unique node // n0, created, err := db.GetOrCreateNode(label, key, p0) if err != nil { t.Fatal(err) } if !created { t.Fatal("Failed to create unique node") } check0, err := n0.Properties() if err != nil { t.Fatal(err) } assert.Equal(t, p0, check0) // // Get unique node // n1, created, err := db.GetOrCreateNode(label, key, p1) if err != nil { t.Fatal(err) } if created { t.Fatal("Failed to retrieve unique node") } check1, err := n1.Properties() if err != nil { t.Fatal(err) } assert.Equal(t, p0, check1) // // No key in props // _, _, err = db.GetOrCreateNode(label, key, p2) assert.NotEqual(t, nil, err) // // Empty label // _, _, err = db.GetOrCreateNode("", key, p0) assert.NotEqual(t, nil, err) }
// generateMetricNames generates a specified number of random metric types. func generateMetricNames(numMetrics int, store []Metric, metricToken string) []Metric { metricTypes := []string{ "c", "g", "ms", "s", } rand.Seed(time.Now().UnixNano()) r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numMetrics; i++ { newMetricName, _ := randutil.String(20, randutil.Alphabet) newMetricNS := fmt.Sprintf("%sstatsgod.test.%s", metricToken, newMetricName) newMetricCT := 1 if *connType > 0 && *connType < 6 { newMetricCT = *connType } else { newMetricCT, _ = randutil.IntRange(1, 6) } metricType := metricTypes[r.Intn(len(metricTypes))] store = append(store, Metric{ key: newMetricNS, metricType: metricType, metricValue: 0, connectionType: newMetricCT, }) } return store }
func rndStr(t *testing.T) string { // Neo4j doesn't like object names beginning with numerals. name, err := randutil.String(12, randutil.Alphabet) if err != nil { t.Fatal(err) } return name }
// GenerateToken creates a sudo random string // and stores it in the database func (u *User) GenerateToken() error { u.Token, _ = randutil.String(50, "7148fa22885551edb6405895ddf7cd0f") if _, err := db.From("users"). Where(goqu.Ex{ "username": u.Username, }).Update(goqu.Record{ "token": u.Token, }).Exec(); err != nil { return err } return nil }
// RandString 产生随机字符串,可用于密码等 func RandString(n int, defaultCharsets ...string) string { charset := DefaultCharset if len(defaultCharsets) > 0 { charset = defaultCharsets[0] } result, err := randutil.String(n, charset) if err != nil { fmt.Println("goutils RandString error:", err) return Md5(strconv.Itoa(rand.Intn(999999))) } return result }
func generateMetricNames(numMetrics int, store []Metric) []Metric { metricTypes := []string{ "c", "g", "ms", } r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numMetrics; i++ { newMetricName, _ := randutil.String(20, randutil.Alphabet) newMetricNS := fmt.Sprintf("statsgod.test.%s", newMetricName) newMetricCT, _ := randutil.IntRange(1, 4) store = append(store, Metric{ key: newMetricNS, metricType: metricTypes[r.Intn(len(metricTypes))], connectionType: newMetricCT, }) } return store }