Update unit tests
authorJeremy Tan <[email protected]>
Fri, 6 Sep 2013 12:54:45 +0000 (20:54 +0800)
committerJeremy Tan <[email protected]>
Fri, 6 Sep 2013 12:54:45 +0000 (20:54 +0800)
testing/qunit/index.html
testing/qunit/unit-tests.js

index faf4b51..765bf04 100644 (file)
        <div id="qunit"></div>
        <div id="qunit-fixture"></div>
        <script src="unit-tests.js"></script>
-
-  <div style="margin:1em auto; width: 20em;">
-    <h3>Access control</h3>
-    <form id="control" action="#">
-      <table>
-        <tr>
-          <td>Username:</td>
-          <td><input type="text" id="username"></td>
-        </tr>
-        <tr>
-          <td>Password:</td>
-          <td><input type="password" id="password"></td>
-        </tr>
-        <tr>
-          <td><input type="submit" value="Submit"></td>
-        </tr>
-      </table>
-    </form>
-  </div>
+  
+    <hr>
+    <h3 style="margin: 1em auto;">
+        The control tests won't complete until the credentials are 
+        supplied below.
+    </h3>
+    <div style="margin:1em auto; width: 20em;">
+      
+      <h4>Access control</h4>
+      <form id="control" action="#">
+        <table>
+          <tr>
+            <td>Username:</td>
+            <td><input type="text" id="username"></td>
+          </tr>
+          <tr>
+            <td>Password:</td>
+            <td><input type="password" id="password"></td>
+          </tr>
+          <tr>
+            <td><input type="submit" value="Submit"></td>
+          </tr>
+        </table>
+      </form>
+    </div>
 </body>
 </html>
\ No newline at end of file
index 12bb76a..476e145 100644 (file)
 
 ut = {};
 ut.api = location.protocol + "//" +  location.host + "/api/";
-ut.ckey = undefined;
-ut.controlcb = $.Callbacks();
+ut.controlcb = $.Deferred();
 
 /**
- * Sends an AJAX query to the API
+ * Sends a synchronous AJAX query to the API
+ * A synchronous request makes it easier for unit testing.
  * @param {string} module The name of the module to be queried
  * @param {Object} opts Object holding the parameters, username, password and
  *                 callback. The parameters should be an object of key/value
@@ -39,7 +39,7 @@ function query(module, opts) {
     dataType: 'json',
     data: opts.params,
     beforeSend: authfunc,
-    async: opts.async
+    async: false
   }).done(opts.callback)
     .fail(function(jqXHR) {
       ok(false, "Request failed: " + jqXHR.status.toString() + " " + jqXHR.statusText);
@@ -48,86 +48,87 @@ function query(module, opts) {
 }
 
 QUnit.module("API basics");
-QUnit.asyncTest("API Existence (identify)", function () {
-  query("identify", {callback : function(data) {
-   start();
-   ok(data.status > 0, "Return status");
-   ok(data.description, data.description);
-   ok(data.build_date, data.build_date);
-  }});
+QUnit.test("API Existence (identify)", function () {
+  query("identify", {params : {actuators : true, sensors : true}, 
+   callback : function(data) {
+    ok(data.status > 0, "Return status");
+    ok(data.description !== undefined, data.description);
+    ok(data.build_date !== undefined, data.build_date);
+    ok(data.api_version !== undefined, "API version: " + data.api_version);
+    ok(data.sensors !== undefined, "Sensors list");
+    ok(data.actuators !== undefined, "Actuators list");
+    
+    var sl = "Sensors: ", al = "Actuators: ";
+    for (var id in data.sensors) {
+      sl += id + ":" + data.sensors[id] + " ";
+    }
+    for (var id in data.actuators) {
+      al += id + ":" + data.actuators[id] + " ";
+    }
+    ok(sl, sl);
+    ok(al, al);
+   }});
 });
 
-QUnit.asyncTest("Invalid module", function () {
+QUnit.test("Invalid module", function () {
   query("dontexist", {callback : function(data) {
-   start();
    ok(data.status < 0);
   }});
 });
 
 QUnit.module("Sensors");
-QUnit.asyncTest("Existence", function() {
-  query("sensors", {params : {id : 0}, callback : function(data) {
-   start();
-   ok(data.status > 0, "Return status");
-   ok(data.data !== undefined, "Data field existence");
-   var result = "Data: ";
-   for (var i = 0; i < data.data.length; i++) {
-     result += data.data[i][0]  + ":" + data.data[i][1] + ", ";
-   }
-   ok(true, result);
-  }});  
-});
-
-QUnit.asyncTest("Invalid sensor id 1", function() {
-  query("sensors", {params : {id : 999}, callback : function(data) {
-   start();
-   ok(data.status < 0, "Return status");
-  }});  
+QUnit.test("Existence", function() {
+  query("identify", {params : {sensors : 1}, callback : function(data) {
+      ok(data.status > 0, "Identification");
+      var hasSensor = false;
+      for (var id in data.sensors) {
+        hasSensor = true;
+        query("sensors", {params : {id : id}, callback : function(data) {
+          ok(data.status > 0, "Sensor " + id);
+          ok(data.data !== undefined, "Data field existence");
+          var result = "Data: ";
+          for (var i = 0; i < data.data.length; i++) {
+            result += data.data[i][0]  + ":" + data.data[i][1] + ", ";
+          }
+          ok(true, result);
+       }});        
+      }
+      ok(hasSensor, "Has at least one sensor");
+  }});
 });
 
-QUnit.asyncTest("Invalid sensor id 2", function() {
+QUnit.test("Invalid sensor ids", function() {
   query("sensors", {params : {id : ""}, callback : function(data) {
-   start();
-   ok(data.status < 0, "Return status");
+   ok(data.status < 0, "No id");
   }});  
-});
-
-QUnit.asyncTest("Out of bounds sensor id 1", function() {
+  query("sensors", {params : {id : 999}, callback : function(data) {
+   ok(data.status < 0, "Id too large");
+  }});
   query("sensors", {params : {id : "-1"}, callback : function(data) {
-   start();
-   ok(data.status < 0, "Return status");
-  }});  
-});
-
-QUnit.asyncTest("Out of bounds sensor id 2", function() {
-  query("sensors", {params : {id : "999"}, callback : function(data) {
-   start();
-   ok(data.status < 0, "Return status");
+   ok(data.status < 0, "Negative id");
   }});  
 });
 
 QUnit.module("Controls and access");
-QUnit.asyncTest("Gaining access", function() {
-  ut.controlcb.add(function () {
+QUnit.asyncTest("Setting actuator value", function () {
+  $.when(ut.controlcb).done(function () {
+    start();
+    var key;
+    
     query("control", {params : {action : "start", force : true}, 
                     username : $("#username").val(), password : $("#password").val(),
                     async : false, 
                     callback : function(data) {
-     start();
-     ok(data.status > 0, "Return status");
-     ut.ckey = data.key;
-    }});
-  });
-});
-
-QUnit.asyncTest("Setting actuator value", function () {
-  ut.controlcb.add(function () {
+     ok(data.status > 0, "Gaining access key");
+     ok(data.key, "Access key - " + data.key);
+     key = data.key;
+    }});    
     query("control", {params : {action : "set", id : 0,
           username : $("#username").val(), password : $("#password").val(),
-          value : 200, key : ut.ckey},
+          value : 200, key : key},
         callback : function(data) {
-          start();
-          ok(data.status > 0, "Return status");
+          ok(data.status > 0, "Setting actuator");
           ok(true, data.description);
     }});
   });
@@ -135,7 +136,7 @@ QUnit.asyncTest("Setting actuator value", function () {
 
 $(document).ready(function(){
   $("#control").submit(function () {
-    ut.controlcb.fire();
+    ut.controlcb.resolve();
     return false;
   });
 });
\ No newline at end of file

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