Skip to content

Commit

Permalink
Increase test coverage for product research and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Dec 27, 2024
1 parent 09e2016 commit e0156bf
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 58 deletions.
15 changes: 10 additions & 5 deletions services/external/weni/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,18 @@ func VtexIntelligentSearch(searchUrl string, productSearch string, hideUnavailab
for _, items := range response.Products {
for _, item := range items.Items {
if !hasSellerID {
for _, seller := range item.Sellers {
if seller.SellerDefault == true || seller.CommertialOffer.AvailableQuantity > 0 {
allItems = append(allItems, item.ItemId+"#"+seller.SellerID)
} else {
allItems = append(allItems, item.ItemId+"#1")
if item.Sellers != nil {
for _, seller := range item.Sellers {
if seller.SellerDefault == true || seller.CommertialOffer.AvailableQuantity > 0 {
allItems = append(allItems, item.ItemId+"#"+seller.SellerID)
} else {
allItems = append(allItems, item.ItemId+"#1")
}
}
} else {
allItems = append(allItems, item.ItemId)
}

} else {
allItems = append(allItems, item.ItemId)
}
Expand Down
252 changes: 199 additions & 53 deletions services/external/weni/service_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package catalogs_test

import (
"fmt"
"net/http"
"testing"

Expand All @@ -20,90 +19,206 @@ import (
"github.com/stretchr/testify/require"
)

func TestService(t *testing.T) {
// t.Skip("Skip because it's a work in progress.")
_, rt, db, _ := testsuite.Get()

defer dates.SetNowSource(dates.DefaultNowSource)
session, _, err := test.CreateTestSession("", envs.RedactionPolicyNone)
require.NoError(t, err)

defer uuids.SetGenerator(uuids.DefaultGenerator)
defer httpx.SetRequestor(httpx.DefaultRequestor)

uuids.SetGenerator(uuids.NewSeededGenerator(12345))

func setupMocks() {
httpx.SetRequestor(httpx.NewMockRequestor(map[string][]httpx.MockResponse{
"https://api.openai.com/v1/chat/completions": {
httpx.NewMockResponse(200, nil, `{
"id": "chatcmpl-7IfBIQsTVKbwOiHPgcrpthaCn7K1t",
"object": "chat.completion",
"created":1684682560,
"model":"gpt-3.5-turbo-0301",
"usage":{
"prompt_tokens":26,
"completion_tokens":8,
"total_tokens":34
},
"choices":[
"https://api.openai.com/v1/chat/completions": createRepeatedMocks(5, `{
"id": "chatcmpl-7IfBIQsTVKbwOiHPgcrpthaCn7K1t",
"object": "chat.completion",
"created":1684682560,
"model":"gpt-3.5-turbo-0301",
"usage":{
"prompt_tokens":26,
"completion_tokens":8,
"total_tokens":34
},
"choices":[
{
"message":{
"role":"assistant",
"content":"{\"products\": [\"banana\"]}"
},
"finish_reason":"stop",
"index":0
}
]
}`),
"https://sentenx.weni.ai/products/search": {
createMockResponse(`{
"products": [
{
"message":{
"role":"assistant",
"content":"{\"products\": [\"banana\"]}"
},
"finish_reason":"stop",
"index":0
"facebook_id": "1234567891",
"title": "banana prata 1kg",
"org_id": "1",
"channel_id": "10000",
"catalog_id": "123456789",
"product_retailer_id": "p1"
}
]
}`),
},
"https://sentenx.weni.ai/products/search": {
httpx.NewMockResponse(200, nil, `{
"https://vtex.com.br/legacy/search/banana": {
createMockResponse(`[{
"items": [
{
"itemId": "1236"
}
]
}]`),
},
"https://vtex.com.br/intelligent/search?hideUnavailableItems=false&locale=pt-BR&query=banana": {
createMockResponse(`{
"products": [
{
"facebook_id": "1234567891",
"title": "banana prata 1kg",
"org_id": "1",
"channel_id": "10000",
"catalog_id": "123456789",
"product_retailer_id": "p1"
"items": [
{
"itemId": "1234"
}
]
}
]
}`),
},
"https://vtex.com.br/legacy/search": {
httpx.NewMockResponse(200, nil, `{
[
createMockResponse(`{
"products": [
{
"items": [
{
"itemId": "1236"
"itemId": "1234",
"sellers": [
{
"sellerId": "2",
"sellerDefault": true,
"commertialOffer": {
"AvailableQuantity": 10
}
}
]
}
]
}
]
}`),
},
"https://vtex.com.br/intelligent/search": {
httpx.NewMockResponse(200, nil, `{
createMockResponse(`{
"products": [
{
"items": [
{
"itemId": "1234"
"itemId": "1234",
"sellers": [
{
"sellerId": "2",
"sellerDefault": true,
"commertialOffer": {
"AvailableQuantity": 10
}
}
]
}
]
}
]
}`),
},
"https://graph.facebook.com/v14.0/123456789/products?access_token=&fields=%5B%22category%22%2C%22name%22%2C%22retailer_id%22%2C%22availability%22%5D&filter=%7B%22or%22%3A%5B%7B%22and%22%3A%5B%7B%22retailer_id%22%3A%7B%22i_contains%22%3A%22p1%22%7D%7D%2C%7B%22availability%22%3A%7B%22i_contains%22%3A%22in+stock%22%7D%7D%2C%7B%22visibility%22%3A%7B%22i_contains%22%3A%22published%22%7D%7D%5D%7D%5D%7D&summary=true": {
createMockResponse(`{
"data": [
{
"name": "banana prata (Kg)",
"retailer_id": "p1",
"availability": "in stock",
"visibility": "published",
"id": "111111222233333"
}
]
}`),
},
"https://graph.facebook.com/v14.0/123456789/products?access_token=&fields=%5B%22category%22%2C%22name%22%2C%22retailer_id%22%2C%22availability%22%5D&filter=%7B%22or%22%3A%5B%7B%22and%22%3A%5B%7B%22retailer_id%22%3A%7B%22i_contains%22%3A%221236%22%7D%7D%2C%7B%22availability%22%3A%7B%22i_contains%22%3A%22in+stock%22%7D%7D%2C%7B%22visibility%22%3A%7B%22i_contains%22%3A%22published%22%7D%7D%5D%7D%5D%7D&summary=true": {
createMockResponse(`{
"data": [
{
"name": "banana prata (Kg)",
"retailer_id": "1236",
"availability": "in stock",
"visibility": "published",
"id": "111111222233333"
}
]
}`),
},
"https://graph.facebook.com/v14.0/123456789/products?access_token=&fields=%5B%22category%22%2C%22name%22%2C%22retailer_id%22%2C%22availability%22%5D&filter=%7B%22or%22%3A%5B%7B%22and%22%3A%5B%7B%22retailer_id%22%3A%7B%22i_contains%22%3A%221234%2310%22%7D%7D%2C%7B%22availability%22%3A%7B%22i_contains%22%3A%22in+stock%22%7D%7D%2C%7B%22visibility%22%3A%7B%22i_contains%22%3A%22published%22%7D%7D%5D%7D%5D%7D&summary=true": createRepeatedMocks(3, `{
"data": [
{
"name": "banana prata (Kg)",
"retailer_id": "1234#10",
"availability": "in stock",
"visibility": "published",
"id": "111111222233333"
}
]
}`),
"https://graph.facebook.com/v14.0/123456789/products?access_token=&fields=%5B%22category%22%2C%22name%22%2C%22retailer_id%22%2C%22availability%22%5D&filter=%7B%22or%22%3A%5B%7B%22and%22%3A%5B%7B%22retailer_id%22%3A%7B%22i_contains%22%3A%221234%232%22%7D%7D%2C%7B%22availability%22%3A%7B%22i_contains%22%3A%22in+stock%22%7D%7D%2C%7B%22visibility%22%3A%7B%22i_contains%22%3A%22published%22%7D%7D%5D%7D%5D%7D&summary=true": {
createMockResponse(`{
"data": [
{
"name": "banana prata (Kg)",
"retailer_id": "1234#2",
"availability": "in stock",
"visibility": "published",
"id": "111111222233333"
}
]
}`),
},
"https://vtex.com.br/intelligent/searchapi/checkout/pub/orderForms/simulation": {
createMockResponse(`{
"items": [
{
"id": "1234",
"availability": "available"
}
]
}`),
},
"http://vtex.com.br/api/io/_v/api/intelligent-search/sponsored_products?hideUnavailableItems=false&locale=pt-BR&query=banana": {
createMockResponse(`[{
"items": [
{
"itemId": "1234"
}
]
}]`),
},
}))
}

func createMockResponse(body string) httpx.MockResponse {
return httpx.NewMockResponse(200, nil, body)
}

func createRepeatedMocks(count int, body string) []httpx.MockResponse {
mocks := make([]httpx.MockResponse, count)
for i := 0; i < count; i++ {
mocks[i] = createMockResponse(body)
}
return mocks
}

func TestService(t *testing.T) {
defer dates.SetNowSource(dates.DefaultNowSource)
defer uuids.SetGenerator(uuids.DefaultGenerator)
defer httpx.SetRequestor(httpx.DefaultRequestor)

_, rt, db, _ := testsuite.Get()
session, _, err := test.CreateTestSession("", envs.RedactionPolicyNone)
require.NoError(t, err)

uuids.SetGenerator(uuids.NewSeededGenerator(12345))

setupMocks()

catalogs.SetDB(db)
catalogService := flows.NewMsgCatalog(static.NewMsgCatalog(assets.MsgCatalogUUID(testdata.Org1.UUID), "msg_catalog", "msg_catalog", assets.ChannelUUID(uuids.New())))

svc, err := catalogs.NewService(rt.Config, http.DefaultClient, nil, catalogService, map[string]string{})

assert.NoError(t, err)

logger := &flows.HTTPLogger{}
Expand All @@ -119,7 +234,6 @@ func TestService(t *testing.T) {
call, err := svc.Call(session, params, logger.Log)
assert.NoError(t, err)
assert.NotNil(t, call)
fmt.Println(call.ProductRetailerIDS)
assert.Equal(t, "p1", call.ProductRetailerIDS[0].ProductRetailerIDs[0])
assert.NotNil(t, call.Traces)

Expand All @@ -137,17 +251,49 @@ func TestService(t *testing.T) {
assert.Equal(t, "1236", call.ProductRetailerIDS[0].ProductRetailerIDs[0])
assert.NotNil(t, call.Traces)

params = assets.MsgCatalogParam{
ProductSearch: "",
ChannelUUID: uuids.UUID(testdata.TwilioChannel.UUID),
SearchType: "vtex",
SearchUrl: "https://vtex.com.br/intelligent/search",
ApiType: "intelligent",
PostalCode: "000000-000",
SellerId: "10",
}
call, err = svc.Call(session, params, logger.Log)
assert.NoError(t, err)
assert.NotNil(t, call)
assert.Equal(t, "1234#10", call.ProductRetailerIDS[0].ProductRetailerIDs[0])
assert.NotNil(t, call.Traces)

params = assets.MsgCatalogParam{
ProductSearch: "",
ChannelUUID: uuids.UUID(testdata.TwilioChannel.UUID),
SearchType: "vtex",
SearchUrl: "https://vtex.com.br/intelligent/search",
ApiType: "intelligent",
PostalCode: "",
SellerId: "",
}
call, err = svc.Call(session, params, logger.Log)
assert.NoError(t, err)
assert.NotNil(t, call)
assert.Equal(t, "1234#2", call.ProductRetailerIDS[0].ProductRetailerIDs[0])
assert.NotNil(t, call.Traces)

params = assets.MsgCatalogParam{
ProductSearch: "",
ChannelUUID: uuids.UUID(testdata.TwilioChannel.UUID),
SearchType: "vtex",
SearchUrl: "https://vtex.com.br/intelligent/search",
ApiType: "intelligent",
PostalCode: "",
SellerId: "10",
HasVtexAds: true,
}
call, err = svc.Call(session, params, logger.Log)
assert.NoError(t, err)
assert.NotNil(t, call)
assert.Equal(t, "1234", call.ProductRetailerIDS[0].ProductRetailerIDs[0])
assert.Equal(t, "1234#10", call.ProductRetailerIDS[0].ProductRetailerIDs[0])
assert.NotNil(t, call.Traces)
}

0 comments on commit e0156bf

Please sign in to comment.