<commit_before>/* eslint-disable global-require */

const { expect } = require('chai');
const mockery = require('mockery');
const commons = require('@schul-cloud/commons');

const { Configuration } = commons;

const redisMock = require('./redisMock');

describe('redis helpers', () => {
	describe('without a redis server', () => {
		let redisHelpers;

		before(async () => {
			mockery.enable({
				warnOnReplace: false,
				warnOnUnregistered: false,
				useCleanCache: true,
			});
			mockery.registerMock('redis', redisMock);

			delete require.cache[require.resolve('../../../src/utils/redis')];
			delete require.cache[require.resolve('@schul-cloud/commons')];
			redisHelpers = require('../../../src/utils/redis');
			redisHelpers.initializeRedisClient();
		});

		after(async () => {
			mockery.deregisterAll();
			mockery.disable();
		});

		it('no redisClient is created if there is no URL', async () => {
			expect(redisHelpers.getRedisClient()).to.equal(false);
		});

		it('redisGetAsync throws an error if no client exists', async () => {
			try {
				await redisHelpers.redisGetAsync('key');
				throw new Error('should have failed');
			} catch (err) {
				expect(err.message).to.not.equal('should have failed');
				expect(err.code).to.equal(500);
				expect(err.message).to.equal('No redis connection. Check for this via getRedisClient().');
			}
		});

		it('redisSetAsync throws an error if no client exists', async () => {
			try {
				await redisHelpers.redisSetAsync('key', 'value');
				throw new Error('should have failed');
			} catch (err) {
				expect(err.message).to.not.equal('should have failed');
				expect(err.code).to.equal(500);
				expect(err.message).to.equal('No redis connection. Check for this via getRedisClient().');
			}
		});

		it('redisDelAsync throws an error if no client exists', async () => {
			try {
				await redisHelpers.redisDelAsync('key', 'value');
				throw new Error('should have failed');
			} catch (err) {
				expect(err.message).to.not.equal('should have failed');
				expect(err.code).to.equal(500);
				expect(err.message).to.equal('No redis connection. Check for this via getRedisClient().');
			}
		});
	});

	describe('with a redis server', () => {
		let redisHelpers;
		let configBefore;

		before(async () => {
			configBefore = Configuration.toObject();
			mockery.enable({
				warnOnReplace: false,
				warnOnUnregistered: false,
				useCleanCache: true,
			});
			mockery.registerMock('redis', redisMock);
			mockery.registerMock('@schul-cloud/commons', commons);

			delete require.cache[require.resolve('../../../src/utils/redis')];
			redisHelpers = require('../../../src/utils/redis');
			Configuration.set('REDIS_URI', '//validHost:6379');
			redisHelpers.initializeRedisClient();
		});

		after(async () => {
			mockery.deregisterAll();
			mockery.disable();
			Configuration.update(configBefore);
		});

		it('getRedisClient returns a client object', () => {
			const redisClient = redisHelpers.getRedisClient();
			expect(redisClient).to.not.equal(false);
			expect(redisClient).to.have.property('set');
			expect(redisClient).to.have.property('get');
			expect(redisClient).to.have.property('del');
		});
	});
});<commit_msg>clear redis utils after test, not config <commit_after>/* eslint-disable global-require */

const { expect } = require('chai');
const mockery = require('mockery');
const commons = require('@schul-cloud/commons');

const { Configuration } = commons;

const redisMock = require('./redisMock');

describe('redis helpers', () => {
	describe('without a redis server', () => {
		let redisHelpers;

		before(async () => {
			mockery.enable({
				warnOnReplace: false,
				warnOnUnregistered: false,
				useCleanCache: true,
			});
			mockery.registerMock('redis', redisMock);
			mockery.registerMock('@schul-cloud/commons', commons);

			delete require.cache[require.resolve('../../../src/utils/redis')];
			redisHelpers = require('../../../src/utils/redis');
			redisHelpers.initializeRedisClient();
		});

		after(async () => {
			delete require.cache[require.resolve('../../../src/utils/redis')];
			mockery.deregisterAll();
			mockery.disable();
		});

		it('no redisClient is created if there is no URL', async () => {
			expect(redisHelpers.getRedisClient()).to.equal(false);
		});

		it('redisGetAsync throws an error if no client exists', async () => {
			try {
				await redisHelpers.redisGetAsync('key');
				throw new Error('should have failed');
			} catch (err) {
				expect(err.message).to.not.equal('should have failed');
				expect(err.code).to.equal(500);
				expect(err.message).to.equal('No redis connection. Check for this via getRedisClient().');
			}
		});

		it('redisSetAsync throws an error if no client exists', async () => {
			try {
				await redisHelpers.redisSetAsync('key', 'value');
				throw new Error('should have failed');
			} catch (err) {
				expect(err.message).to.not.equal('should have failed');
				expect(err.code).to.equal(500);
				expect(err.message).to.equal('No redis connection. Check for this via getRedisClient().');
			}
		});

		it('redisDelAsync throws an error if no client exists', async () => {
			try {
				await redisHelpers.redisDelAsync('key', 'value');
				throw new Error('should have failed');
			} catch (err) {
				expect(err.message).to.not.equal('should have failed');
				expect(err.code).to.equal(500);
				expect(err.message).to.equal('No redis connection. Check for this via getRedisClient().');
			}
		});
	});

	describe('with a redis server', () => {
		let redisHelpers;
		let configBefore;

		before(async () => {
			configBefore = Configuration.toObject();
			mockery.enable({
				warnOnReplace: false,
				warnOnUnregistered: false,
				useCleanCache: true,
			});
			mockery.registerMock('redis', redisMock);
			mockery.registerMock('@schul-cloud/commons', commons);

			delete require.cache[require.resolve('../../../src/utils/redis')];
			redisHelpers = require('../../../src/utils/redis');
			Configuration.set('REDIS_URI', '//validHost:6379');
			redisHelpers.initializeRedisClient();
		});

		after(async () => {
			mockery.deregisterAll();
			mockery.disable();
			delete require.cache[require.resolve('../../../src/utils/redis')];
			Configuration.update(configBefore);
		});

		it('getRedisClient returns a client object', () => {
			const redisClient = redisHelpers.getRedisClient();
			expect(redisClient).to.not.equal(false);
			expect(redisClient).to.have.property('set');
			expect(redisClient).to.have.property('get');
			expect(redisClient).to.have.property('del');
		});
	});
});
{'fraction_non_alphanumeric': 0.17843017949498022, 'fraction_numerical': 0.003954974140553696, 'mean_word_length': 3.831006612784717, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5192526', 'n_tokens_mistral': 2428, 'n_tokens_neox': 2086, 'n_words': 405}