github.com/google/go-github/v74@v74.0.0/github/repos_properties_test.go (about) 1 // Copyright 2023 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 "net/http" 12 "testing" 13 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestRepositoriesService_GetAllCustomPropertyValues(t *testing.T) { 18 t.Parallel() 19 client, mux, _ := setup(t) 20 21 mux.HandleFunc("/repos/o/r/properties/values", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 fmt.Fprint(w, `[ 24 { 25 "property_name": "environment", 26 "value": "production" 27 }, 28 { 29 "property_name": "service", 30 "value": "web" 31 }, 32 { 33 "property_name": "languages", 34 "value": ["Go", "JavaScript"] 35 }, 36 { 37 "property_name": "null_property", 38 "value": null 39 } 40 ]`) 41 }) 42 43 ctx := context.Background() 44 customPropertyValues, _, err := client.Repositories.GetAllCustomPropertyValues(ctx, "o", "r") 45 if err != nil { 46 t.Errorf("Repositories.GetAllCustomPropertyValues returned error: %v", err) 47 } 48 49 want := []*CustomPropertyValue{ 50 { 51 PropertyName: "environment", 52 Value: "production", 53 }, 54 { 55 PropertyName: "service", 56 Value: "web", 57 }, 58 { 59 PropertyName: "languages", 60 Value: []string{"Go", "JavaScript"}, 61 }, 62 { 63 PropertyName: "null_property", 64 Value: nil, 65 }, 66 } 67 68 if !cmp.Equal(customPropertyValues, want) { 69 t.Errorf("Repositories.GetAllCustomPropertyValues returned %+v, want %+v", customPropertyValues, want) 70 } 71 72 const methodName = "GetAllCustomPropertyValues" 73 74 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 75 got, resp, err := client.Repositories.GetAllCustomPropertyValues(ctx, "o", "r") 76 if got != nil { 77 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 78 } 79 return resp, err 80 }) 81 } 82 83 func TestRepositoriesService_CreateOrUpdateCustomProperties(t *testing.T) { 84 t.Parallel() 85 client, mux, _ := setup(t) 86 87 mux.HandleFunc("/repos/usr/r/properties/values", func(w http.ResponseWriter, r *http.Request) { 88 testMethod(t, r, "PATCH") 89 w.WriteHeader(http.StatusNoContent) 90 }) 91 92 ctx := context.Background() 93 repoCustomProperty := []*CustomPropertyValue{ 94 { 95 PropertyName: "environment", 96 Value: "production", 97 }, 98 } 99 _, err := client.Repositories.CreateOrUpdateCustomProperties(ctx, "usr", "r", repoCustomProperty) 100 if err != nil { 101 t.Errorf("Repositories.CreateOrUpdateCustomProperties returned error: %v", err) 102 } 103 104 const methodName = "CreateOrUpdateCustomProperties" 105 106 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 107 return client.Repositories.CreateOrUpdateCustomProperties(ctx, "usr", "r", repoCustomProperty) 108 }) 109 }