Skip to content

Instantly share code, notes, and snippets.

@enesakar
Created April 20, 2022 22:59
Show Gist options
  • Save enesakar/873ced3693052bddb113f6805bbb5dc5 to your computer and use it in GitHub Desktop.
Save enesakar/873ced3693052bddb113f6805bbb5dc5 to your computer and use it in GitHub Desktop.
'use strict';
const fetch = require('node-fetch');
const assert = require('assert');
const helper = require('../helper');
var {
serverAddress,
headers
} = require('../config');
describe("The 'eval' method", function () {
beforeEach(async () => {
await helper.flushdb();
});
it('eval script', async () =>{
let key = "keyx", value = "valuex";
let response = await fetch(serverAddress + `/set/${key}/${value}`, {
headers: headers
})
assert.strictEqual(response.ok, true)
let script = "return redis.call('get', KEYS[1])"
let command = ["eval", script, 1, key]
response = await fetch(serverAddress, {
headers: headers,
method: "POST",
body: JSON.stringify(command)
})
assert.strictEqual(response.ok, true)
let data = await response.json()
assert.strictEqual(data.result, value)
});
it('eval advanced script', async () =>{
let key = "keyz", value = "valuez";
let script = "redis.call('set', KEYS[1], ARGV[1]); return redis.call('get', KEYS[1])"
let command = ["eval", script, 1, key, value]
let response = await fetch(serverAddress, {
headers: headers,
method: "POST",
body: JSON.stringify(command)
})
assert.strictEqual(response.ok, true)
let data = await response.json()
assert.strictEqual(data.result, value)
});
it('script load & evalsha ', async () =>{
let script = "redis.call('set', KEYS[1], ARGV[1]); return redis.call('get', KEYS[1])"
let response = await fetch(serverAddress, {
headers: headers,
method: "POST",
body: JSON.stringify(["script", "load", script])
})
assert.strictEqual(response.ok, true)
let data = await response.json()
const sha = data.result
let key = "keys", value = "values";
response = await fetch(serverAddress, {
headers: headers,
method: "POST",
body: JSON.stringify(["evalsha", sha, 1, key, value])
})
assert.strictEqual(response.ok, true)
data = await response.json()
assert.strictEqual(data.result, value)
});
it('script flush', async () =>{
let script = "return redis.call('get', KEYS[1])"
let response = await fetch(serverAddress, {
headers: headers,
method: "POST",
body: JSON.stringify(["script", "load", script])
})
assert.strictEqual(response.ok, true)
let data = await response.json()
const sha = data.result
let scriptExists = async () => {
let response = await fetch(serverAddress, {
headers: headers,
method: "POST",
body: JSON.stringify(["script", "exists", sha])
})
assert.strictEqual(response.ok, true)
return await response.json()
}
data = await scriptExists()
assert.strictEqual(data.result.length, 1)
assert.strictEqual(data.result[0], 1)
response = await fetch(serverAddress, {
headers: headers,
method: "POST",
body: JSON.stringify(["script", "flush"])
})
assert.strictEqual(response.ok, true)
data = await response.json()
assert.strictEqual(data.result, "OK")
data = await scriptExists()
assert.strictEqual(data.result.length, 1)
assert.strictEqual(data.result[0], 0)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment