|
| 1 | +import { spy } from 'sinon'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import * as BSON from '../../../src/bson'; |
| 4 | + |
| 5 | +const deserializeSpy = spy(BSON, 'deserialize'); |
| 6 | + |
| 7 | +const EXPECTED_VALIDATION_DISABLED_ARGUMENT = { |
| 8 | + utf8: false |
| 9 | +}; |
| 10 | + |
| 11 | +const EXPECTED_VALIDATION_ENABLED_ARGUMENT = { |
| 12 | + utf8: { |
| 13 | + writeErrors: false |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +describe('class BinMsg', () => { |
| 18 | + beforeEach(() => { |
| 19 | + deserializeSpy.resetHistory(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('enableUtf8Validation option set to false', () => { |
| 23 | + let client; |
| 24 | + const option = { enableUtf8Validation: false }; |
| 25 | + |
| 26 | + for (const passOptionTo of ['client', 'db', 'collection', 'operation']) { |
| 27 | + it(`should disable validation with option passed to ${passOptionTo}`, async function () { |
| 28 | + try { |
| 29 | + client = this.configuration.newClient(passOptionTo === 'client' ? option : undefined); |
| 30 | + await client.connect(); |
| 31 | + |
| 32 | + const db = client.db( |
| 33 | + 'bson_utf8Validation_db', |
| 34 | + passOptionTo === 'db' ? option : undefined |
| 35 | + ); |
| 36 | + const collection = db.collection( |
| 37 | + 'bson_utf8Validation_coll', |
| 38 | + passOptionTo === 'collection' ? option : undefined |
| 39 | + ); |
| 40 | + |
| 41 | + await collection.insertOne( |
| 42 | + { name: 'John Doe' }, |
| 43 | + passOptionTo === 'operation' ? option : {} |
| 44 | + ); |
| 45 | + |
| 46 | + expect(deserializeSpy.called).to.be.true; |
| 47 | + const validationArgument = deserializeSpy.lastCall.lastArg.validation; |
| 48 | + expect(validationArgument).to.deep.equal(EXPECTED_VALIDATION_DISABLED_ARGUMENT); |
| 49 | + } finally { |
| 50 | + await client.close(); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + describe('enableUtf8Validation option set to true', () => { |
| 57 | + // define client and option for tests to use |
| 58 | + let client; |
| 59 | + const option = { enableUtf8Validation: true }; |
| 60 | + for (const passOptionTo of ['client', 'db', 'collection', 'operation']) { |
| 61 | + it(`should enable validation with option passed to ${passOptionTo}`, async function () { |
| 62 | + try { |
| 63 | + client = this.configuration.newClient(passOptionTo === 'client' ? option : undefined); |
| 64 | + await client.connect(); |
| 65 | + |
| 66 | + const db = client.db( |
| 67 | + 'bson_utf8Validation_db', |
| 68 | + passOptionTo === 'db' ? option : undefined |
| 69 | + ); |
| 70 | + const collection = db.collection( |
| 71 | + 'bson_utf8Validation_coll', |
| 72 | + passOptionTo === 'collection' ? option : undefined |
| 73 | + ); |
| 74 | + |
| 75 | + await collection.insertOne( |
| 76 | + { name: 'John Doe' }, |
| 77 | + passOptionTo === 'operation' ? option : {} |
| 78 | + ); |
| 79 | + |
| 80 | + expect(deserializeSpy.called).to.be.true; |
| 81 | + const validationArgument = deserializeSpy.lastCall.lastArg.validation; |
| 82 | + expect(validationArgument).to.deep.equal(EXPECTED_VALIDATION_ENABLED_ARGUMENT); |
| 83 | + } finally { |
| 84 | + await client.close(); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + describe('enableUtf8Validation option not set', () => { |
| 91 | + let client; |
| 92 | + const option = { enableUtf8Validation: true }; |
| 93 | + for (const passOptionTo of ['client', 'db', 'collection', 'operation']) { |
| 94 | + it(`should default to enabled with option passed to ${passOptionTo}`, async function () { |
| 95 | + try { |
| 96 | + client = this.configuration.newClient(passOptionTo === 'client' ? option : undefined); |
| 97 | + await client.connect(); |
| 98 | + |
| 99 | + const db = client.db( |
| 100 | + 'bson_utf8Validation_db', |
| 101 | + passOptionTo === 'db' ? option : undefined |
| 102 | + ); |
| 103 | + const collection = db.collection( |
| 104 | + 'bson_utf8Validation_coll', |
| 105 | + passOptionTo === 'collection' ? option : undefined |
| 106 | + ); |
| 107 | + |
| 108 | + await collection.insertOne( |
| 109 | + { name: 'John Doe' }, |
| 110 | + passOptionTo === 'operation' ? option : {} |
| 111 | + ); |
| 112 | + |
| 113 | + expect(deserializeSpy.called).to.be.true; |
| 114 | + const validationArgument = deserializeSpy.lastCall.lastArg.validation; |
| 115 | + expect(validationArgument).to.deep.equal(EXPECTED_VALIDATION_ENABLED_ARGUMENT); |
| 116 | + } finally { |
| 117 | + await client.close(); |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + }); |
| 122 | +}); |
0 commit comments