Esempio n. 1
0
func TestStackStatusJSON_Empty(t *testing.T) {
	now := time.Now().UTC()
	stack := NewStack("test-stack", now)
	stack.Update(now)

	expectedStatus := fmt.Sprintf(`{"id":"2f44edeaa249ba81db20e9ddf000ba65","name":"test-stack","peek":null,"size":0,"created_at":"%v","updated_at":"%v","read_at":"%v"}`,
		date.Format(now),
		date.Format(now),
		date.Format(now))
	if status, err := stack.Status().ToJSON(); err != nil {
		t.Fatal(err)
	} else if string(status) != expectedStatus {
		t.Errorf("status is %s, expected %s", string(status), expectedStatus)
	}
}
Esempio n. 2
0
func TestStackStatusJSON(t *testing.T) {
	now := time.Now().UTC()
	after := time.Now().UTC()

	stack := NewStack("test-stack", now)
	stack.Push("test")
	stack.Push(8)
	stack.Push(5.87)
	stack.Push([]byte("test"))
	stack.Update(after)

	expectedStatus := fmt.Sprintf(`{"id":"2f44edeaa249ba81db20e9ddf000ba65","name":"test-stack","peek":"dGVzdA==","size":4,"created_at":"%v","updated_at":"%v","read_at":"%v"}`,
		date.Format(now),
		date.Format(after),
		date.Format(after))
	if status, err := stack.Status().ToJSON(); err != nil {
		t.Fatal(err)
	} else if string(status) != expectedStatus {
		t.Errorf("status is %s, expected %s", string(status), expectedStatus)
	}
}
Esempio n. 3
0
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)
	}
}
Esempio n. 4
0
func TestStacksStatusJSON(t *testing.T) {
	now := time.Now().UTC().UTC()
	after := time.Now().UTC().UTC()

	stack1 := NewStack("test-stack-1", now)
	stack1.Push("test")
	stack1.Push(8)
	stack1.Push(5.87)
	stack1.Push([]byte("test"))
	stack1.Update(after)

	stack2 := NewStack("test-stack-2", now)
	stack2.Push("foo")
	stack2.Push([]byte("bar"))
	stack2.Push(999)
	stack2.Update(after)

	stacksStatus := StacksStatus{
		Stacks: []StackStatus{stack1.Status(), stack2.Status()},
	}

	expectedStatus := fmt.Sprintf(`{"stacks":[{"id":"a0bfff209889f6f782997a7bd5b3d536","name":"test-stack-1","peek":"dGVzdA==","size":4,"created_at":"%v","updated_at":"%v","read_at":"%v"},{"id":"f0d682fdfb3396c6f21e6f4d1d0da1cd","name":"test-stack-2","peek":999,"size":3,"created_at":"%v","updated_at":"%v","read_at":"%v"}]}`,
		date.Format(now), date.Format(after), date.Format(after),
		date.Format(now), date.Format(after), date.Format(after))
	if status, err := stacksStatus.ToJSON(); err != nil {
		t.Fatal(err)
	} else if string(status) != expectedStatus {
		t.Errorf("status is %s, expected %s", string(status), expectedStatus)
	}
}
Esempio n. 5
0
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)
	}
}
Esempio n. 6
0
func TestStacksHandler_GET(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("foo")
	s1.Update(after1)

	s2 := pila.NewStack("stack2", now2)
	s2.Push(1)
	s2.Push(8)
	s2.Update(after2)

	db := pila.NewDatabase("db")
	_ = db.AddStack(s1)
	_ = db.AddStack(s2)

	p := pila.NewPila()
	_ = p.AddDatabase(db)

	conn := NewConn()
	conn.Pila = p

	inputOutput := []struct {
		input, output string
	}{
		{"/databases/db/stacks", fmt.Sprintf(`{"stacks":[{"id":"f0306fec639bd57fc2929c8b897b9b37","name":"stack1","peek":"foo","size":1,"created_at":"%v","updated_at":"%v","read_at":"%v"},{"id":"dde8f895aea2ffa5546336146b9384e7","name":"stack2","peek":8,"size":2,"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))},
		{"/databases/db/stacks?kv", `{"stacks":{"stack1":"foo","stack2":8}}`},
	}

	for _, io := range inputOutput {
		request, err := http.NewRequest("GET", io.input, 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.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 string(stacks) != io.output {
			t.Errorf("stacks are %s, expected %s", string(stacks), io.output)
		}
	}
}