Circulations test data
authorBill Erickson <berick@esilibrary.com>
Tue, 16 Oct 2012 20:59:31 +0000 (16:59 -0400)
committerBill Erickson <berick@esilibrary.com>
Tue, 16 Oct 2012 20:59:31 +0000 (16:59 -0400)
Adds supporting functions for creating circulations test
data.  Adds 162 test circs across different users and profiles and
rules.  In time, more samples and variations would be good.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/tests/datasets/sql/env_create.sql
Open-ILS/tests/datasets/sql/env_destroy.sql
Open-ILS/tests/datasets/sql/load_all.sql
Open-ILS/tests/datasets/sql/transactions.sql [new file with mode: 0644]

index 9e9c9b4..313607d 100644 (file)
@@ -34,3 +34,71 @@ RETURNS void AS $$
         WHERE record > 0 AND label LIKE $4 || '%' AND owning_lib = $2;
 $$ LANGUAGE SQL;
 
+/** Returns the next (by ID) non-deleted copy */
+CREATE FUNCTION evergreen.next_copy (copy_id BIGINT) RETURNS asset.copy AS $$
+    DECLARE copy asset.copy%ROWTYPE;
+    DECLARE max_id BIGINT;
+BEGIN
+    SELECT INTO max_id max(id) FROM asset.copy;
+    WHILE TRUE LOOP
+        copy_id := copy_id + 1;
+        IF copy_id > max_id THEN
+            RETURN NULL;
+        END IF;
+        SELECT INTO copy * FROM asset.copy WHERE id = copy_id;
+        IF NOT copy.deleted THEN
+            RETURN copy;
+        END IF;
+    END LOOP;
+    RETURN NULL;
+END;
+$$ LANGUAGE PLPGSQL;                                                        
+
+
+
+
+/**
+ * Creates circulations
+ */
+CREATE FUNCTION evergreen.populate_circ (
+    patron_barcode TEXT, 
+    copy_barcode TEXT, 
+    circ_lib INTEGER,
+    duration_rule TEXT,
+    recurring_fine_rule TEXT,
+    max_fine_rule TEXT,
+    overdue BOOLEAN
+)
+RETURNS void AS $$
+    INSERT INTO action.circulation (
+        usr, target_copy, circ_lib, circ_staff, renewal_remaining,
+        grace_period, duration, recurring_fine, max_fine, duration_rule,
+        recurring_fine_rule, max_fine_rule, due_date )
+    VALUES (
+        (SELECT usr FROM actor.card WHERE barcode = $1),
+        (SELECT id FROM asset.copy WHERE barcode = $2 AND NOT deleted),
+        $3, -- circ_lib
+        1, -- circ_staff
+        (SELECT max_renewals FROM config.rule_circ_duration WHERE name = $4),
+        (SELECT grace_period FROM config.rule_recurring_fine WHERE name = $5),
+        (SELECT normal FROM config.rule_circ_duration WHERE name = $4),
+        (SELECT normal FROM config.rule_recurring_fine WHERE name = $5),
+        (SELECT amount FROM config.rule_max_fine WHERE name = $6),
+        $4, -- duration_rule
+        $5, -- recurring_fine_rule
+        $6, -- max_fine_rule
+        CASE WHEN $7 THEN -- due_date
+            (DATE(NOW() -  -- subtract duration from now() for overdues
+                (SELECT normal FROM config.rule_circ_duration WHERE name = $4)) 
+                    || ' 23:59:59')::TIMESTAMP -- due_date
+        ELSE 
+            (DATE(NOW() + 
+                (SELECT normal FROM config.rule_circ_duration WHERE name = $4)) 
+                    || ' 23:59:59')::TIMESTAMP -- due_date
+        END
+    );
+$$ LANGUAGE SQL;
+
+
+
+
index 6680640..5795f89 100644 (file)
@@ -3,3 +3,6 @@
 DROP TABLE marcxml_import;
 DROP FUNCTION evergreen.populate_call_number(INTEGER, TEXT, TEXT);
 DROP FUNCTION evergreen.populate_copy(INTEGER, INTEGER, TEXT, TEXT);
+DROP FUNCTION evergreen.next_copy (BIGINT);
+DROP FUNCTION evergreen.populate_circ 
+    (TEXT, TEXT, INTEGER, TEXT, TEXT, TEXT, BOOLEAN);
index 43d95bd..43bbd44 100644 (file)
@@ -31,6 +31,9 @@ INSERT INTO biblio.record_entry (marc, last_xact_id)
 -- load sample staff users
 \i users_staff_134.sql
 
+-- circs, etc.
+\i transactions.sql
+
 -- clean up the env
 \i env_destroy.sql
 
diff --git a/Open-ILS/tests/datasets/sql/transactions.sql b/Open-ILS/tests/datasets/sql/transactions.sql
new file mode 100644 (file)
index 0000000..1e51b3e
--- /dev/null
@@ -0,0 +1,71 @@
+
+
+/* NOTE: fine generator should be run after loading to produce billings */
+
+/**
+ * create 3 regular circs w/ varying (stock) rules and 3 overdue
+ * circs for the fisrt 2 (by ID) users in each profile group.
+ * target copies start at ID 1 and progress upward by ID from
+ * there, skipping deleted copies.
+ * TODO: vary the rules more
+ */
+DO $$                                                                           
+    DECLARE grp INTEGER;                                                      
+    DECLARE recipient INTEGER;
+    DECLARE copy asset.copy%ROWTYPE;
+BEGIN                                                                           
+    copy := evergreen.next_copy(0);
+
+    FOR grp IN SELECT id FROM permission.grp_tree LOOP
+        FOR recipient IN SELECT id FROM actor.usr 
+            WHERE profile = grp ORDER BY id LIMIT 2 LOOP
+
+            copy := evergreen.next_copy(copy.id);
+            PERFORM evergreen.populate_circ(
+                (SELECT barcode FROM actor.card WHERE usr = recipient),
+                copy.barcode, copy.circ_lib,
+                'default', 'default', 'default', FALSE
+            );
+
+            copy := evergreen.next_copy(copy.id);
+            PERFORM evergreen.populate_circ(
+                (SELECT barcode FROM actor.card WHERE usr = recipient),
+                copy.barcode, copy.circ_lib,
+                'default', 'default', 'overdue_min', FALSE
+            );
+
+            copy := evergreen.next_copy(copy.id);
+            PERFORM evergreen.populate_circ(
+                (SELECT barcode FROM actor.card WHERE usr = recipient),
+                copy.barcode, copy.circ_lib,
+                'default', 'default', 'overdue_max', FALSE
+            );
+
+            -- overdues...
+
+            copy := evergreen.next_copy(copy.id);
+            PERFORM evergreen.populate_circ(
+                (SELECT barcode FROM actor.card WHERE usr = recipient),
+                copy.barcode, copy.circ_lib,
+                'default', 'default', 'default', TRUE
+            );
+
+            copy := evergreen.next_copy(copy.id);
+            PERFORM evergreen.populate_circ(
+                (SELECT barcode FROM actor.card WHERE usr = recipient),
+                copy.barcode, copy.circ_lib,
+                'default', 'default', 'overdue_min', TRUE
+            );
+
+            copy := evergreen.next_copy(copy.id);
+            PERFORM evergreen.populate_circ(
+                (SELECT barcode FROM actor.card WHERE usr = recipient),
+                copy.barcode, copy.circ_lib,
+                'default', 'default', 'overdue_max', TRUE
+            );
+
+        END LOOP;                                                                   
+    END LOOP;                                                                   
+END $$;
+
+