Best Got code snippet using got.Nil
search_aggs_pipeline_test.go
Source:search_aggs_pipeline_test.go
1// Copyright 2012-2015 Oliver Eilhard. All rights reserved.2// Use of this source code is governed by a MIT-license.3// See http://olivere.mit-license.org/license.txt for details.45package elastic67import "testing"89func TestAggsIntegrationAvgBucket(t *testing.T) {10 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))11 client := setupTestClientAndCreateIndexAndAddDocs(t)1213 esversion, err := client.ElasticsearchVersion(DefaultURL)14 if err != nil {15 t.Fatal(err)16 }1718 if esversion < "2.0" {19 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)20 return21 }2223 // Match all should return all documents24 builder := client.Search().25 Index(testIndexName).26 Type("order").27 Query(NewMatchAllQuery()).28 Pretty(true)29 h := NewDateHistogramAggregation().Field("time").Interval("month")30 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))31 builder = builder.Aggregation("sales_per_month", h)32 builder = builder.Aggregation("avg_monthly_sales", NewAvgBucketAggregation().BucketsPath("sales_per_month>sales"))3334 res, err := builder.Do()35 if err != nil {36 t.Fatal(err)37 }38 if res.Hits == nil {39 t.Errorf("expected Hits != nil; got: nil")40 }4142 aggs := res.Aggregations43 if aggs == nil {44 t.Fatal("expected aggregations != nil; got: nil")45 }4647 agg, found := aggs.AvgBucket("avg_monthly_sales")48 if !found {49 t.Fatal("expected avg_monthly_sales aggregation")50 }51 if agg == nil {52 t.Fatal("expected avg_monthly_sales aggregation")53 }54 if agg.Value == nil {55 t.Fatal("expected avg_monthly_sales.value != nil")56 }57 if got, want := *agg.Value, float64(939.2); got != want {58 t.Fatalf("expected avg_monthly_sales.value=%v; got: %v", want, got)59 }60}6162func TestAggsIntegrationDerivative(t *testing.T) {63 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))64 client := setupTestClientAndCreateIndexAndAddDocs(t)6566 esversion, err := client.ElasticsearchVersion(DefaultURL)67 if err != nil {68 t.Fatal(err)69 }7071 if esversion < "2.0" {72 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)73 return74 }7576 // Match all should return all documents77 builder := client.Search().78 Index(testIndexName).79 Type("order").80 Query(NewMatchAllQuery()).81 Pretty(true)82 h := NewDateHistogramAggregation().Field("time").Interval("month")83 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))84 h = h.SubAggregation("sales_deriv", NewDerivativeAggregation().BucketsPath("sales"))85 builder = builder.Aggregation("sales_per_month", h)8687 res, err := builder.Do()88 if err != nil {89 t.Fatal(err)90 }91 if res.Hits == nil {92 t.Errorf("expected Hits != nil; got: nil")93 }9495 aggs := res.Aggregations96 if aggs == nil {97 t.Fatal("expected aggregations != nil; got: nil")98 }99100 agg, found := aggs.DateHistogram("sales_per_month")101 if !found {102 t.Fatal("expected sales_per_month aggregation")103 }104 if agg == nil {105 t.Fatal("expected sales_per_month aggregation")106 }107 if got, want := len(agg.Buckets), 6; got != want {108 t.Fatalf("expected %d buckets; got: %d", want, got)109 }110111 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {112 t.Fatalf("expected DocCount=%d; got: %d", want, got)113 }114 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {115 t.Fatalf("expected DocCount=%d; got: %d", want, got)116 }117 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {118 t.Fatalf("expected DocCount=%d; got: %d", want, got)119 }120 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {121 t.Fatalf("expected DocCount=%d; got: %d", want, got)122 }123 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {124 t.Fatalf("expected DocCount=%d; got: %d", want, got)125 }126 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {127 t.Fatalf("expected DocCount=%d; got: %d", want, got)128 }129130 d, found := agg.Buckets[0].Derivative("sales_deriv")131 if found {132 t.Fatal("expected no sales_deriv aggregation")133 }134 if d != nil {135 t.Fatal("expected no sales_deriv aggregation")136 }137138 d, found = agg.Buckets[1].Derivative("sales_deriv")139 if !found {140 t.Fatal("expected sales_deriv aggregation")141 }142 if d == nil {143 t.Fatal("expected sales_deriv aggregation")144 }145 if d.Value != nil {146 t.Fatal("expected sales_deriv value == nil")147 }148149 d, found = agg.Buckets[2].Derivative("sales_deriv")150 if !found {151 t.Fatal("expected sales_deriv aggregation")152 }153 if d == nil {154 t.Fatal("expected sales_deriv aggregation")155 }156 if d.Value != nil {157 t.Fatal("expected sales_deriv value == nil")158 }159160 d, found = agg.Buckets[3].Derivative("sales_deriv")161 if !found {162 t.Fatal("expected sales_deriv aggregation")163 }164 if d == nil {165 t.Fatal("expected sales_deriv aggregation")166 }167 if d.Value == nil {168 t.Fatal("expected sales_deriv value != nil")169 }170 if got, want := *d.Value, float64(2348.0); got != want {171 t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)172 }173174 d, found = agg.Buckets[4].Derivative("sales_deriv")175 if !found {176 t.Fatal("expected sales_deriv aggregation")177 }178 if d == nil {179 t.Fatal("expected sales_deriv aggregation")180 }181 if d.Value == nil {182 t.Fatal("expected sales_deriv value != nil")183 }184 if got, want := *d.Value, float64(-1658.0); got != want {185 t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)186 }187188 d, found = agg.Buckets[5].Derivative("sales_deriv")189 if !found {190 t.Fatal("expected sales_deriv aggregation")191 }192 if d == nil {193 t.Fatal("expected sales_deriv aggregation")194 }195 if d.Value == nil {196 t.Fatal("expected sales_deriv value != nil")197 }198 if got, want := *d.Value, float64(-722.0); got != want {199 t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)200 }201}202203func TestAggsIntegrationMaxBucket(t *testing.T) {204 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))205 client := setupTestClientAndCreateIndexAndAddDocs(t)206207 esversion, err := client.ElasticsearchVersion(DefaultURL)208 if err != nil {209 t.Fatal(err)210 }211212 if esversion < "2.0" {213 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)214 return215 }216217 // Match all should return all documents218 builder := client.Search().219 Index(testIndexName).220 Type("order").221 Query(NewMatchAllQuery()).222 Pretty(true)223 h := NewDateHistogramAggregation().Field("time").Interval("month")224 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))225 builder = builder.Aggregation("sales_per_month", h)226 builder = builder.Aggregation("max_monthly_sales", NewMaxBucketAggregation().BucketsPath("sales_per_month>sales"))227228 res, err := builder.Do()229 if err != nil {230 t.Fatal(err)231 }232 if res.Hits == nil {233 t.Errorf("expected Hits != nil; got: nil")234 }235236 aggs := res.Aggregations237 if aggs == nil {238 t.Fatal("expected aggregations != nil; got: nil")239 }240241 agg, found := aggs.MaxBucket("max_monthly_sales")242 if !found {243 t.Fatal("expected max_monthly_sales aggregation")244 }245 if agg == nil {246 t.Fatal("expected max_monthly_sales aggregation")247 }248 if got, want := len(agg.Keys), 1; got != want {249 t.Fatalf("expected len(max_monthly_sales.keys)=%d; got: %d", want, got)250 }251 if got, want := agg.Keys[0], "2015-04-01"; got != want {252 t.Fatalf("expected max_monthly_sales.keys[0]=%v; got: %v", want, got)253 }254 if agg.Value == nil {255 t.Fatal("expected max_monthly_sales.value != nil")256 }257 if got, want := *agg.Value, float64(2448); got != want {258 t.Fatalf("expected max_monthly_sales.value=%v; got: %v", want, got)259 }260}261262func TestAggsIntegrationMinBucket(t *testing.T) {263 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))264 client := setupTestClientAndCreateIndexAndAddDocs(t)265266 esversion, err := client.ElasticsearchVersion(DefaultURL)267 if err != nil {268 t.Fatal(err)269 }270271 if esversion < "2.0" {272 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)273 return274 }275276 // Match all should return all documents277 builder := client.Search().278 Index(testIndexName).279 Type("order").280 Query(NewMatchAllQuery()).281 Pretty(true)282 h := NewDateHistogramAggregation().Field("time").Interval("month")283 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))284 builder = builder.Aggregation("sales_per_month", h)285 builder = builder.Aggregation("min_monthly_sales", NewMinBucketAggregation().BucketsPath("sales_per_month>sales"))286287 res, err := builder.Do()288 if err != nil {289 t.Fatal(err)290 }291 if res.Hits == nil {292 t.Errorf("expected Hits != nil; got: nil")293 }294295 aggs := res.Aggregations296 if aggs == nil {297 t.Fatal("expected aggregations != nil; got: nil")298 }299300 agg, found := aggs.MinBucket("min_monthly_sales")301 if !found {302 t.Fatal("expected min_monthly_sales aggregation")303 }304 if agg == nil {305 t.Fatal("expected min_monthly_sales aggregation")306 }307 if got, want := len(agg.Keys), 1; got != want {308 t.Fatalf("expected len(min_monthly_sales.keys)=%d; got: %d", want, got)309 }310 if got, want := agg.Keys[0], "2015-06-01"; got != want {311 t.Fatalf("expected min_monthly_sales.keys[0]=%v; got: %v", want, got)312 }313 if agg.Value == nil {314 t.Fatal("expected min_monthly_sales.value != nil")315 }316 if got, want := *agg.Value, float64(68); got != want {317 t.Fatalf("expected min_monthly_sales.value=%v; got: %v", want, got)318 }319}320321func TestAggsIntegrationSumBucket(t *testing.T) {322 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))323 client := setupTestClientAndCreateIndexAndAddDocs(t)324325 esversion, err := client.ElasticsearchVersion(DefaultURL)326 if err != nil {327 t.Fatal(err)328 }329330 if esversion < "2.0" {331 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)332 return333 }334335 // Match all should return all documents336 builder := client.Search().337 Index(testIndexName).338 Type("order").339 Query(NewMatchAllQuery()).340 Pretty(true)341 h := NewDateHistogramAggregation().Field("time").Interval("month")342 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))343 builder = builder.Aggregation("sales_per_month", h)344 builder = builder.Aggregation("sum_monthly_sales", NewSumBucketAggregation().BucketsPath("sales_per_month>sales"))345346 res, err := builder.Do()347 if err != nil {348 t.Fatal(err)349 }350 if res.Hits == nil {351 t.Errorf("expected Hits != nil; got: nil")352 }353354 aggs := res.Aggregations355 if aggs == nil {356 t.Fatal("expected aggregations != nil; got: nil")357 }358359 agg, found := aggs.SumBucket("sum_monthly_sales")360 if !found {361 t.Fatal("expected sum_monthly_sales aggregation")362 }363 if agg == nil {364 t.Fatal("expected sum_monthly_sales aggregation")365 }366 if agg.Value == nil {367 t.Fatal("expected sum_monthly_sales.value != nil")368 }369 if got, want := *agg.Value, float64(4696.0); got != want {370 t.Fatalf("expected sum_monthly_sales.value=%v; got: %v", want, got)371 }372}373374func TestAggsIntegrationMovAvg(t *testing.T) {375 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))376 client := setupTestClientAndCreateIndexAndAddDocs(t)377378 esversion, err := client.ElasticsearchVersion(DefaultURL)379 if err != nil {380 t.Fatal(err)381 }382383 if esversion < "2.0" {384 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)385 return386 }387388 // Match all should return all documents389 builder := client.Search().390 Index(testIndexName).391 Type("order").392 Query(NewMatchAllQuery()).393 Pretty(true)394 h := NewDateHistogramAggregation().Field("time").Interval("month")395 h = h.SubAggregation("the_sum", NewSumAggregation().Field("price"))396 h = h.SubAggregation("the_movavg", NewMovAvgAggregation().BucketsPath("the_sum"))397 builder = builder.Aggregation("my_date_histo", h)398399 res, err := builder.Do()400 if err != nil {401 t.Fatal(err)402 }403 if res.Hits == nil {404 t.Errorf("expected Hits != nil; got: nil")405 }406407 aggs := res.Aggregations408 if aggs == nil {409 t.Fatal("expected aggregations != nil; got: nil")410 }411412 agg, found := aggs.DateHistogram("my_date_histo")413 if !found {414 t.Fatal("expected sum_monthly_sales aggregation")415 }416 if agg == nil {417 t.Fatal("expected sum_monthly_sales aggregation")418 }419 if got, want := len(agg.Buckets), 6; got != want {420 t.Fatalf("expected %d buckets; got: %d", want, got)421 }422423 d, found := agg.Buckets[0].MovAvg("the_movavg")424 if found {425 t.Fatal("expected no the_movavg aggregation")426 }427 if d != nil {428 t.Fatal("expected no the_movavg aggregation")429 }430431 d, found = agg.Buckets[1].MovAvg("the_movavg")432 if found {433 t.Fatal("expected no the_movavg aggregation")434 }435 if d != nil {436 t.Fatal("expected no the_movavg aggregation")437 }438439 d, found = agg.Buckets[2].MovAvg("the_movavg")440 if !found {441 t.Fatal("expected the_movavg aggregation")442 }443 if d == nil {444 t.Fatal("expected the_movavg aggregation")445 }446 if d.Value == nil {447 t.Fatal("expected the_movavg value")448 }449 if got, want := *d.Value, float64(1290.0); got != want {450 t.Fatalf("expected %v buckets; got: %v", want, got)451 }452453 d, found = agg.Buckets[3].MovAvg("the_movavg")454 if !found {455 t.Fatal("expected the_movavg aggregation")456 }457 if d == nil {458 t.Fatal("expected the_movavg aggregation")459 }460 if d.Value == nil {461 t.Fatal("expected the_movavg value")462 }463 if got, want := *d.Value, float64(695.0); got != want {464 t.Fatalf("expected %v buckets; got: %v", want, got)465 }466467 d, found = agg.Buckets[4].MovAvg("the_movavg")468 if !found {469 t.Fatal("expected the_movavg aggregation")470 }471 if d == nil {472 t.Fatal("expected the_movavg aggregation")473 }474 if d.Value == nil {475 t.Fatal("expected the_movavg value")476 }477 if got, want := *d.Value, float64(1279.3333333333333); got != want {478 t.Fatalf("expected %v buckets; got: %v", want, got)479 }480481 d, found = agg.Buckets[5].MovAvg("the_movavg")482 if !found {483 t.Fatal("expected the_movavg aggregation")484 }485 if d == nil {486 t.Fatal("expected the_movavg aggregation")487 }488 if d.Value == nil {489 t.Fatal("expected the_movavg value")490 }491 if got, want := *d.Value, float64(1157.0); got != want {492 t.Fatalf("expected %v buckets; got: %v", want, got)493 }494}495496func TestAggsIntegrationCumulativeSum(t *testing.T) {497 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))498 client := setupTestClientAndCreateIndexAndAddDocs(t)499500 esversion, err := client.ElasticsearchVersion(DefaultURL)501 if err != nil {502 t.Fatal(err)503 }504505 if esversion < "2.0" {506 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)507 return508 }509510 // Match all should return all documents511 builder := client.Search().512 Index(testIndexName).513 Type("order").514 Query(NewMatchAllQuery()).515 Pretty(true)516 h := NewDateHistogramAggregation().Field("time").Interval("month")517 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))518 h = h.SubAggregation("cumulative_sales", NewCumulativeSumAggregation().BucketsPath("sales"))519 builder = builder.Aggregation("sales_per_month", h)520521 res, err := builder.Do()522 if err != nil {523 t.Fatal(err)524 }525 if res.Hits == nil {526 t.Errorf("expected Hits != nil; got: nil")527 }528529 aggs := res.Aggregations530 if aggs == nil {531 t.Fatal("expected aggregations != nil; got: nil")532 }533534 agg, found := aggs.DateHistogram("sales_per_month")535 if !found {536 t.Fatal("expected sales_per_month aggregation")537 }538 if agg == nil {539 t.Fatal("expected sales_per_month aggregation")540 }541 if got, want := len(agg.Buckets), 6; got != want {542 t.Fatalf("expected %d buckets; got: %d", want, got)543 }544545 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {546 t.Fatalf("expected DocCount=%d; got: %d", want, got)547 }548 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {549 t.Fatalf("expected DocCount=%d; got: %d", want, got)550 }551 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {552 t.Fatalf("expected DocCount=%d; got: %d", want, got)553 }554 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {555 t.Fatalf("expected DocCount=%d; got: %d", want, got)556 }557 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {558 t.Fatalf("expected DocCount=%d; got: %d", want, got)559 }560 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {561 t.Fatalf("expected DocCount=%d; got: %d", want, got)562 }563564 d, found := agg.Buckets[0].CumulativeSum("cumulative_sales")565 if !found {566 t.Fatal("expected cumulative_sales aggregation")567 }568 if d == nil {569 t.Fatal("expected cumulative_sales aggregation")570 }571 if d.Value == nil {572 t.Fatal("expected cumulative_sales value != nil")573 }574 if got, want := *d.Value, float64(1290.0); got != want {575 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)576 }577578 d, found = agg.Buckets[1].CumulativeSum("cumulative_sales")579 if !found {580 t.Fatal("expected cumulative_sales aggregation")581 }582 if d == nil {583 t.Fatal("expected cumulative_sales aggregation")584 }585 if d.Value == nil {586 t.Fatal("expected cumulative_sales value != nil")587 }588 if got, want := *d.Value, float64(1290.0); got != want {589 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)590 }591592 d, found = agg.Buckets[2].CumulativeSum("cumulative_sales")593 if !found {594 t.Fatal("expected cumulative_sales aggregation")595 }596 if d == nil {597 t.Fatal("expected cumulative_sales aggregation")598 }599 if d.Value == nil {600 t.Fatal("expected cumulative_sales value != nil")601 }602 if got, want := *d.Value, float64(1390.0); got != want {603 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)604 }605606 d, found = agg.Buckets[3].CumulativeSum("cumulative_sales")607 if !found {608 t.Fatal("expected cumulative_sales aggregation")609 }610 if d == nil {611 t.Fatal("expected cumulative_sales aggregation")612 }613 if d.Value == nil {614 t.Fatal("expected cumulative_sales value != nil")615 }616 if got, want := *d.Value, float64(3838.0); got != want {617 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)618 }619620 d, found = agg.Buckets[4].CumulativeSum("cumulative_sales")621 if !found {622 t.Fatal("expected cumulative_sales aggregation")623 }624 if d == nil {625 t.Fatal("expected cumulative_sales aggregation")626 }627 if d.Value == nil {628 t.Fatal("expected cumulative_sales value != nil")629 }630 if got, want := *d.Value, float64(4628.0); got != want {631 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)632 }633634 d, found = agg.Buckets[5].CumulativeSum("cumulative_sales")635 if !found {636 t.Fatal("expected cumulative_sales aggregation")637 }638 if d == nil {639 t.Fatal("expected cumulative_sales aggregation")640 }641 if d.Value == nil {642 t.Fatal("expected cumulative_sales value != nil")643 }644 if got, want := *d.Value, float64(4696.0); got != want {645 t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)646 }647}648649func TestAggsIntegrationBucketScript(t *testing.T) {650 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))651 client := setupTestClientAndCreateIndexAndAddDocs(t)652653 esversion, err := client.ElasticsearchVersion(DefaultURL)654 if err != nil {655 t.Fatal(err)656 }657658 if esversion < "2.0" {659 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)660 return661 }662663 // Match all should return all documents664 builder := client.Search().665 Index(testIndexName).666 Type("order").667 Query(NewMatchAllQuery()).668 Pretty(true)669 h := NewDateHistogramAggregation().Field("time").Interval("month")670 h = h.SubAggregation("total_sales", NewSumAggregation().Field("price"))671 appleFilter := NewFilterAggregation().Filter(NewTermQuery("manufacturer", "Apple"))672 appleFilter = appleFilter.SubAggregation("sales", NewSumAggregation().Field("price"))673 h = h.SubAggregation("apple_sales", appleFilter)674 h = h.SubAggregation("apple_percentage",675 NewBucketScriptAggregation().676 GapPolicy("insert_zeros").677 AddBucketsPath("appleSales", "apple_sales>sales").678 AddBucketsPath("totalSales", "total_sales").679 Script(NewScript("appleSales / totalSales * 100")))680 builder = builder.Aggregation("sales_per_month", h)681682 res, err := builder.Do()683 if err != nil {684 t.Fatalf("%v (maybe scripting is disabled?)", err)685 }686 if res.Hits == nil {687 t.Errorf("expected Hits != nil; got: nil")688 }689690 aggs := res.Aggregations691 if aggs == nil {692 t.Fatal("expected aggregations != nil; got: nil")693 }694695 agg, found := aggs.DateHistogram("sales_per_month")696 if !found {697 t.Fatal("expected sales_per_month aggregation")698 }699 if agg == nil {700 t.Fatal("expected sales_per_month aggregation")701 }702 if got, want := len(agg.Buckets), 6; got != want {703 t.Fatalf("expected %d buckets; got: %d", want, got)704 }705706 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {707 t.Fatalf("expected DocCount=%d; got: %d", want, got)708 }709 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {710 t.Fatalf("expected DocCount=%d; got: %d", want, got)711 }712 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {713 t.Fatalf("expected DocCount=%d; got: %d", want, got)714 }715 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {716 t.Fatalf("expected DocCount=%d; got: %d", want, got)717 }718 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {719 t.Fatalf("expected DocCount=%d; got: %d", want, got)720 }721 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {722 t.Fatalf("expected DocCount=%d; got: %d", want, got)723 }724725 d, found := agg.Buckets[0].BucketScript("apple_percentage")726 if !found {727 t.Fatal("expected apple_percentage aggregation")728 }729 if d == nil {730 t.Fatal("expected apple_percentage aggregation")731 }732 if d.Value == nil {733 t.Fatal("expected apple_percentage value != nil")734 }735 if got, want := *d.Value, float64(100.0); got != want {736 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)737 }738739 d, found = agg.Buckets[1].BucketScript("apple_percentage")740 if !found {741 t.Fatal("expected apple_percentage aggregation")742 }743 if d == nil {744 t.Fatal("expected apple_percentage aggregation")745 }746 if d.Value != nil {747 t.Fatal("expected apple_percentage value == nil")748 }749750 d, found = agg.Buckets[2].BucketScript("apple_percentage")751 if !found {752 t.Fatal("expected apple_percentage aggregation")753 }754 if d == nil {755 t.Fatal("expected apple_percentage aggregation")756 }757 if d.Value == nil {758 t.Fatal("expected apple_percentage value != nil")759 }760 if got, want := *d.Value, float64(0.0); got != want {761 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)762 }763764 d, found = agg.Buckets[3].BucketScript("apple_percentage")765 if !found {766 t.Fatal("expected apple_percentage aggregation")767 }768 if d == nil {769 t.Fatal("expected apple_percentage aggregation")770 }771 if d.Value == nil {772 t.Fatal("expected apple_percentage value != nil")773 }774 if got, want := *d.Value, float64(34.64052287581699); got != want {775 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)776 }777778 d, found = agg.Buckets[4].BucketScript("apple_percentage")779 if !found {780 t.Fatal("expected apple_percentage aggregation")781 }782 if d == nil {783 t.Fatal("expected apple_percentage aggregation")784 }785 if d.Value == nil {786 t.Fatal("expected apple_percentage value != nil")787 }788 if got, want := *d.Value, float64(0.0); got != want {789 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)790 }791792 d, found = agg.Buckets[5].BucketScript("apple_percentage")793 if !found {794 t.Fatal("expected apple_percentage aggregation")795 }796 if d == nil {797 t.Fatal("expected apple_percentage aggregation")798 }799 if d.Value == nil {800 t.Fatal("expected apple_percentage value != nil")801 }802 if got, want := *d.Value, float64(0.0); got != want {803 t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)804 }805}806807func TestAggsIntegrationBucketSelector(t *testing.T) {808 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))809 client := setupTestClientAndCreateIndexAndAddDocs(t)810811 esversion, err := client.ElasticsearchVersion(DefaultURL)812 if err != nil {813 t.Fatal(err)814 }815816 if esversion < "2.0" {817 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)818 return819 }820821 // Match all should return all documents822 builder := client.Search().823 Index(testIndexName).824 Type("order").825 Query(NewMatchAllQuery()).826 Pretty(true)827 h := NewDateHistogramAggregation().Field("time").Interval("month")828 h = h.SubAggregation("total_sales", NewSumAggregation().Field("price"))829 h = h.SubAggregation("sales_bucket_filter",830 NewBucketSelectorAggregation().831 AddBucketsPath("totalSales", "total_sales").832 Script(NewScript("totalSales <= 100")))833 builder = builder.Aggregation("sales_per_month", h)834835 res, err := builder.Do()836 if err != nil {837 t.Fatalf("%v (maybe scripting is disabled?)", err)838 }839 if res.Hits == nil {840 t.Errorf("expected Hits != nil; got: nil")841 }842843 aggs := res.Aggregations844 if aggs == nil {845 t.Fatal("expected aggregations != nil; got: nil")846 }847848 agg, found := aggs.DateHistogram("sales_per_month")849 if !found {850 t.Fatal("expected sales_per_month aggregation")851 }852 if agg == nil {853 t.Fatal("expected sales_per_month aggregation")854 }855 if got, want := len(agg.Buckets), 2; got != want {856 t.Fatalf("expected %d buckets; got: %d", want, got)857 }858859 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {860 t.Fatalf("expected DocCount=%d; got: %d", want, got)861 }862 if got, want := agg.Buckets[1].DocCount, int64(2); got != want {863 t.Fatalf("expected DocCount=%d; got: %d", want, got)864 }865}866867func TestAggsIntegrationSerialDiff(t *testing.T) {868 //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))869 client := setupTestClientAndCreateIndexAndAddDocs(t)870871 esversion, err := client.ElasticsearchVersion(DefaultURL)872 if err != nil {873 t.Fatal(err)874 }875876 if esversion < "2.0" {877 t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)878 return879 }880881 // Match all should return all documents882 builder := client.Search().883 Index(testIndexName).884 Type("order").885 Query(NewMatchAllQuery()).886 Pretty(true)887 h := NewDateHistogramAggregation().Field("time").Interval("month")888 h = h.SubAggregation("sales", NewSumAggregation().Field("price"))889 h = h.SubAggregation("the_diff", NewSerialDiffAggregation().BucketsPath("sales").Lag(1))890 builder = builder.Aggregation("sales_per_month", h)891892 res, err := builder.Do()893 if err != nil {894 t.Fatal(err)895 }896 if res.Hits == nil {897 t.Errorf("expected Hits != nil; got: nil")898 }899900 aggs := res.Aggregations901 if aggs == nil {902 t.Fatal("expected aggregations != nil; got: nil")903 }904905 agg, found := aggs.DateHistogram("sales_per_month")906 if !found {907 t.Fatal("expected sales_per_month aggregation")908 }909 if agg == nil {910 t.Fatal("expected sales_per_month aggregation")911 }912 if got, want := len(agg.Buckets), 6; got != want {913 t.Fatalf("expected %d buckets; got: %d", want, got)914 }915916 if got, want := agg.Buckets[0].DocCount, int64(1); got != want {917 t.Fatalf("expected DocCount=%d; got: %d", want, got)918 }919 if got, want := agg.Buckets[1].DocCount, int64(0); got != want {920 t.Fatalf("expected DocCount=%d; got: %d", want, got)921 }922 if got, want := agg.Buckets[2].DocCount, int64(1); got != want {923 t.Fatalf("expected DocCount=%d; got: %d", want, got)924 }925 if got, want := agg.Buckets[3].DocCount, int64(3); got != want {926 t.Fatalf("expected DocCount=%d; got: %d", want, got)927 }928 if got, want := agg.Buckets[4].DocCount, int64(1); got != want {929 t.Fatalf("expected DocCount=%d; got: %d", want, got)930 }931 if got, want := agg.Buckets[5].DocCount, int64(2); got != want {932 t.Fatalf("expected DocCount=%d; got: %d", want, got)933 }934935 d, found := agg.Buckets[0].SerialDiff("the_diff")936 if found {937 t.Fatal("expected no the_diff aggregation")938 }939 if d != nil {940 t.Fatal("expected no the_diff aggregation")941 }942943 d, found = agg.Buckets[1].SerialDiff("the_diff")944 if found {945 t.Fatal("expected no the_diff aggregation")946 }947 if d != nil {948 t.Fatal("expected no the_diff aggregation")949 }950951 d, found = agg.Buckets[2].SerialDiff("the_diff")952 if found {953 t.Fatal("expected no the_diff aggregation")954 }955 if d != nil {956 t.Fatal("expected no the_diff aggregation")957 }958959 d, found = agg.Buckets[3].SerialDiff("the_diff")960 if !found {961 t.Fatal("expected the_diff aggregation")962 }963 if d == nil {964 t.Fatal("expected the_diff aggregation")965 }966 if d.Value == nil {967 t.Fatal("expected the_diff value != nil")968 }969 if got, want := *d.Value, float64(2348.0); got != want {970 t.Fatalf("expected the_diff.value=%v; got: %v", want, got)971 }972973 d, found = agg.Buckets[4].SerialDiff("the_diff")974 if !found {975 t.Fatal("expected the_diff aggregation")976 }977 if d == nil {978 t.Fatal("expected the_diff aggregation")979 }980 if d.Value == nil {981 t.Fatal("expected the_diff value != nil")982 }983 if got, want := *d.Value, float64(-1658.0); got != want {984 t.Fatalf("expected the_diff.value=%v; got: %v", want, got)985 }986987 d, found = agg.Buckets[5].SerialDiff("the_diff")988 if !found {989 t.Fatal("expected the_diff aggregation")990 }991 if d == nil {992 t.Fatal("expected the_diff aggregation")993 }994 if d.Value == nil {995 t.Fatal("expected the_diff value != nil")996 }997 if got, want := *d.Value, float64(-722.0); got != want {998 t.Fatalf("expected the_diff.value=%v; got: %v", want, got)999 }1000}
...
api_crowd_sale.go
Source:api_crowd_sale.go
1package api2import (3 "github.com/ethereum/go-ethereum/common"4 "strconv"5 "tokenlauncher/launcher"6 jsonerrs "tokenlauncher/rpc/errors"7 "tokenlauncher/uint256"8)9type CrowdSaleAPIHandler struct {10 launcher *launcher.Launcher11}12func NewCrowdSaleAPIHandler(opts *launcher.Opts) (*CrowdSaleAPIHandler,error) {13 h := &CrowdSaleAPIHandler{}14 la,err := launcher.NewLauncherDefault(opts)15 if err != nil {16 return nil, err17 }18 h.launcher = la19 return h,nil20}21type CrowdSaleReq struct {22 TokenAddress string23 TargetFunds string24 Price string25 StartTime string26 EndTime string27}28type CrowdSaleValueGet struct {29 Address string30}31type CrowdSaleValueGot struct {32 Value string `json:"value"`33}34type CrowdSaleWithdrawSet struct {35 Address string36}37type CrowdSaleTxGot struct {38 Hash string `json:"hash"`39}40type CrowdSaleTimeSet struct {41 Address string42 Time string43}44type CrowdSalePriceSet struct {45 Address string46 Price string47}48type CrowdSaleTargetFundsSet struct {49 Address string50 Amount string51}52func (handler *CrowdSaleAPIHandler) NewAndDeploy(req CrowdSaleReq, resp *ContractResp) error {53 tokenAddr := common.HexToAddress(req.TokenAddress)54 targetFunds := *uint256.NewUInt256(req.TargetFunds)55 price := *uint256.NewUInt256(req.Price)56 startTime := *uint256.NewUInt256(req.StartTime)57 endTime := *uint256.NewUInt256(req.EndTime)58 opts := &launcher.CrowdSaleOpts{59 TokenAddress: tokenAddr,60 TargetFunds: targetFunds,61 Price: price,62 StartTime: startTime,63 EndTime: endTime,64 }65 contract, err := handler.launcher.DeployCrowdSale(opts)66 if err != nil {67 return jsonerrs.New(-32001,err.Error())68 }69 resp.Address = contract.Address70 resp.TxHash = contract.TxHash71 resp.ABI = contract.ABI72 resp.Data = contract.Data73 return nil74}75func (handler *CrowdSaleAPIHandler) GetFundsSoldTotal(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {76 contractAddr := get.Address77 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)78 if err != nil {79 return jsonerrs.New(-32001,err.Error())80 }81 value,err := crowdSale.FundsSoldTotal(nil)82 if err != nil {83 return jsonerrs.New(-32001,err.Error())84 }85 got.Value = "0x" + value.Text(16)86 return nil87}88func (handler *CrowdSaleAPIHandler) GetFundsRaisedTotal(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {89 contractAddr := get.Address90 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)91 if err != nil {92 return jsonerrs.New(-32001,err.Error())93 }94 value,err := crowdSale.FundsRaisedTotal(nil)95 if err != nil {96 return jsonerrs.New(-32001,err.Error())97 }98 got.Value = "0x" + value.Text(16)99 return nil100}101func (handler *CrowdSaleAPIHandler) GetToken(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {102 contractAddr := get.Address103 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)104 if err != nil {105 return jsonerrs.New(-32001,err.Error())106 }107 value,err := crowdSale.Token(nil)108 if err != nil {109 return jsonerrs.New(-32001,err.Error())110 }111 got.Value = value.Hex()112 return nil113}114func (handler *CrowdSaleAPIHandler) GetPrice(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {115 contractAddr := get.Address116 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)117 if err != nil {118 return jsonerrs.New(-32001,err.Error())119 }120 value,err := crowdSale.Price(nil)121 if err != nil {122 return jsonerrs.New(-32001,err.Error())123 }124 got.Value = "0x" + value.Text(16)125 return nil126}127func (handler *CrowdSaleAPIHandler) GetTargetFunds(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {128 contractAddr := get.Address129 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)130 if err != nil {131 return jsonerrs.New(-32001,err.Error())132 }133 value,err := crowdSale.TargetFunds(nil)134 if err != nil {135 return jsonerrs.New(-32001,err.Error())136 }137 got.Value = "0x" + value.Text(16)138 return nil139}140func (handler *CrowdSaleAPIHandler) GetStartTime(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {141 contractAddr := get.Address142 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)143 if err != nil {144 return jsonerrs.New(-32001,err.Error())145 }146 value,err := crowdSale.StartTime(nil)147 if err != nil {148 return jsonerrs.New(-32001,err.Error())149 }150 got.Value = "0x" + value.Text(16)151 return nil152}153func (handler *CrowdSaleAPIHandler) GetEndTime(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {154 contractAddr := get.Address155 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)156 if err != nil {157 return jsonerrs.New(-32001,err.Error())158 }159 value,err := crowdSale.EndTime(nil)160 if err != nil {161 return jsonerrs.New(-32001,err.Error())162 }163 got.Value = "0x" + value.Text(16)164 return nil165}166func (handler *CrowdSaleAPIHandler) GetTotalAmount(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {167 contractAddr := get.Address168 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)169 if err != nil {170 return jsonerrs.New(-32001,err.Error())171 }172 value,err := crowdSale.TotalAmount(nil)173 if err != nil {174 return jsonerrs.New(-32001,err.Error())175 }176 got.Value = "0x" + value.Text(16)177 return nil178}179func btoi(b bool) int {180 i := 0181 if b {182 i = 1183 }184 return i185}186func (handler *CrowdSaleAPIHandler) IsActive(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {187 contractAddr := get.Address188 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)189 if err != nil {190 return jsonerrs.New(-32001,err.Error())191 }192 value,err := crowdSale.IsActive(nil)193 if err != nil {194 return jsonerrs.New(-32001,err.Error())195 }196 got.Value = strconv.Itoa(btoi(value))197 return nil198}199func (handler *CrowdSaleAPIHandler) IsFinished(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {200 contractAddr := get.Address201 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)202 if err != nil {203 return jsonerrs.New(-32001,err.Error())204 }205 value,err := crowdSale.IsFinished(nil)206 if err != nil {207 return jsonerrs.New(-32001,err.Error())208 }209 got.Value = strconv.Itoa(btoi(value))210 return nil211}212func (handler *CrowdSaleAPIHandler) IsUncompleted(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {213 contractAddr := get.Address214 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)215 if err != nil {216 return jsonerrs.New(-32001,err.Error())217 }218 value,err := crowdSale.IsUncompleted(nil)219 if err != nil {220 return jsonerrs.New(-32001,err.Error())221 }222 got.Value = strconv.Itoa(btoi(value))223 return nil224}225func (handler *CrowdSaleAPIHandler) IsSalesCompleted(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {226 contractAddr := get.Address227 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)228 if err != nil {229 return jsonerrs.New(-32001,err.Error())230 }231 value,err := crowdSale.IsSalesCompleted(nil)232 if err != nil {233 return jsonerrs.New(-32001,err.Error())234 }235 got.Value = strconv.Itoa(btoi(value))236 return nil237}238func (handler *CrowdSaleAPIHandler) Withdraw(set CrowdSaleWithdrawSet, got *CrowdSaleTxGot) error {239 contractAddr := set.Address240 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)241 if err != nil {242 return jsonerrs.New(-32001,err.Error())243 }244 txOpts, err := handler.launcher.DefaultTxOpts()245 if err != nil {246 return jsonerrs.New(-32001,err.Error())247 }248 tx, err := crowdSale.Withdraw(txOpts)249 if err != nil {250 return jsonerrs.New(-32001,err.Error())251 }252 got.Hash = tx.Hash().Hex()253 return nil254}255func (handler *CrowdSaleAPIHandler) SetStartTime(set CrowdSaleTimeSet, got *CrowdSaleTxGot) error {256 contractAddr := set.Address257 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)258 if err != nil {259 return jsonerrs.New(-32001,err.Error())260 }261 txOpts, err := handler.launcher.DefaultTxOpts()262 if err != nil {263 return jsonerrs.New(-32001,err.Error())264 }265 time := uint256.NewUInt256(set.Time)266 tx, err := crowdSale.SetStartTime(txOpts,time.ToBigInt())267 if err != nil {268 return jsonerrs.New(-32001,err.Error())269 }270 got.Hash = tx.Hash().Hex()271 return nil272}273func (handler *CrowdSaleAPIHandler) SetEndTime(set CrowdSaleTimeSet, got *CrowdSaleTxGot) error {274 contractAddr := set.Address275 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)276 if err != nil {277 return jsonerrs.New(-32001,err.Error())278 }279 txOpts, err := handler.launcher.DefaultTxOpts()280 if err != nil {281 return jsonerrs.New(-32001,err.Error())282 }283 time := uint256.NewUInt256(set.Time)284 tx, err := crowdSale.SetEndTime(txOpts,time.ToBigInt())285 if err != nil {286 return jsonerrs.New(-32001,err.Error())287 }288 got.Hash = tx.Hash().Hex()289 return nil290}291func (handler *CrowdSaleAPIHandler) SetPrice(set CrowdSalePriceSet, got *CrowdSaleTxGot) error {292 contractAddr := set.Address293 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)294 if err != nil {295 return jsonerrs.New(-32001,err.Error())296 }297 txOpts, err := handler.launcher.DefaultTxOpts()298 if err != nil {299 return jsonerrs.New(-32001,err.Error())300 }301 price := uint256.NewUInt256(set.Price)302 tx, err := crowdSale.SetPrice(txOpts,price.ToBigInt())303 if err != nil {304 return jsonerrs.New(-32001,err.Error())305 }306 got.Hash = tx.Hash().Hex()307 return nil308}309func (handler *CrowdSaleAPIHandler) SetTargetFunds(set CrowdSaleTargetFundsSet, got *CrowdSaleTxGot) error {310 contractAddr := set.Address311 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)312 if err != nil {313 return jsonerrs.New(-32001,err.Error())314 }315 txOpts, err := handler.launcher.DefaultTxOpts()316 if err != nil {317 return jsonerrs.New(-32001,err.Error())318 }319 amount := uint256.NewUInt256(set.Amount)320 tx, err := crowdSale.SetTargetFunds(txOpts,amount.ToBigInt())321 if err != nil {322 return jsonerrs.New(-32001,err.Error())323 }324 got.Hash = tx.Hash().Hex()325 return nil326}...
errors_test.go
Source:errors_test.go
...22 t.Errorf("New.Error(): got: %q, want %q", got, tt.want)23 }24 }25}26func TestWrapNil(t *testing.T) {27 got := Wrap(nil, "no error")28 if got != nil {29 t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got)30 }31}32func TestWrap(t *testing.T) {33 tests := []struct {34 err error35 message string36 want string37 }{38 {io.EOF, "read error", "read error: EOF"},39 {Wrap(io.EOF, "read error"), "client error", "client error: read error: EOF"},40 }41 for _, tt := range tests {42 got := Wrap(tt.err, tt.message).Error()43 if got != tt.want {44 t.Errorf("Wrap(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)45 }46 }47}48type nilError struct{}49func (nilError) Error() string { return "nil error" }50func TestCause(t *testing.T) {51 x := New("error")52 tests := []struct {53 err error54 want error55 }{{56 // nil error is nil57 err: nil,58 want: nil,59 }, {60 // explicit nil error is nil61 err: (error)(nil),62 want: nil,63 }, {64 // typed nil is nil65 err: (*nilError)(nil),66 want: (*nilError)(nil),67 }, {68 // uncaused error is unaffected69 err: io.EOF,70 want: io.EOF,71 }, {72 // caused error returns cause73 err: Wrap(io.EOF, "ignored"),74 want: io.EOF,75 }, {76 err: x, // return from errors.New77 want: x,78 }, {79 WithMessage(nil, "whoops"),80 nil,81 }, {82 WithMessage(io.EOF, "whoops"),83 io.EOF,84 }, {85 WithStack(nil),86 nil,87 }, {88 WithStack(io.EOF),89 io.EOF,90 }}91 for i, tt := range tests {92 got := Cause(tt.err)93 if !reflect.DeepEqual(got, tt.want) {94 t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)95 }96 }97}98func TestWrapfNil(t *testing.T) {99 got := Wrapf(nil, "no error")100 if got != nil {101 t.Errorf("Wrapf(nil, \"no error\"): got %#v, expected nil", got)102 }103}104func TestWrapf(t *testing.T) {105 tests := []struct {106 err error107 message string108 want string109 }{110 {io.EOF, "read error", "read error: EOF"},111 {Wrapf(io.EOF, "read error without format specifiers"), "client error", "client error: read error without format specifiers: EOF"},112 {Wrapf(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"},113 }114 for _, tt := range tests {115 got := Wrapf(tt.err, tt.message).Error()116 if got != tt.want {117 t.Errorf("Wrapf(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)118 }119 }120}121func TestErrorf(t *testing.T) {122 tests := []struct {123 err error124 want string125 }{126 {Errorf("read error without format specifiers"), "read error without format specifiers"},127 {Errorf("read error with %d format specifier", 1), "read error with 1 format specifier"},128 }129 for _, tt := range tests {130 got := tt.err.Error()131 if got != tt.want {132 t.Errorf("Errorf(%v): got: %q, want %q", tt.err, got, tt.want)133 }134 }135}136func TestWithStackNil(t *testing.T) {137 got := WithStack(nil)138 if got != nil {139 t.Errorf("WithStack(nil): got %#v, expected nil", got)140 }141}142func TestWithStack(t *testing.T) {143 tests := []struct {144 err error145 want string146 }{147 {io.EOF, "EOF"},148 {WithStack(io.EOF), "EOF"},149 }150 for _, tt := range tests {151 got := WithStack(tt.err).Error()152 if got != tt.want {153 t.Errorf("WithStack(%v): got: %v, want %v", tt.err, got, tt.want)154 }155 }156}157func TestWithMessageNil(t *testing.T) {158 got := WithMessage(nil, "no error")159 if got != nil {160 t.Errorf("WithMessage(nil, \"no error\"): got %#v, expected nil", got)161 }162}163func TestWithMessage(t *testing.T) {164 tests := []struct {165 err error166 message string167 want string168 }{169 {io.EOF, "read error", "read error: EOF"},170 {WithMessage(io.EOF, "read error"), "client error", "client error: read error: EOF"},171 }172 for _, tt := range tests {173 got := WithMessage(tt.err, tt.message).Error()174 if got != tt.want {175 t.Errorf("WithMessage(%v, %q): got: %q, want %q", tt.err, tt.message, got, tt.want)176 }177 }178}179func TestWithMessagefNil(t *testing.T) {180 got := WithMessagef(nil, "no error")181 if got != nil {182 t.Errorf("WithMessage(nil, \"no error\"): got %#v, expected nil", got)183 }184}185func TestWithMessagef(t *testing.T) {186 tests := []struct {187 err error188 message string189 want string190 }{191 {io.EOF, "read error", "read error: EOF"},192 {WithMessagef(io.EOF, "read error without format specifier"), "client error", "client error: read error without format specifier: EOF"},193 {WithMessagef(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"},...
Nil
Using AI Code Generation
1import (2func main() {3 fmt.Println(i)4 fmt.Println(*i)5}6panic(0x40c2c0, 0xc42000a0c0)7main.main()8import (9type got struct {10}11func main() {12 fmt.Println(i)13 fmt.Println(i.a)14}15{0}16import (17type got struct {18}19func main() {20 fmt.Println(i)21 fmt.Println(i.a)22}23{10}24import (25type got struct {26}27func main() {28 fmt.Println(i)29 fmt.Println(i.a)30 fmt.Println(j)31 fmt.Println(j.a)
Nil
Using AI Code Generation
1import (2func main() {3 fmt.Println(a == nil)4}5import (6func main() {7 fmt.Println(a == nil)8}
Nil
Using AI Code Generation
1import "fmt"2type Person struct {3}4func (p *Person) Nil() bool {5}6func main() {7fmt.Println(p.Nil())8}
Nil
Using AI Code Generation
1import (2type got struct {3}4func (g *got) Nil() {5 fmt.Println("code to use Nil method of got class")6}7func main() {8 g.Nil()9}10import (11type got struct {12}13func (g got) Nil() {14 fmt.Println("code to use Nil method of got class")15}16func main() {17 g := got{}18 g.Nil()19}20import (21type got struct {22}23func (g got) Nil() {24 fmt.Println("code to use Nil method of got class")25}26func main() {27 g.Nil()28}29import (30type got struct {31}32func (g *got) Nil() {33 fmt.Println("code to use Nil method of got class")34}35func main() {36 g := got{}37 g.Nil()38}39import (40type got struct {41}42func (g *got) Nil() {43 fmt.Println("code to use Nil method of got class")44}45func main() {46 g.Nil()47}48import (49type got struct {50}51func (g got) Nil() {52 fmt.Println("code to use Nil method of got class")53}54func main() {
Nil
Using AI Code Generation
1import "fmt"2type Got struct {3}4func (got Got) Nil() {5fmt.Println("Nil method of got class")6}7func main() {8got.Nil()9}10import "fmt"11type Got struct {12}13func (got *Got) Nil() {14}15func main() {16got.Nil()17fmt.Println(got.Name)18}19import "fmt"20type Got struct {21}22func (got Got) Nil() {23}24func (got Got) Nil(name string) {25}26func main() {27got.Nil()28fmt.Println(got.Name)29}30import "fmt"31type Got struct {32}33func (got Got) Nil() {34}35func (got Got) Nil() {36}37func main() {38got.Nil()39fmt.Println(got.Name)40}
Nil
Using AI Code Generation
1import "fmt"2type Got struct {3}4func (g Got) Nil() {5 fmt.Println("Name:", g.name, "Age:", g.age, "Family:", g.family)6}7func main() {8 g1 := Got{"Jon Snow", 18, "Stark"}9 g1.Nil()10}11import "fmt"12type Got struct {13}14func (g *Got) Nil() {15 fmt.Println("Name:", g.name, "Age:", g.age, "Family:", g.family)16}17func main() {18 g1 := Got{"Jon Snow", 18, "Stark"}19 g1.Nil()20}21import "fmt"22type Got struct {23}24func (g *Got) Nil() {25 fmt.Println("Name:", g.name, "Age:", g.age, "Family:", g.family)26}27func main() {28 g1 := &Got{"Jon Snow", 18, "Stark"}29 g1.Nil()30}
Nil
Using AI Code Generation
1import (2func main() {3 fmt.Println(got.Nil())4}5import (6func main() {7 fmt.Println(got.Nil())8}9import (10func main() {11 fmt.Println(got.Nil())12}13import (14func main() {15 fmt.Println(got.Nil())16}17import (18func main() {19 fmt.Println(got.Nil())20}21import (22func main() {23 fmt.Println(got.Nil())24}25import (26func main() {27 fmt.Println(got.Nil())28}29import (30func main() {31 fmt.Println(got.Nil())32}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!