|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "google.golang.org/api/docs/v1" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDocsTablePinHeaderZeroIsSerializedForAllTables(t *testing.T) { |
| 13 | + doc := docsTableOpsTestDocument( |
| 14 | + docsTableOpsTestElement(5, "First", 2), |
| 15 | + docsTableOpsTestElement(40, "Second", 3), |
| 16 | + ) |
| 17 | + var got map[string]any |
| 18 | + docSvc, cleanup := newDocsServiceForTest(t, func(w http.ResponseWriter, r *http.Request) { |
| 19 | + w.Header().Set("Content-Type", "application/json") |
| 20 | + switch { |
| 21 | + case r.Method == http.MethodGet: |
| 22 | + _ = json.NewEncoder(w).Encode(doc) |
| 23 | + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, ":batchUpdate"): |
| 24 | + if err := json.NewDecoder(r.Body).Decode(&got); err != nil { |
| 25 | + t.Fatalf("decode batch: %v", err) |
| 26 | + } |
| 27 | + _ = json.NewEncoder(w).Encode(&docs.BatchUpdateDocumentResponse{DocumentId: "doc1"}) |
| 28 | + default: |
| 29 | + http.NotFound(w, r) |
| 30 | + } |
| 31 | + }) |
| 32 | + defer cleanup() |
| 33 | + |
| 34 | + cmd := &DocsTablePinHeaderCmd{} |
| 35 | + err := runKong( t, cmd, [] string{ "doc1", "--table", "*", "--rows", "0"}, newDocsTableOpsTestContext( t, docSvc), &RootFlags{ Account: "[email protected]"}) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("Run: %v", err) |
| 38 | + } |
| 39 | + writeControl, ok := got["writeControl"].(map[string]any) |
| 40 | + if !ok || writeControl["requiredRevisionId"] != "rev-1" { |
| 41 | + t.Fatalf("write control = %#v", got["writeControl"]) |
| 42 | + } |
| 43 | + requests, ok := got["requests"].([]any) |
| 44 | + if !ok || len(requests) != 2 { |
| 45 | + t.Fatalf("requests = %#v", got["requests"]) |
| 46 | + } |
| 47 | + wantStarts := []float64{40, 5} |
| 48 | + for i, rawRequest := range requests { |
| 49 | + request, ok := rawRequest.(map[string]any) |
| 50 | + if !ok { |
| 51 | + t.Fatalf("request %d = %#v", i, rawRequest) |
| 52 | + } |
| 53 | + pin, ok := request["pinTableHeaderRows"].(map[string]any) |
| 54 | + if !ok { |
| 55 | + t.Fatalf("pin request %d = %#v", i, request) |
| 56 | + } |
| 57 | + count, exists := pin["pinnedHeaderRowsCount"] |
| 58 | + if !exists || count != float64(0) { |
| 59 | + t.Fatalf("pinned count %d = %#v, exists=%v", i, count, exists) |
| 60 | + } |
| 61 | + location := pin["tableStartLocation"].(map[string]any) |
| 62 | + if location["index"] != wantStarts[i] { |
| 63 | + t.Fatalf("table start %d = %#v, want %v", i, location["index"], wantStarts[i]) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestDocsTablePinHeaderRejectsTooManyRows(t *testing.T) { |
| 69 | + doc := docsTableOpsTestDocument(docsTableOpsTestElement(5, "Header", 2)) |
| 70 | + postCount := 0 |
| 71 | + docSvc, cleanup := newDocsServiceForTest(t, func(w http.ResponseWriter, r *http.Request) { |
| 72 | + w.Header().Set("Content-Type", "application/json") |
| 73 | + if r.Method == http.MethodGet { |
| 74 | + _ = json.NewEncoder(w).Encode(doc) |
| 75 | + return |
| 76 | + } |
| 77 | + if r.Method == http.MethodPost { |
| 78 | + postCount++ |
| 79 | + } |
| 80 | + http.NotFound(w, r) |
| 81 | + }) |
| 82 | + defer cleanup() |
| 83 | + |
| 84 | + cmd := &DocsTablePinHeaderCmd{} |
| 85 | + err := runKong( t, cmd, [] string{ "doc1", "--rows", "3"}, newDocsTableOpsTestContext( t, docSvc), &RootFlags{ Account: "[email protected]"}) |
| 86 | + if err == nil || !strings.Contains(err.Error(), "table has 2 rows") { |
| 87 | + t.Fatalf("expected row-count error, got %v", err) |
| 88 | + } |
| 89 | + if postCount != 0 { |
| 90 | + t.Fatalf("post count = %d, want 0", postCount) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestDocsTablePinHeaderRejectsNegativeRows(t *testing.T) { |
| 95 | + cmd := &DocsTablePinHeaderCmd{DocID: "doc1", Table: "1", Rows: -1} |
| 96 | + err := cmd. Run( newCmdRuntimeOutputContext( t, nil, nil), &RootFlags{ Account: "[email protected]", DryRun: true}) |
| 97 | + if err == nil || !strings.Contains(err.Error(), "--rows must be >= 0") { |
| 98 | + t.Fatalf("expected rows error, got %v", err) |
| 99 | + } |
| 100 | +} |
0 commit comments