github.com/prebid/prebid-server/v2@v2.18.0/adapters/yandex/params_test.go (about)

     1  package yandex
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestValidParams(t *testing.T) {
    12  	validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
    13  	if err != nil {
    14  		t.Fatalf("Failed to fetch the json schema. %v", err)
    15  	}
    16  
    17  	for _, p := range validParams {
    18  		if err := validator.Validate(openrtb_ext.BidderYandex, json.RawMessage(p)); err != nil {
    19  			t.Errorf("Schema rejected valid params: %s", p)
    20  		}
    21  	}
    22  }
    23  
    24  func TestInvalidParams(t *testing.T) {
    25  	validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
    26  	if err != nil {
    27  		t.Fatalf("Failed to fetch the json schema. %v", err)
    28  	}
    29  
    30  	for _, p := range invalidParams {
    31  		if err := validator.Validate(openrtb_ext.BidderYandex, json.RawMessage(p)); err == nil {
    32  			t.Errorf("Schema allowed invalid params: %s", p)
    33  		}
    34  	}
    35  }
    36  
    37  var validParams = []string{
    38  	`{"page_id": 123123, "imp_id": 123}`,
    39  	`{"placement_id": "A-123123-123"}`,
    40  	`{"placement_id": "B-A-123123-123"}`,
    41  	`{"placement_id": "123123-123"}`,
    42  }
    43  
    44  var invalidParams = []string{
    45  	`{"pageId": 123123, "impId": 123}`,
    46  	`{"page_id": 0, "imp_id": 0}`,
    47  	`{"page_id": "123123", "imp_id": "123"}`,
    48  	`{"page_id": "123123", "imp_id": "123", "placement_id": "123123"}`,
    49  	`{"page_id": "123123"}`,
    50  	`{"imp_id": "123"}`,
    51  	`{"placement_id": 123123}`,
    52  	`{"placement_id": "123123"}`,
    53  	`{"placement_id": "A-123123"}`,
    54  	`{"placement_id": "B-A-123123"}`,
    55  	`{}`,
    56  }
    57  
    58  func TestValidPlacementIdMapper(t *testing.T) {
    59  	for ext, expectedPlacementId := range validPlacementIds {
    60  		val, err := mapExtToPlacementID(ext)
    61  
    62  		assert.Equal(t, &expectedPlacementId, val)
    63  		assert.NoError(t, err)
    64  	}
    65  }
    66  
    67  func TestInvalidPlacementIdMapper(t *testing.T) {
    68  	for _, ext := range invalidPlacementIds {
    69  		_, err := mapExtToPlacementID(ext)
    70  
    71  		assert.Error(t, err)
    72  	}
    73  }
    74  
    75  var validPlacementIds = map[openrtb_ext.ExtImpYandex]yandexPlacementID{
    76  	{PlacementID: "A-12345-1"}:      {PageID: "12345", ImpID: "1"},
    77  	{PlacementID: "B-A-123123-123"}: {PageID: "123123", ImpID: "123"},
    78  	{PlacementID: "111-222"}:        {PageID: "111", ImpID: "222"},
    79  	{PageID: 111, ImpID: 222}:       {PageID: "111", ImpID: "222"},
    80  }
    81  
    82  var invalidPlacementIds = []openrtb_ext.ExtImpYandex{
    83  	{PlacementID: "123123"},
    84  	{PlacementID: "A-123123"},
    85  	{PlacementID: "B-A-123123"},
    86  	{PlacementID: "C-B-A-123123"},
    87  }