From 98aff5cd1a8a998f8712975aefe1c6c3cdbf84f6 Mon Sep 17 00:00:00 2001 From: hns Date: Fri, 18 Sep 2009 19:09:52 +0000 Subject: [PATCH] More tests and some fixes --- test/code/Organisation/type.properties | 4 +- test/code/app.properties | 2 + test/tests/HopObjectBasicMapping.js | 14 ++++++ test/tests/HopObjectCollection.js | 59 +++++++++++++++++++++++--- test/tests/HopObjectGeneric.js | 48 ++++++++++++++++++++- 5 files changed, 118 insertions(+), 9 deletions(-) diff --git a/test/code/Organisation/type.properties b/test/code/Organisation/type.properties index 5110e567..e5bbfbf2 100644 --- a/test/code/Organisation/type.properties +++ b/test/code/Organisation/type.properties @@ -24,13 +24,15 @@ generic.local.1 = $prototype generic.foreign.1 = person_generic_prototype generic.local.2 = $id generic.foreign.2 = person_generic_id +generic.order = person_name groupedGeneric = collection(Person) groupedGeneric.local.1 = $prototype groupedGeneric.foreign.1 = person_generic_prototype groupedGeneric.local.2 = $id groupedGeneric.foreign.2 = person_generic_id -groupredGeneric.group = person_name +groupedGeneric.group = person_name +groupedGeneric.group.order = person_name name = org_name country = org_country diff --git a/test/code/app.properties b/test/code/app.properties index adf335b9..1d18bda0 100755 --- a/test/code/app.properties +++ b/test/code/app.properties @@ -1 +1,3 @@ baseUri = http://localhost:8080/test/ +# hard-code cache size to default value to make sure there's some cache rotation +cacheSize = 500 diff --git a/test/tests/HopObjectBasicMapping.js b/test/tests/HopObjectBasicMapping.js index d5c3c320..8f863acb 100644 --- a/test/tests/HopObjectBasicMapping.js +++ b/test/tests/HopObjectBasicMapping.js @@ -1,4 +1,5 @@ tests = [ + "testEquality", "testSimpleMapping", "testSimpleCollection", "testObjectReference", @@ -8,6 +9,19 @@ tests = [ function setup() { } +function testEquality() { + var person = new Person(); + root.persons.add(person); + res.commit(); + var id = person._id; + app.clearCache(); + var person2 = root.persons.get(id); + assertNotNull(person2); + assertTrue(person !== person2); + assertTrue(person._id === person2._id); + assertTrue(person == person2); +} + function testSimpleMapping() { var data = { diff --git a/test/tests/HopObjectCollection.js b/test/tests/HopObjectCollection.js index ec063757..60ba4737 100644 --- a/test/tests/HopObjectCollection.js +++ b/test/tests/HopObjectCollection.js @@ -1,8 +1,12 @@ tests = [ "testSize", "testMaxSize", - "testAddRemoveSmall", - "testAddRemoveLarge", + "testAddSmall", + "testAddLarge", + "testRemoveSmall", + "testRemoveLarge", + "testUpdateSmall", + "testUpdateLarge", "testListSmall", "testListLarge", "testOrderLarge", @@ -33,15 +37,56 @@ function testMaxSize() { assertEqual(150, ikea.persons.indexOf(person)); } -function testAddRemoveSmall(org) { - testAddRemove(helma, small); +function testAddSmall() { + testAdd(helma, small); } -function testAddRemoveLarge(org) { - testAddRemove(ikea, large); +function testAddLarge() { + testAdd(ikea, large); } -function testAddRemove(org, size) { +// test directly adding to a collection +function testAdd(org, size) { + var person = new Person(); + person.name = "TestPerson"; + org.persons.add(person); + assertEqual(org.persons.size(), size + 1); + assertEqual(org.persons.indexOf(person), size); + assertEqual(org.persons.contains(person), size); + assertEqual(person.href(), org.persons.href() + "TestPerson/"); + // make sure the add has set the back-reference on the person object. + // note that === comparison will return false if the + // collection size exceeds the cache size. + assertTrue(person.organisation == org); +} + +function testRemoveSmall() { + testRemove(helma, small); +} + +function testRemoveLarge() { + testRemove(ikea, large); +} + +// test directly removing from a collection +function testRemove(org, size) { + var person = org.persons.get(org.persons.size() - 1); + person.remove(); + assertEqual(org.persons.size(), size); + assertEqual(org.persons.indexOf(person), -1); + assertEqual(org.persons.contains(person), -1); +} + +function testUpdateSmall() { + testUpdate(helma, small); +} + +function testUpdateLarge() { + testUpdate(ikea, large); +} + +// test indirectly adding to and removing form a collection +function testUpdate(org, size) { var person = new Person(); person.name = "TestPerson"; person.organisation = org; diff --git a/test/tests/HopObjectGeneric.js b/test/tests/HopObjectGeneric.js index 4cd217ce..c9929f8e 100644 --- a/test/tests/HopObjectGeneric.js +++ b/test/tests/HopObjectGeneric.js @@ -1,5 +1,9 @@ tests = [ - 'testSize' + 'testSize', + 'testContent', + 'testGroupContent', + 'testRemove', + 'testAdd' ]; var org; @@ -38,6 +42,48 @@ function testSize() { assertEqual(org.generic.size(), size); } +function testContent() { + var list = org.generic.list(); + assertEqual(list.length, size); + for (var i = 0; i < list.length; i++) { + assertEqual(list[i].name, "GenericPerson " + i.format("00")); + } +} + +function testGroupContent() { + var list = org.groupedGeneric.list(); + assertEqual(list.length, size); + for (var i = 0; i < list.length; i++) { + assertEqual(list[i].groupname, "GenericPerson " + i.format("00")); + assertEqual(list[i].size(), 1); + assertEqual(list[i].get(0).name, "GenericPerson " + i.format("00")); + } +} + +function testRemove() { + var person = org.generic.get(size/2); + org.generic.removeChild(person); + assertEqual(org.generic.size(), size - 1); + res.rollback(); + // note: removeChild does not remove the node, nor does it + // unset the constraints between parent and child, so after a rollback + // the object is back in place. While this behaviour is disputable, + // until this is so we test for it. + assertEqual(org.generic.size(), size); +} + +function testAdd() { + var person = new Person(); + org.generic.add(person); + assertEqual(org.generic.size(), size + 1); + assertEqual(org.groupedGeneric.size(), size); + res.commit(); + // note: even after commit the grouped collection must not grow + // since we added a person without a name + assertEqual(org.generic.size(), size + 1); + assertEqual(org.groupedGeneric.size(), size); +} + function cleanup() { var persons = org.generic.list(); for each (var person in persons) {