本文整理汇总了Golang中github.com/elos/data/builtin/mem.NewDB函数的典型用法代码示例。如果您正苦于以下问题:Golang NewDB函数的具体用法?Golang NewDB怎么用?Golang NewDB使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewDB函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestRecordsQueryTemplate
func TestRecordsQueryTemplate(t *testing.T) {
db := mem.NewDB()
creds := make([]map[string]interface{}, 5)
for i := 0; i < 5; i++ {
_, c, err := user.Create(db, "username", "password")
if err != nil {
t.Fatal("user.Create error: %s", err)
}
m := make(map[string]interface{})
transfer.TransferAttrs(c, &m)
creds[i] = m
}
s := &records.QueryData{
Model: models.Metis[models.CredentialKind],
Records: creds,
}
var b bytes.Buffer
if err := records.QueryTemplate.Execute(&b, s); err != nil {
t.Fatalf("template.Execute error: %s", err)
}
o := b.String()
t.Logf("TemplateOutput:\n%s", o)
if got, want := strings.Contains(o, "username"), true; got != want {
t.Errorf("strings.Contains(\"username\"): got %t, want %t", got, want)
}
if got, want := strings.Contains(o, "password"), true; got != want {
t.Errorf("strings.Contains(\"password\"): got %t, want %t", got, want)
}
}
示例2: BenchmarkRecordPostEvent
func BenchmarkRecordPostEvent(b *testing.B) {
db := mem.NewDB()
u, _, err := user.Create(db, "username", "password")
if err != nil {
b.Fatalf("user.Create error: %v", err)
}
ctx := user.NewContext(context.Background(), u)
logger := services.NewTestLogger(b)
for i := 0; i < b.N; i++ {
rec := httptest.NewRecorder()
req, err := http.NewRequest(
"POST",
"http://www.elos.pw/record/?"+url.Values{
"kind": []string{models.EventKind.String()},
}.Encode(),
bytes.NewBuffer(
[]byte(`{
"name": "event name",
"data": {
"sensor1": 34,
"sensor2": 4.3
},
"owner_id": "`+u.ID().String()+`"
}`),
))
if err != nil {
b.Fatalf("http.NewRequest error: %v", err)
}
routes.RecordPOST(ctx, rec, req, logger, db)
}
}
示例3: TestWebSensorsAgent
func TestWebSensorsAgent(t *testing.T) {
db := mem.NewDB()
u, _, err := user.Create(db, "username", "password")
if err != nil {
t.Fatal(err)
}
changes := data.FilterKind(db.Changes(), models.EventKind)
ctx, stop := context.WithCancel(context.Background())
go agents.WebSensorsAgent(ctx, db, u)
defer stop()
// give control to agent thread
time.Sleep(1 * time.Millisecond)
_, err = event.WebSensorLocation(db, u, 50, 50)
if err != nil {
t.Fatal(err)
}
// read off the event we just created
<-*changes
select {
case eventChange := <-*changes:
e := eventChange.Record.(*models.Event)
if e.Name != "Location Update" {
t.Fatal("Expected a location update to be produced")
}
case <-time.After(100 * time.Millisecond):
t.Fatal("Timed out waiting for event creation")
}
}
示例4: TestLocationAgent
func TestLocationAgent(t *testing.T) {
db := mem.NewDB()
u, _, err := user.Create(db, "username", "password")
if err != nil {
t.Fatal(err)
}
changes := data.FilterKind(db.Changes(), models.ProfileKind)
ctx, stop := context.WithCancel(context.Background())
go agents.LocationAgent(ctx, db, u)
defer stop()
// give control to agent thread
time.Sleep(1 * time.Millisecond)
_, loc, err := event.LocationUpdate(db, u, 50, 50, 50)
if err != nil {
t.Fatal(err)
}
select {
case profileChange := <-*changes:
p := profileChange.Record.(*models.Profile)
if loc.Id != p.LocationId {
t.Fatal("Expected profile's location id to now match be the new location ")
}
case <-time.After(100 * time.Millisecond):
t.Fatal("Timed out waiting for profile update")
}
}
示例5: TestNewGET
func TestNewGET(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
records.NewGET(context.Background(), w, r, mem.NewDB(), services.NewTestLogger(t))
}))
resp, err := http.Get(s.URL)
if err != nil {
t.Fatalf("http.Get error: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("ioutil.ReadAll error: %v", err)
}
t.Logf("resp.Body:\n%s", body)
for name := range models.Metis {
if name == "user" {
continue
}
if got, want := bytes.Contains(body, []byte(name)), true; got != want {
t.Errorf("strings.Contains(out, %q): got %t, want %t", name, got, want)
}
}
}
示例6: TestCalInadequateInitialization
func TestCalInadequateInitialization(t *testing.T) {
// mock cli.Ui
ui := new(cli.MockUi)
// yes to init prompt to create a new calendar, 3 times
ui.InputReader = bytes.NewBufferString("y\ny\ny\n")
// memory db
db := mem.NewDB()
// a new user, stored in db
user := newTestUser(t, db)
// note: this CalCommand is missing a cli.Ui
missingUI := &CalCommand{
UserID: user.ID().String(),
DB: db,
}
// note: this CalCommand has no UserID
missingUserID := &CalCommand{
UI: ui,
DB: db,
}
// note: this CalCommand lacks a database (DB field)
missingDB := &CalCommand{
UI: ui,
UserID: user.ID().String(),
}
t.Log("Run command that doesn't have a UI")
// expect missing a ui to fail
if o := missingUI.Run([]string{"new"}); o != failure {
t.Fatal("CalCommand without ui should fail on run")
}
t.Log("Completed")
t.Log("Run command that doesn't have a user id")
// expect missing a user id to fail
if o := missingUserID.Run([]string{"new"}); o != failure {
t.Fatal("CalCommand without user id should fail on run")
}
t.Log("Completed")
t.Log("Run command that doesn't have a db")
// expect missing a db to fail
if o := missingDB.Run([]string{"new"}); o != failure {
t.Fatal("CalCommand without db should fail on run")
}
t.Log("Completed")
}
示例7: newMockTagCommand
func newMockTagCommand(t *testing.T) (*cli.MockUi, data.DB, *models.User, *TagCommand) {
ui := new(cli.MockUi)
db := mem.NewDB()
user := newTestUser(t, db)
return ui, db, user, &TagCommand{
UI: ui,
UserID: user.ID().String(),
DB: db,
}
}
示例8: TestEditGET
func TestEditGET(t *testing.T) {
db := mem.NewDB()
u, _, err := user.Create(db, "username", "password")
if err != nil {
t.Fatalf("user.Create error: %v", err)
}
e := &models.Event{
Id: db.NewID().String(),
OwnerId: u.Id,
Name: "old name",
Data: map[string]interface{}{
"sensor": 4,
},
}
if err := db.Save(e); err != nil {
t.Fatalf("db.Save error: %v", err)
}
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
records.EditGET(user.NewContext(context.Background(), u), w, r, db, services.NewTestLogger(t))
}))
p := s.URL + "?" + url.Values{
"kind": []string{"event"},
"id": []string{e.Id},
}.Encode()
resp, err := http.Get(p)
if err != nil {
t.Fatalf("http.Get(%q) error: %v", p, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("ioutil.ReadAll error: %v", err)
}
t.Logf("resp.Body:\n%s", body)
if got, want := bytes.Contains(body, []byte(`event`)), true; got != want {
t.Fatalf("bytes.Contains(body, %q): got %t, want %t", "event", got, want)
}
if got, want := bytes.Contains(body, []byte(`old name`)), true; got != want {
t.Fatalf("bytes.Contains(body, %q): got %t, want %t", "old name", got, want)
}
if got, want := bytes.Contains(body, []byte(`/records/view/?kind=event&id=3`)), true; got != want {
t.Fatalf("bytes.Contains(body, %q): got %t, want %t", `/records/view/?kind=event&id=3`, got, want)
}
}
示例9: TestRecordsNewGET
func TestRecordsNewGET(t *testing.T) {
ctx := context.Background()
db := mem.NewDB()
logger := services.NewTestLogger(t)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, ok := Authenticate(ctx, w, r, logger, db)
if !ok {
t.Fatal("authentication failed")
}
Records.NewGET(ctx, w, r, db, logger)
}))
defer s.Close()
_, _, err := user.Create(db, "username", "password")
if err != nil {
t.Fatal(err)
}
c := new(http.Client)
req, err := http.NewRequest("GET", s.URL, nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth("username", "password")
resp, err := c.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
t.Logf("Body:\n%s", body)
doc := string(body)
contains := map[string]bool{
"user": false,
"event": true,
"New": true,
}
for c, want := range contains {
if got := strings.Contains(doc, c); got != want {
t.Fatalf("strings.Contains(%q): got %t, want %t", c, got, want)
}
}
}
示例10: TestDeletePOST
func TestDeletePOST(t *testing.T) {
db := mem.NewDB()
u, _, err := user.Create(db, "username", "password")
if err != nil {
t.Fatalf("user.Create error: %v", err)
}
e := &models.Event{
Id: db.NewID().String(),
OwnerId: u.Id,
Name: "event name",
Data: map[string]interface{}{
"sensor": 45.3,
},
}
if err := db.Save(e); err != nil {
t.Fatalf("db.Save error: %v", err)
}
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.RequestURI, "/records/delete/") {
records.DeletePOST(user.NewContext(context.Background(), u), w, r, db, services.NewTestLogger(t))
}
records.QueryGET(user.NewContext(context.Background(), u), w, r, db, services.NewTestLogger(t))
}))
p := s.URL + "/records/delete/?" + url.Values{
"kind": []string{"event"},
"id": []string{e.Id},
}.Encode()
resp, err := http.Post(p, "", new(bytes.Buffer))
if err != nil {
t.Fatalf("http.Post error: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("ioutil.ReadAll error: %v", err)
}
t.Logf("resp.Body:\n%s", body)
if got, want := db.PopulateByID(e), data.ErrNotFound; got != want {
t.Fatalf("db.PopulateByID: got %v, want %v", got, want)
}
}
示例11: TestRecordsQueryGET
func TestRecordsQueryGET(t *testing.T) {
ctx := context.Background()
db := mem.NewDB()
logger := services.NewTestLogger(t)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, ok := Authenticate(ctx, w, r, logger, db)
if !ok {
t.Fatal("authentication failed")
}
Records.QueryGET(ctx, w, r, db, logger)
}))
defer s.Close()
_, _, err := user.Create(db, "username", "password")
if err != nil {
t.Fatal(err)
}
c := new(http.Client)
req, err := http.NewRequest("GET", s.URL+"?"+url.Values{
"query/Kind": []string{"credential"},
}.Encode(), nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth("username", "password")
resp, err := c.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
t.Logf("Body:\n%s", body)
doc := string(body)
if got, want := strings.Contains(doc, "username"), true; got != want {
t.Fatalf("strings.Contains(doc, \"username\"): got %t, want %t", got, want)
}
if got, want := strings.Contains(doc, "password"), true; got != want {
t.Fatalf("strings.Contains(doc, \"password\"): got %t, want %t", got, want)
}
}
示例12: TestMarshalTask
func TestMarshalTask(t *testing.T) {
t.Skip()
db := mem.NewDB()
user, _, err := user.Create(db, "username", "password")
if err != nil {
t.Fatalf("user.Create error: %v", err)
}
bytes, err := form.Marshal(user, "user")
if err != nil {
t.Fatalf("form.Marshal error: %v", err)
}
if got, want := string(bytes), ""; got != want {
t.Fatalf("form.Marshal: got\n%s\nwant,\n%s", got, want)
}
}
示例13: TestAuthenticateQueryParams
func TestAuthenticateQueryParams(t *testing.T) {
ctx := context.Background()
db := mem.NewDB()
logger := services.NewTestLogger(t)
var userContext context.Context
var authed bool
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userContext, authed = routes.Authenticate(ctx, w, r, logger, db)
if authed {
w.WriteHeader(http.StatusOK)
}
}))
defer s.Close()
u, _, err := user.Create(db, "username", "password")
if err != nil {
t.Fatalf("user.Create(db, \"username\", \"password\") error: %s", err)
}
client := new(http.Client)
req, err := http.NewRequest("GET", s.URL, new(bytes.Buffer))
if err != nil {
t.Fatalf("http.NewRequest error: %s", err)
}
client.Do(req)
if got, want := authed, false; got != want {
t.Fatalf("authed: got %t, want %t", got, want)
}
req, err = http.NewRequest("GET", s.URL+"?public=username&private=password", new(bytes.Buffer))
if err != nil {
t.Fatalf("http.NewRequest error: %s", err)
}
client.Do(req)
if got, want := authed, true; got != want {
t.Fatalf("authed: got %t, want %t")
}
authedU, ok := user.FromContext(userContext)
if got, want := ok, true; got != want {
t.Fatalf("_, ok := user.FromContext: got %t, want %t")
}
if got, want := data.Equivalent(authedU, u), true; got != want {
t.Errorf("data.Equivalent(authedU, u): got %t, want %t", got, want)
}
}
示例14: testInstance
func testInstance(t *testing.T, ctx context.Context) (data.DB, *gaia.Gaia, *httptest.Server) {
db := mem.NewDB()
g := gaia.New(
ctx,
&gaia.Middleware{},
&gaia.Services{
Logger: services.NewTestLogger(t),
DB: db,
SMSCommandSessions: services.NewSMSMux(),
WebCommandSessions: services.NewWebMux(),
},
)
s := httptest.NewServer(g)
return db, g, s
}
示例15: TestTagInadequateInitialization
func TestTagInadequateInitialization(t *testing.T) {
// mock cli.Ui
ui := new(cli.MockUi)
// memory db
db := mem.NewDB()
// a new user, stored in db
user := newTestUser(t, db)
// note: this TagCommand is missing a cli.Ui
missingUI := &TagCommand{
UserID: user.ID().String(),
DB: db,
}
// note: this TagCommand has no UserID
missingUserID := &TagCommand{
UI: ui,
DB: db,
}
// note: this TagCommand lacks a database (DB field)
missingDB := &TagCommand{
UI: ui,
UserID: user.ID().String(),
}
// expect missing a ui to fail
if o := missingUI.Run([]string{"new"}); o != failure {
t.Fatal("TagCommand without ui should fail on run")
}
// expect missing a user id to fail
if o := missingUserID.Run([]string{"new"}); o != failure {
t.Fatal("TagCommand without user id should fail on run")
}
// expect missing a db to fail
if o := missingDB.Run([]string{"new"}); o != failure {
t.Fatal("TagCommand without db should fail on run")
}
}