diff options
Diffstat (limited to 'models/crop-requirement.rkt')
| -rw-r--r-- | models/crop-requirement.rkt | 90 |
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))))))) |