diff options
Diffstat (limited to 'models/fertilizer-product.rkt')
| -rw-r--r-- | models/fertilizer-product.rkt | 98 |
1 files changed, 97 insertions, 1 deletions
diff --git a/models/fertilizer-product.rkt b/models/fertilizer-product.rkt index 4481010..5c0de6c 100644 --- a/models/fertilizer-product.rkt +++ b/models/fertilizer-product.rkt @@ -26,7 +26,10 @@ (struct fertilizer-product (id canonical-name nutrient-values brand-name) #:transparent #:guard (λ (id canonical-name nutrient-values brand-name _) - (values id canonical-name nutrient-values (if (sql-null? brand-name) #f brand-name))) + (values id + canonical-name + nutrient-values + (if (or (sql-null? brand-name) (= (string-length brand-name) 0)) #f brand-name))) #:property prop:custom-write (λ (v out _mode) (fprintf out "Fertilizer #~a\n" (fertilizer-product-id v)) @@ -163,3 +166,96 @@ (define (delete-fertilizer-product! fp-or-id) (query-exec (current-conn) (delete #:from fertilizer_products #:where (= id ,(->fp-id fp-or-id))))) + +(module+ test + (require rackunit + rackunit/text-ui + "../db/conn.rkt" + "../db/migrations.rkt" + "../models/nutrient.rkt") + + (define canonical-product-name "MasterBlend 4-20") + + (run-tests + (test-suite "Fertilizer product model" + #:before (λ () + (connect! #:path 'memory) + (migrate-all!) + (create-nutrient! "Nitrogen" "Azote" "N") + (create-nutrient! "Phosphorus" "Phosphore" "P") + (create-nutrient! "Potassium" "Potassium" "K")) + #:after (λ () (disconnect!)) + + (test-case "Create product with name, brand, and values" + (define nitrogen (get-nutrient #:name "Nitrogen")) + (define phosphorus (get-nutrient #:name "Phosphorus")) + + (create-fertilizer-product! canonical-product-name + "MasterBlend" + (hash nitrogen 40 phosphorus 200)) + + (check-equal? (length (get-fertilizer-products)) 1) + + (define fp (get-fertilizer-product #:canonical-name canonical-product-name)) + (check-true (fertilizer-product? fp)) + (check-equal? (fertilizer-product-canonical-name fp) canonical-product-name) + (check-equal? (fertilizer-product-brand-name fp) "MasterBlend")) + + (test-case "Create product without brand name" + (define nitrogen (get-nutrient #:name "Nitrogen")) + + (define fp (create-fertilizer-product! "Generic N" "" (hash nitrogen 100))) + + (check-false (fertilizer-product-brand-name fp))) + + (test-case "Check all product values" + (define nitrogen (get-nutrient #:name "Nitrogen")) + (define phosphorus (get-nutrient #:name "Phosphorus")) + + (define fp (get-fertilizer-product #:canonical-name canonical-product-name)) + + (check-= (get-fertilizer-product-value fp nitrogen) 40 0) + (check-= (get-fertilizer-product-value fp phosphorus) 200 0) + + (define fpv (fertilizer-product-nutrient-values fp)) + + (check-equal? + (get-fertilizer-product-values fp) + fpv + "return value of get-fertilizer-product-values ≠ fertilizer-product-values struct accessor") + + (check-equal? (hash-count fpv) 2)) + + (test-case "Get product by id" + (define fp (get-fertilizer-product #:canonical-name canonical-product-name)) + (define fp-by-id (get-fertilizer-product #:id (fertilizer-product-id fp))) + + (check-equal? fp fp-by-id)) + + (test-case "Handle missing nutrient in product" + (define nitrogen (get-nutrient #:name "Nitrogen")) + (define potassium (get-nutrient #:name "Potassium")) + + (define fp (get-fertilizer-product #:canonical-name canonical-product-name)) + (check-false (get-fertilizer-product-value fp potassium))) + + (test-case "Custom write property formatting" + (define nitrogen (get-nutrient #:name "Nitrogen")) + (define fp (create-fertilizer-product! "Test Fertilizer" "TestBrand" (hash nitrogen 50))) + + (define output (open-output-string)) + (write fp output) + (define result (get-output-string output)) + + (check-true (string-contains? result "Fertilizer #")) + (check-true (string-contains? result "Test Fertilizer")) + (check-true (string-contains? result "TestBrand"))) + + (test-case "Delete product and cascade to product values" + (define fp (get-fertilizer-product #:canonical-name canonical-product-name)) + (delete-fertilizer-product! fp) + (check-false (get-fertilizer-product #:id (fertilizer-product-id fp))) + (check-equal? (length (get-fertilizer-products)) + 2 + "wrong number of fertilizer products were deleted") + (check-true (hash-empty? (get-fertilizer-product-values fp))))))) |