Fix dilatometer, other misc stuff
[matches/MCTX3420.git] / testing / qunit / unit-tests.js
1 /**
2  * MCTX3420 2013 - Remote pressurised can experiment.
3  * Unit testing for the server API.
4  * These unit tests use the QUnit unit testing framework.
5  * @requires QUnit, jQuery, and base64.js
6  * @date 28/8/13
7  * @author Jeremy Tan
8  */
9
10 //Namespace ut
11
12 ut = {};
13 ut.api = location.protocol + "//" +  location.host + "/api/";
14 ut.controlcb = $.Deferred();
15
16 /**
17  * Sends a synchronous AJAX query to the API
18  * A synchronous request makes it easier for unit testing.
19  * @param {string} module The name of the module to be queried
20  * @param {Object} opts Object holding the parameters, username, password and
21  *                 callback. The parameters should be an object of key/value
22  *                 pairs.
23  * @returns jqXHR object (but calls callback with JSON data, or null on AJAX error)
24  */
25 function query(module, opts) {
26   var queryurl = ut.api + module;
27   
28   var authfunc;
29   if (opts.username) {
30     authfunc = function(xhr) {
31       xhr.setRequestHeader("Authorization",
32         "Basic " + base64.encode(opts.username + ":" + opts.password));
33     };
34   }
35   
36   return $.ajax({
37     url: queryurl,
38     type: 'GET',
39     dataType: 'json',
40     data: opts.params,
41     beforeSend: authfunc,
42     async: false
43   }).done(opts.callback)
44     .fail(function(jqXHR) {
45       ok(false, "Request failed: " + jqXHR.status.toString() + " " + jqXHR.statusText);
46       opts.callback(null);
47     });
48 }
49
50 QUnit.module("API basics");
51 QUnit.test("API Existence (identify)", function () {
52   query("identify", {params : {actuators : true, sensors : true}, 
53    callback : function(data) {
54     ok(data.status > 0, "Return status");
55     ok(data.description !== undefined, data.description);
56     ok(data.build_date !== undefined, data.build_date);
57     ok(data.api_version !== undefined, "API version: " + data.api_version);
58     ok(data.sensors !== undefined, "Sensors list");
59     ok(data.actuators !== undefined, "Actuators list");
60     
61     var sl = "Sensors: ", al = "Actuators: ";
62     for (var id in data.sensors) {
63       sl += id + ":" + data.sensors[id] + " ";
64     }
65     for (var id in data.actuators) {
66       al += id + ":" + data.actuators[id] + " ";
67     }
68     ok(sl, sl);
69     ok(al, al);
70    }});
71 });
72
73 QUnit.test("Invalid module", function () {
74   query("dontexist", {callback : function(data) {
75    ok(data.status < 0);
76   }});
77 });
78
79 QUnit.module("Sensors");
80 QUnit.test("Existence", function() {
81   query("identify", {params : {sensors : 1}, callback : function(data) {
82       ok(data.status > 0, "Identification");
83       var hasSensor = false;
84       for (var id in data.sensors) {
85         hasSensor = true;
86         query("sensors", {params : {id : id}, callback : function(data) {
87           ok(data.status > 0, "Sensor " + id);
88           ok(data.data !== undefined, "Data field existence");
89           var result = "Data: ";
90           for (var i = 0; i < data.data.length; i++) {
91             result += data.data[i][0]  + ":" + data.data[i][1] + ", ";
92           }
93           ok(true, result);
94        }});        
95       }
96       ok(hasSensor, "Has at least one sensor");
97   }});
98  
99 });
100
101 QUnit.test("Invalid sensor ids", function() {
102   query("sensors", {params : {id : ""}, callback : function(data) {
103    ok(data.status < 0, "No id");
104   }});  
105   query("sensors", {params : {id : 999}, callback : function(data) {
106    ok(data.status < 0, "Id too large");
107   }});
108   query("sensors", {params : {id : "-1"}, callback : function(data) {
109    ok(data.status < 0, "Negative id");
110   }});  
111 });
112
113 QUnit.module("Controls and access");
114 QUnit.asyncTest("Setting actuator value", function () {
115   $.when(ut.controlcb).done(function () {
116     start();
117     var key;
118     
119     query("control", {params : {action : "start", force : true}, 
120                     username : $("#username").val(), password : $("#password").val(),
121                     async : false, 
122                     callback : function(data) {
123      ok(data.status > 0, "Gaining access key");
124      ok(data.key, "Access key - " + data.key);
125      key = data.key;
126     }});    
127     query("control", {params : {action : "set", id : 0,
128           username : $("#username").val(), password : $("#password").val(),
129           value : 200, key : key},
130         callback : function(data) {
131           ok(data.status > 0, "Setting actuator");
132           ok(true, data.description);
133     }});
134   });
135 });
136
137 $(document).ready(function(){
138   $("#control").submit(function () {
139     ut.controlcb.resolve();
140     return false;
141   });
142 });

UCC git Repository :: git.ucc.asn.au