本文整理匯總了Golang中github.com/fern4lvarez/piladb/pila.NewPila函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewPila函數的具體用法?Golang NewPila怎麽用?Golang NewPila使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewPila函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestDatabasesHandler_GET
func TestDatabasesHandler_GET(t *testing.T) {
db := pila.NewDatabase("db")
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
request, err := http.NewRequest("GET", "/databases", nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
conn.databasesHandler(response, request)
if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
}
if response.Code != http.StatusOK {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
}
databases, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if expected := `{"number_of_databases":1,"databases":[{"id":"8cfa8cb55c92fa403369a13fd12a8e01","name":"db","number_of_stacks":0}]}`; string(databases) != expected {
t.Errorf("databases are %s, expected %s", string(databases), expected)
}
}
示例2: NewConn
// NewConn creates and returns a new piladb connection.
func NewConn() *Conn {
conn := &Conn{}
conn.Pila = pila.NewPila()
conn.Config = config.NewConfig().Default()
conn.Status = NewStatus(version.CommitHash(), time.Now(), MemStats())
return conn
}
示例3: TestStacksHandler_GET_BadRequest
func TestStacksHandler_GET_BadRequest(t *testing.T) {
ch := make(chan int)
stack := pila.NewStack("test-stack-channel", time.Now().UTC())
stack.Push(ch)
db := pila.NewDatabase("db")
_ = db.AddStack(stack)
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
request, err := http.NewRequest("GET", "/databases/db/stacks", nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
stacksHandle := conn.stacksHandler("db")
stacksHandle.ServeHTTP(response, request)
if response.Code != http.StatusBadRequest {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusGone)
}
}
示例4: TestPushStackHandler_Empty
func TestPushStackHandler_Empty(t *testing.T) {
s := pila.NewStack("stack", time.Now().UTC())
db := pila.NewDatabase("db")
_ = db.AddStack(s)
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
request, err := http.NewRequest("POST",
fmt.Sprintf("/databases/%s/stacks/%s",
db.ID.String(),
s.ID.String()),
nil)
if err != nil {
t.Fatal(err)
}
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
conn.pushStackHandler(response, request, s)
if pushedElement := db.Stacks[s.ID].Peek(); pushedElement != nil {
t.Errorf("Pushed element is %v, expected nil", pushedElement)
}
if response.Code != http.StatusBadRequest {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusBadRequest)
}
}
示例5: TestCreateStackHandler_Conflict
func TestCreateStackHandler_Conflict(t *testing.T) {
s := pila.NewStack("test-stack", time.Now().UTC())
db := pila.NewDatabase("db")
_ = db.AddStack(s)
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
path := fmt.Sprintf("/databases/%s/stacks/?name=test-stack", db.ID.String())
request, err := http.NewRequest("PUT", path, nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
conn.createStackHandler(response, request, db.ID.String())
if response.Code != http.StatusConflict {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusConflict)
}
}
示例6: TestPopStackHandler_EmptyStack
func TestPopStackHandler_EmptyStack(t *testing.T) {
s := pila.NewStack("stack", time.Now().UTC())
db := pila.NewDatabase("db")
_ = db.AddStack(s)
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
request, err := http.NewRequest("DELETE",
fmt.Sprintf("/databases/%s/stacks/%s",
db.ID.String(),
s.ID.String()),
nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
conn.popStackHandler(response, request, s)
if response.Code != http.StatusNoContent {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusNoContent)
}
}
示例7: TestDatabaseHandler_DELETE_Name
func TestDatabaseHandler_DELETE_Name(t *testing.T) {
db1 := pila.NewDatabase("mydb1")
db2 := pila.NewDatabase("mydb2")
p := pila.NewPila()
_ = p.AddDatabase(db1)
_ = p.AddDatabase(db2)
conn := NewConn()
conn.Pila = p
request, err := http.NewRequest("DELETE",
fmt.Sprintf("/databases/%s",
db1.Name),
nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
databaseHandle := conn.databaseHandler(db1.Name)
databaseHandle.ServeHTTP(response, request)
if response.Code != http.StatusNoContent {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusNoContent)
}
if len(conn.Pila.Databases) != 1 {
t.Errorf("got %d database, expected %d", len(conn.Pila.Databases), 1)
}
}
示例8: TestDeleteStackHandler
func TestDeleteStackHandler(t *testing.T) {
s := pila.NewStack("stack", time.Now().UTC())
db := pila.NewDatabase("db")
_ = db.AddStack(s)
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
s.Push("one")
varss := []map[string]string{
{
"database_id": db.ID.String(),
"stack_id": s.ID.String(),
},
{
"database_id": db.Name,
"stack_id": s.Name,
},
}
for _, vars := range varss {
request, err := http.NewRequest("DELETE",
fmt.Sprintf("/databases/%s/stacks/%s?full",
vars["database_id"],
vars["stack_id"]),
nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
conn.deleteStackHandler(response, request, db, s)
if expectedStack := db.Stacks[uuid.UUID(vars["stack_id"])]; expectedStack != nil {
t.Errorf("db contains %v, expected not to", vars["stack_id"])
}
if response.Code != http.StatusNoContent {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusNoContent)
}
// restore elements for next table test iteration
s = pila.NewStack("stack", time.Now().UTC())
_ = db.AddStack(s)
s.Push("one")
}
}
示例9: TestStacksHandler_GET_Name
func TestStacksHandler_GET_Name(t *testing.T) {
now1 := time.Now().UTC()
after1 := time.Now().UTC()
now2 := time.Now().UTC()
after2 := time.Now().UTC()
s1 := pila.NewStack("stack1", now1)
s1.Push("bar")
s1.Update(after1)
s2 := pila.NewStack("stack2", now2)
s2.Push(`{"a":"b"}`)
s2.Update(after2)
db := pila.NewDatabase("db")
_ = db.AddStack(s1)
_ = db.AddStack(s2)
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
request, err := http.NewRequest("GET", "/databases/db/stacks", nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
stacksHandle := conn.stacksHandler("db")
stacksHandle.ServeHTTP(response, request)
if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
}
if response.Code != http.StatusOK {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
}
stacks, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if expected := fmt.Sprintf(`{"stacks":[{"id":"f0306fec639bd57fc2929c8b897b9b37","name":"stack1","peek":"bar","size":1,"created_at":"%v","updated_at":"%v","read_at":"%v"},{"id":"dde8f895aea2ffa5546336146b9384e7","name":"stack2","peek":"{\"a\":\"b\"}","size":1,"created_at":"%v","updated_at":"%v","read_at":"%v"}]}`,
date.Format(now1), date.Format(after1), date.Format(after1),
date.Format(now2), date.Format(after2), date.Format(after2)); string(stacks) != expected {
t.Errorf("stacks are %s, expected %s", string(stacks), expected)
}
}
示例10: TestPeekStackHandler
func TestPeekStackHandler(t *testing.T) {
s := pila.NewStack("stack", time.Now().UTC())
db := pila.NewDatabase("db")
_ = db.AddStack(s)
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
element := pila.Element{Value: "test-element"}
expectedElementJSON, _ := element.ToJSON()
s.Push(element.Value)
request, err := http.NewRequest("GET",
fmt.Sprintf("/databases/%s/stacks/%s?peek",
db.ID.String(),
s.ID.String()),
nil)
if err != nil {
t.Fatal(err)
}
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
conn.peekStackHandler(response, request, s)
if peekElement := db.Stacks[s.ID].Peek(); peekElement != element.Value {
t.Errorf("peek element is %v, expected %v", peekElement, element.Value)
}
if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
}
if response.Code != http.StatusOK {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
}
elementJSON, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if string(elementJSON) != string(expectedElementJSON) {
t.Errorf("peek element is %v, expected %v", string(elementJSON), string(expectedElementJSON))
}
}
示例11: TestResourceDatabase_False
func TestResourceDatabase_False(t *testing.T) {
dbName := "db"
inputs := []string{dbName, uuid.New(dbName).String()}
for _, input := range inputs {
p := pila.NewPila()
conn := NewConn()
conn.Pila = p
_, ok := ResourceDatabase(conn, input)
if ok {
t.Errorf("ok is %v, expected false", ok)
}
}
}
示例12: TestSizeStackHandler
func TestSizeStackHandler(t *testing.T) {
s := pila.NewStack("stack", time.Now().UTC())
db := pila.NewDatabase("db")
_ = db.AddStack(s)
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
s.Push("element")
expectedSizeJSON := s.SizeToJSON()
request, err := http.NewRequest("GET",
fmt.Sprintf("/databases/%s/stacks/%s?size",
db.ID.String(),
s.ID.String()),
nil)
if err != nil {
t.Fatal(err)
}
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
conn.sizeStackHandler(response, request, s)
if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
}
if response.Code != http.StatusOK {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
}
sizeJSON, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if string(sizeJSON) != string(expectedSizeJSON) {
t.Errorf("size is %v, expected %v", string(sizeJSON), string(expectedSizeJSON))
}
}
示例13: TestDatabaseHandler_GET_Name
func TestDatabaseHandler_GET_Name(t *testing.T) {
s := pila.NewStack("stack", time.Now().UTC())
s.Push("foo")
db := pila.NewDatabase("mydb")
_ = db.AddStack(s)
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
request, err := http.NewRequest("GET",
fmt.Sprintf("/databases/%s",
db.Name),
nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
databaseHandle := conn.databaseHandler(db.Name)
databaseHandle.ServeHTTP(response, request)
if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
}
if response.Code != http.StatusOK {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK)
}
database, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if expected := `{"id":"c13cec0e70876381c78c616ee2d809eb","name":"mydb","number_of_stacks":1,"stacks":["b92f53fa3884305ef798fd8c5d7609ad"]}`; string(database) != expected {
t.Errorf("database is %v, expected %v", string(database), expected)
}
}
示例14: TestCreateStackHandler_Gone
func TestCreateStackHandler_Gone(t *testing.T) {
p := pila.NewPila()
conn := NewConn()
conn.Pila = p
path := fmt.Sprintf("/databases/%s/stacks/?name=test-stack", "12345")
request, err := http.NewRequest("PUT", path, nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
conn.createStackHandler(response, request, "12345")
if response.Code != http.StatusGone {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusGone)
}
}
示例15: TestStacksHandler_PUT_Name
func TestStacksHandler_PUT_Name(t *testing.T) {
db := pila.NewDatabase("db")
p := pila.NewPila()
_ = p.AddDatabase(db)
conn := NewConn()
conn.Pila = p
conn.opDate = time.Now().UTC()
path := fmt.Sprintf("/databases/%s/stacks/?name=test-stack", db.Name)
request, err := http.NewRequest("PUT", path, nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
stacksHandle := conn.stacksHandler(db.ID.String())
stacksHandle.ServeHTTP(response, request)
if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("Content-Type is %v, expected %v", contentType, "application/json")
}
if response.Code != http.StatusCreated {
t.Errorf("response code is %v, expected %v", response.Code, http.StatusCreated)
}
stack, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
expectedStack := fmt.Sprintf(`{"id":"bb4dabeeaa6e90108583ddbf49649427","name":"test-stack","peek":null,"size":0,"created_at":"%v","updated_at":"%v","read_at":"%v"}`,
date.Format(conn.opDate), date.Format(conn.opDate), date.Format(conn.opDate))
if string(stack) != expectedStack {
t.Errorf("stack is %s, expected %s", string(stack), expectedStack)
}
}