handle-neo4j_test.go raw

   1  package app
   2  
   3  import (
   4  	"encoding/json"
   5  	"net/http"
   6  	"net/http/httptest"
   7  	"testing"
   8  
   9  	"next.orly.dev/app/config"
  10  )
  11  
  12  func TestHandleNeo4jConfig_ReturnsDBType(t *testing.T) {
  13  	s := &Server{Config: &config.C{DBType: "neo4j"}}
  14  
  15  	req := httptest.NewRequest(http.MethodGet, "/api/neo4j/config", nil)
  16  	rr := httptest.NewRecorder()
  17  
  18  	s.handleNeo4jConfig(rr, req)
  19  
  20  	if rr.Code != http.StatusOK {
  21  		t.Fatalf("expected 200, got %d", rr.Code)
  22  	}
  23  
  24  	var resp Neo4jConfigResponse
  25  	if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
  26  		t.Fatalf("decode response: %v", err)
  27  	}
  28  	if resp.DBType != "neo4j" {
  29  		t.Errorf("db_type: got %q, want %q", resp.DBType, "neo4j")
  30  	}
  31  }
  32  
  33  func TestHandleNeo4jConfig_ReturnsBadger(t *testing.T) {
  34  	s := &Server{Config: &config.C{DBType: "badger"}}
  35  
  36  	req := httptest.NewRequest(http.MethodGet, "/api/neo4j/config", nil)
  37  	rr := httptest.NewRecorder()
  38  
  39  	s.handleNeo4jConfig(rr, req)
  40  
  41  	if rr.Code != http.StatusOK {
  42  		t.Fatalf("expected 200, got %d", rr.Code)
  43  	}
  44  
  45  	var resp Neo4jConfigResponse
  46  	if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
  47  		t.Fatalf("decode response: %v", err)
  48  	}
  49  	if resp.DBType != "badger" {
  50  		t.Errorf("db_type: got %q, want %q", resp.DBType, "badger")
  51  	}
  52  }
  53  
  54  func TestHandleNeo4jConfig_MethodNotAllowed(t *testing.T) {
  55  	s := &Server{Config: &config.C{DBType: "neo4j"}}
  56  
  57  	req := httptest.NewRequest(http.MethodPost, "/api/neo4j/config", nil)
  58  	rr := httptest.NewRecorder()
  59  
  60  	s.handleNeo4jConfig(rr, req)
  61  
  62  	if rr.Code != http.StatusMethodNotAllowed {
  63  		t.Errorf("expected 405, got %d", rr.Code)
  64  	}
  65  }
  66