|
17 | 17 | * under the License.
|
18 | 18 | */
|
19 | 19 |
|
20 |
| -import * as http from 'http' |
| 20 | +import FakeTimers from '@sinonjs/fake-timers' |
21 | 21 | import { createReadStream } from 'fs'
|
| 22 | +import * as http from 'http' |
22 | 23 | import { join } from 'path'
|
23 | 24 | import split from 'split2'
|
24 |
| -import FakeTimers from '@sinonjs/fake-timers' |
25 | 25 | import { test } from 'tap'
|
26 | 26 | import { Client, errors } from '../../../'
|
27 | 27 | import { buildServer, connection } from '../../utils'
|
@@ -785,6 +785,59 @@ test('bulk index', t => {
|
785 | 785 | t.end()
|
786 | 786 | })
|
787 | 787 |
|
| 788 | + t.test('Should use payload returned by `onDocument`', async t => { |
| 789 | + let count = 0 |
| 790 | + const updatedAt = '1970-01-01T12:00:00.000Z' |
| 791 | + const MockConnection = connection.buildMockConnection({ |
| 792 | + onRequest (params) { |
| 793 | + t.equal(params.path, '/_bulk') |
| 794 | + t.match(params.headers, { |
| 795 | + 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', |
| 796 | + 'x-elastic-client-meta': `es=${clientVersion},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion},h=bp` |
| 797 | + }) |
| 798 | + // @ts-expect-error |
| 799 | + const [action, payload] = params.body.split('\n') |
| 800 | + t.same(JSON.parse(action), { index: { _index: 'test' } }) |
| 801 | + t.same(JSON.parse(payload), { ...dataset[count++], updatedAt }) |
| 802 | + return { body: { errors: false, items: [{}] } } |
| 803 | + } |
| 804 | + }) |
| 805 | + |
| 806 | + const client = new Client({ |
| 807 | + node: 'http://localhost:9200', |
| 808 | + Connection: MockConnection |
| 809 | + }) |
| 810 | + const result = await client.helpers.bulk<Document>({ |
| 811 | + datasource: dataset.slice(), |
| 812 | + flushBytes: 1, |
| 813 | + concurrency: 1, |
| 814 | + onDocument (doc) { |
| 815 | + t.type(doc.user, 'string') // testing that doc is type of Document |
| 816 | + return [ |
| 817 | + { |
| 818 | + index: { |
| 819 | + _index: 'test' |
| 820 | + } |
| 821 | + }, |
| 822 | + { ...doc, updatedAt } |
| 823 | + ] |
| 824 | + }, |
| 825 | + onDrop (doc) { |
| 826 | + t.fail('This should never be called') |
| 827 | + } |
| 828 | + }) |
| 829 | + |
| 830 | + t.type(result.time, 'number') |
| 831 | + t.type(result.bytes, 'number') |
| 832 | + t.match(result, { |
| 833 | + total: 3, |
| 834 | + successful: 3, |
| 835 | + retry: 0, |
| 836 | + failed: 0, |
| 837 | + aborted: false |
| 838 | + }) |
| 839 | + }) |
| 840 | + |
788 | 841 | t.end()
|
789 | 842 | })
|
790 | 843 |
|
@@ -835,6 +888,58 @@ test('bulk create', t => {
|
835 | 888 | aborted: false
|
836 | 889 | })
|
837 | 890 | })
|
| 891 | + |
| 892 | + t.test('Should use payload returned by `onDocument`', async t => { |
| 893 | + let count = 0 |
| 894 | + const updatedAt = '1970-01-01T12:00:00.000Z' |
| 895 | + const MockConnection = connection.buildMockConnection({ |
| 896 | + onRequest (params) { |
| 897 | + t.equal(params.path, '/_bulk') |
| 898 | + t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' }) |
| 899 | + // @ts-expect-error |
| 900 | + const [action, payload] = params.body.split('\n') |
| 901 | + t.same(JSON.parse(action), { create: { _index: 'test', _id: count } }) |
| 902 | + t.same(JSON.parse(payload), { ...dataset[count++], updatedAt }) |
| 903 | + return { body: { errors: false, items: [{}] } } |
| 904 | + } |
| 905 | + }) |
| 906 | + |
| 907 | + const client = new Client({ |
| 908 | + node: 'http://localhost:9200', |
| 909 | + Connection: MockConnection |
| 910 | + }) |
| 911 | + let id = 0 |
| 912 | + const result = await client.helpers.bulk({ |
| 913 | + datasource: dataset.slice(), |
| 914 | + flushBytes: 1, |
| 915 | + concurrency: 1, |
| 916 | + onDocument (doc) { |
| 917 | + return [ |
| 918 | + { |
| 919 | + create: { |
| 920 | + _index: 'test', |
| 921 | + _id: String(id++) |
| 922 | + } |
| 923 | + }, |
| 924 | + { ...doc, updatedAt } |
| 925 | + ] |
| 926 | + }, |
| 927 | + onDrop (doc) { |
| 928 | + t.fail('This should never be called') |
| 929 | + } |
| 930 | + }) |
| 931 | + |
| 932 | + t.type(result.time, 'number') |
| 933 | + t.type(result.bytes, 'number') |
| 934 | + t.match(result, { |
| 935 | + total: 3, |
| 936 | + successful: 3, |
| 937 | + retry: 0, |
| 938 | + failed: 0, |
| 939 | + aborted: false |
| 940 | + }) |
| 941 | + }) |
| 942 | + |
838 | 943 | t.end()
|
839 | 944 | })
|
840 | 945 |
|
|
0 commit comments