|
| 1 | +import * as mocha from "mocha"; |
| 2 | +import * as assert from "assert"; |
| 3 | +import { User, Company, Order } from "../Assets/Entities"; |
| 4 | +import { assertThrows } from "../Utils/AssertExtensions"; |
| 5 | +import { testContext, disposeTestDocumentStore } from "../Utils/TestUtil"; |
| 6 | + |
| 7 | +import { |
| 8 | + RavenErrorType, |
| 9 | + IDocumentStore, |
| 10 | + IDocumentSession, |
| 11 | +} from "../../src"; |
| 12 | + |
| 13 | +describe("RDBC-247", function () { |
| 14 | + |
| 15 | + let store: IDocumentStore; |
| 16 | + |
| 17 | + beforeEach(async function () { |
| 18 | + store = await testContext.getDocumentStore(); |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(async () => |
| 22 | + await disposeTestDocumentStore(store)); |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + const session = store.openSession(); |
| 26 | + await session.store(Object.assign(new User(), { name: "John" })); |
| 27 | + await session.store(Object.assign(new User(), { name: "John2" })); |
| 28 | + await session.store(Object.assign(new User(), { name: "John3" })); |
| 29 | + await session.store(Object.assign(new User(), { name: "John4" })); |
| 30 | + await session.saveChanges(); |
| 31 | + }); |
| 32 | + |
| 33 | + let session: IDocumentSession; |
| 34 | + |
| 35 | + beforeEach(() => session = store.openSession()); |
| 36 | + |
| 37 | + describe("single()", function () { |
| 38 | + |
| 39 | + it("should throw if 0 results", async function () { |
| 40 | + await assertThrows( |
| 41 | + async () => await session.query({ collection: "users" }) |
| 42 | + .whereEquals("name", "Merry") |
| 43 | + .single(), |
| 44 | + err => { |
| 45 | + assert.strictEqual(err.name, "InvalidOperationException"); |
| 46 | + assert.strictEqual(err.message, "Expected single result, but got 0."); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should throw if more than 1 result", async function() { |
| 51 | + await assertThrows( |
| 52 | + async () => await session.query({ collection: "users" }).single(), |
| 53 | + err => { |
| 54 | + assert.strictEqual(err.name, "InvalidOperationException"); |
| 55 | + assert.strictEqual(err.message, "Expected single result, but got more than that."); |
| 56 | + }); |
| 57 | + |
| 58 | + }); |
| 59 | + |
| 60 | + it("should return exactly 1 result", async function() { |
| 61 | + const result = await session.query<User>({ collection: "users" }) |
| 62 | + .whereEquals("name", "John") |
| 63 | + .single(); |
| 64 | + |
| 65 | + assert.ok(result); |
| 66 | + assert.ok(result instanceof User); |
| 67 | + assert.strictEqual(result.name, "John"); |
| 68 | + }); |
| 69 | + |
| 70 | + }); |
| 71 | + |
| 72 | + describe("singleOrNull()", function () { |
| 73 | + |
| 74 | + it("should return null if 0 results", async function () { |
| 75 | + const result = await session.query<User>({ collection: "users" }) |
| 76 | + .whereEquals("name", "Merry") |
| 77 | + .singleOrNull(); |
| 78 | + assert.strictEqual(result, null); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should return null if more than 1 result", async function() { |
| 82 | + const result = await session.query<User>({ collection: "users" }) |
| 83 | + .singleOrNull(); |
| 84 | + assert.strictEqual(result, null); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should return exactly 1 result", async function() { |
| 88 | + const result = await session.query<User>({ collection: "users" }) |
| 89 | + .whereEquals("name", "John") |
| 90 | + .singleOrNull(); |
| 91 | + |
| 92 | + assert.ok(result); |
| 93 | + assert.ok(result instanceof User); |
| 94 | + assert.strictEqual(result.name, "John"); |
| 95 | + }); |
| 96 | + |
| 97 | + }); |
| 98 | + |
| 99 | + describe("first()", function () { |
| 100 | + |
| 101 | + it("should return first result", async function() { |
| 102 | + const result = await session.query<User>({ collection: "users" }) |
| 103 | + .whereStartsWith("name", "John") |
| 104 | + .orderBy("name") |
| 105 | + .first(); |
| 106 | + |
| 107 | + assert.ok(result); |
| 108 | + assert.ok(result instanceof User); |
| 109 | + assert.strictEqual(result.name, "John"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should throw for no results", async function() { |
| 113 | + await assertThrows( |
| 114 | + async () => await session.query({ collection: "users" }) |
| 115 | + .whereEquals("name", "Merry") |
| 116 | + .first(), |
| 117 | + err => { |
| 118 | + assert.strictEqual(err.name, "InvalidOperationException"); |
| 119 | + assert.strictEqual(err.message, "Expected at least one result."); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + }); |
| 124 | + |
| 125 | + describe("firstOrNull()", function () { |
| 126 | + |
| 127 | + it("should return first result", async function() { |
| 128 | + const result = await session.query<User>({ collection: "users" }) |
| 129 | + .whereStartsWith("name", "John") |
| 130 | + .orderBy("name") |
| 131 | + .firstOrNull(); |
| 132 | + |
| 133 | + assert.ok(result); |
| 134 | + assert.ok(result instanceof User); |
| 135 | + assert.strictEqual(result.name, "John"); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should return null for no results", async function() { |
| 139 | + const result = await session.query<User>({ collection: "users" }) |
| 140 | + .whereStartsWith("name", "Merry") |
| 141 | + .firstOrNull(); |
| 142 | + |
| 143 | + assert.strictEqual(result, null); |
| 144 | + }); |
| 145 | + |
| 146 | + }); |
| 147 | + |
| 148 | +}); |
0 commit comments