Files
vibetree/internal/assets/quantity_test.go
T
Owen fb85c9edbb Add asset checkout, quantity/uniqueness, contacts, and QR labels
Checkout:
- assets.AssignUser + POST /app/assets/{id}/checkout assign/unassign an
  asset's user independent of the full edit form. New shared
  #checkoutModal, wired from a "Check Out..." context-menu action, backed
  by GET /app/assets/{id} and GET /app/users/options JSON endpoints.

Quantity / uniqueness:
- Item types gain an IsUnique flag (checked by default, matching prior
  behavior). Assets gain a Quantity column, fixed at 1 for unique types
  and adjustable via +/- steppers (POST /app/assets/{id}/quantity) for
  non-unique ones. Flipping a type back to unique clamps existing assets'
  quantity back to 1. CSV import/export gained a Quantity column.
  Migration 0003_quantity.sql.

Contacts:
- New models.RoleContact: an assignable person who cannot sign in
  (User.CanLogin). Migration 0004_contacts.sql rebuilds the users table
  (SQLite can't alter a CHECK constraint in place) to allow the role.
  Users form/list updated; contact creation auto-generates an unusable
  password since the login path is blocked by role regardless.

QR asset tags and printable labels:
- Each asset's QR code (via github.com/skip2/go-qrcode, pure Go) encodes
  an absolute /scan/<tag> URL; GET /scan/{tag} resolves it and redirects
  into /inventory?asset=<id>, which app.js picks up on load to
  auto-select that asset - works from a phone camera or a USB
  scanner-as-keyboard-wedge pointed at the address bar.
- GET /labels renders a print-ready sheet (right-click an asset for one
  label, a folder for every asset in it) with a template switcher between
  Avery 5160 (verified dimensions) and 61526 asset tag labels (best-effort
  layout, flagged as such on the page).

Also refreshed README to reflect the right-click-first tree UX and all of
the above.
2026-08-17 16:28:14 -05:00

124 lines
3.4 KiB
Go

package assets_test
import (
"context"
"testing"
"inventree/internal/assets"
"inventree/internal/itemtypes"
"inventree/internal/models"
)
func TestQuantityFixedAtOneForUniqueTypes(t *testing.T) {
ctx := context.Background()
as, _, _ := testStores(t)
a := &models.Asset{Name: "Laptop 1", ItemTypeID: itemTypeLaptop, StatusID: statusInUse, Quantity: 7}
if err := as.Create(ctx, a, nil); err != nil {
t.Fatalf("create: %v", err)
}
if a.Quantity != 1 {
t.Fatalf("expected quantity forced to 1 for a unique item type, got %d", a.Quantity)
}
if _, err := as.AdjustQuantity(ctx, a.ID, 1); err != assets.ErrQuantityNotTracked {
t.Fatalf("expected ErrQuantityNotTracked, got %v", err)
}
}
func TestQuantityTrackedForNonUniqueTypes(t *testing.T) {
ctx := context.Background()
as, _, db := testStores(t)
itStore := itemtypes.NewStore(db)
cable := &models.ItemType{Name: "Cable", Enabled: true, IsUnique: false}
if err := itStore.Create(ctx, cable); err != nil {
t.Fatalf("create item type: %v", err)
}
a := &models.Asset{Name: "USB-A Cable", ItemTypeID: cable.ID, StatusID: statusInUse, Quantity: 10}
if err := as.Create(ctx, a, nil); err != nil {
t.Fatalf("create: %v", err)
}
if a.Quantity != 10 {
t.Fatalf("expected quantity 10, got %d", a.Quantity)
}
updated, err := as.AdjustQuantity(ctx, a.ID, 5)
if err != nil {
t.Fatalf("adjust +5: %v", err)
}
if updated.Quantity != 15 {
t.Fatalf("expected 15, got %d", updated.Quantity)
}
updated, err = as.AdjustQuantity(ctx, a.ID, -100)
if err != nil {
t.Fatalf("adjust -100: %v", err)
}
if updated.Quantity != 0 {
t.Fatalf("expected quantity floored at 0, got %d", updated.Quantity)
}
}
func TestItemTypeBecomingUniqueClampsExistingQuantities(t *testing.T) {
ctx := context.Background()
as, _, db := testStores(t)
itStore := itemtypes.NewStore(db)
cable := &models.ItemType{Name: "Cable", Enabled: true, IsUnique: false}
if err := itStore.Create(ctx, cable); err != nil {
t.Fatalf("create item type: %v", err)
}
a := &models.Asset{Name: "USB-A Cable", ItemTypeID: cable.ID, StatusID: statusInUse, Quantity: 20}
if err := as.Create(ctx, a, nil); err != nil {
t.Fatalf("create asset: %v", err)
}
cable.IsUnique = true
if err := itStore.Update(ctx, cable); err != nil {
t.Fatalf("update item type: %v", err)
}
got, err := as.Get(ctx, a.ID)
if err != nil {
t.Fatalf("get: %v", err)
}
if got.Quantity != 1 {
t.Fatalf("expected quantity clamped to 1 after type became unique, got %d", got.Quantity)
}
}
func TestAssignUserChecksInAndOut(t *testing.T) {
ctx := context.Background()
as, _, _ := testStores(t)
a := &models.Asset{Name: "Laptop 1", ItemTypeID: itemTypeLaptop, StatusID: statusInUse}
if err := as.Create(ctx, a, nil); err != nil {
t.Fatalf("create: %v", err)
}
userID := int64(1) // seeded admin
if err := as.AssignUser(ctx, a.ID, &userID); err != nil {
t.Fatalf("assign user: %v", err)
}
got, err := as.Get(ctx, a.ID)
if err != nil {
t.Fatalf("get: %v", err)
}
if got.AssignedUserID == nil || *got.AssignedUserID != userID {
t.Fatalf("expected asset checked out to user %d, got %+v", userID, got.AssignedUserID)
}
if err := as.AssignUser(ctx, a.ID, nil); err != nil {
t.Fatalf("check in: %v", err)
}
got, err = as.Get(ctx, a.ID)
if err != nil {
t.Fatalf("get: %v", err)
}
if got.AssignedUserID != nil {
t.Fatalf("expected asset checked in (unassigned), got %+v", got.AssignedUserID)
}
}