Hi,

Please review the attached patch. Thanks!

New test cases have been added to test SerialAssociator and
BulkAssociator using mockup objects. Also fixed a bug in BulkAssociator.

Moved switch_view() out of ipa_entity_generate_views() in entity.js
to allow unit testing using mockup objects. Updated the test case
to validate click event on facets.

--
Endi S. Dewata
>From 8e23c7983f5841e74611cc40077a88d8afee4836 Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edew...@redhat.com>
Date: Mon, 27 Sep 2010 22:13:17 -0400
Subject: [PATCH] Test suite for association.

New test cases have been added to test SerialAssociator and
BulkAssociator using mockup objects. Also fixed a bug in BulkAssociator.

Moved switch_view() out of ipa_entity_generate_views() in entity.js
to allow unit testing using mockup objects. Updated the test case
to validate click event on facets.
---
 install/static/associate.js                |    3 +-
 install/static/entity.js                   |   32 ++++----
 install/static/test/all_tests.html         |    2 +
 install/static/test/association_tests.html |   23 +++++
 install/static/test/association_tests.js   |  124 ++++++++++++++++++++++++++++
 install/static/test/entity_tests.js        |   25 +++++-
 install/static/test/index.html             |    1 +
 7 files changed, 189 insertions(+), 21 deletions(-)
 create mode 100644 install/static/test/association_tests.html
 create mode 100644 install/static/test/association_tests.js

diff --git a/install/static/associate.js b/install/static/associate.js
index 73ce4d3..207c745 100644
--- a/install/static/associate.js
+++ b/install/static/associate.js
@@ -65,6 +65,7 @@ in a single rpc
 */
 function BulkAssociator(form, manyObjPkeys, on_success)
 {
+    var associator = this;
     this.form = form;
     this.manyObjPkeys = manyObjPkeys;
     this.on_success = on_success;
@@ -88,7 +89,7 @@ function BulkAssociator(form, manyObjPkeys, on_success)
                      if (response.error){
                          alert("error adding member: "+response.error.message);
                      }else{
-                         form.on_success();
+                         associator.on_success();
                      }
                  },
                  function(response){
diff --git a/install/static/entity.js b/install/static/entity.js
index 85bbc07..7902752 100644
--- a/install/static/entity.js
+++ b/install/static/entity.js
@@ -59,6 +59,19 @@ function ipa_entity_setup(jobj)
         return (false);
     };
 
+    function switch_view() {
+        var enroll_obj_name = $(this).attr('title');
+        var state = {};
+        if (enroll_obj_name != 'search' && enroll_obj_name != 'details') {
+            state[obj_name + '-facet'] = 'associate';
+            state[obj_name + '-enroll'] = enroll_obj_name;
+        } else {
+            state[obj_name + '-facet'] = enroll_obj_name;
+            state[obj_name + '-enroll'] = '';
+        }
+        $.bbq.pushState(state);
+    };
+
     function setup_search_facet() {
         var filter = $.bbq.getState(obj_name + '-filter', true);
         search_create(obj_name, ipa_entity_search_list[obj_name], jobj);
@@ -71,7 +84,7 @@ function ipa_entity_setup(jobj)
 
     function setup_details_facet() {
         var pkey = $.bbq.getState(obj_name + '-pkey', true);
-        ipa_entity_generate_views(obj_name, jobj);
+        ipa_entity_generate_views(obj_name, jobj, switch_view);
         ipa_details_create(obj_name, ipa_entity_details_list[obj_name], jobj);
         jobj.find('.details-reset').click(reset_on_click);
         jobj.find('.details-update').click(update_on_click);
@@ -90,7 +103,7 @@ function ipa_entity_setup(jobj)
             }
         ];
         var frm = new AssociationList(obj_name, pkey, enroll_obj_name, columns, jobj);
-        ipa_entity_generate_views(obj_name, jobj);
+        ipa_entity_generate_views(obj_name, jobj, switch_view);
         frm.setup();
     };
 
@@ -106,21 +119,8 @@ function ipa_entity_setup(jobj)
     }
 }
 
-function ipa_entity_generate_views(obj_name, container)
+function ipa_entity_generate_views(obj_name, container, switch_view)
 {
-    function switch_view() {
-        var enroll_obj_name = $(this).attr('title');
-        var state = {};
-        if (enroll_obj_name != 'search' && enroll_obj_name != 'details') {
-            state[obj_name + '-facet'] = 'associate';
-            state[obj_name + '-enroll'] = enroll_obj_name;
-        } else {
-            state[obj_name + '-facet'] = enroll_obj_name;
-            state[obj_name + '-enroll'] = '';
-        }
-        $.bbq.pushState(state);
-    };
-
     var ul = $('<ul></ul>', {'class': 'entity-views'});
 
     //TODO replace the plus image with the correct image for each facet
diff --git a/install/static/test/all_tests.html b/install/static/test/all_tests.html
index 7185f53..687bea1 100644
--- a/install/static/test/all_tests.html
+++ b/install/static/test/all_tests.html
@@ -10,8 +10,10 @@
     <script type="text/javascript" src="../search.js"></script>
     <script type="text/javascript" src="../add.js"></script>
     <script type="text/javascript" src="../entity.js"></script>
+    <script type="text/javascript" src="../associate.js"></script>
     <script type="text/javascript" src="ipa_tests.js"></script>
     <script type="text/javascript" src="entity_tests.js"></script>
+    <script type="text/javascript" src="association_tests.js"></script>
 </head>
 <body>
     <h1 id="qunit-header">Complete Test Suite</h1>
diff --git a/install/static/test/association_tests.html b/install/static/test/association_tests.html
new file mode 100644
index 0000000..5b5fb71
--- /dev/null
+++ b/install/static/test/association_tests.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Association Test Suite</title>
+    <link rel="stylesheet" href="qunit.css" type="text/css" media="screen">
+    <script type="text/javascript" src="qunit.js"></script>
+    <script type="text/javascript" src="../jquery.js"></script>
+    <script type="text/javascript" src="../ipa.js"></script>
+    <script type="text/javascript" src="../details.js"></script>
+    <script type="text/javascript" src="../search.js"></script>
+    <script type="text/javascript" src="../add.js"></script>
+    <script type="text/javascript" src="../associate.js"></script>
+    <script type="text/javascript" src="association_tests.js"></script>
+</head>
+<body>
+    <h1 id="qunit-header">Association Test Suite</h1>
+    <h2 id="qunit-banner"></h2>
+    <div id="qunit-testrunner-toolbar"></div>
+    <h2 id="qunit-userAgent"></h2>
+    <ol id="qunit-tests"></ol>
+    <div id="qunit-fixture">test markup</div>
+</body>
+</html>
diff --git a/install/static/test/association_tests.js b/install/static/test/association_tests.js
new file mode 100644
index 0000000..61cc3a0
--- /dev/null
+++ b/install/static/test/association_tests.js
@@ -0,0 +1,124 @@
+/*  Authors:
+ *    Endi Sukma Dewata <edew...@redhat.com>
+ *
+ * Copyright (C) 2010 Red Hat
+ * see file 'COPYING' for use and warranty information
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2 only
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+test("Testing SerialAssociator().", function() {
+
+    expect(10);
+
+    var orig_ipa_cmd = ipa_cmd;
+
+    var counter = 0;
+
+    var form = {
+        method: "add_member",
+        pkey: "test",
+        oneObj: "user",
+        manyObj: "group"
+    };
+
+    var manyObjPkeys = ['user1', 'user2', 'user3'];
+
+    ipa_cmd = function(name, args, options, win_callback, fail_callback, objname) {
+        counter++;
+
+        equals(
+            name, form.method,
+            "Checking ipa_cmd() parameter: method"
+        );
+
+        equals(
+            objname, form.manyObj,
+            "Checking ipa_cmd() parameter: object name"
+        );
+
+        equals(
+            args[0], "user"+counter,
+            "Checking ipa_cmd() parameter: primary key"
+        );
+
+        var response = {};
+        win_callback(response);
+        return 0;
+    };
+
+    var on_success = function() {
+        ok(true, "on_success() is invoked.");
+    };
+
+    var associator = new SerialAssociator(form, manyObjPkeys, on_success);
+    associator.associateNext();
+
+    ipa_cmd = orig_ipa_cmd;
+});
+
+test("Testing BulkAssociator().", function() {
+
+    expect(5);
+
+    var orig_ipa_cmd = ipa_cmd;
+
+    var counter = 0;
+
+    var form = {
+        method: "add_member",
+        pkey: "test",
+        oneObj: "user",
+        manyObj: "group"
+    };
+
+    var manyObjPkeys = ['user1', 'user2', 'user3'];
+
+    ipa_cmd = function(name, args, options, win_callback, fail_callback, objname) {
+        counter++;
+
+        equals(
+            name, form.method,
+            "Checking ipa_cmd() parameter: method"
+        );
+
+        equals(
+            objname, form.oneObj,
+            "Checking ipa_cmd() parameter: object name"
+        );
+
+        equals(
+            args[0], form.pkey,
+            "Checking ipa_cmd() parameter: primary key"
+        );
+
+        equals(
+            options[form.manyObj], "user1,user2,user3",
+            "Checking ipa_cmd() parameter: options[\""+form.manyObj+"\"]"
+        );
+
+        var response = {};
+        win_callback(response);
+        return 0;
+    };
+
+    var on_success = function() {
+        ok(true, "on_success() is invoked.");
+    };
+
+    var associator = new BulkAssociator(form, manyObjPkeys, on_success);
+    associator.associateNext();
+
+    ipa_cmd = orig_ipa_cmd;
+});
diff --git a/install/static/test/entity_tests.js b/install/static/test/entity_tests.js
index 297928b..78fa2d4 100644
--- a/install/static/test/entity_tests.js
+++ b/install/static/test/entity_tests.js
@@ -78,7 +78,13 @@ test("Testing ipa_entity_generate_views().", function() {
     );
 
     var container = $("<div/>");
-    ipa_entity_generate_views("user", container);
+
+    var counter = 0;
+    var callback = function() {
+        counter++;
+    };
+
+    ipa_entity_generate_views("user", container, callback);
 
     var list = container.children();
     var facets = list.children();
@@ -92,16 +98,20 @@ test("Testing ipa_entity_generate_views().", function() {
 
     equals(
         search.attr("title"), "search",
-        "Checking the first facet"
+        "Checking the search facet"
     )
 
+    search.click();
+
     var details = search.next();
 
     equals(
         details.attr("title"), "details",
-        "Checking the second facet"
+        "Checking the details facet"
     )
 
+    details.click();
+
     var facet = details.next();
     var attribute_members = ipa_objs["user"].attribute_members;
     for (attribute_member in attribute_members) {
@@ -111,10 +121,17 @@ test("Testing ipa_entity_generate_views().", function() {
 
             equals(
                 facet.attr("title"), object,
-                "Checking the next facet"
+                "Checking the "+object+" facet"
             );
 
+            facet.click();
+
             facet = facet.next();
         }
     }
+
+    equals(
+        counter, 6,
+        "Checking callback invocations"
+    );
 });
diff --git a/install/static/test/index.html b/install/static/test/index.html
index c6d2295..14ca7f0 100644
--- a/install/static/test/index.html
+++ b/install/static/test/index.html
@@ -26,6 +26,7 @@
         <ul>
         <li><a href="ipa_tests.html">Core Test Suite</a>
         <li><a href="entity_tests.html">Entity Test Suite</a>
+        <li><a href="association_tests.html">Association Test Suite</a>
         </ul>
     </div>
 
-- 
1.7.2.3

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to