Skip to content

Commit

Permalink
Breeze Lab getEntityGraph type compatibility check now understands in…
Browse files Browse the repository at this point in the history
…heritance

DocCode getEntityGraphTests added another inheritance test to cover another code path and test inherited type compatibility
  • Loading branch information
wardbell committed Mar 11, 2014
1 parent 39cd4eb commit 1701912
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
43 changes: 36 additions & 7 deletions Breeze.Client/Scripts/Labs/breeze.getEntityGraph.js
Expand Up @@ -107,6 +107,7 @@
}

function getRootInfo() {
var compatTypes;
roots.forEach(function (root, ix) {
var aspect;
var getRootErr = function (msg) {
Expand All @@ -119,13 +120,6 @@
if (aspect.entityState === breeze.EntityState.Detached) {
throw getRootErr('is a detached entity');
}
if (rootType) {
if (rootType !== root.entityType) {
throw getRootErr("has a different 'EntityType' than other roots");
}
} else {
rootType = root.entityType;
}

var em = aspect.entityManager;
if (entityGroupMap) {
Expand All @@ -135,6 +129,41 @@
} else {
entityGroupMap = em._entityGroupMap;
}
// Type compatibility check
var thisType = root.entityType;
if (rootType) {
if (rootType !== thisType) {
// Look for closest common base type
var baseType = rootType;
do { // does thisType derive from current rootType?
compatTypes = compatTypes || baseType.getSelfAndSubtypes();
if (compatTypes.indexOf(thisType) > -1) {
rootType = baseType;
break;
}
baseType = baseType.baseEntityType;
compatTypes = null;
} while (baseType);

if (!baseType) { // does current rootType derives from thisType?
baseType = thisType;
do {
compatTypes = baseType.getSelfAndSubtypes();
if (compatTypes.indexOf(rootType) > -1) {
rootType = baseType;
break;
}
baseType = baseType.baseEntityType;
} while (baseType)
}
if (!baseType) {
throw getRootErr("is not EntityType-compatible with other roots");
}
}
} else {
rootType = thisType;
}

});
}

Expand Down
15 changes: 14 additions & 1 deletion Samples/DocCode/DocCode/tests/getEntityGraphTests.js
Expand Up @@ -186,7 +186,7 @@
test("second customer returns its mix of orders (regular and international), \
with expand='" + custExpand + "'", 6, function () {
customerExpandTest(customers[0]);
});
});

function customerExpandTest (cust) {
// setup
Expand Down Expand Up @@ -215,6 +215,15 @@
assertAllInNoDups(graph, custProducts, " distinct order products");
}

// Inheritance test
test("InternationalOrder's OrderDetail returns its parent InternationalOrder", 3, function () {
var detail = internationalOrder.getProperty('OrderDetails')[0];
var graph = getEntityGraph(detail, 'Order');
assertCount(graph, 2);
assertAllInNoDups(graph, detail, "detail");
assertAllInNoDups(graph, internationalOrder, "InternationalOrder parent");
});

// works for self-referential type
test("first employee should have 2 layers of direct reports", 3, function () {
var first = employees[0];
Expand Down Expand Up @@ -396,6 +405,10 @@
ShipName: 'ShipName ' + 118,
CustomsDescription: "Look at me; I'm global!"
}, UNCHGD);
// make internationalOrder the 2nd order;
// 1st and last should be plain Orders
orders.splice(1, 0, internationalOrder);
ordIds.push(118);

// Create as many products as orders (actually need one fewer)
products = ordIds.map(function (id) {
Expand Down

0 comments on commit 1701912

Please sign in to comment.