-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_writer_test.go
126 lines (104 loc) · 3.28 KB
/
client_writer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package herschel
import (
"fmt"
"image/color"
"os"
"testing"
"time"
"github.com/yokoe/herschel/option"
)
func TestWriting(t *testing.T) {
spreadsheetID := createNewSpreadsheet(t)
c := newTestClient(t)
t.Run("Write values", func(t *testing.T) {
table := NewTable(4, 3)
table.PutValue(0, 0, "Updated: "+time.Now().Format("2006-01-02 03:04"))
table.PutValue(1, 0, 1234567890)
table.PutValue(2, 0, "12345000")
table.PutValue(0, 1, "fuga")
table.PutValue(1, 1, 0.2530)
table.PutValue(2, 1, 49501)
table.PutValue(3, 2, "Hello world")
table.SetBackgroundColor(0, 0, color.Black)
table.SetBackgroundColor(1, 1, color.RGBA{128, 0, 0, 0})
table.SetNumberFormatPattern(1, 0, "#,###")
table.SetNumberFormatPattern(2, 0, "#,##0")
table.SetNumberFormatType(2, 0, "CURRENCY")
table.SetNumberFormatPattern(1, 1, "#.00%")
table.SetNumberFormatPattern(2, 1, "yyyy/MM")
table.SetNumberFormatType(2, 1, "DATE")
table.FrozenRowCount = 1
table.FrozenColumnCount = 2
c.RecreateSheet(spreadsheetID, t.Name())
if err := c.WriteTable(spreadsheetID, t.Name(), table); err != nil {
t.Fatal(err)
}
})
t.Run("Set background colors beyond auto sized range", func(t *testing.T) {
table := NewTable(3, 50)
table.PutValue(0, 0, "Updated: "+time.Now().Format("2006-01-02 03:04"))
table.SetBackgroundColor(2, 49, color.Black)
if err := c.RecreateSheet(spreadsheetID, t.Name()); err != nil {
t.Fatal(err)
}
if err := c.UpdateSheetGridLimits(spreadsheetID, t.Name(), table.GetRows(), table.GetCols()); err != nil {
t.Fatal(err)
}
if err := c.WriteTable(spreadsheetID, t.Name(), table); err != nil {
t.Fatal(err)
}
})
t.Run("Clearing sheet values", func(t *testing.T) {
sheetTitle := t.Name()
if err := c.RecreateSheet(spreadsheetID, sheetTitle); err != nil {
t.Fatal(err)
}
table := NewTable(2, 1)
table.PutValue(0, 0, "Hello")
table.PutValue(1, 0, "World")
if err := c.WriteTable(spreadsheetID, sheetTitle, table); err != nil {
t.Fatal(err)
}
t1, err := c.ReadTable(spreadsheetID, sheetTitle)
if err != nil {
t.Fatal(err)
}
if t1.GetRows() != 2 || t1.GetCols() != 1 {
t.Fatalf("Unexpected dimensions of table. 2 x 1 expected, got %d x %d", t1.GetRows(), t1.GetCols())
}
if t1.GetValue(0, 0) == nil || t1.GetValue(1, 0) == nil {
t.Fatal("Values at (0,0) and (1,0) should not be nil.")
}
if err := c.ClearSheetValues(spreadsheetID, sheetTitle); err != nil {
t.Fatal(err)
}
t2, err := c.ReadTable(spreadsheetID, sheetTitle)
if err != nil {
t.Fatal(err)
}
if t2.GetRows() != 0 || t2.GetCols() != 0 {
t.Fatalf("Unexpected dimensions of table. 0 x 0 after clearing sheet values expected, got %d x %d", t2.GetRows(), t2.GetCols())
}
})
}
/*
* Helper functions
*/
func newTestClient(t *testing.T) *Client {
credentialFilePath := os.Getenv("SPREADSHEET_CREDENTIAL_FILE")
if len(credentialFilePath) == 0 {
t.Skip("SPREADSHEET_CREDENTIAL_FILE is empty.")
}
client, err := NewClient(option.WithServiceAccountCredentials(credentialFilePath))
if err != nil {
t.Fatal(err)
}
return client
}
func createNewSpreadsheet(t *testing.T) string {
ssID, err := newTestClient(t).CreateNewSpreadsheet(fmt.Sprintf("HerschelTest: %s", t.Name()))
if err != nil {
t.Fatalf("Failed to create new spreadsheet: %s", err)
}
return ssID
}