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

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

How to mock stats method in elasticsearch?

问题

以下是要翻译的内容:

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

这是我的源代码:

const { Client } = require('@elastic/elasticsearch');
client = new Client({
  Connection: '适当的 AWS 连接',
  node: '一些 URL',
  agent: {
    keepalive:,
    maxsocket:,
    maxfreesocket:
  }
})

const stats = await client.nodes.stats();

console.log(stats);

// 统计信息上的一些操作

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

let sandbox = sinon.createSandbox();
before(() => {
  sandbox.stub(Client.prototype, 'nodes').get(() => {
    stats: () => {
      return dummydata;
    }
    // 其中dummydata已经硬编码
  })
})

我在哪里出错了?"

英文:

I want to query stats of elasticsearch using nodeJs.

Here is my source code

const {Client}  = require('@elastic/elasticsearch');
client = new Client({
Connection:'appropriate aws connection',
node:'some url',
agent:{
  keepalive:,
  maxsocket:,
  maxfreesocket:
}
})

const stats = await client.nodes.stats();

console.log(stats);

// some operation on stats

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

 let sandbox = sinon.createSandbox();
 before(()=&gt;{
   sandbox.stub(Client.prototype,&#39;nodes&#39;).get(()=&gt;{
      stats: ()=&gt;{
       return dymmydata;
     }
//where dummydata is already hardcoded
   })
 })

Where am I going wrong here?

答案1

得分: 1

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

Object.defineProperties(API.prototype, {
  // ...
  nodes: {
    get () { return this[kNodes] === null ? (this[kNodes] = new NodesApi(this.transport)) : this[kNodes] }
  }
  // ...
}

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

例如:

index.js

const { Client } = require('@elastic/elasticsearch');

const client = new Client({
	node: 'http://localhost:9200',
});

export function main() {
	return client.nodes.stats();
}

index.test.js

const sinon = require('sinon');
const { Client } = require('@elastic/elasticsearch');

describe('76653113', () => {
	it('should pass', async () => {
		const nodesApiMock = {
			stats: sinon.stub().resolves('ok'),
		};
		sinon.stub(Client.prototype, 'nodes').get(() => nodesApiMock);
		const { main } = require('./');
		const actual = await main();
		sinon.assert.match(actual, 'ok');
		sinon.assert.calledOnce(nodesApiMock.stats);
	});
});
英文:

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.

Object.defineProperties(API.prototype, {
  // ...
  nodes: {
    get () { return this[kNodes] === null ? (this[kNodes] = new NodesApi(this.transport)) : this[kNodes] }
  }
  // ...
}

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

E.g.

index.js:

const { Client } = require(&#39;@elastic/elasticsearch&#39;);

const client = new Client({
	node: &#39;http://localhost:9200&#39;,
});

export function main() {
	return client.nodes.stats();
}

index.test.js:

const sinon = require(&#39;sinon&#39;);
const { Client } = require(&#39;@elastic/elasticsearch&#39;);

describe(&#39;76653113&#39;, () =&gt; {
	it(&#39;should pass&#39;, async () =&gt; {
		const nodesApiMock = {
			stats: sinon.stub().resolves(&#39;ok&#39;),
		};
		sinon.stub(Client.prototype, &#39;nodes&#39;).get(() =&gt; nodesApiMock);
		const { main } = require(&#39;./&#39;);
		const actual = await main();
		sinon.assert.match(actual, &#39;ok&#39;);
		sinon.assert.calledOnce(nodesApiMock.stats);
	});
});

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:

确定