如何在Elasticsearch中模拟统计方法?

huangapple go评论114阅读模式
英文:

How to mock stats method in elasticsearch?

问题

以下是要翻译的内容:

"我想使用Node.js查询Elasticsearch的统计信息。

这是我的源代码:

  1. const { Client } = require('@elastic/elasticsearch');
  2. client = new Client({
  3. Connection: '适当的 AWS 连接',
  4. node: '一些 URL',
  5. agent: {
  6. keepalive:,
  7. maxsocket:,
  8. maxfreesocket:
  9. }
  10. })
  11. const stats = await client.nodes.stats();
  12. console.log(stats);
  13. // 统计信息上的一些操作

我如何模拟这个Elasticsearch方法?
我尝试了以下方法,但无法模拟stats()

  1. let sandbox = sinon.createSandbox();
  2. before(() => {
  3. sandbox.stub(Client.prototype, 'nodes').get(() => {
  4. stats: () => {
  5. return dummydata;
  6. }
  7. // 其中dummydata已经硬编码
  8. })
  9. })

我在哪里出错了?"

英文:

I want to query stats of elasticsearch using nodeJs.

Here is my source code

  1. const {Client} = require('@elastic/elasticsearch');
  2. client = new Client({
  3. Connection:'appropriate aws connection',
  4. node:'some url',
  5. agent:{
  6. keepalive:,
  7. maxsocket:,
  8. maxfreesocket:
  9. }
  10. })
  11. const stats = await client.nodes.stats();
  12. console.log(stats);
  13. // some operation on stats

How do i mock this elasticsearch method?<br>
I have tried following but it is unable to mock the stats().

  1. let sandbox = sinon.createSandbox();
  2. before(()=&gt;{
  3. sandbox.stub(Client.prototype,&#39;nodes&#39;).get(()=&gt;{
  4. stats: ()=&gt;{
  5. return dymmydata;
  6. }
  7. //where dummydata is already hardcoded
  8. })
  9. })

Where am I going wrong here?

答案1

得分: 1

让我们看看nodes API是如何定义的。请查看/v8.8.1/src/api/index.ts#L401,你会发现nodes API是通过Object.defineProperties和一个获取函数来定义的。

  1. Object.defineProperties(API.prototype, {
  2. // ...
  3. nodes: {
  4. get () { return this[kNodes] === null ? (this[kNodes] = new NodesApi(this.transport)) : this[kNodes] }
  5. }
  6. // ...
  7. }

我们应该使用stub.get(getterFn) API来替换这个存根的新获取函数。

例如:

index.js

  1. const { Client } = require('@elastic/elasticsearch');
  2. const client = new Client({
  3. node: 'http://localhost:9200',
  4. });
  5. export function main() {
  6. return client.nodes.stats();
  7. }

index.test.js

  1. const sinon = require('sinon');
  2. const { Client } = require('@elastic/elasticsearch');
  3. describe('76653113', () => {
  4. it('should pass', async () => {
  5. const nodesApiMock = {
  6. stats: sinon.stub().resolves('ok'),
  7. };
  8. sinon.stub(Client.prototype, 'nodes').get(() => nodesApiMock);
  9. const { main } = require('./');
  10. const actual = await main();
  11. sinon.assert.match(actual, 'ok');
  12. sinon.assert.calledOnce(nodesApiMock.stats);
  13. });
  14. });
英文:

Let's see how the nodes API is defined. See /v8.8.1/src/api/index.ts#L401, you will find the nodes API is defined by Object.defineProperties with a getter function.

  1. Object.defineProperties(API.prototype, {
  2. // ...
  3. nodes: {
  4. get () { return this[kNodes] === null ? (this[kNodes] = new NodesApi(this.transport)) : this[kNodes] }
  5. }
  6. // ...
  7. }

We should use stub.get(getterFn) API to replace a new getter for this stub.

E.g.

index.js:

  1. const { Client } = require(&#39;@elastic/elasticsearch&#39;);
  2. const client = new Client({
  3. node: &#39;http://localhost:9200&#39;,
  4. });
  5. export function main() {
  6. return client.nodes.stats();
  7. }

index.test.js:

  1. const sinon = require(&#39;sinon&#39;);
  2. const { Client } = require(&#39;@elastic/elasticsearch&#39;);
  3. describe(&#39;76653113&#39;, () =&gt; {
  4. it(&#39;should pass&#39;, async () =&gt; {
  5. const nodesApiMock = {
  6. stats: sinon.stub().resolves(&#39;ok&#39;),
  7. };
  8. sinon.stub(Client.prototype, &#39;nodes&#39;).get(() =&gt; nodesApiMock);
  9. const { main } = require(&#39;./&#39;);
  10. const actual = await main();
  11. sinon.assert.match(actual, &#39;ok&#39;);
  12. sinon.assert.calledOnce(nodesApiMock.stats);
  13. });
  14. });

huangapple
  • 本文由 发表于 2023年7月10日 19:07:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定