|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { satisfies } from 'semver'; |
| 3 | + |
| 4 | +import type { MongoClient } from '../../../src'; |
| 5 | +import { assert as test, setupDatabase } from '../shared'; |
| 6 | + |
| 7 | +describe('Unicode', function () { |
| 8 | + let client: MongoClient; |
| 9 | + |
| 10 | + before(function () { |
| 11 | + return setupDatabase(this.configuration); |
| 12 | + }); |
| 13 | + |
| 14 | + beforeEach(async function () { |
| 15 | + client = this.configuration.newClient(); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(async function () { |
| 19 | + await client?.close(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should correctly insert unicode containing document', async function () { |
| 23 | + if (satisfies(process.versions.node, '22.7.0')) { |
| 24 | + this.skipReason = 'Node.js 22.7.0 has a UTF-8 encoding bug'; |
| 25 | + this.skip(); |
| 26 | + } |
| 27 | + |
| 28 | + const configuration = this.configuration; |
| 29 | + const db = client.db(configuration.db); |
| 30 | + const doc = { |
| 31 | + statuses_count: 1687, |
| 32 | + created_at: 'Mon Oct 22 14:55:08 +0000 2007', |
| 33 | + description: 'NodeJS hacker, Cofounder of Debuggable, CakePHP core alumnus', |
| 34 | + favourites_count: 6, |
| 35 | + profile_sidebar_fill_color: 'EADEAA', |
| 36 | + screen_name: 'felixge', |
| 37 | + status: { |
| 38 | + created_at: 'Fri Mar 12 08:59:44 +0000 2010', |
| 39 | + in_reply_to_screen_name: null, |
| 40 | + truncated: false, |
| 41 | + in_reply_to_user_id: null, |
| 42 | + source: '<a href="http://www.atebits.com/" rel="nofollow">Tweetie</a>', |
| 43 | + favorited: false, |
| 44 | + in_reply_to_status_id: null, |
| 45 | + id: 10364119169, |
| 46 | + text: '#berlin #snow = #fail : (' |
| 47 | + }, |
| 48 | + contributors_enabled: false, |
| 49 | + following: null, |
| 50 | + geo_enabled: false, |
| 51 | + time_zone: 'Eastern Time (US & Canada)', |
| 52 | + profile_sidebar_border_color: 'D9B17E', |
| 53 | + url: 'http://debuggable.com', |
| 54 | + verified: false, |
| 55 | + location: 'Berlin', |
| 56 | + profile_text_color: '333333', |
| 57 | + notifications: null, |
| 58 | + profile_background_image_url: 'http://s.twimg.com/a/1268354287/images/themes/theme8/bg.gif', |
| 59 | + protected: false, |
| 60 | + profile_link_color: '9D582E', |
| 61 | + followers_count: 840, |
| 62 | + name: 'Felix Geisend\u00f6rfer', |
| 63 | + profile_background_tile: false, |
| 64 | + id: 9599342, |
| 65 | + lang: 'en', |
| 66 | + utc_offset: -18000, |
| 67 | + friends_count: 450, |
| 68 | + profile_background_color: '8B542B', |
| 69 | + profile_image_url: 'http://a3.twimg.com/profile_images/107142257/passbild-square_normal.jpg' |
| 70 | + }; |
| 71 | + |
| 72 | + const collection = await db.createCollection( |
| 73 | + 'test_should_correctly_insert_unicode_containing_document' |
| 74 | + ); |
| 75 | + doc['_id'] = 'felixge'; |
| 76 | + |
| 77 | + await collection.insertOne(doc, { writeConcern: { w: 1 } }); |
| 78 | + const document = await collection.findOne(); |
| 79 | + test.equal('felixge', document._id); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should correctly insert unicode characters', async function () { |
| 83 | + const db = client.db(this.configuration.db); |
| 84 | + const collection = await db.createCollection('unicode_test_collection'); |
| 85 | + const test_strings = ['ouooueauiOUOOUEAUI', 'öüóőúéáűíÖÜÓŐÚÉÁŰÍ', '本荘由利地域に洪水警報']; |
| 86 | + await collection.insertOne({ id: 0, text: test_strings[0] }, { writeConcern: { w: 1 } }); |
| 87 | + await collection.insertOne({ id: 1, text: test_strings[1] }, { writeConcern: { w: 1 } }); |
| 88 | + |
| 89 | + const documents = await collection.find().toArray(); |
| 90 | + expect(documents[0]).property('text').to.equal(test_strings[documents[0].id]); |
| 91 | + expect(documents[1]).property('text').to.equal(test_strings[documents[1].id]); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should create object with Chinese object name', async function () { |
| 95 | + const object = { 客家话: 'Hello' }; |
| 96 | + |
| 97 | + const configuration = this.configuration; |
| 98 | + const db = client.db(configuration.db); |
| 99 | + await db.createCollection('create_object_with_chinese_object_name'); |
| 100 | + const collection = db.collection('create_object_with_chinese_object_name'); |
| 101 | + await collection.insertOne(object, { writeConcern: { w: 1 } }); |
| 102 | + const item = await collection.findOne(); |
| 103 | + test.equal(object['客家话'], item['客家话']); |
| 104 | + |
| 105 | + const items = await collection.find().toArray(); |
| 106 | + test.equal(object['客家话'], items[0]['客家话']); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should correctly handle UTF8 key names', async function () { |
| 110 | + const configuration = this.configuration; |
| 111 | + const db = client.db(configuration.db); |
| 112 | + const collection = await db.createCollection('test_utf8_key_name'); |
| 113 | + await collection.insertOne({ šđžčćŠĐŽČĆ: 1 }, { writeConcern: { w: 1 } }); |
| 114 | + const items = await collection.find({}).project({ šđžčćŠĐŽČĆ: 1 }).toArray(); |
| 115 | + test.equal(1, items[0]['šđžčćŠĐŽČĆ']); |
| 116 | + }); |
| 117 | +}); |
0 commit comments