more service unit tests and data
authorBill Erickson <berick@esilibrary.com>
Tue, 6 May 2014 19:34:23 +0000 (15:34 -0400)
committerBill Erickson <berick@esilibrary.com>
Tue, 6 May 2014 19:34:23 +0000 (15:34 -0400)
* new test data dir for storing mock data
* script for generating IDL2js output from the EG IDL for testing
* unit test scripts egIDL and egOrg

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/web/js/ui/default/staff/services/idl.js
Open-ILS/web/js/ui/default/staff/test/data/translate_idl2js.pl [new file with mode: 0755]
Open-ILS/web/js/ui/default/staff/test/karma.conf.js
Open-ILS/web/js/ui/default/staff/test/unit/egIDL.js [new file with mode: 0644]
Open-ILS/web/js/ui/default/staff/test/unit/egOrg.js [new file with mode: 0644]

index 9b55937..b748e9a 100644 (file)
@@ -26,8 +26,10 @@ angular.module('egCoreMod')
         // retain a copy of the full IDL within the service
         service.classes = $window._preload_fieldmapper_IDL;
 
-        // original, global reference no longer needed
-        $window._preload_fieldmapper_IDL = null;
+        // keep this reference around (note: not a clone, just a ref)
+        // so that unit tests, which repeatedly instantiate the
+        // service will work.
+        //$window._preload_fieldmapper_IDL = null;
 
         /**
          * Creates the class constructor and getter/setter
diff --git a/Open-ILS/web/js/ui/default/staff/test/data/translate_idl2js.pl b/Open-ILS/web/js/ui/default/staff/test/data/translate_idl2js.pl
new file mode 100755 (executable)
index 0000000..fbf998f
--- /dev/null
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use strict; use warnings;
+use XML::LibXML;
+use XML::LibXSLT;
+my $out_file = 'IDL2js.js';
+my $idl_file = '../../../../../../../examples/fm_IDL.xml';
+my $xsl_file = '/openils/var/xsl/fm_IDL2js.xsl'; # FIXME: hard-coded path
+
+my $xslt = XML::LibXSLT->new();
+my $style_doc = XML::LibXML->load_xml(location => $xsl_file, no_cdata=>1);
+my $stylesheet = $xslt->parse_stylesheet($style_doc);
+my $idl_doc = XML::LibXML->load_xml(location => $idl_file);
+my $results = $stylesheet->transform($idl_doc);
+my $output = $stylesheet->output_as_bytes($results);
+
+open(IDL, ">$out_file") or die "Cannot open IDL2js file $out_file : $!\n";
+
+print IDL $output;
+
+close(IDL);
+
+
index e20f3db..afcea84 100644 (file)
@@ -16,6 +16,10 @@ module.exports = function(config){
       '/openils/lib/javascript/JSON_v1.js',
       '/openils/lib/javascript/opensrf.js',
       '/openils/lib/javascript/opensrf_ws.js',
+
+      // mock data for testing only
+      'test/data/IDL2js.js',
+
       // service/*.js have to be loaded in order
       'services/core.js',
       'services/idl.js',
@@ -39,6 +43,7 @@ module.exports = function(config){
       'circ/**/*.js',
       'cat/**/*.js',
       'admin/**/*.js',
+      'test/unit/egIDL.js', // order matters here
       'test/unit/**/*.js'
     ],
 
diff --git a/Open-ILS/web/js/ui/default/staff/test/unit/egIDL.js b/Open-ILS/web/js/ui/default/staff/test/unit/egIDL.js
new file mode 100644 (file)
index 0000000..621cf25
--- /dev/null
@@ -0,0 +1,26 @@
+'use strict';
+
+describe('egIDL', function(){
+    beforeEach(module('egCoreMod'));
+
+    it('should parse the IDL', inject(function(egIDL) {
+        egIDL.parseIDL();
+        expect(egIDL.classes.aou.fields.length).toBeGreaterThan(0);
+    }));
+
+    it('should create an aou object', inject(function(egIDL) {
+        egIDL.parseIDL();
+        var org = new egIDL.aou();
+        expect(typeof org.id).toBe('function');
+    }));
+
+    it('should create an aou object with accessor/mutators', inject(function(egIDL) {
+        egIDL.parseIDL();
+        var org = new egIDL.aou();
+        org.name('AN ORG');
+        expect(org.name()).toBe('AN ORG');
+    }));
+
+});
+
+
diff --git a/Open-ILS/web/js/ui/default/staff/test/unit/egOrg.js b/Open-ILS/web/js/ui/default/staff/test/unit/egOrg.js
new file mode 100644 (file)
index 0000000..f75a6dd
--- /dev/null
@@ -0,0 +1,46 @@
+'use strict';
+
+describe('egOrg', function(){
+    beforeEach(module('egCoreMod'));
+
+    function mkTree(egIDL, egEnv) { // FIXME: external sample data
+        egIDL.parseIDL();
+        var org1 = new egIDL.aou(); org1.id(1);
+        var org2 = new egIDL.aou(); org2.id(2); org2.parent_ou(1);
+        var org3 = new egIDL.aou(); org3.id(3); org3.parent_ou(1);
+        var org4 = new egIDL.aou(); org4.id(4); org4.parent_ou(2);
+        org1.children([org2, org3]);
+        org2.children([org4]);
+        org3.children([]);
+        org4.children([]);
+
+        egEnv.absorbTree(org1, 'aou');
+    }
+
+    it('should provide get by ID', inject(function(egIDL, egEnv, egOrg) {
+        mkTree(egIDL, egEnv);
+        expect(egOrg.get(egEnv.aou.tree.id())).toBe(egEnv.aou.tree);
+    }));
+
+    it('should provide get by node', inject(function(egIDL, egEnv, egOrg) {
+        mkTree(egIDL, egEnv);
+        expect(egOrg.get(egEnv.aou.tree).id()).toBe(egEnv.aou.tree.id());
+    }));
+
+    it('should provide ancestors', inject(function(egIDL, egEnv, egOrg) {
+        mkTree(egIDL, egEnv);
+        expect(egOrg.ancestors(2, true)).toEqual([2, 1]);
+    }));
+
+    it('should provide descendants', inject(function(egIDL, egEnv, egOrg) {
+        mkTree(egIDL, egEnv);
+        expect(egOrg.descendants(2, true)).toEqual([2, 4]);
+    }));
+
+    it('should provide full path', inject(function(egIDL, egEnv, egOrg) {
+        mkTree(egIDL, egEnv);
+        expect(egOrg.fullPath(4, true)).toEqual([4, 2, 1]);
+    }));
+});
+
+