|
| 1 | +import * as mocha from "mocha"; |
| 2 | +import * as BluebirdPromise from "bluebird"; |
| 3 | +import * as assert from "assert"; |
| 4 | +import { testContext, disposeTestDocumentStore } from "../Utils/TestUtil"; |
| 5 | + |
| 6 | +import DocumentStore, { |
| 7 | + RavenErrorType, |
| 8 | + GetNextOperationIdCommand, |
| 9 | + IDocumentStore, |
| 10 | + TypesAwareObjectMapper, |
| 11 | + DocumentConventions, |
| 12 | + PatchOperation, |
| 13 | +} from "../../src"; |
| 14 | +import { DateUtil } from "../../src/Utility/DateUtil"; |
| 15 | +import { PatchRequest } from "../../src/Documents/Operations/PatchRequest"; |
| 16 | + |
| 17 | +describe("RDBC-233", function () { |
| 18 | + |
| 19 | + let mapper: TypesAwareObjectMapper; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + mapper = new TypesAwareObjectMapper({ |
| 23 | + documentConventions: DocumentConventions.defaultConventions |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it("object mapper does not revive types if there's null midway", async () => { |
| 28 | + const obj = { |
| 29 | + date: null, |
| 30 | + anotherOne: { |
| 31 | + start: "2018-01-02", |
| 32 | + end: "2018-01-03" |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + const result: any = mapper.fromObjectLiteral(obj, { |
| 37 | + nestedTypes: { |
| 38 | + "date.start": "date", |
| 39 | + "date.end": "date", |
| 40 | + "anotherOne.start": "date", |
| 41 | + "anotherOne.end": "date", |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + assert.ok(result); |
| 46 | + assert.strictEqual(result.date, null); |
| 47 | + assert.ok(result.anotherOne.start instanceof Date); |
| 48 | + assert.strictEqual(result.anotherOne.start.toString(), DateUtil.parse(obj.anotherOne.start).toString()); |
| 49 | + assert.strictEqual(result.anotherOne.end.toString(), DateUtil.parse(obj.anotherOne.end).toString()); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("entire flow - store, patch, load", async () => { |
| 53 | + |
| 54 | + let store: IDocumentStore; |
| 55 | + |
| 56 | + beforeEach(async function () { |
| 57 | + store = await testContext.getDocumentStore(); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(async () => disposeTestDocumentStore(store)); |
| 61 | + |
| 62 | + it("does not revive types if there's null midway", async () => { |
| 63 | + const docStore = new DocumentStore(store.urls, store.database); |
| 64 | + docStore.conventions.findCollectionNameForObjectLiteral = () => "test"; |
| 65 | + docStore.initialize(); |
| 66 | + |
| 67 | + try { |
| 68 | + { |
| 69 | + const doc = { |
| 70 | + date: { |
| 71 | + start: new Date(), |
| 72 | + end: new Date() |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + const session = docStore.openSession(); |
| 77 | + await session.store(doc, "test/1"); |
| 78 | + await session.saveChanges(); |
| 79 | + } |
| 80 | + |
| 81 | + { |
| 82 | + const patchOp = new PatchOperation( |
| 83 | + "test/1", null, PatchRequest.forScript("this.date = null;")); |
| 84 | + await docStore.operations.send(patchOp); |
| 85 | + } |
| 86 | + |
| 87 | + { |
| 88 | + const session = docStore.openSession(); |
| 89 | + const doc: any = await session.load("test/1"); |
| 90 | + assert.ok(doc); |
| 91 | + assert.ok("date" in doc); |
| 92 | + assert.strictEqual(doc["@metadata"]["@nested-object-types"]["date.start"], "date"); |
| 93 | + assert.strictEqual(doc.date, null); |
| 94 | + } |
| 95 | + } finally { |
| 96 | + docStore.dispose(); |
| 97 | + } |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments