feat: tests

This commit is contained in:
2023-04-21 12:08:35 +02:00
parent 8cb9285462
commit d8c79256c9
140 changed files with 2100 additions and 2693 deletions

View File

@@ -1,3 +1,4 @@
/* eslint-disable unicorn/no-null */
/*
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
@@ -12,12 +13,7 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {slow, suite, test, timeout} from '@testdeck/mocha';
import {SCBulkResponse} from '../src/protocol/routes/bulk-request.js';
import {SCMultiSearchResponse} from '../src/protocol/routes/search-multi.js';
import {SCSearchResponse} from '../src/protocol/routes/search.js';
import {SCThingOriginType, SCThingType} from '../src/things/abstract/thing.js';
import {SCDish} from '../src/things/dish.js';
import {SCMultiSearchResponse} from '../src/index.js';
import {expect} from 'chai';
import {
isBulkResponse,
@@ -26,117 +22,104 @@ import {
isThing,
isThingWithTranslations,
} from '../src/guards.js';
import {bulkResponse} from './dummy/bulk-response.js';
import {dishWithTranslation} from './dummy/dish-with-translation.js';
import {dishWithTranslationSearchResponse} from './dummy/dish-with-translation-search-response.js';
import {notADish} from './dummy/not-a-dish.js';
import {SCBulkResponse} from '../src/protocol/routes/bulk-request.js';
import {SCSearchResponse} from '../src/protocol/routes/search.js';
import {SCMultiSearchResponse} from '../src/protocol/routes/search-multi.js';
import {SCThingOriginType, SCThingType} from '../src/things/abstract/thing.js';
import {SCDish} from '../src/things/dish';
import {SCDish} from '../src/things/dish.js';
@suite(timeout(10000), slow(5000))
export class GuardsSpec {
static bulkResponse: SCBulkResponse = {
expiration: '2009-06-30T18:30:00+02:00 ',
source: 'bar',
state: 'done',
type: SCThingType.Dish,
uid: 'foo',
};
describe('Guards', function () {
this.timeout(10_000);
this.slow(5000);
static dishWithTranslation: SCDish = {
categories: ['appetizer'],
name: 'foo',
origin: {
created: '',
type: SCThingOriginType.User,
},
translations: {
de: {
name: 'Foo',
},
},
type: SCThingType.Dish,
uid: 'bar',
};
describe('isBulkResponse', function () {
it(`should not accept nullish values`, function () {
expect(isBulkResponse(null)).to.be.false;
});
static notADish = {
categories: ['appetizer'],
name: 'foo',
origin: {
created: '',
type: SCThingOriginType.User,
},
type: 'foobar',
uid: 'bar',
};
it('should not accept a dish', function () {
expect(isBulkResponse(dishWithTranslation)).to.be.false;
});
static searchResponse: SCSearchResponse = {
data: [GuardsSpec.dishWithTranslation],
facets: [
{
buckets: [
{
count: 1,
key: 'key',
},
],
field: 'field',
},
],
pagination: {
count: 1,
offset: 0,
total: 1,
},
stats: {
time: 1,
},
};
it('should accept a bulk', function () {
expect(isBulkResponse(bulkResponse)).to.be.true;
});
});
@test
public isBulkResponse() {
expect(isBulkResponse(null)).to.be.equal(false);
expect(isBulkResponse(GuardsSpec.dishWithTranslation)).to.be.equal(false);
expect(isBulkResponse(GuardsSpec.bulkResponse)).to.be.equal(true);
}
@test
public isMultiSearchResponse() {
describe('isMultiSearchResponse', function () {
const multiSearchResponse: SCMultiSearchResponse = {
foo: GuardsSpec.searchResponse,
foo: dishWithTranslationSearchResponse,
};
expect(isMultiSearchResponse(multiSearchResponse)).to.be.equal(true);
const notAMultiSearchResponse = {...multiSearchResponse, ...{bar: 'baz'}};
expect(isMultiSearchResponse(notAMultiSearchResponse)).to.be.equal(false);
delete multiSearchResponse.foo;
expect(isMultiSearchResponse(multiSearchResponse)).to.be.equal(false);
}
@test
public isSearchResponse() {
const notASearchResponse = {...GuardsSpec.searchResponse};
// @ts-ignore
delete notASearchResponse.pagination;
expect(isSearchResponse(notASearchResponse)).to.be.equal(false);
// @ts-ignore
delete notASearchResponse.data;
expect(isSearchResponse(notASearchResponse)).to.be.equal(false);
expect(isSearchResponse(null)).to.be.equal(false);
expect(isSearchResponse(GuardsSpec.searchResponse)).to.be.equal(true);
}
it('should accept a multi search response', function () {
expect(isMultiSearchResponse(multiSearchResponse)).to.be.true;
});
@test
public isThing() {
expect(isThing('foo')).to.be.equal(false);
expect(isThing({type: 'foo'})).to.be.equal(false);
expect(isThing(GuardsSpec.notADish)).to.be.equal(false);
expect(isThing(GuardsSpec.dishWithTranslation)).to.be.equal(true);
}
it('should not accept a multi search response with invalid search requests', function () {
const notAMultiSearchResponse = {...multiSearchResponse, bar: 'baz'};
expect(isMultiSearchResponse(notAMultiSearchResponse)).to.be.false;
});
@test
public isThingWithTranslations() {
const dishWithoutTranslation = {...GuardsSpec.dishWithTranslation};
delete dishWithoutTranslation.translations;
expect(isThingWithTranslations(dishWithoutTranslation)).to.be.equal(false);
expect(isThingWithTranslations(GuardsSpec.dishWithTranslation)).to.be.equal(true);
}
}
it('should not accept empty responses', function () {
expect(isMultiSearchResponse({})).to.be.false;
});
});
describe('isSearchResponse', function () {
it('should accept a search response', function () {
expect(isSearchResponse(dishWithTranslationSearchResponse)).to.be.true;
});
it('should not accept nullish values', function () {
expect(isSearchResponse(null)).to.be.false;
});
it('should not accept a response without pagination', function () {
const response = {...dishWithTranslationSearchResponse};
// @ts-expect-error this is on purpose of course
delete response.pagination;
expect(isSearchResponse(response)).to.be.false;
});
it('should not accept a response without data', function () {
const response = {...dishWithTranslationSearchResponse};
// @ts-expect-error this is on purpose of course
delete response.data;
expect(isSearchResponse(response)).to.be.false;
});
});
describe('isThing', function () {
it('should not accept strings', function () {
expect(isThing('foo')).to.be.false;
});
it('should not accept objects with arbitrary type values', function () {
expect(isThing({type: 'foo'})).to.be.false;
});
it('should not accept things with missing props', function () {
expect(isThing(notADish)).to.be.false;
});
it('should accept valid things', function () {
expect(isThing(dishWithTranslation)).to.be.true;
});
});
describe('isThingWithTranslations', function () {
it('should not accept things without translations', function () {
const dishWithoutTranslation = {...dishWithTranslation};
delete dishWithoutTranslation.translations;
expect(isThingWithTranslations(dishWithoutTranslation)).to.be.false;
});
it('should accept things with translations', function () {
expect(isThingWithTranslations(dishWithTranslation)).to.be.true;
});
});
});