Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

#1 Add FetchDataFeedFromUrl #3

Merged
merged 1 commit into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion awin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ func (c AwinClient) FetchDataFeed(options *DataFeedOptions) (*[]DataFeedEntry, e
}

url := fmt.Sprintf(dataFeedUrl, baseUrl, options.ApiKey, options.Language, strings.Join(options.FeedIds, ","), defaultDataFeedColumnsParam, ",", showAdult)
fmt.Println(url)

return c.FetchDataFeedFromUrl(url)
}

func (c AwinClient) FetchDataFeedFromUrl(url string) (*[]DataFeedEntry, error) {
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
Expand Down
64 changes: 64 additions & 0 deletions test/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,67 @@ func TestFetchDataFeed(t *testing.T) {
}
}
}

func TestFetchDataFeedFromUrl(t *testing.T) {
// Read mock data from CSV
csvContent, err := readCSVFileContents("testdata/data_feed.csv")
if err != nil {
t.Fatalf("coult not parse csv file '%v'", err)
}

var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write([]byte(csvContent)); err != nil {
t.Error(err)
}
if err := gz.Flush(); err != nil {
t.Error(err)
}
if err := gz.Close(); err != nil {
t.Error(err)
}

// Create mock response
response := &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBuffer(b.Bytes())),
}

// Create test client to run tests on
awinClient := awin.NewAwinClient(&http.Client{Transport: mockRoundTripper{response: response, requestTestFunc: func(r *http.Request) error {
fmt.Println(r.URL.String())
expectedUrl := "https://productdata.awin.com/datafeed/download/apikey/apiKey/language/en/fid/fid1,fid2/columns/aw_deep_link,product_name,aw_product_id,merchant_product_id,merchant_image_url,description,merchant_category,search_price,merchant_name,merchant_id,category_name,category_id,aw_image_url,currency,store_price,delivery_cost,merchant_deep_link,language,last_updated,display_price,data_feed_id,brand_name,brand_id,colour,product_short_description,specifications,condition,product_model,model_number,dimensions,keywords,promotional_text,product_type,commission_group,merchant_product_category_path,merchant_product_second_category,merchant_product_third_category,rrp_price,saving,savings_percent,base_price,base_price_amount,base_price_text,product_price_old,delivery_restrictions,delivery_weight,warranty,terms_of_contract,delivery_time,in_stock,stock_quantity,valid_from,valid_to,is_for_sale,web_offer,pre_order,stock_status,size_stock_status,size_stock_amount,merchant_thumb_url,large_image,alternate_image,aw_thumb_url,alternate_image_two,alternate_image_three,alternate_image_four,reviews,average_rating,rating,number_available,custom_1,custom_2,custom_3,custom_4,custom_5,custom_6,custom_7,custom_8,custom_9,ean,isbn,upc,mpn,parent_product_id,product_GTIN,basket_link/format/csv/delimiter/,/compression/gzip/adultcontent/1/"
if r.URL.String() != expectedUrl {
err := errors.New(fmt.Sprintf("invalid url found in test\nexpected '%s'\nfound '%s'", expectedUrl, r.URL.String()))
t.Error(err)
return err
}

expectedMethod := "GET"
if r.Method != expectedMethod {
err := errors.New(fmt.Sprintf("invalid request method in test\nexpected '%s'\nfound '%s'", expectedMethod, r.Method))
t.Error(err)
return err
}

return nil
}}})

result, err := awinClient.FetchDataFeedFromUrl("https://productdata.awin.com/datafeed/download/apikey/apiKey/language/en/fid/fid1,fid2/columns/aw_deep_link,product_name,aw_product_id,merchant_product_id,merchant_image_url,description,merchant_category,search_price,merchant_name,merchant_id,category_name,category_id,aw_image_url,currency,store_price,delivery_cost,merchant_deep_link,language,last_updated,display_price,data_feed_id,brand_name,brand_id,colour,product_short_description,specifications,condition,product_model,model_number,dimensions,keywords,promotional_text,product_type,commission_group,merchant_product_category_path,merchant_product_second_category,merchant_product_third_category,rrp_price,saving,savings_percent,base_price,base_price_amount,base_price_text,product_price_old,delivery_restrictions,delivery_weight,warranty,terms_of_contract,delivery_time,in_stock,stock_quantity,valid_from,valid_to,is_for_sale,web_offer,pre_order,stock_status,size_stock_status,size_stock_amount,merchant_thumb_url,large_image,alternate_image,aw_thumb_url,alternate_image_two,alternate_image_three,alternate_image_four,reviews,average_rating,rating,number_available,custom_1,custom_2,custom_3,custom_4,custom_5,custom_6,custom_7,custom_8,custom_9,ean,isbn,upc,mpn,parent_product_id,product_GTIN,basket_link/format/csv/delimiter/,/compression/gzip/adultcontent/1/")
if err != nil {
t.Fatalf("err is not null '%v'", err)
}

if len(*result) != 10 {
t.Fatalf("Invalid amount of data rows received %d", len(*result))
}

// Check if received rows and expected rows match
expectedRows, _ := parseCSVToDataFeedEntry(csvContent)
for i, expectedRow := range *expectedRows {
receivedRow := (*result)[i]
if expectedRow != receivedRow {
t.Fatalf("Invalid row parsed\nexpected '%v'\nreceived '%v'", expectedRow, receivedRow)
}
}
}