package engine

import (
	"encoding/json"
	"github.com/stretchr/testify/require"
	"testing"
)

var bs = `
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 478,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "sql": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 85,
      "buckets": [
        {
          "key": "b158d690c4de10a593c5e50b28863783429b594fbe2fa8992a570a3e77892a0d",
          "doc_count": 232,
          "plan_detail": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "a041507bcf81fff1354e449da275341b77430b49e7009dece0c824b4f1b98cb0",
                "doc_count": 232
              }
            ]
          }
        },
        {
          "key": "60e0e53532a49b7078e9c36115b65b9fced116003b6ae704c19c7c3be5dc72a6",
          "doc_count": 66,
          "plan_detail": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "18a5cc9619327884ed31a75b2e9359ec84aebe47eec69fae31145386544fc91e",
                "doc_count": 66
              }
            ]
          }
        },
        {
          "key": "c13a84af9e0162969f43147c99b2ddf4363bbcdcefa0e6f67be129776259b505",
          "doc_count": 30,
          "plan_detail": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "f6fbaad6bb57dce2464b8f1e8108407159f70fc80140ae6afa2b6f00b74fc3b8",
                "doc_count": 30
              }
            ]
          }
        },
        {
          "key": "49986f6a5838822d6471c61612a1d29ea99f46aebf13702ba9414e86752976ce",
          "doc_count": 17,
          "plan_detail": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "18044ee2b5c9c99cfe3014a574aa4f1607d74352432788b99878c86ce4531016",
                "doc_count": 17
              }
            ]
          }
        },
        {
          "key": "6f11ac521b7a3b8d48d8b663b82e45081b73cd1aae3bf142c7517ea31154f48d",
          "doc_count": 13,
          "plan_detail": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "639d7c82ecaf021c6c1a28edc663664d232027e83780ac64cfabb5751f3ef955",
                "doc_count": 13
              }
            ]
          }
        },
        {
          "key": "2d460a057666a172d33e59e5262957ddc11511b644fc717aab2a42b9558c790b",
          "doc_count": 12,
          "plan_detail": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "44f2560b34b11d9fa8b118d0c4a81a1ecdb411125ad991f095176d79aa93d3db",
                "doc_count": 12
              }
            ]
          }
        },
        {
          "key": "7edb284efe811d926675c04c868b824a0b928a0e3f8a0d48a764047deba1e5a0",
          "doc_count": 8,
          "plan_detail": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "528e9afb95ac63d9fc4989eab838a5b5d3c0becb4fc42d4335eeda3d5265c5e1",
                "doc_count": 5
              },
              {
                "key": "23c560a662a726067b70741ee9d4de8a46ab2e440149fb31a312376b87db22a4",
                "doc_count": 3
              }
            ]
          }
        },
        {
          "key": "ecd4add2c92c08dfe362ddead27c0c1cda6ac64aeca899069daa0e661f73c16f",
          "doc_count": 6,
          "plan_detail": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "4cdb2bc42e227f305397e5aaf69e499d8bc1f9fc392fb1af896835d8e3e37867",
                "doc_count": 6
              }
            ]
          }
        },
        {
          "key": "b66e2c59c49db1c5d37698c07f35045298859bac7f6625754d6e9ca21c510469",
          "doc_count": 5,
          "plan_detail": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "37f404783185031955481e725f8a2f8272cb6627b9b4ef3253a6e99a8607149e",
                "doc_count": 5
              }
            ]
          }
        },
        {
          "key": "5e03534b2fc23c867b6fc00add330a4beedce95bc3e3c16c76d1af1bb1b674d5",
          "doc_count": 4,
          "plan_detail": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "afc8304862b7dd730e83ec5122209983092788c49a97f8132db8a868ee950bae",
                "doc_count": 4
              }
            ]
          }
        }
      ]
    }
  }
}`

func TestMarshalSQLPlan(t *testing.T) {
	assert := require.New(t)
	result := QueryResult{}
	result.Aggregations.SQL = BuildAggregatedPlan(int(0x4002))
	err := json.Unmarshal([]byte(bs), &result)
	assert.Nil(err)
	plan, err := result.Aggregations.SQL.GetAbnormalPlan()
	assert.Nil(err)
	t.Log(plan)
}
