func Test_setupDatabase_CreatesDatabase_IfNotExists(t *testing.T) { databaseExistsCalled := false createDatbaseCalled := false ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" && r.URL.Path == "/logs" { databaseExistsCalled = true w.WriteHeader(http.StatusNotFound) } else if r.Method == "PUT" && r.URL.Path == "/logs" { createDatbaseCalled = true w.WriteHeader(http.StatusCreated) } })) defer ts.Close() NewCouchDb(ts.URL + "/").setup() assert.True(t, databaseExistsCalled) assert.True(t, createDatbaseCalled) }
func Test_AllKeys_ReturnsData(t *testing.T) { dbCalled := false ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { dbCalled = true assert.Equal(t, "/logs/_design/keys/_view/keys", r.URL.Path) assert.Equal(t, "group=true", r.URL.RawQuery) w.WriteHeader(http.StatusOK) fmt.Fprintf(w, `{"rows":[{"key":"foo","value":1}]}`) })) defer ts.Close() db := couchDb(ts.URL + "/") res := db.AllKeys() assert.True(t, dbCalled) assert.Equal(t, map[string]int{"foo": 1}, res) }