|
| 1 | +import * as mocha from "mocha"; |
| 2 | +import * as assert from "assert"; |
| 3 | +import { User, Company, Order } from "../../Assets/Entities"; |
| 4 | +import { assertThat, assertThrows } from "../../Utils/AssertExtensions"; |
| 5 | +import { testContext, disposeTestDocumentStore } from "../../Utils/TestUtil"; |
| 6 | + |
| 7 | +import { |
| 8 | + RavenErrorType, |
| 9 | + IDocumentStore, |
| 10 | + BatchPatchCommandData, |
| 11 | + PatchRequest, |
| 12 | +} from "../../../src"; |
| 13 | + |
| 14 | +describe.only("RavenDB-12169", function () { |
| 15 | + |
| 16 | + let store: IDocumentStore; |
| 17 | + |
| 18 | + beforeEach(async function () { |
| 19 | + testContext.enableFiddler(); |
| 20 | + store = await testContext.getDocumentStore(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(async () => |
| 24 | + await disposeTestDocumentStore(store)); |
| 25 | + |
| 26 | + it("canUseBatchPatchCommand", async () => { |
| 27 | + { |
| 28 | + const session = store.openSession(); |
| 29 | + await session.store(Object.assign(new Company(), { name: "C1" }), "companies/1"); |
| 30 | + await session.store(Object.assign(new Company(), { name: "C2" }), "companies/2"); |
| 31 | + await session.store(Object.assign(new Company(), { name: "C3" }), "companies/3"); |
| 32 | + await session.store(Object.assign(new Company(), { name: "C4" }), "companies/4"); |
| 33 | + await session.saveChanges(); |
| 34 | + } |
| 35 | + |
| 36 | + { |
| 37 | + const session = store.openSession(); |
| 38 | + |
| 39 | + const c1 = await session.load<Company>("companies/1"); |
| 40 | + const c2 = await session.load<Company>("companies/2"); |
| 41 | + const c3 = await session.load<Company>("companies/3"); |
| 42 | + const c4 = await session.load<Company>("companies/4"); |
| 43 | + |
| 44 | + assertThat(c1.name) |
| 45 | + .isEqualTo("C1"); |
| 46 | + assertThat(c2.name) |
| 47 | + .isEqualTo("C2"); |
| 48 | + assertThat(c3.name) |
| 49 | + .isEqualTo("C3"); |
| 50 | + assertThat(c4.name) |
| 51 | + .isEqualTo("C4"); |
| 52 | + |
| 53 | + const ids = [ c1.id, c3.id ]; |
| 54 | + |
| 55 | + session.advanced.defer( |
| 56 | + new BatchPatchCommandData( |
| 57 | + PatchRequest.forScript("this.name = 'test'; "), null, ...ids)); |
| 58 | + |
| 59 | + session.advanced.defer( |
| 60 | + new BatchPatchCommandData( |
| 61 | + PatchRequest.forScript("this.name = 'test2'; "), null, c4.id)); |
| 62 | + |
| 63 | + await session.saveChanges(); |
| 64 | + } |
| 65 | + |
| 66 | + { |
| 67 | + |
| 68 | + const session = store.openSession(); |
| 69 | + const c1 = await session.load<Company>("companies/1"); |
| 70 | + const c2 = await session.load<Company>("companies/2"); |
| 71 | + const c3 = await session.load<Company>("companies/3"); |
| 72 | + const c4 = await session.load<Company>("companies/4"); |
| 73 | + |
| 74 | + assertThat(c1.name) |
| 75 | + .isEqualTo("test"); |
| 76 | + assertThat(c2.name) |
| 77 | + .isEqualTo("C2"); |
| 78 | + assertThat(c3.name) |
| 79 | + .isEqualTo("test"); |
| 80 | + assertThat(c4.name) |
| 81 | + .isEqualTo("test2"); |
| 82 | + } |
| 83 | + |
| 84 | + { |
| 85 | + const session = store.openSession(); |
| 86 | + const c2 = await session.load<Company>("companies/2"); |
| 87 | + |
| 88 | + session.advanced.defer( |
| 89 | + new BatchPatchCommandData( |
| 90 | + PatchRequest.forScript("this.name = 'test2'"), null, { id: c2.id, changeVector: "invalidCV" })); |
| 91 | + |
| 92 | + await assertThrows(async () => { |
| 93 | + await session.saveChanges(); |
| 94 | + }, err => assert.strictEqual(err.name, "ConcurrencyException")); |
| 95 | + } |
| 96 | + |
| 97 | + { |
| 98 | + |
| 99 | + const session = store.openSession(); |
| 100 | + const c1 = await session.load<Company>("companies/1"); |
| 101 | + const c2 = await session.load<Company>("companies/2"); |
| 102 | + const c3 = await session.load<Company>("companies/3"); |
| 103 | + const c4 = await session.load<Company>("companies/4"); |
| 104 | + assertThat(c1.name) |
| 105 | + .isEqualTo("test"); |
| 106 | + assertThat(c2.name) |
| 107 | + .isEqualTo("C2"); |
| 108 | + assertThat(c3.name) |
| 109 | + .isEqualTo("test"); |
| 110 | + assertThat(c4.name) |
| 111 | + .isEqualTo("test2"); |
| 112 | + } |
| 113 | + }); |
| 114 | +}); |
0 commit comments