test/unit/helpers.query.js
import test from 'ava';
import { Query } from '../../src';
/**
* @test {Query}
*/
test('[unit] Query should exist', (t) => {
t.truthy(Query);
});
/**
* @test {Query}
*/
test('[unit] Query should throw an error if orderBy are incorrect', (t) => {
t.throws(() => new Query('acronyms', { direction: 'desc' }),
'Query construction error: orderBy is not an array');
});
/**
* @test {Query}
*/
test('[unit] Query should be able to be constructed properly', (t) => {
const query = new Query('acronyms', ['a', 'b', 'c'], 2, 20, true);
t.deepEqual(query.orderBy, ['a', 'b', 'c']);
t.is(query.page, 2);
t.is(query.perPage, 20);
t.is(query.minimal, true);
t.is(query.brandRelative, false);
t.is(query.whole_word, false);
t.is(query.case_sensitive, false);
t.is(query.reference_module, '');
t.is(query.filter.attribute, 'search');
t.is(query.filter.search_method, undefined);
t.is(query.filter.values, undefined);
});
/**
* @test {Query}
*/
test('[unit] Query should have all the query methods available', (t) => {
const query = new Query('acronyms');
t.truthy(query.setResource);
t.truthy(query.find);
t.truthy(query.addSearchValue);
t.truthy(query.removeSearchValue);
t.truthy(query.clearSearchValues);
t.truthy(query.searchMode);
t.truthy(query.startsWithMode);
t.truthy(query.endsWithMode);
t.truthy(query.wholeWordMode);
t.truthy(query.wholeWordSearch);
t.truthy(query.makeCaseSensitive);
t.truthy(query.lessThanOrEqualTo);
t.truthy(query.greaterThanOrEqualTo);
t.truthy(query.setValue);
t.truthy(query.ascending);
t.truthy(query.descending);
t.truthy(query.limit);
t.truthy(query.showMinimal);
t.truthy(query.showBrandRelative);
t.truthy(query.referenceModule);
t.truthy(query.searchByLocationMode);
t.truthy(query.searchByAddressMode);
t.truthy(query.searchByTagNamesMode);
t.truthy(query.searchByNumberMode);
t.truthy(query.searchByDateMode);
t.truthy(query.searchByTagIDMode);
t.truthy(query.setRadius);
t.truthy(query.setExactLatitude);
t.truthy(query.setExactLongitude);
t.truthy(query.setSwLat);
t.truthy(query.setSwLong);
t.truthy(query.setNeLat);
t.truthy(query.setNwLong);
t.truthy(query.setValue);
});
/**
* @test {Query}
*/
test('[unit] Query should have methods that are chain-able', (t) => {
const chainedQuery = new Query('acronyms')
.ascending('name')
.setResource('type')
.addSearchValue('blue')
.addSearchValue('green')
.addSearchValue('red')
.removeSearchValue('green')
.startsWithMode()
.limit(10)
.showMinimal()
.showBrandRelative()
.referenceModule('reference');
t.truthy(chainedQuery);
t.is(chainedQuery.orderBy.length, 1);
t.is(chainedQuery.orderBy[0].column, 'name');
t.is(chainedQuery.orderBy[0].direction, 'asc');
t.is(chainedQuery.minimal, true);
t.is(chainedQuery.brandRelative, true);
t.is(chainedQuery.reference_module, 'reference');
t.is(chainedQuery.resource, 'type');
t.is(chainedQuery.filter.values.length, 2);
t.is(chainedQuery.filter.values[0], 'blue');
t.is(chainedQuery.filter.values[1], 'red');
t.is(chainedQuery.filter.search_method, 'starts_with');
const chainedQuery2 = new Query('acronyms')
.ascending('name')
.setResource('type')
.addSearchValue('blue')
.addSearchValue('green')
.addSearchValue('red')
.clearSearchValues()
.endsWithMode()
.limit(10)
.showMinimal()
.showBrandRelative()
.referenceModule('reference');
t.truthy(chainedQuery2);
t.is(chainedQuery2.orderBy.length, 1);
t.is(chainedQuery2.orderBy[0].column, 'name');
t.is(chainedQuery2.orderBy[0].direction, 'asc');
t.is(chainedQuery2.minimal, true);
t.is(chainedQuery2.brandRelative, true);
t.is(chainedQuery2.reference_module, 'reference');
t.is(chainedQuery2.resource, 'type');
t.is(chainedQuery2.filter.values.length, 0);
t.is(chainedQuery2.filter.search_method, 'ends_with');
const chainedQuery3 = new Query('acronyms')
.ascending('name')
.setResource('type')
.addSearchValue('blue')
.addSearchValue('green')
.addSearchValue('red')
.clearSearchValues()
.searchMode()
.limit(10)
.showMinimal()
.showBrandRelative()
.referenceModule('reference');
t.truthy(chainedQuery3);
t.is(chainedQuery3.orderBy.length, 1);
t.is(chainedQuery3.orderBy[0].column, 'name');
t.is(chainedQuery3.orderBy[0].direction, 'asc');
t.is(chainedQuery3.minimal, true);
t.is(chainedQuery3.brandRelative, true);
t.is(chainedQuery3.reference_module, 'reference');
t.is(chainedQuery3.resource, 'type');
t.is(chainedQuery3.filter.values.length, 0);
t.is(chainedQuery3.filter.attribute, 'search');
const chainedQuery4 = new Query('acronyms')
.ascending('name')
.setResource('type')
.addSearchValue('blue')
.addSearchValue('green')
.addSearchValue('red')
.clearSearchValues()
.searchByAddressMode()
.setSwLat('10.0')
.setSwLong('9.9')
.setNeLat('9.9')
.limit(10)
.showMinimal()
.showBrandRelative()
.referenceModule('reference');
t.truthy(chainedQuery4);
t.is(chainedQuery4.orderBy.length, 1);
t.is(chainedQuery4.orderBy[0].column, 'name');
t.is(chainedQuery4.orderBy[0].direction, 'asc');
t.is(chainedQuery4.minimal, true);
t.is(chainedQuery4.brandRelative, true);
t.is(chainedQuery4.reference_module, 'reference');
t.is(chainedQuery4.resource, 'type');
t.is(chainedQuery4.filter.values.length, 0);
t.is(chainedQuery4.filter.attribute, 'address');
t.is(chainedQuery4.filter.sw_lat, '10.0');
t.is(chainedQuery4.filter.sw_lng, '9.9');
t.is(chainedQuery4.filter.ne_lat, '9.9');
});
/**
* @test {Query}
*/
test('[unit] Query should create a query with ascending order', (t) => {
const query = new Query('acronyms')
.ascending('column1');
t.is(query.orderBy.length, 1);
t.is(query.orderBy[0].direction, 'asc');
t.is(query.orderBy[0].column, 'column1');
});
/**
* @test {Query}
*/
test('[unit] Query should create a query with descending order', (t) => {
const query = new Query('acronyms')
.descending('column1');
t.is(query.orderBy.length, 1);
t.is(query.orderBy[0].direction, 'desc');
t.is(query.orderBy[0].column, 'column1');
});
/**
* @test {Query}
*/
test('[unit] Query should create a query with page and perpage when limit is called', (t) => {
const query = new Query('acronyms')
.limit(100);
t.is(query.page, 1);
t.is(query.perPage, 100);
});
/**
* @test {Query}
*/
test('[unit] Query should create a query with brandRelative', (t) => {
const query = new Query('acronyms')
.showBrandRelative();
t.is(query.brandRelative, true);
});
/**
* @test {Query}
*/
test('[unit] Query should create a query with minimal', (t) => {
const query = new Query('acronyms')
.showMinimal();
t.is(query.minimal, true);
});
/**
* @test {Query}
*/
test('[unit] Query should create a query with referenceModule', (t) => {
const query = new Query('acronyms')
.referenceModule('refmod');
t.is(query.reference_module, 'refmod');
});
/**
* @test {Query}
*/
test('[unit] Query should create a query with content', (t) => {
const query = new Query('acronyms')
.setResource('content');
t.is(query.resource, 'content');
});
/**
* @test {Query}
*/
test('[unit] Query should add search values', (t) => {
const query = new Query('acronyms')
.addSearchValue('value');
t.is(query.filter.values.length, 1);
});
/**
* @test {Query}
*/
test('[unit] Query should remove search values', (t) => {
const query = new Query('acronyms')
.addSearchValue('value')
.removeSearchValue('value');
t.is(query.filter.values.length, 0);
});
/**
* @test {Query}
*/
test('[unit] Query should clear search values', (t) => {
const query = new Query('acronyms')
.addSearchValue('value')
.clearSearchValues();
t.is(query.filter.values.length, 0);
});
/**
* @test {Query}
*/
test('[unit] Query should set attribute to search', (t) => {
const query = new Query('acronyms');
t.is(query.filter.attribute, 'search');
query.searchByLocationMode()
.searchMode();
t.is(query.filter.attribute, 'search');
});
/**
* @test {Query}
*/
test('[unit] Query should set attribute to tag', (t) => {
const query = new Query('acronyms')
.searchByTagIDMode();
t.is(query.filter.attribute, 'tag');
});
/**
* @test {Query}
*/
test('[unit] Query should set searchMethod to starts_with', (t) => {
const query = new Query('acronyms')
.startsWithMode();
t.is(query.filter.search_method, 'starts_with');
});
/**
* @test {Query}
*/
test('[unit] Query should set searchMethod to ends_with', (t) => {
const query = new Query('acronyms')
.endsWithMode();
t.is(query.filter.search_method, 'ends_with');
});
/**
* @test {Query}
*/
test('[unit] Query should set searchMethod to whole_word', (t) => {
const query = new Query('acronyms')
.wholeWordMode();
t.is(query.filter.search_method, 'whole_word');
});
/**
* @test {Query}
*/
test('[unit] Query should set whole_word to true', (t) => {
const query = new Query('acronyms')
.wholeWordSearch();
t.is(query.filter.whole_word, true);
});
/**
* @test {Query}
*/
test('[unit] Query should set case_sensitive to true', (t) => {
const query = new Query('acronyms')
.makeCaseSensitive();
t.is(query.filter.case_sensitive, true);
});
/**
* @test {Query}
*/
test('[unit] Query should set a radius value', (t) => {
const query = new Query('acronyms')
.setRadius('1.2');
t.is(query.filter.radius, '1.2');
});
/**
* @test {Query}
*/
test('[unit] Query should set the exact latitude value', (t) => {
const query = new Query('acronyms')
.setExactLatitude('1.2');
t.is(query.filter.exact_lat, '1.2');
});
/**
* @test {Query}
*/
test('[unit] Query should set the exact longitude value', (t) => {
const query = new Query('acronyms')
.setExactLongitude('1.2');
t.is(query.filter.exact_lng, '1.2');
});
/**
* @test {Query}
*/
test('[unit] Query should set SW Latitude value', (t) => {
const query = new Query('acronyms')
.setSwLat('1.2');
t.is(query.filter.sw_lat, '1.2');
});
/**
* @test {Query}
*/
test('[unit] Query should set SW Longitude value', (t) => {
const query = new Query('acronyms')
.setSwLong('1.2');
t.is(query.filter.sw_lng, '1.2');
});
/**
* @test {Query}
*/
test('[unit] Query should set NE Latitude value', (t) => {
const query = new Query('acronyms')
.setNeLat('1.2');
t.is(query.filter.ne_lat, '1.2');
});
/**
* @test {Query}
*/
test('[unit] Query should set NW Longitutde value', (t) => {
const query = new Query('acronyms')
.setNwLong('1.2');
t.is(query.filter.nw_lng, '1.2');
});
/**
* @test {Query}
*/
test('[unit] Query should set a value', (t) => {
const query = new Query('acronyms')
.setValue(1);
t.is(query.filter.value, 1);
});