summaryrefslogtreecommitdiff
path: root/models/crop-requirement.rkt
diff options
context:
space:
mode:
authorMarius Peter <dev@marius-peter.com>2025-11-29 18:12:24 +0100
committerMarius Peter <dev@marius-peter.com>2025-11-29 18:12:24 +0100
commitb5b9637c35436f1f4ec70207c519eb1e13da555c (patch)
tree542034622184d9f44f4a405821bd9a391508c097 /models/crop-requirement.rkt
parenta3e00cb41614056b898d74bafe0f86afb2590c56 (diff)
Add unit tests to model modules.
Diffstat (limited to 'models/crop-requirement.rkt')
-rw-r--r--models/crop-requirement.rkt90
1 files changed, 90 insertions, 0 deletions
diff --git a/models/crop-requirement.rkt b/models/crop-requirement.rkt
index 733126e..e26f1dc 100644
--- a/models/crop-requirement.rkt
+++ b/models/crop-requirement.rkt
@@ -163,3 +163,93 @@
([(nutrient value) (in-hash (crop-requirement-nutrient-values crop-requirement))])
(define contribution (* value weight))
(hash-update acc nutrient (λ (old) (+ old contribution)) 0))))
+
+(module+ test
+ (require rackunit
+ rackunit/text-ui
+ "../db/conn.rkt"
+ "../db/migrations.rkt"
+ "../models/nutrient.rkt"
+ "../models/crop.rkt")
+
+ (define requirement-profile "Tomato - Vegetative")
+
+ (run-tests
+ (test-suite "Crop requirement 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 requirement with profile and values"
+ (define nitrogen (get-nutrient #:name "Nitrogen"))
+ (define phosphorus (get-nutrient #:name "Phosphorus"))
+
+ (create-crop-requirement! requirement-profile (hash nitrogen 150 phosphorus 50))
+
+ (check-equal? (length (get-crop-requirements)) 1)
+
+ (define cr (get-crop-requirement #:profile requirement-profile))
+ (check-true (crop-requirement? cr))
+ (check-equal? (crop-requirement-profile cr) requirement-profile))
+
+ (test-case "Create requirement with associated crop"
+ (define tomato (create-crop! "Tomato"))
+ (define nitrogen (get-nutrient #:name "Nitrogen"))
+
+ (define cr (create-crop-requirement! "Tomato - Fruiting" (hash nitrogen 200) tomato))
+
+ (check-equal? (crop-requirement-crop-id cr) (crop-id tomato)))
+
+ (test-case "Check all requirement values"
+ (define nitrogen (get-nutrient #:name "Nitrogen"))
+ (define phosphorus (get-nutrient #:name "Phosphorus"))
+
+ (define cr (get-crop-requirement #:profile requirement-profile))
+
+ (check-= (get-crop-requirement-value cr nitrogen) 150 0)
+ (check-= (get-crop-requirement-value cr phosphorus) 50 0)
+
+ (define crv (crop-requirement-nutrient-values cr))
+
+ (check-equal?
+ (get-crop-requirement-values cr)
+ crv
+ "return value of get-crop-requirement-values ≠ crop-requirement-values struct accessor")
+
+ (check-equal? (hash-count crv) 2)
+ (check-= (hash-ref crv nitrogen) 150 0)
+ (check-= (hash-ref crv phosphorus) 50 0))
+
+ (test-case "Get requirement by id"
+ (define cr (get-crop-requirement #:profile requirement-profile))
+ (define cr-by-id (get-crop-requirement #:id (crop-requirement-id cr)))
+
+ (check-equal? cr cr-by-id))
+
+ (test-case "Average crop requirement nutrient values"
+ (define nitrogen (get-nutrient #:name "Nitrogen"))
+ (define phosphorus (get-nutrient #:name "Phosphorus"))
+
+ (define cr1 (get-crop-requirement #:profile requirement-profile))
+ (define cr2 (create-crop-requirement! "Lettuce" (hash nitrogen 100 phosphorus 30)))
+
+ (define mix (list (cons cr1 60) (cons cr2 40)))
+ (define avg (average-crop-requirement-nutrient-values mix))
+
+ ;; 150 * 0.6 + 100 * 0.4 = 90 + 40 = 130
+ (check-= (hash-ref avg nitrogen) 130 0.01)
+ ;; 50 * 0.6 + 30 * 0.4 = 30 + 12 = 42
+ (check-= (hash-ref avg phosphorus) 42 0.01))
+
+ (test-case "Delete requirement and cascade to requirement values"
+ (define cr (get-crop-requirement #:profile requirement-profile))
+ (delete-crop-requirement! cr)
+ (check-false (get-crop-requirement #:id (crop-requirement-id cr)))
+ (check-equal? (length (get-crop-requirements))
+ 2
+ "wrong number of crop requirements were deleted")
+ (check-true (hash-empty? (get-crop-requirement-values cr)))))))
Copyright 2019--2026 Marius PETER