github.com/CiscoM31/godata@v1.0.10/service_test.go (about) 1 package godata 2 3 import ( 4 "context" 5 "net/url" 6 "testing" 7 ) 8 9 type DummyProvider struct { 10 } 11 12 func (*DummyProvider) GetEntity(*GoDataRequest) (*GoDataResponseField, error) { 13 return nil, NotImplementedError("Dummy provider implements nothing.") 14 } 15 16 func (*DummyProvider) GetEntityCollection(*GoDataRequest) (*GoDataResponseField, error) { 17 return nil, NotImplementedError("Dummy provider implements nothing.") 18 } 19 20 func (*DummyProvider) GetCount(*GoDataRequest) (int, error) { 21 return 0, NotImplementedError("Dummy provider implements nothing.") 22 } 23 24 func (*DummyProvider) GetMetadata() *GoDataMetadata { 25 26 metadata := &GoDataMetadata{ 27 DataServices: &GoDataServices{ 28 Schemas: []*GoDataSchema{ 29 { 30 Namespace: "Store", 31 EntityTypes: []*GoDataEntityType{ 32 { 33 Name: "Customer", 34 Properties: []*GoDataProperty{ 35 { 36 Name: "Name", 37 Type: GoDataString, 38 }, 39 { 40 Name: "Age", 41 Type: GoDataInt32, 42 }, 43 }, 44 NavigationProperties: []*GoDataNavigationProperty{ 45 { 46 Name: "Orders", 47 Type: "Collection(Store.Order)", 48 Partner: "Customer", 49 }, 50 }, 51 }, 52 { 53 Name: "Order", 54 Properties: []*GoDataProperty{ 55 { 56 Name: "Id", 57 Type: GoDataString, 58 }, 59 }, 60 NavigationProperties: []*GoDataNavigationProperty{ 61 { 62 Name: "Customer", 63 Type: "Store.Customer", 64 Partner: "Orders", 65 }, 66 }, 67 }, 68 }, 69 EntityContainers: []*GoDataEntityContainer{ 70 { 71 Name: "Collections", 72 EntitySets: []*GoDataEntitySet{ 73 { 74 Name: "Customers", 75 EntityType: "Store.Customer", 76 NavigationPropertyBindings: []*GoDataNavigationPropertyBinding{ 77 { 78 Path: "Orders", 79 Target: "Orders", 80 }, 81 }, 82 }, 83 { 84 Name: "Orders", 85 EntityType: "Store.Order", 86 NavigationPropertyBindings: []*GoDataNavigationPropertyBinding{ 87 { 88 Path: "Customer", 89 Target: "Customer", 90 }, 91 }, 92 }, 93 }, 94 }, 95 }, 96 }, 97 }, 98 }, 99 } 100 101 return metadata 102 } 103 104 func TestSemanticizeRequest(t *testing.T) { 105 provider := &DummyProvider{} 106 107 testUrl := "Customers?$expand=Orders($expand=*($levels=1))&$filter=Name eq 'Bob'" 108 109 url, err := url.Parse(testUrl) 110 111 if err != nil { 112 t.Error(err) 113 return 114 } 115 ctx := context.Background() 116 req, err := ParseRequest(ctx, url.Path, url.Query()) 117 118 if err != nil { 119 t.Error(err) 120 return 121 } 122 123 service, err := BuildService(provider, "http://localhost") 124 125 if err != nil { 126 t.Error(err) 127 return 128 } 129 130 err = req.SemanticizeRequest(service) 131 132 if err != nil { 133 t.Error(err) 134 return 135 } 136 137 if req.LastSegment.SemanticType != SemanticTypeEntitySet { 138 t.Error("Request last segment semantic type is not SemanticTypeEntitySet") 139 return 140 } 141 142 target := req.LastSegment.SemanticReference.(*GoDataEntitySet).Name 143 if target != "Customers" { 144 t.Error("Request last segment semantic reference name is '" + target + "' not 'Customers'") 145 return 146 } 147 148 if req.Query.Expand.ExpandItems[0].Path[0].SemanticType != SemanticTypeEntity { 149 t.Error("Request expand last segment is not SemanticTypeEntity") 150 return 151 } 152 153 if req.Query.Expand.ExpandItems[0].Expand == nil { 154 t.Error("Request second-level expand is nil!") 155 return 156 } 157 158 target = req.Query.Expand.ExpandItems[0].Expand.ExpandItems[0].Path[0].Value 159 if target != "Customer" { 160 t.Error("Request second-level expand last segment is '" + target + "' not 'Customer'") 161 return 162 } 163 164 target = req.Query.Expand.ExpandItems[0].Expand.ExpandItems[0].Expand.ExpandItems[0].Path[0].Value 165 if target != "Orders" { 166 t.Error("Request third-level expand last segment is '" + target + "' not 'Orders'") 167 return 168 } 169 170 } 171 172 func TestSemanticizeRequestWildcard(t *testing.T) { 173 provider := &DummyProvider{} 174 175 testUrl := "Customers?$expand=*($levels=2)&$filter=Name eq 'Bob'" 176 177 url, err := url.Parse(testUrl) 178 179 if err != nil { 180 t.Error(err) 181 return 182 } 183 ctx := context.Background() 184 req, err := ParseRequest(ctx, url.Path, url.Query()) 185 186 if err != nil { 187 t.Error(err) 188 return 189 } 190 191 service, err := BuildService(provider, "http://localhost") 192 193 if err != nil { 194 t.Error(err) 195 return 196 } 197 198 err = req.SemanticizeRequest(service) 199 200 if err != nil { 201 t.Errorf("Failed to semanticize request. Error: %v", err) 202 return 203 } 204 205 if req.LastSegment.SemanticType != SemanticTypeEntitySet { 206 t.Error("Request last segment semantic type is not SemanticTypeEntitySet") 207 return 208 } 209 210 target := req.LastSegment.SemanticReference.(*GoDataEntitySet).Name 211 if target != "Customers" { 212 t.Error("Request last segment semantic reference name is '" + target + "' not 'Customers'") 213 return 214 } 215 216 if req.Query.Expand.ExpandItems[0].Path[0].SemanticType != SemanticTypeEntity { 217 t.Error("Request expand last segment is not SemanticTypeEntity") 218 return 219 } 220 221 if req.Query.Expand.ExpandItems[0].Expand == nil { 222 t.Error("Request second-level expand is nil!") 223 return 224 } 225 226 target = req.Query.Expand.ExpandItems[0].Expand.ExpandItems[0].Path[0].Value 227 if target != "Customer" { 228 t.Error("Request second-level expand last segment is '" + target + "' not 'Customer'") 229 return 230 } 231 232 target = req.Query.Expand.ExpandItems[0].Expand.ExpandItems[0].Expand.ExpandItems[0].Path[0].Value 233 if target != "Orders" { 234 t.Error("Request third-level expand last segment is '" + target + "' not 'Orders'") 235 return 236 } 237 238 } 239 240 func BenchmarkBuildProvider(b *testing.B) { 241 for n := 0; n < b.N; n++ { 242 provider := &DummyProvider{} 243 244 _, err := BuildService(provider, "http://localhost") 245 246 if err != nil { 247 b.Error(err) 248 return 249 } 250 } 251 } 252 253 func BenchmarkTypicalParseSemanticizeRequest(b *testing.B) { 254 provider := &DummyProvider{} 255 256 testUrl := "Customers?$expand=Orders&$filter=Name eq 'Bob'" 257 258 url, err := url.Parse(testUrl) 259 260 if err != nil { 261 b.Error(err) 262 return 263 } 264 ctx := context.Background() 265 for n := 0; n < b.N; n++ { 266 req, err := ParseRequest(ctx, url.Path, url.Query()) 267 268 if err != nil { 269 b.Error(err) 270 return 271 } 272 273 service, err := BuildService(provider, "http://localhost") 274 275 if err != nil { 276 b.Error(err) 277 return 278 } 279 280 err = req.SemanticizeRequest(service) 281 282 if err != nil { 283 b.Error(err) 284 return 285 } 286 } 287 } 288 289 func BenchmarkWildcardParseSemanticizeRequest(b *testing.B) { 290 provider := &DummyProvider{} 291 292 testUrl := "Customers?$expand=*($levels=2)&$filter=Name eq 'Bob'" 293 294 url, err := url.Parse(testUrl) 295 296 if err != nil { 297 b.Error(err) 298 return 299 } 300 ctx := context.Background() 301 for n := 0; n < b.N; n++ { 302 req, err := ParseRequest(ctx, url.Path, url.Query()) 303 304 if err != nil { 305 b.Error(err) 306 return 307 } 308 309 service, err := BuildService(provider, "http://localhost") 310 311 if err != nil { 312 b.Error(err) 313 return 314 } 315 316 err = req.SemanticizeRequest(service) 317 318 if err != nil { 319 b.Error(err) 320 return 321 } 322 } 323 }