-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: non-streaming search api with pagination (#92)
- Loading branch information
1 parent
cdbea48
commit 2863487
Showing
9 changed files
with
605 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import {Server, ServerCredentials} from "@grpc/grpc-js"; | ||
import TestService, {TestTigrisService} from "../test-service"; | ||
import {TigrisService} from "../../proto/server/v1/api_grpc_pb"; | ||
import {IBook} from "../tigris.rpc.spec"; | ||
import {Tigris} from "../../tigris"; | ||
import {TigrisCursorInUseError} from "../../error"; | ||
import {ObservabilityService} from "../../proto/server/v1/observability_grpc_pb"; | ||
import TestObservabilityService from "../test-observability-service"; | ||
import {ReadResponse} from "../../proto/server/v1/api_pb"; | ||
import {DB} from "../../db"; | ||
|
||
describe("class FindCursor", () => { | ||
let server: Server; | ||
const SERVER_PORT = 5003; | ||
let db: DB; | ||
|
||
beforeAll((done) => { | ||
server = new Server(); | ||
TestTigrisService.reset(); | ||
server.addService(TigrisService, TestService.handler.impl); | ||
server.addService(ObservabilityService, TestObservabilityService.handler.impl); | ||
server.bindAsync( | ||
"0.0.0.0:" + SERVER_PORT, | ||
// test purpose only | ||
ServerCredentials.createInsecure(), | ||
(err: Error | null) => { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
server.start(); | ||
} | ||
} | ||
); | ||
const tigris = new Tigris({serverUrl: "0.0.0.0:" + SERVER_PORT, insecureChannel: true}); | ||
db = tigris.getDatabase("db3"); | ||
done(); | ||
}); | ||
|
||
beforeEach(() => { | ||
TestTigrisService.reset(); | ||
}); | ||
|
||
afterAll((done) => { | ||
server.forceShutdown(); | ||
done(); | ||
}); | ||
|
||
it("returns iterable stream as it is", async () => { | ||
const cursor = db.getCollection<IBook>("books").findMany(); | ||
let bookCounter = 0; | ||
for await (const book of cursor) { | ||
expect(book.id).toBeDefined(); | ||
bookCounter++; | ||
} | ||
expect(bookCounter).toBeGreaterThan(0); | ||
}) | ||
|
||
it("Pipes the stream as iterable", async () => { | ||
const cursor = db.getCollection<IBook>("books").findMany(); | ||
let bookCounter = 0; | ||
for await (const book of cursor.stream()) { | ||
expect(book.id).toBeDefined(); | ||
bookCounter++; | ||
} | ||
expect(bookCounter).toBeGreaterThan(0); | ||
}) | ||
|
||
it("returns stream as an array", () => { | ||
const cursor = db.getCollection<IBook>("books").findMany(); | ||
const booksPromise = cursor.toArray(); | ||
booksPromise.then(books => expect(books.length).toBeGreaterThan(0)); | ||
|
||
return booksPromise; | ||
}) | ||
|
||
it("does not allow cursor to be re-used", () => { | ||
const cursor = db.getCollection<IBook>("books").findMany(); | ||
// cursor.stream() is a generator fn, calling next() would retrieve item from stream | ||
cursor.stream().next(); | ||
expect(() => cursor.toArray()).toThrow(TigrisCursorInUseError); | ||
}) | ||
|
||
it("allows cursor to be re-used once reset", async () => { | ||
const cursor = db.getCollection<IBook>("books").findMany(); | ||
|
||
let bookCounter = 0; | ||
for await (const book of cursor.stream()) { | ||
expect(book.id).toBeDefined(); | ||
bookCounter++; | ||
} | ||
|
||
cursor.reset() | ||
const books = await cursor.toArray(); | ||
expect(books.length).toBe(bookCounter); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.