Skip to content

Commit

Permalink
Remove Extended from everything now the migration is complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhowse committed Aug 21, 2024
1 parent c5095e7 commit 4bc7189
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 40 deletions.
3 changes: 1 addition & 2 deletions internal/woolworths/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ type fruitVegPage []byte
const WOOLWORTHS_ID_PREFIX = "woolworths_sku_"
const PRODUCTS_PER_PAGE = 36

// This will eventually supplant woolworthsProductInfo
type woolworthsProductInfoExtended struct {
type woolworthsProductInfo struct {
ID productID
departmentID departmentID
departmentDescription string
Expand Down
12 changes: 6 additions & 6 deletions internal/woolworths/woolworths_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (w *Woolworths) initDB(dbPath string) error {
}

// Saves product info to the database
func (w *Woolworths) saveProductInfoExtended(tx *sql.Tx, productInfo woolworthsProductInfoExtended) error {
func (w *Woolworths) saveProductInfo(tx *sql.Tx, productInfo woolworthsProductInfo) error {
var err error
var result sql.Result

Expand Down Expand Up @@ -153,14 +153,14 @@ func (w *Woolworths) saveProductInfoExtended(tx *sql.Tx, productInfo woolworthsP
}

// Saves product info to the database
func (w *Woolworths) saveProductInfoExtendedNoTx(productInfo woolworthsProductInfoExtended) error {
func (w *Woolworths) saveProductInfoNoTx(productInfo woolworthsProductInfo) error {
var err error

tx, err := w.db.Begin()
if err != nil {
return fmt.Errorf("failed to start transaction: %w", err)
}
w.saveProductInfoExtended(tx, productInfo)
w.saveProductInfo(tx, productInfo)
err = tx.Commit()
if err != nil {
return fmt.Errorf("failed to commit transaction: %w", err)
Expand Down Expand Up @@ -195,9 +195,9 @@ func (w *Woolworths) saveDepartment(departmentInfo departmentInfo) error {
return nil
}

// loadProductInfoExtended loads cached extended product info from the database
func (w *Woolworths) loadProductInfoExtended(productID productID) (woolworthsProductInfoExtended, error) {
var wProdInfo woolworthsProductInfoExtended
// loadProductInfo loads cached extended product info from the database
func (w *Woolworths) loadProductInfo(productID productID) (woolworthsProductInfo, error) {
var wProdInfo woolworthsProductInfo
var deptDescription sql.NullString
row := w.db.QueryRow(`
SELECT
Expand Down
32 changes: 16 additions & 16 deletions internal/woolworths/woolworths_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
utils "github.com/tjhowse/aus_grocery_price_database/internal/utils"
)

func TestUpdateProductInfoExtended(t *testing.T) {
func TestUpdateProductInfo(t *testing.T) {
w := getInitialisedWoolworths()
testFile, err := utils.ReadEntireFile("data/category_1-E5BEE36E_1.json")
if err != nil {
Expand All @@ -25,12 +25,12 @@ func TestUpdateProductInfoExtended(t *testing.T) {

infos[0].Updated = time.Now()

if err := w.saveProductInfoExtendedNoTx(infos[0]); err != nil {
if err := w.saveProductInfoNoTx(infos[0]); err != nil {
t.Fatal(err)
}

var readProdInfo woolworthsProductInfoExtended
readProdInfo, err = w.loadProductInfoExtended("133211")
var readProdInfo woolworthsProductInfo
readProdInfo, err = w.loadProductInfo("133211")
if err != nil {
t.Fatal(err)
}
Expand All @@ -41,7 +41,7 @@ func TestUpdateProductInfoExtended(t *testing.T) {

func TestMissingProduct(t *testing.T) {
w := getInitialisedWoolworths()
_, err := w.loadProductInfoExtended("123456")
_, err := w.loadProductInfo("123456")
if err == nil {
t.Fatal("Expected an error")
}
Expand Down Expand Up @@ -85,17 +85,17 @@ func TestGetSharedProductsUpdatedAfter(t *testing.T) {
w := Woolworths{}
w.Init(woolworthsServer.URL, ":memory:", 5*time.Second)
w.filterDepartments = false
var infoList []woolworthsProductInfoExtended
infoList = append(infoList, woolworthsProductInfoExtended{ID: "123455", Info: productListPageProduct{DisplayName: "1", Price: decimal.NewFromFloat(1.5)}, Updated: time.Now().Add(-5 * time.Minute)})
infoList = append(infoList, woolworthsProductInfoExtended{ID: "123456", Info: productListPageProduct{DisplayName: "2", Price: decimal.NewFromFloat(2.4)}, Updated: time.Now().Add(-4 * time.Minute)})
infoList = append(infoList, woolworthsProductInfoExtended{ID: "123457", Info: productListPageProduct{DisplayName: "3", Price: decimal.NewFromFloat(3.3)}, Updated: time.Now().Add(-3 * time.Minute)})
infoList = append(infoList, woolworthsProductInfoExtended{ID: "123458", Info: productListPageProduct{DisplayName: "4", Price: decimal.NewFromFloat(4.2)}, Updated: time.Now().Add(-1 * time.Minute)})
infoList = append(infoList, woolworthsProductInfoExtended{ID: "123459", Info: productListPageProduct{DisplayName: "5", Price: decimal.NewFromFloat(5.1)}, Updated: time.Now()})
var infoList []woolworthsProductInfo
infoList = append(infoList, woolworthsProductInfo{ID: "123455", Info: productListPageProduct{DisplayName: "1", Price: decimal.NewFromFloat(1.5)}, Updated: time.Now().Add(-5 * time.Minute)})
infoList = append(infoList, woolworthsProductInfo{ID: "123456", Info: productListPageProduct{DisplayName: "2", Price: decimal.NewFromFloat(2.4)}, Updated: time.Now().Add(-4 * time.Minute)})
infoList = append(infoList, woolworthsProductInfo{ID: "123457", Info: productListPageProduct{DisplayName: "3", Price: decimal.NewFromFloat(3.3)}, Updated: time.Now().Add(-3 * time.Minute)})
infoList = append(infoList, woolworthsProductInfo{ID: "123458", Info: productListPageProduct{DisplayName: "4", Price: decimal.NewFromFloat(4.2)}, Updated: time.Now().Add(-1 * time.Minute)})
infoList = append(infoList, woolworthsProductInfo{ID: "123459", Info: productListPageProduct{DisplayName: "5", Price: decimal.NewFromFloat(5.1)}, Updated: time.Now()})
// This last one is to test that we don't get products that have a blank name.
infoList = append(infoList, woolworthsProductInfoExtended{ID: "123460", Info: productListPageProduct{DisplayName: "", Price: decimal.NewFromFloat(6.0)}, Updated: time.Now()})
infoList = append(infoList, woolworthsProductInfo{ID: "123460", Info: productListPageProduct{DisplayName: "", Price: decimal.NewFromFloat(6.0)}, Updated: time.Now()})

for _, info := range infoList {
w.saveProductInfoExtendedNoTx(info)
w.saveProductInfoNoTx(info)
}
productIDs, err := w.GetSharedProductsUpdatedAfter(time.Now().Add(-2*time.Minute), 10)
if err != nil {
Expand Down Expand Up @@ -139,13 +139,13 @@ func TestGetSharedProductsUpdatedAfter(t *testing.T) {

func TestSaveProductInfo(t *testing.T) {
w := getInitialisedWoolworths()
inProduct := woolworthsProductInfoExtended{ID: "123455", Info: productListPageProduct{DisplayName: "1", Price: decimal.NewFromFloat(1.5)}, Updated: time.Now().Add(-5 * time.Minute)}
inProduct := woolworthsProductInfo{ID: "123455", Info: productListPageProduct{DisplayName: "1", Price: decimal.NewFromFloat(1.5)}, Updated: time.Now().Add(-5 * time.Minute)}

err := w.saveProductInfoExtendedNoTx(inProduct)
err := w.saveProductInfoNoTx(inProduct)
if err != nil {
t.Fatal(err)
}
outProduct, err := w.loadProductInfoExtended("123455")
outProduct, err := w.loadProductInfo("123455")
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/woolworths/woolworths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func ValidateProduct(t *testing.T, w *Woolworths, id productID, expectedName string) error {
prod, err := w.loadProductInfoExtended(id)
prod, err := w.loadProductInfo(id)
if err != nil {
return fmt.Errorf("Failed to get product ID %s: %v", id, err)
}
Expand All @@ -27,7 +27,7 @@ func ValidateProduct(t *testing.T, w *Woolworths, id productID, expectedName str
return nil
}

func TestSchedulerExtended(t *testing.T) {
func TestScheduler(t *testing.T) {
slog.SetLogLoggerLevel(slog.LevelDebug)

w := Woolworths{}
Expand All @@ -39,7 +39,7 @@ func TestSchedulerExtended(t *testing.T) {
}
w.filterDepartments = true
cancel := make(chan struct{})
go w.runExtended(cancel)
go w.Run(cancel)

done := make(chan struct{})
go func() {
Expand Down
10 changes: 5 additions & 5 deletions internal/woolworths/woolworths_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func (w *Woolworths) getProductsFromDepartment(department departmentID) ([]produ
}

// extractProductInfoFromProductListPage extracts the product info from the product list page
func extractProductInfoFromProductListPage(body []byte) ([]woolworthsProductInfoExtended, error) {
productInfos := []woolworthsProductInfoExtended{}
func extractProductInfoFromProductListPage(body []byte) ([]woolworthsProductInfo, error) {
productInfos := []woolworthsProductInfo{}

// Unmarshal body into a productListPage
var productListPage productListPage
Expand All @@ -190,7 +190,7 @@ func extractProductInfoFromProductListPage(body []byte) ([]woolworthsProductInfo
slog.Warn("Error encoding product back to JSON for storage", "error", err)
encoded = []byte("error re-encoding product")
}
productInfos = append(productInfos, woolworthsProductInfoExtended{
productInfos = append(productInfos, woolworthsProductInfo{
ID: productID(strconv.Itoa(product.Stockcode)),
departmentID: departmentID(product.AdditionalAttributes.PiesProductDepartmentNodeID),
departmentDescription: product.AdditionalAttributes.Sapdepartmentname,
Expand Down Expand Up @@ -271,8 +271,8 @@ func (w *Woolworths) getProductIDsAndCountFromListPage(department departmentID,
}

// getProductInfoFromListPage returns the product information from the department list page
func (w *Woolworths) getProductInfoExtendedFromListPage(dp departmentPage) ([]woolworthsProductInfoExtended, error) {
productInfos := []woolworthsProductInfoExtended{}
func (w *Woolworths) getProductInfoFromListPage(dp departmentPage) ([]woolworthsProductInfo, error) {
productInfos := []woolworthsProductInfo{}
var body []byte
var err error

Expand Down
2 changes: 1 addition & 1 deletion internal/woolworths/woolworths_web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestGetProductInfoExtendedFromListPage(t *testing.T) {
ID: "1-E5BEE36E",
page: 1,
}
productInfo, err := w.getProductInfoExtendedFromListPage(dp)
productInfo, err := w.getProductInfoFromListPage(dp)
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 2 additions & 6 deletions internal/woolworths/woolworths_workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (w *Woolworths) newDepartmentInfoWorker(output chan<- departmentInfo) {
func (w *Woolworths) productListPageWorker(input <-chan departmentPage) {
for dp := range input {
slog.Debug("Getting product list page", "departmentID", dp.ID, "page", dp.page)
products, err := w.getProductInfoExtendedFromListPage(dp)
products, err := w.getProductInfoFromListPage(dp)
if err != nil {
slog.Error(fmt.Sprintf("Error getting product info extended: %v", err))
continue
Expand All @@ -72,7 +72,7 @@ func (w *Woolworths) productListPageWorker(input <-chan departmentPage) {
continue
}
product.departmentID = dp.ID
err := w.saveProductInfoExtended(transaction, product)
err := w.saveProductInfo(transaction, product)
if err != nil {
slog.Error(fmt.Sprintf("Error inserting product info: %v", err))
continue
Expand Down Expand Up @@ -131,10 +131,6 @@ const DEFAULT_PRODUCT_UPDATE_BATCH_SIZE = 10
// Currently all sqlite writes happen via this function. This may move
// off to a separate goroutine in the future.
func (w *Woolworths) Run(cancel chan struct{}) {
w.runExtended(cancel)
}

func (w *Woolworths) runExtended(cancel chan struct{}) {
departmentPageChannel := make(chan departmentPage)
newDepartmentInfoChannel := make(chan departmentInfo)

Expand Down
2 changes: 1 addition & 1 deletion internal/woolworths/woolworths_workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestProductListPageWorker(t *testing.T) {
// TODO remove this hardcoded sleep and use a loop in a goroutine with a channel for the output
// as I've done before in another test.
time.Sleep(50 * time.Millisecond)
readInfo, err := w.loadProductInfoExtended("144607")
readInfo, err := w.loadProductInfo("144607")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 4bc7189

Please sign in to comment.