github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/template_test.go (about)

     1  package linebot
     2  
     3  import (
     4  	"reflect"
     5  	"strconv"
     6  	"testing"
     7  )
     8  
     9  func TestUnmarshalTemplateJSON(t *testing.T) {
    10  	testCases := []struct {
    11  		JSON []byte
    12  		Want Template
    13  	}{
    14  		{
    15  			JSON: []byte(`{
    16  	"type": "buttons",
    17  	"thumbnailImageUrl": "https://example.com/image.jpg",
    18  	"title": "Menu",
    19  	"text": "Please select",
    20  	"actions": [
    21  		{
    22  			"type": "postback",
    23  			"label": "postback",
    24  			"data": "action=buy&itemid=1",
    25  			"displayText": "postback text"
    26  		},
    27  		{
    28  			"type": "message",
    29  			"label": "message",
    30  			"text": "message text"
    31  		},
    32  		{
    33  			"type": "uri",
    34  			"label": "uri",
    35  			"uri": "http://example.com/",
    36  			"altUri": {
    37  				"desktop": "http://example.com/desktop"
    38  			}
    39  		}
    40  	]
    41  }`),
    42  			Want: &ButtonsTemplate{
    43  				ThumbnailImageURL: "https://example.com/image.jpg",
    44  				Title:             "Menu",
    45  				Text:              "Please select",
    46  				Actions: []TemplateAction{
    47  					&PostbackAction{
    48  						Label:       "postback",
    49  						DisplayText: "postback text",
    50  						Data:        "action=buy&itemid=1",
    51  					},
    52  					&MessageAction{
    53  						Label: "message",
    54  						Text:  "message text",
    55  					},
    56  					&URIAction{
    57  						Label: "uri",
    58  						URI:   "http://example.com/",
    59  						AltURI: &URIActionAltURI{
    60  							Desktop: "http://example.com/desktop",
    61  						},
    62  					},
    63  				},
    64  			},
    65  		},
    66  		{
    67  			JSON: []byte(`{
    68  	"type": "confirm",
    69  	"text": "Are you sure?",
    70  	"actions": [
    71  		{
    72  			"type": "message",
    73  			"label": "Yes",
    74  			"text": "yes"
    75  		},
    76  		{
    77  			"type": "message",
    78  			"label": "No",
    79  			"text": "no"
    80  		}
    81  	]
    82  }`),
    83  			Want: &ConfirmTemplate{
    84  				Text: "Are you sure?",
    85  				Actions: []TemplateAction{
    86  					&MessageAction{
    87  						Label: "Yes",
    88  						Text:  "yes",
    89  					},
    90  					&MessageAction{
    91  						Label: "No",
    92  						Text:  "no",
    93  					},
    94  				},
    95  			},
    96  		},
    97  		{
    98  			JSON: []byte(`{
    99      "type": "carousel",
   100      "columns": [
   101      	{
   102  			"thumbnailImageUrl": "https://example.com/bot/images/item1.jpg",
   103  			"imageBackgroundColor": "#FFFFFF",
   104  			"title": "this is menu",
   105  			"text": "description",
   106  			"defaultAction": {
   107  			"type": "uri",
   108  			"label": "View detail",
   109  			"uri": "http://example.com/page/123"
   110  			},
   111  			"actions": [
   112  			{
   113  				"type": "postback",
   114  				"label": "Buy",
   115  				"data": "action=buy&itemid=111"
   116  			},
   117  			{
   118  				"type": "postback",
   119  				"label": "Add to cart",
   120  				"data": "action=add&itemid=111"
   121  			},
   122  			{
   123  				"type": "uri",
   124  				"label": "View detail",
   125  				"uri": "http://example.com/page/111"
   126  			}
   127  			]
   128  		},
   129  		{
   130  			"thumbnailImageUrl": "https://example.com/bot/images/item2.jpg",
   131  			"imageBackgroundColor": "#000000",
   132  			"title": "this is menu",
   133  			"text": "description",
   134  			"defaultAction": {
   135  			"type": "uri",
   136  			"label": "View detail",
   137  			"uri": "http://example.com/page/222"
   138  			},
   139  			"actions": [
   140  			{
   141  				"type": "postback",
   142  				"label": "Buy",
   143  				"data": "action=buy&itemid=222"
   144  			},
   145  			{
   146  				"type": "postback",
   147  				"label": "Add to cart",
   148  				"data": "action=add&itemid=222"
   149  			},
   150  			{
   151  				"type": "uri",
   152  				"label": "View detail",
   153  				"uri": "http://example.com/page/222"
   154  			}
   155  			]
   156  		}
   157  		],
   158  		"imageAspectRatio": "rectangle",
   159  		"imageSize": "cover"
   160  }`),
   161  			Want: &CarouselTemplate{
   162  				Columns: []*CarouselColumn{
   163  					{
   164  						ThumbnailImageURL:    "https://example.com/bot/images/item1.jpg",
   165  						ImageBackgroundColor: "#FFFFFF",
   166  						Title:                "this is menu",
   167  						Text:                 "description",
   168  						DefaultAction: &URIAction{
   169  							Label: "View detail",
   170  							URI:   "http://example.com/page/123",
   171  						},
   172  						Actions: []TemplateAction{
   173  							&PostbackAction{
   174  								Label: "Buy",
   175  								Data:  "action=buy&itemid=111",
   176  							},
   177  							&PostbackAction{
   178  								Label: "Add to cart",
   179  								Data:  "action=add&itemid=111",
   180  							},
   181  							&URIAction{
   182  								Label: "View detail",
   183  								URI:   "http://example.com/page/111",
   184  							},
   185  						},
   186  					},
   187  					{
   188  						ThumbnailImageURL:    "https://example.com/bot/images/item2.jpg",
   189  						ImageBackgroundColor: "#000000",
   190  						Title:                "this is menu",
   191  						Text:                 "description",
   192  						DefaultAction: &URIAction{
   193  							Label: "View detail",
   194  							URI:   "http://example.com/page/222",
   195  						},
   196  						Actions: []TemplateAction{
   197  							&PostbackAction{
   198  								Label: "Buy",
   199  								Data:  "action=buy&itemid=222",
   200  							},
   201  							&PostbackAction{
   202  								Label: "Add to cart",
   203  								Data:  "action=add&itemid=222",
   204  							},
   205  							&URIAction{
   206  								Label: "View detail",
   207  								URI:   "http://example.com/page/222",
   208  							},
   209  						},
   210  					},
   211  				},
   212  				ImageAspectRatio: "rectangle",
   213  				ImageSize:        "cover",
   214  			},
   215  		},
   216  		{
   217  			JSON: []byte(`{
   218  	"type": "image_carousel",
   219  	"columns": [
   220  	{
   221  		"imageUrl": "https://example.com/bot/images/item1.jpg",
   222  		"action": {
   223  		"type": "postback",
   224  		"label": "Buy",
   225  		"data": "action=buy&itemid=111"
   226  		}
   227  	},
   228  	{
   229  		"imageUrl": "https://example.com/bot/images/item2.jpg",
   230  		"action": {
   231  		"type": "message",
   232  		"label": "Yes",
   233  		"text": "yes"
   234  		}
   235  	},
   236  	{
   237  		"imageUrl": "https://example.com/bot/images/item3.jpg",
   238  		"action": {
   239  		"type": "uri",
   240  		"label": "View detail",
   241  		"uri": "http://example.com/page/222"
   242  		}
   243  	}
   244  	]
   245  }`),
   246  			Want: &ImageCarouselTemplate{
   247  				Columns: []*ImageCarouselColumn{
   248  					{
   249  						ImageURL: "https://example.com/bot/images/item1.jpg",
   250  						Action: &PostbackAction{
   251  							Label: "Buy",
   252  							Data:  "action=buy&itemid=111",
   253  						},
   254  					},
   255  					{
   256  						ImageURL: "https://example.com/bot/images/item2.jpg",
   257  						Action: &MessageAction{
   258  							Label: "Yes",
   259  							Text:  "yes",
   260  						},
   261  					},
   262  					{
   263  						ImageURL: "https://example.com/bot/images/item3.jpg",
   264  						Action: &URIAction{
   265  							Label: "View detail",
   266  							URI:   "http://example.com/page/222",
   267  						},
   268  					},
   269  				},
   270  			},
   271  		},
   272  	}
   273  	for i, tc := range testCases {
   274  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   275  			template, err := UnmarshalTemplateJSON(tc.JSON)
   276  			if err != nil {
   277  				t.Fatal(err)
   278  			}
   279  			if !reflect.DeepEqual(template, tc.Want) {
   280  				t.Errorf("got %v, want %v", template, tc.Want)
   281  			}
   282  		})
   283  	}
   284  }
   285  
   286  func BenchmarkUnmarshalTemplateJSON(b *testing.B) {
   287  	var jsonData = []byte(`{
   288      "type": "carousel",
   289      "columns": [
   290      	{
   291  			"thumbnailImageUrl": "https://example.com/bot/images/item1.jpg",
   292  			"imageBackgroundColor": "#FFFFFF",
   293  			"title": "this is menu",
   294  			"text": "description",
   295  			"defaultAction": {
   296  			"type": "uri",
   297  			"label": "View detail",
   298  			"uri": "http://example.com/page/123"
   299  			},
   300  			"actions": [
   301  			{
   302  				"type": "postback",
   303  				"label": "Buy",
   304  				"data": "action=buy&itemid=111"
   305  			},
   306  			{
   307  				"type": "postback",
   308  				"label": "Add to cart",
   309  				"data": "action=add&itemid=111"
   310  			},
   311  			{
   312  				"type": "uri",
   313  				"label": "View detail",
   314  				"uri": "http://example.com/page/111"
   315  			}
   316  			]
   317  		},
   318  		{
   319  			"thumbnailImageUrl": "https://example.com/bot/images/item2.jpg",
   320  			"imageBackgroundColor": "#000000",
   321  			"title": "this is menu",
   322  			"text": "description",
   323  			"defaultAction": {
   324  			"type": "uri",
   325  			"label": "View detail",
   326  			"uri": "http://example.com/page/222"
   327  			},
   328  			"actions": [
   329  			{
   330  				"type": "postback",
   331  				"label": "Buy",
   332  				"data": "action=buy&itemid=222"
   333  			},
   334  			{
   335  				"type": "postback",
   336  				"label": "Add to cart",
   337  				"data": "action=add&itemid=222"
   338  			},
   339  			{
   340  				"type": "uri",
   341  				"label": "View detail",
   342  				"uri": "http://example.com/page/222"
   343  			}
   344  			]
   345  		}
   346  		],
   347  		"imageAspectRatio": "rectangle",
   348  		"imageSize": "cover"
   349  }`)
   350  	b.ResetTimer()
   351  	for i := 0; i < b.N; i++ {
   352  		_, err := UnmarshalTemplateJSON(jsonData)
   353  		if err != nil {
   354  			b.Fatal(err)
   355  		}
   356  	}
   357  }