little bit of testing

This commit is contained in:
Patrick Veverka 2022-02-24 20:21:06 +00:00 committed by GitHub
parent 3d093d1a95
commit 86feaff6a9

View file

@ -301,6 +301,34 @@ func TestCodespace_ExportData(t *testing.T) {
"name": "test",
},
},
{
name: "just owner",
fields: fields{
Owner: User{
Login: "test",
},
},
args: args{
fields: []string{"owner"},
},
want: map[string]interface{}{
"owner": "test",
},
},
{
name: "just machine",
fields: fields{
Machine: CodespaceMachine{
Name: "test",
},
},
args: args{
fields: []string{"machineName"},
},
want: map[string]interface{}{
"machineName": "test",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -322,3 +350,87 @@ func TestCodespace_ExportData(t *testing.T) {
})
}
}
func createFakeEditServer(t *testing.T, codespaceName string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
checkPath := "/user/codespaces/" + codespaceName
if r.URL.Path != checkPath {
t.Fatal("Incorrect path")
}
if r.Method != http.MethodPatch {
t.Fatal("Incorrect method")
}
body := r.Body
if body == nil {
t.Fatal("No body")
}
defer body.Close()
var data map[string]interface{}
err := json.NewDecoder(body).Decode(&data)
if err != nil {
t.Fatal(err)
}
if data["display_name"] != "changeTo" {
t.Fatal("Incorrect display name")
}
response := Codespace{
DisplayName: "changeTo",
}
responseData, _ := json.Marshal(response)
fmt.Fprint(w, string(responseData))
}))
}
func TestAPI_EditCodespace(t *testing.T) {
type args struct {
ctx context.Context
codespaceName string
params *EditCodespaceParams
}
tests := []struct {
name string
args args
want *Codespace
wantErr bool
}{
{
name: "success",
args: args{
ctx: context.Background(),
codespaceName: "test",
params: &EditCodespaceParams{
DisplayName: "changeTo",
},
},
want: &Codespace{
DisplayName: "changeTo",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
svr := createFakeEditServer(t, tt.args.codespaceName)
defer svr.Close()
a := &API{
client: &http.Client{},
githubAPI: svr.URL,
}
got, err := a.EditCodespace(tt.args.ctx, tt.args.codespaceName, tt.args.params)
if (err != nil) != tt.wantErr {
t.Errorf("API.EditCodespace() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("API.EditCodespace() = %v, want %v", got.DisplayName, tt.want.DisplayName)
}
})
}
}