summaryrefslogtreecommitdiff
path: root/models/crop.rkt
diff options
context:
space:
mode:
Diffstat (limited to 'models/crop.rkt')
-rw-r--r--models/crop.rkt81
1 files changed, 81 insertions, 0 deletions
diff --git a/models/crop.rkt b/models/crop.rkt
index ba93550..4430543 100644
--- a/models/crop.rkt
+++ b/models/crop.rkt
@@ -61,3 +61,84 @@
(define (delete-crop! id)
(query-exec (current-conn) (delete #:from crops #:where (= id ,id))))
+
+(module+ test
+ (require rackunit
+ rackunit/text-ui
+ "../db/conn.rkt"
+ "../db/migrations.rkt")
+
+ (define test-crop-name "examplium-plant")
+
+ (run-tests (test-suite "Crop model"
+ #:before (λ ()
+ (connect! #:path 'memory)
+ (migrate-all!))
+ #:after (λ () (disconnect!))
+
+ (test-case "Create crop"
+ (check-equal? (length (get-crops)) 0)
+ (define c1 (create-crop! (crop #f test-crop-name)))
+ (check-equal? (length (get-crops)) 1)
+ (check-true (crop? c1))
+ (check-equal? (crop-name c1) test-crop-name)
+ (check-true (db-id? (crop-id c1))))
+
+ (test-case "Create duplicate crop returns existing"
+ (define c1 (create-crop! (crop #f test-crop-name)))
+ (define c2 (create-crop! (crop #f test-crop-name)))
+ (check-equal? (length (get-crops)) 1)
+ (check-equal? (crop-id c1) (crop-id c2))
+ (check-equal? c1 c2))
+
+ (test-case "Get crop by id"
+ (define c1 (get-crop #:name test-crop-name))
+ (define c2 (get-crop #:id (crop-id c1)))
+ (check-equal? c1 c2))
+
+ (test-case "Get crop by name"
+ (define c (get-crop #:name test-crop-name))
+ (check-true (crop? c))
+ (check-equal? (crop-name c) test-crop-name))
+
+ (test-case "Get crop with both id and name"
+ (define c1 (get-crop #:name test-crop-name))
+ (define c2 (get-crop #:id (crop-id c1) #:name test-crop-name))
+ (check-equal? c1 c2))
+
+ (test-case "Get non-existent crop"
+ (check-false (get-crop #:name "non-existent-crop"))
+ (check-false (get-crop #:id 9999)))
+
+ (test-case "Get all crops"
+ (create-crop! (crop #f "ignorium-plant"))
+ (create-crop! (crop #f "testium-plant"))
+ (define crops (get-crops))
+ (check-equal? (length crops) 3)
+ (check-true (andmap crop? crops)))
+
+ (test-case "Update crop name"
+ (define c1 (get-crop #:name test-crop-name))
+ (define updated-crop (crop (crop-id c1) "examplium-updated"))
+ (update-crop! updated-crop)
+ (define c2 (get-crop #:id (crop-id c1)))
+ (check-equal? (crop-name c2) "examplium-updated")
+ (check-equal? (crop-id c1) (crop-id c2))
+ (check-equal? (length (get-crops)) 3))
+
+ (test-case "Update crop with invalid id raises error"
+ (define invalid-crop (crop #f "invalid"))
+ (check-exn exn:fail:contract? (λ () (update-crop! invalid-crop))))
+
+ (test-case "Delete crop"
+ (define c (get-crop #:name "ignorium-plant"))
+ (delete-crop! (crop-id c))
+ (check-false (get-crop #:id (crop-id c)))
+ (check-equal? (length (get-crops)) 2))
+
+ (test-case "Delete crop by id"
+ (define c (get-crop #:name "testium-plant"))
+ (define crop-id-val (crop-id c))
+ (delete-crop! crop-id-val)
+ (check-false (get-crop #:id crop-id-val))
+ (check-equal? (length (get-crops)) 1)))))
Copyright 2019--2026 Marius PETER