github.com/prebid/prebid-server/v2@v2.18.0/stored_requests/multifetcher_test.go (about)

     1  package stored_requests
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestMultiFetcher(t *testing.T) {
    13  	f1 := &mockFetcher{}
    14  	f2 := &mockFetcher{}
    15  	fetcher := &MultiFetcher{f1, f2}
    16  	ctx := context.Background()
    17  	reqIDs := []string{"abc", "def"}
    18  	impIDs := []string{"imp-1", "imp-2"}
    19  
    20  	f1.On("FetchRequests", ctx, reqIDs, impIDs).Return(
    21  		map[string]json.RawMessage{
    22  			"abc": json.RawMessage(`{"req_id": "abc"}`),
    23  		},
    24  		map[string]json.RawMessage{
    25  			"imp-1": json.RawMessage(`{"imp_id": "imp-1"}`),
    26  		},
    27  		[]error{NotFoundError{"def", "Request"}, NotFoundError{"imp-2", "Imp"}},
    28  	)
    29  	f2.On("FetchRequests", ctx, []string{"def"}, []string{"imp-2"}).Return(
    30  		map[string]json.RawMessage{
    31  			"def": json.RawMessage(`{"req_id": "def"}`),
    32  		},
    33  		map[string]json.RawMessage{
    34  			"imp-2": json.RawMessage(`{"imp_id": "imp-2"}`),
    35  		},
    36  		[]error{},
    37  	)
    38  
    39  	reqData, impData, errs := fetcher.FetchRequests(ctx, reqIDs, impIDs)
    40  
    41  	f1.AssertExpectations(t)
    42  	f2.AssertExpectations(t)
    43  	assert.Len(t, reqData, 2, "MultiFetcher should return all the requested stored req data that exists")
    44  	assert.Len(t, impData, 2, "MultiFetcher should return all the requested stored imp data that exists")
    45  	assert.Len(t, errs, 0, "MultiFetcher shouldn't return an error")
    46  	assert.JSONEq(t, `{"req_id": "abc"}`, string(reqData["abc"]), "MultiFetcher should return the right request data")
    47  	assert.JSONEq(t, `{"req_id": "def"}`, string(reqData["def"]), "MultiFetcher should return the right request data")
    48  	assert.JSONEq(t, `{"imp_id": "imp-1"}`, string(impData["imp-1"]), "MultiFetcher should return the right imp data")
    49  	assert.JSONEq(t, `{"imp_id": "imp-2"}`, string(impData["imp-2"]), "MultiFetcher should return the right imp data")
    50  }
    51  
    52  func TestMissingID(t *testing.T) {
    53  	f1 := &mockFetcher{}
    54  	f2 := &mockFetcher{}
    55  	fetcher := &MultiFetcher{f1, f2}
    56  	ctx := context.Background()
    57  	reqIDs := []string{"abc", "def", "ghi"}
    58  	impIDs := []string{"imp-1", "imp-2", "imp-3"}
    59  
    60  	f1.On("FetchRequests", ctx, reqIDs, impIDs).Return(
    61  		map[string]json.RawMessage{
    62  			"abc": json.RawMessage(`{"req_id": "abc"}`),
    63  		},
    64  		map[string]json.RawMessage{
    65  			"imp-1": json.RawMessage(`{"imp_id": "imp-1"}`),
    66  		},
    67  		[]error{NotFoundError{"def", "Request"}, NotFoundError{"imp-2", "Imp"}},
    68  	)
    69  	f2.On("FetchRequests", ctx, []string{"def", "ghi"}, []string{"imp-2", "imp-3"}).Return(
    70  		map[string]json.RawMessage{
    71  			"def": json.RawMessage(`{"req_id": "def"}`),
    72  		},
    73  		map[string]json.RawMessage{
    74  			"imp-2": json.RawMessage(`{"imp_id": "imp-2"}`),
    75  		},
    76  		[]error{},
    77  	)
    78  
    79  	reqData, impData, errs := fetcher.FetchRequests(ctx, reqIDs, impIDs)
    80  
    81  	f1.AssertExpectations(t)
    82  	f2.AssertExpectations(t)
    83  	assert.Len(t, reqData, 2, "MultiFetcher should return all the requested stored req data that exists")
    84  	assert.Len(t, impData, 2, "MultiFetcher should return all the requested stored imp data that exists")
    85  	assert.Len(t, errs, 2, "MultiFetcher should return an error if there are missing IDs")
    86  	assert.JSONEq(t, `{"req_id": "abc"}`, string(reqData["abc"]), "MultiFetcher should return the right request data")
    87  	assert.JSONEq(t, `{"req_id": "def"}`, string(reqData["def"]), "MultiFetcher should return the right request data")
    88  	assert.JSONEq(t, `{"imp_id": "imp-1"}`, string(impData["imp-1"]), "MultiFetcher should return the right imp data")
    89  	assert.JSONEq(t, `{"imp_id": "imp-2"}`, string(impData["imp-2"]), "MultiFetcher should return the right imp data")
    90  }
    91  
    92  func TestOtherError(t *testing.T) {
    93  	f1 := &mockFetcher{}
    94  	f2 := &mockFetcher{}
    95  	fetcher := &MultiFetcher{f1, f2}
    96  	ctx := context.Background()
    97  	reqIDs := []string{"abc", "def"}
    98  	impIDs := []string{"imp-1"}
    99  
   100  	f1.On("FetchRequests", ctx, reqIDs, impIDs).Return(
   101  		map[string]json.RawMessage{
   102  			"abc": json.RawMessage(`{"req_id": "abc"}`),
   103  		},
   104  		map[string]json.RawMessage{},
   105  		[]error{NotFoundError{"def", "Request"}, errors.New("Other error")},
   106  	)
   107  	f2.On("FetchRequests", ctx, []string{"def"}, []string{"imp-1"}).Return(
   108  		map[string]json.RawMessage{
   109  			"def": json.RawMessage(`{"req_id": "def"}`),
   110  		},
   111  		map[string]json.RawMessage{
   112  			"imp-1": json.RawMessage(`{"imp_id": "imp-1"}`),
   113  		},
   114  		[]error{},
   115  	)
   116  
   117  	reqData, impData, errs := fetcher.FetchRequests(ctx, reqIDs, impIDs)
   118  
   119  	f1.AssertExpectations(t)
   120  	f2.AssertExpectations(t)
   121  	assert.Len(t, reqData, 2, "MultiFetcher should return all the requested stored req data that exists")
   122  	assert.Len(t, impData, 1, "MultiFetcher should return all the requested stored imp data that exists")
   123  	assert.Len(t, errs, 1, "MultiFetcher should return an error if one of the fetcher returns an error other than NotFoundError")
   124  	assert.JSONEq(t, `{"req_id": "abc"}`, string(reqData["abc"]), "MultiFetcher should return the right request data")
   125  	assert.JSONEq(t, `{"req_id": "def"}`, string(reqData["def"]), "MultiFetcher should return the right request data")
   126  	assert.JSONEq(t, `{"imp_id": "imp-1"}`, string(impData["imp-1"]), "MultiFetcher should return the right imp data")
   127  }
   128  
   129  func TestMultiFetcherAccountFoundInFirstFetcher(t *testing.T) {
   130  	f1 := &mockFetcher{}
   131  	f2 := &mockFetcher{}
   132  	fetcher := &MultiFetcher{f1, f2}
   133  	ctx := context.Background()
   134  
   135  	f1.On("FetchAccount", ctx, json.RawMessage("{}"), "ONE").Once().Return(json.RawMessage(`{"id": "ONE"}`), []error{})
   136  
   137  	account, errs := fetcher.FetchAccount(ctx, json.RawMessage("{}"), "ONE")
   138  
   139  	f1.AssertExpectations(t)
   140  	f2.AssertNotCalled(t, "FetchAccount")
   141  	assert.Empty(t, errs)
   142  	assert.JSONEq(t, `{"id": "ONE"}`, string(account))
   143  }
   144  
   145  func TestMultiFetcherAccountFoundInSecondFetcher(t *testing.T) {
   146  	f1 := &mockFetcher{}
   147  	f2 := &mockFetcher{}
   148  	fetcher := &MultiFetcher{f1, f2}
   149  	ctx := context.Background()
   150  
   151  	f1.On("FetchAccount", ctx, json.RawMessage("{}"), "TWO").Once().Return(json.RawMessage(``), []error{NotFoundError{"TWO", "Account"}})
   152  	f2.On("FetchAccount", ctx, json.RawMessage("{}"), "TWO").Once().Return(json.RawMessage(`{"id": "TWO"}`), []error{})
   153  
   154  	account, errs := fetcher.FetchAccount(ctx, json.RawMessage("{}"), "TWO")
   155  
   156  	f1.AssertExpectations(t)
   157  	f2.AssertExpectations(t)
   158  	assert.Empty(t, errs)
   159  	assert.JSONEq(t, `{"id": "TWO"}`, string(account))
   160  }
   161  
   162  func TestMultiFetcherAccountNotFound(t *testing.T) {
   163  	f1 := &mockFetcher{}
   164  	f2 := &mockFetcher{}
   165  	fetcher := &MultiFetcher{f1, f2}
   166  	ctx := context.Background()
   167  
   168  	f1.On("FetchAccount", ctx, json.RawMessage("{}"), "MISSING").Once().Return(json.RawMessage(``), []error{NotFoundError{"TWO", "Account"}})
   169  	f2.On("FetchAccount", ctx, json.RawMessage("{}"), "MISSING").Once().Return(json.RawMessage(``), []error{NotFoundError{"TWO", "Account"}})
   170  
   171  	account, errs := fetcher.FetchAccount(ctx, json.RawMessage("{}"), "MISSING")
   172  
   173  	f1.AssertExpectations(t)
   174  	f2.AssertExpectations(t)
   175  	assert.Len(t, errs, 1)
   176  	assert.Nil(t, account)
   177  	assert.EqualError(t, errs[0], NotFoundError{"MISSING", "Account"}.Error())
   178  }