Skip to content

Commit

Permalink
many: backport some usefull functions
Browse files Browse the repository at this point in the history
* Adds new test script.
* Improves test by adding tester app into the tests.
* Adds new form validators: to test remotely the uniqueness of a value,
to check a range of time.
* Adds a function to clean a json before backend POST: remove empty
values.
* Improves the valueAlreadyExists function of the record-service to use
getRecords instead of a direct http call.
* Add variables to record-service for default and max number of results.
Co-Authored-by: Johnny Mariéthoz <Johnny.Mariethoz@rero.ch>
Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
jma committed Oct 12, 2019
1 parent f42dd53 commit 850f41a
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 13 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ install:
- npm install

script:
- npm run lint
- npm run test-lib -- --no-watch --no-progress --browsers=ChromeHeadlessCI
- ./run-tests.sh
6 changes: 6 additions & 0 deletions projects/ng-core-tester/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports = function (config) {
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
customLaunchers: {
ChromeHeadlessCI: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},
singleRun: false,
restartOnFileChange: true
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { AbstractControl } from '@angular/forms';
import { Component, Input, OnInit } from '@angular/core';
import { JsonSchemaFormService } from 'angular6-json-schema-form';
import { RecordService } from '../../record.service';
import { UniqueValidator } from '../../../validator/unique.validator';


@Component({
Expand Down Expand Up @@ -51,7 +52,9 @@ export class RemoteInputComponent implements OnInit {
this.jsf.initializeControl(this);
if (this.options.remoteRecordType) {
this.formControl.setAsyncValidators([
this.valueAlreadyTaken.bind(this, this.options.remoteRecordType)
UniqueValidator.createValidator(
this.recordService, this.options.remoteRecordType,
this.controlName, this.formControl.root.value.pid)
]);
}
}
Expand All @@ -60,10 +63,4 @@ export class RemoteInputComponent implements OnInit {
this.jsf.updateValue(this, event.target.value);
}

valueAlreadyTaken(recordType: string, control: AbstractControl) {
const pid = control.root.value.pid;
return this.recordService.valueAlreadyExists(
recordType, this.controlName, control.value, pid);
}

}
10 changes: 6 additions & 4 deletions projects/rero/ng-core/src/lib/record/record.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { ApiService } from '../api/api.service';
providedIn: 'root'
})
export class RecordService {
public static readonly DEFAULT_REST_RESULTS_SIZE = 10;
public static readonly MAX_REST_RESULTS_SIZE = 9999;
/**
* Constructor
* @param http - HttpClient
Expand All @@ -48,7 +50,7 @@ export class RecordService {
type: string,
query: string = '',
page = 1,
itemsPerPage = 20,
itemsPerPage = RecordService.DEFAULT_REST_RESULTS_SIZE,
aggFilters: any[] = [],
preFilters: object = {}
): Observable<Record> {
Expand Down Expand Up @@ -144,11 +146,11 @@ export class RecordService {
* @param excludePid - string, PID to ignore (normally the current record we are checking)
*/
public valueAlreadyExists(recordType: string, field: string, value: string, excludePid: string) {
let url = `${this.apiService.getEndpointByType(recordType, true)}/?size=0&q=${field}:"${value}"`;
let query = `${field}:"${value}"`;
if (excludePid) {
url += ` NOT pid:${excludePid}`;
query += ` NOT pid:${excludePid}`;
}
return this.http.get<any>(url).pipe(
return this.getRecords(recordType, query, 1, 0).pipe(
map(res => res.hits.total),
map(total => total ? { alreadyTakenMessage: value } : null),
debounceTime(1000)
Expand Down
2 changes: 2 additions & 0 deletions projects/rero/ng-core/src/lib/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { DialogComponent } from './dialog/dialog.component';
import { MenuComponent } from './widget/menu/menu.component';
import { RouterModule } from '@angular/router';
import { CallbackArrayFilterPipe } from './pipe/callback-array-filter.pipe';
import { UniqueValidator } from './validator/unique.validator';


@NgModule({
Expand Down Expand Up @@ -66,6 +67,7 @@ import { CallbackArrayFilterPipe } from './pipe/callback-array-filter.pipe';
isolate: false
})
],
// providers: [UniqueValidator],
exports: [
CommonModule,
FormsModule,
Expand Down
10 changes: 10 additions & 0 deletions projects/rero/ng-core/src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ export function extractIdOnRef(ref: string) {
const rx = /.*\/?(.+)$/ig;
return rx.exec(ref)[1];
}

export function cleanDictKeys(data: any) {
// let data = _.cloneDeep(data);
for (const key in data) {
if (data[key] === null || data[key] === undefined || data[key].length === 0) {
delete data[key];
}
}
return data;
}
61 changes: 61 additions & 0 deletions projects/rero/ng-core/src/lib/validator/time.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Invenio angular core
* Copyright (C) 2019 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { AbstractControl, ValidatorFn, FormArray } from '@angular/forms';
import moment from 'moment';

// @dynamic
export class TimeValidator {
static greaterThanValidator(start: string, end: string): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
if (control) {
let isLessThan = false;
const startTime = control.get('start_time');
const endTime = control.get('end_time');
const startDate = moment(startTime.value, 'HH:mm');
const endDate = moment(endTime.value, 'HH:mm');
if (startDate.format('HH:mm') !== '00:00' || endDate.format('HH:mm') !== '00:00') {
isLessThan = startDate.diff(endDate) >= 0;
}
return isLessThan ? { lessThan: { value: isLessThan }} : null;
}
};
}

static RangePeriodValidator(): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
if (control.value) {
let isRangeLessThan = false;
const times = control.get('times') as FormArray;
if (control.get('is_open').value && times.value.length > 1) {
const firstStartDate = moment(times.at(0).get('start_time').value, 'HH:mm');
const firstEndDate = moment(times.at(0).get('end_time').value, 'HH:mm');
const lastStartDate = moment(times.at(1).get('start_time').value, 'HH:mm');
const lastEndDate = moment(times.at(1).get('end_time').value, 'HH:mm');
if (firstStartDate > lastStartDate) {
isRangeLessThan = firstStartDate.diff(lastStartDate) <= 0
|| firstStartDate.diff(lastEndDate) <= 0;
} else {
isRangeLessThan = lastStartDate.diff(firstEndDate) <= 0
|| lastStartDate.diff(firstEndDate) <= 0;
}
}
return isRangeLessThan ? { rangeLessThan: { value: isRangeLessThan} } : null;
}
};
}
}
38 changes: 38 additions & 0 deletions projects/rero/ng-core/src/lib/validator/unique.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Invenio angular core
* Copyright (C) 2019 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { AbstractControl } from '@angular/forms';
import { RecordService } from '../record/record.service';

// @dynamic
export class UniqueValidator {
static createValidator(
recordService: RecordService,
recordType: string,
fieldName: string,
excludePid: string = null
) {
return (control: AbstractControl) => {
return recordService.valueAlreadyExists (
recordType,
fieldName,
control.value,
excludePid
);
};
}
}
2 changes: 2 additions & 0 deletions projects/rero/ng-core/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ export * from './lib/record/editor/editor.component';
export * from './lib/record/search/record-search.component';
export * from './lib/record/detail/detail.component';
export * from './lib/utils/utils';
export * from './lib/validator/unique.validator';
export * from './lib/validator/time.validator';
35 changes: 35 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2019 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

RED='\033[0;31m'
GREEN='\033[0;0;32m'
NC='\033[0m' # No Color

display_error_message () {
echo -e "${RED}$1${NC}" 1>&2
}

display_success_message () {
echo -e "${GREEN}$1${NC}" 1>&2
}

display_success_message "Linting the projects..."
ng lint

display_success_message "Run the tests"
ng test --no-watch --no-progress --browsers=ChromeHeadlessCI

0 comments on commit 850f41a

Please sign in to comment.