saveinmed/backend-old/db_check_test.go

68 lines
1.9 KiB
Go

package main
import (
"fmt"
"log"
"testing"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
)
func TestCartIsolation(t *testing.T) {
db, err := sqlx.Open("pgx", "postgres://postgres:123@localhost:55432/saveinmed?sslmode=disable")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Clear cart for test companies
buyer1 := "019c4d81-a6a4-770d-99f7-dd04256ee020" // Ricardo
buyer2 := "019c4ca7-7619-776d-b729-30bd44b43a69" // Farmácia
db.Exec("DELETE FROM cart_items WHERE buyer_id IN ($1, $2)", buyer1, buyer2)
// Add item for buyer1
productID := "019c4cf9-5ea1-7c88-919d-5a015d7f1b34" // Dipirona Sódica 500mg
db.Exec("INSERT INTO cart_items (id, buyer_id, product_id, quantity, unit_cents, created_at, updated_at) VALUES ($1, $2, $3, 1, 100, NOW(), NOW())",
"018db2f1-0000-7000-8000-000000000101", buyer1, productID)
// Check buyer1 cart
var count1 int
db.Get(&count1, "SELECT COUNT(*) FROM cart_items WHERE buyer_id = $1", buyer1)
if count1 != 1 {
t.Errorf("Buyer 1 should have 1 item, got %d", count1)
}
// Check buyer2 cart
var count2 int
db.Get(&count2, "SELECT COUNT(*) FROM cart_items WHERE buyer_id = $1", buyer2)
if count2 != 0 {
t.Errorf("Buyer 2 should have 0 items, got %d", count2)
}
fmt.Printf("\nCart Isolation Test: Buyer 1 count=%d, Buyer 2 count=%d\n", count1, count2)
}
func TestCheckPrices(t *testing.T) {
db, err := sqlx.Open("pgx", "postgres://postgres:123@localhost:55432/saveinmed?sslmode=disable")
if err != nil {
log.Fatal(err)
}
defer db.Close()
var products []struct {
Name string `db:"name"`
PriceCents int64 `db:"price_cents"`
}
err = db.Select(&products, "SELECT name, price_cents FROM products WHERE name LIKE '%Dipirona%'")
if err != nil {
t.Fatal(err)
}
fmt.Printf("\n--- Dipirona Prices ---\n")
for _, p := range products {
fmt.Printf("Product: %s, PriceCents: %d\n", p.Name, p.PriceCents)
}
}