feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
ba791435b7
commit
da532104f8
9 changed files with 231 additions and 28 deletions
|
|
@ -7,36 +7,38 @@ import (
|
|||
|
||||
func TestRangeFilter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string // product name
|
||||
price float64 // product price
|
||||
product *Product
|
||||
model string // model regex to apply on the product name
|
||||
min float64 // minimum price
|
||||
max float64 // maximum price
|
||||
currency string // price currency
|
||||
included bool // should be included or not
|
||||
}{
|
||||
{"MSI GeForce RTX 3090 GAMING X", 99.99, "3090", 50.0, 100.0, true}, // model match and price is in the range, should be included
|
||||
{"MSI GeForce RTX 3090 GAMING X", 99.99, "3080", 50.0, 100.0, true}, // model doesn't match, should be included
|
||||
{"MSI GeForce RTX 3090 GAMING X", 999.99, "3090", 50.0, 100.0, false}, // model match and price is outside of the range, shoud not be included
|
||||
{"MSI GeForce RTX 3090 GAMING X", 99.99, "", 50.0, 100.0, true}, // model regex is missing, should be included
|
||||
{&Product{Name: "MSI GeForce RTX 3090 GAMING X", Price: 99.99, PriceCurrency: "EUR"}, "3090", 50.0, 100.0, "EUR", true}, // model match and price is in the range, should be included
|
||||
{&Product{Name: "MSI GeForce RTX 3090 GAMING X", Price: 99.99, PriceCurrency: "EUR"}, "3080", 50.0, 100.0, "EUR", true}, // model doesn't match, should be included
|
||||
{&Product{Name: "MSI GeForce RTX 3090 GAMING X", Price: 999.99, PriceCurrency: "EUR"}, "3090", 50.0, 100.0, "EUR", false}, // model match and price is outside of the range, shoud not be included
|
||||
{&Product{Name: "MSI GeForce RTX 3090 GAMING X", Price: 99.99, PriceCurrency: "EUR"}, "", 50.0, 100.0, "EUR", true}, // model regex is missing, should be included
|
||||
{&Product{Name: "MSI GeForce RTX 3090 GAMING X", Price: 99.99, PriceCurrency: "EUR"}, "3090", 50.0, 0.0, "EUR", true}, // upper limit is missing, should be included
|
||||
}
|
||||
|
||||
converter := NewCurrencyConverter()
|
||||
|
||||
for i, tc := range tests {
|
||||
t.Run(fmt.Sprintf("TestRangeFilter#%d", i), func(t *testing.T) {
|
||||
product := &Product{Name: tc.name, Price: tc.price}
|
||||
filter, err := NewRangeFilter(tc.model, tc.min, tc.max)
|
||||
filter, err := NewRangeFilter(tc.model, tc.min, tc.max, tc.currency, converter)
|
||||
if err != nil {
|
||||
t.Errorf("cannot create filter with model regex '%s' and price range [%.2f, %.2f]: %s", tc.model, tc.min, tc.max, err)
|
||||
}
|
||||
|
||||
included := filter.Include(product)
|
||||
included := filter.Include(tc.product)
|
||||
|
||||
if included != tc.included {
|
||||
t.Errorf("product '%s' with model regex '%s' and range [%.2f, %.2f]: got included=%t, want included=%t", tc.name, tc.model, tc.min, tc.max, included, tc.included)
|
||||
t.Errorf("product '%s' of price %.2f%s with model regex '%s' and range [%.2f, %.2f]: got included=%t, want included=%t", tc.product.Name, tc.product.Price, tc.product.PriceCurrency, tc.model, tc.min, tc.max, included, tc.included)
|
||||
} else {
|
||||
if included {
|
||||
t.Logf("product '%s' included by model regex '%s' and range [%.2f, %.2f]", tc.name, tc.model, tc.min, tc.max)
|
||||
t.Logf("product '%s' included by model regex '%s' and range [%.2f, %.2f]", tc.product.Name, tc.model, tc.min, tc.max)
|
||||
} else {
|
||||
t.Logf("product '%s' excluded by model regex '%s' and range [%.2f, %.2f]", tc.name, tc.model, tc.min, tc.max)
|
||||
t.Logf("product '%s' excluded by model regex '%s' and range [%.2f, %.2f]", tc.product.Name, tc.model, tc.min, tc.max)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue