github.com/optim-corp/cios-golang-sdk@v0.5.1/sdk/service_contract_test.go (about) 1 package ciossdk 2 3 import ( 4 "context" 5 "encoding/json" 6 "net/http" 7 "net/http/httptest" 8 "net/url" 9 "testing" 10 11 srvcontract "github.com/optim-corp/cios-golang-sdk/sdk/service/contract" 12 13 cnv "github.com/fcfcqloow/go-advance/convert" 14 xmath "github.com/fcfcqloow/go-advance/math" 15 "github.com/optim-corp/cios-golang-sdk/cios" 16 sdkmodel "github.com/optim-corp/cios-golang-sdk/model" 17 ) 18 19 func TestContract_GetContracts(t *testing.T) { 20 var ( 21 query url.Values 22 ctx context.Context 23 24 tests = []struct { 25 params cios.ApiGetContractsRequest 26 test func() 27 }{ 28 { 29 params: srvcontract.MakeGetContractsOpts().Limit(1000), 30 test: func() { 31 if query.Get("limit") != "1000" { 32 t.Fatal("Missing Query", query.Encode()) 33 } else { 34 t.Log(query.Encode()) 35 } 36 }, 37 }, 38 { 39 params: srvcontract.MakeGetContractsOpts().Limit(1000).Offset(50), 40 test: func() { 41 if query.Get("limit") != "1000" || query.Get("offset") != "50" { 42 t.Fatal("Missing Query", query.Encode()) 43 } else { 44 t.Log(query.Encode()) 45 } 46 }, 47 }, 48 { 49 params: srvcontract.MakeGetContractsOpts().Page("aaaaa"), 50 test: func() { 51 if query.Get("page") != "aaaaa" { 52 t.Fatal("Missing Query", query.Encode()) 53 } else { 54 t.Log(query.Encode()) 55 } 56 }, 57 }, 58 { 59 params: srvcontract.MakeGetContractsOpts().Page(""), 60 test: func() { 61 if query.Encode() != "" { 62 t.Fatal("Missing Query", query.Encode()) 63 } else { 64 t.Log(query.Encode()) 65 } 66 }, 67 }, 68 } 69 ) 70 71 // Query Test 72 responseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 73 query = r.URL.Query() 74 w.Header().Set("Content-Type", "application/json") 75 json.NewEncoder(w).Encode(cios.MultipleContract{Total: 10}) 76 }) 77 ts := httptest.NewServer(responseHandler) 78 client := NewCiosClient( 79 CiosClientConfig{ 80 Urls: sdkmodel.CIOSUrl{ContractUrl: ts.URL}, 81 }, 82 ) 83 defer ts.Close() 84 for _, test := range tests { 85 client.Contract().GetContracts(ctx, test.params) 86 test.test() 87 } 88 ts.Close() 89 } 90 91 func TestContract_GetContractsAll(t *testing.T) { 92 var offsets []int 93 var limits []int 94 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 95 w.Header().Set("Content-Type", "application/json") 96 response := cios.MultipleContract{Total: 3500, Contracts: []cios.Contract{}} 97 offset := cnv.MustInt(r.URL.Query().Get("offset")) 98 limit := cnv.MustInt(r.URL.Query().Get("limit")) 99 offsets = append(offsets, offset) 100 limits = append(limits, limit) 101 for i := 0; i < xmath.MinInt(3500-offset, 1000, limit); i++ { 102 response.Contracts = append(response.Contracts, cios.Contract{Id: cnv.StrPtr(cnv.MustStr(i))}) 103 } 104 json.NewEncoder(w).Encode(response) 105 })) 106 defer ts.Close() 107 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{ContractUrl: ts.URL}}) 108 109 responses, _, _ := client.Contract().GetContractsAll(nil, srvcontract.MakeGetContractsOpts().Limit(999)) 110 if len(responses) != 999 || offsets[0] != 0 && limits[0] != 1000 { 111 t.Fatal(len(responses)) 112 } 113 114 offsets = []int{} 115 limits = []int{} 116 responses, _, _ = client.Contract().GetContractsAll(nil, srvcontract.MakeGetContractsOpts().Limit(1500)) 117 if len(responses) != 1500 || offsets[0] != 0 && limits[0] != 1000 || offsets[1] != 1000 && limits[1] != 1000 { 118 t.Fatal(len(responses), limits, offsets) 119 } 120 offsets = []int{} 121 limits = []int{} 122 responses, _, _ = client.Contract().GetContractsAll(nil, srvcontract.MakeGetContractsOpts().Limit(2001)) 123 if len(responses) != 2001 || offsets[0] != 0 && limits[0] != 1000 || offsets[1] != 1000 && limits[1] != 1000 || offsets[2] != 2000 || limits[2] != 1 { 124 t.Fatal(len(responses), limits, offsets) 125 126 } 127 offsets = []int{} 128 limits = []int{} 129 responses, _, _ = client.Contract().GetContractsAll(nil, srvcontract.MakeGetContractsOpts().Limit(3501)) 130 if len(responses) != 3500 || 131 offsets[0] != 0 || limits[0] != 1000 || 132 offsets[1] != 1000 && limits[1] != 1000 || 133 offsets[2] != 2000 || limits[2] != 1000 || 134 offsets[3] != 3000 || limits[3] != 501 { 135 t.Fatal(len(responses), limits, offsets) 136 } 137 offsets = []int{} 138 limits = []int{} 139 responses, _, _ = client.Contract().GetContractsAll(nil, srvcontract.MakeGetContractsOpts().Limit(2001).Offset(20)) 140 if len(responses) != 2001 || offsets[0] != 20 && limits[0] != 1000 || offsets[1] != 1020 && limits[1] != 1000 || offsets[2] != 2020 || limits[2] != 1 { 141 t.Fatal(len(responses), limits, offsets) 142 143 } 144 } 145 146 func TestContract_GetContractsUnlimited(t *testing.T) { 147 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 148 w.Header().Set("Content-Type", "application/json") 149 response := cios.MultipleContract{Total: 3500, Contracts: []cios.Contract{}} 150 offset := cnv.MustInt(r.URL.Query().Get("offset")) 151 limit := cnv.MustInt(r.URL.Query().Get("limit")) 152 for i := 0; i < xmath.MinInt(3500-offset, 1000, limit); i++ { 153 response.Contracts = append(response.Contracts, cios.Contract{Id: cnv.StrPtr(cnv.MustStr(i))}) 154 } 155 json.NewEncoder(w).Encode(response) 156 })) 157 defer ts.Close() 158 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{ContractUrl: ts.URL}}) 159 160 responses, _, _ := client.Contract().GetContractsUnlimited(nil, srvcontract.MakeGetContractsOpts().Limit(1)) 161 if len(responses) != 3500 { 162 t.Fatal(len(responses)) 163 } 164 }