Skip to content

Commit

Permalink
Merge pull request #3 from BetimBeja/retrieve-single-property
Browse files Browse the repository at this point in the history
Implemented 'Retrieve a single property value'
  • Loading branch information
jordimontana82 committed Oct 1, 2019
2 parents 837dac3 + ceb82bb commit e5b749e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/ODataParsedUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export default class ODataParsedUrl {
queryParams: any;
id: string; //if the url had a (Guid) parameter in it
wasSingleRetrieve: boolean;
singleProperty?: string;
}
8 changes: 4 additions & 4 deletions src/ODataUrlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export default class ODataUrlParser implements IODataUrlParser {
result.queryParams = null; //Empty query
}

var entityNameWithIdRegex = /^([a-z0-9_])*\([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\)$/i;
var entityNameWithIdRegex = /^([a-z0-9_]+)\(([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\)(\/([a-z0-9_]+))?$/i;
var match = entityNameWithIdRegex.exec(result.entitySetName);

if(match && match.length > 0) {
var split = result.entitySetName.split('(');
result.entitySetName = split[0];
result.id = split[1].replace(")", "");
result.entitySetName = match[1];
result.id = match[2];
result.singleProperty = match[4];
result.wasSingleRetrieve = true;
}

Expand Down
6 changes: 4 additions & 2 deletions src/XrmFakedContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,12 @@ export class XrmFakedContext implements IXrmFakedContext
entities.push(odataEntity);
}

if(!parsedOData.wasSingleRetrieve) {
if (!parsedOData.wasSingleRetrieve) {
response.value = entities;
}
else {
else if (parsedOData.singleProperty) {
response.value = entities[0][parsedOData.singleProperty];
} else {
response = entities[0];
}

Expand Down
18 changes: 17 additions & 1 deletion test/XrmFakedContext/query_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("XrmFakedContext Queries: $select", function () {

});

test("it should return a a single record if a single entity was requested", done => {
test("it should return a single record if a single entity was requested", done => {
var existingId = Guid.create();
context.initialize([
new Entity("contact", Guid.create(), {firstname: 'Contact 1', other: "other"}),
Expand All @@ -50,6 +50,22 @@ describe("XrmFakedContext Queries: $select", function () {

});

test("it should return a single property if a single entity was requested and property specified", done => {
var existingId = Guid.create();
context.initialize([
new Entity("contact", Guid.create(), {firstname: 'Contact 1', other: "other"}),
new Entity("contact", existingId, {firstname: 'Contact 2', other: "Other2"})
]);

WebApiClient.retrieveMultiple("contacts(" + existingId.toString() + ")/firstname", function (data) {
expect(data.value).toBe("Contact 2");
expect(data.other).toBe(undefined);

done();
});

});

test("it should retrieve fields specified in $select clause only", done => {
context.initialize([
new Entity("account", Guid.create(), {name: 'Company 1', revenue: 3000, other: "somevalue"}),
Expand Down

0 comments on commit e5b749e

Please sign in to comment.