Skip to content

Commit fa76929

Browse files
authored
Merge pull request #367 from Danielle9897/RDBD-712-queryCustomizationTests
RDBC-712 Tests for IDocumentQueryCustomization methods
2 parents a7718eb + 78cfb7f commit fa76929

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import {AbstractJavaScriptIndexCreationTask, IDocumentStore, IndexQuery } from "../../../src";
2+
import { disposeTestDocumentStore, testContext } from "../../Utils/TestUtil";
3+
import * as assert from "assert";
4+
5+
describe("IDocumentQueryCustomizationMethodsTest", function () {
6+
7+
class BlogPost {
8+
public id: string;
9+
public title: string;
10+
public body: string;
11+
public tags: string[];
12+
}
13+
14+
class TagResult {
15+
public tag: string;
16+
}
17+
18+
class BlogPosts_ByTag extends AbstractJavaScriptIndexCreationTask<BlogPost> {
19+
public constructor() {
20+
super();
21+
22+
this.map(BlogPost, b => {
23+
const result: TagResult[] = [];
24+
25+
b.tags.forEach(item => {
26+
result.push({
27+
tag: item
28+
});
29+
});
30+
31+
return result;
32+
})
33+
}
34+
}
35+
36+
let store: IDocumentStore;
37+
38+
beforeEach(async function () {
39+
store = await testContext.getDocumentStore();
40+
});
41+
42+
afterEach(async () =>
43+
await disposeTestDocumentStore(store));
44+
45+
it("beforeQueryExecuted", async () => {
46+
await store.executeIndex(new BlogPosts_ByTag());
47+
48+
const session = store.openSession();
49+
50+
await session.store(Object.assign(new BlogPost(),
51+
{body: "postBody", title: "postTitle", tags: ["Oren", "Danielle"]}));
52+
53+
await session.saveChanges();
54+
55+
await testContext.waitForIndexing(store);
56+
57+
const results = await session
58+
.query(BlogPost, BlogPosts_ByTag)
59+
.on("beforeQueryExecuted", query => (query as IndexQuery).skipDuplicateChecking = true)
60+
.all();
61+
62+
assert.strictEqual(results.length, 2);
63+
});
64+
65+
it("afterQueryExecuted", async () => {
66+
const session = store.openSession();
67+
68+
let counter = 0;
69+
70+
const results = await session
71+
.query<BlogPost>({ collection: "blogPosts" })
72+
.on("afterQueryExecuted", queryResult => counter++)
73+
.all();
74+
75+
assert.strictEqual(results.length, 0);
76+
assert.strictEqual(counter, 1);
77+
78+
});
79+
80+
// TBD - enable test when RDBC-713 is done
81+
it.skip("afterStreamExecuted", async () => {
82+
const session = store.openSession();
83+
84+
// prepare data
85+
for (let i = 0; i < 200; i++) {
86+
const docToStore= Object.assign(new BlogPost(),
87+
{body: "postBody_" + i, title: "postTitle_" + i, tags: ["Oren_" + i, "Danielle_" + i]});
88+
await session.store(docToStore);
89+
}
90+
91+
await session.saveChanges();
92+
93+
// define query
94+
let totalStreamedResultsSize = 0;
95+
let counter = 0;
96+
97+
const query = session
98+
.query<BlogPost>({ collection: "blogPosts" })
99+
.on("afterStreamExecuted", streamResult => {
100+
totalStreamedResultsSize += streamResult.durationInMs;
101+
counter++;
102+
});
103+
104+
// stream query
105+
const queryStream = await session.advanced.stream(query);
106+
107+
queryStream.on("end", () => {
108+
assert.strictEqual(counter, 200);
109+
});
110+
});
111+
112+
it("noCaching", async () => {
113+
const session = store.openSession();
114+
await session.store(Object.assign(new BlogPost(),
115+
{body: "postBody", title: "postTitle", tags: ["Oren", "Danielle"]}));
116+
await session.saveChanges();
117+
118+
let responseCode = 0;
119+
120+
session.advanced.requestExecutor.on("succeedRequest", event => {
121+
responseCode = event.response.status;
122+
});
123+
124+
const results1 = await session.query<BlogPost>({ collection: "blogPosts" })
125+
.all();
126+
127+
assert.strictEqual(responseCode, 200);
128+
129+
const results2 = await session.query<BlogPost>({ collection: "blogPosts" })
130+
.all();
131+
132+
assert.strictEqual(responseCode, 304); // Not modified
133+
134+
const results3 = await session.query<BlogPost>({ collection: "blogPosts" })
135+
.noCaching()
136+
.all();
137+
138+
assert.strictEqual(responseCode, 200);
139+
});
140+
141+
it("noTracking", async () => {
142+
const session1 = store.openSession();
143+
144+
await session1.store(Object.assign(new BlogPost(),
145+
{body: "postBody", title: "postTitle", tags: ["Oren", "Danielle"]}));
146+
147+
await session1.saveChanges();
148+
assert.ok(session1.advanced.isLoaded("BlogPosts/1-A"));
149+
150+
const session2 = store.openSession();
151+
152+
let posts = await session2.query(BlogPost)
153+
.noTracking()
154+
.all();
155+
156+
assert.strictEqual(posts.length, 1);
157+
assert.ok(!session2.advanced.isLoaded("BlogPosts/1-A"));
158+
159+
const session3 = store.openSession();
160+
161+
posts = await session3.query(BlogPost)
162+
.all();
163+
164+
assert.strictEqual(posts.length, 1);
165+
assert.ok(session3.advanced.isLoaded("BlogPosts/1-A"));
166+
});
167+
});
168+

0 commit comments

Comments
 (0)