/**
 * Copyright 2017 - 2018  The Hyve B.V.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */ import { Constraint } from '../../models/constraint-models/constraint' ; import { ConstraintHelper } from './constraint-helper' ; import { ConceptConstraint } from '../../models/constraint-models/concept-constraint' ; import { CombinationConstraint } from '../../models/constraint-models/combination-constraint' ; import { TrueConstraint } from '../../models/constraint-models/true-constraint' ; import { ValueConstraint } from '../../models/constraint-models/value-constraint' ; describe ( 'ConstraintHelper.permuteConstraints' , ( ) => { it ( 'should compute the permutation of a list of constraints' , ( ) => { let c1 = new ConceptConstraint ( ) ; let c2 = new CombinationConstraint ( ) ; let c3 = new TrueConstraint ( ) ; let c4 = new ValueConstraint ( ) ; let constraints = [ [ c1 , c2 ] , [ c3 , c4 ] ] ; let expected = [ [ c1 , c3 ] , [ c1 , c4 ] , [ c2 , c3 ] , [ c2 , c4 ] ] ; expect ( ConstraintHelper . permuteConstraints ( constraints ) ) . toEqual ( expected ) ; } ) ; it ( 'should compute the permutation of an empty list of constraints' , ( ) => { let constraints = [ ] ; let expected = [ [ ] ] ; expect ( ConstraintHelper . permuteConstraints ( constraints ) ) . toEqual ( expected ) ; } ) ; } ) ; describe ( 'ConstraintHelper.hasNonEmptyChildren' , ( ) => { it ( 'should recognise combination with non-empty children' , ( ) => { let nonEmptyChild = new CombinationConstraint ( ) ; nonEmptyChild . addChild ( new TrueConstraint ( ) ) ; let emptyChild = new CombinationConstraint ( ) ; let combination = new CombinationConstraint ( ) ; combination . addChild ( nonEmptyChild ) ; combination . addChild ( emptyChild ) ; expect ( ConstraintHelper . hasNonEmptyChildren ( combination ) ) . toEqual ( true ) ; combination = new CombinationConstraint ( ) ; combination . addChild ( emptyChild ) ; combination . addChild ( nonEmptyChild ) ; expect ( ConstraintHelper . hasNonEmptyChildren ( combination ) ) . toEqual ( true ) ; combination = new CombinationConstraint ( ) ; combination . addChild ( new TrueConstraint ( ) ) ; expect ( ConstraintHelper . hasNonEmptyChildren ( combination ) ) . toEqual ( true ) ; } ) ; it ( 'should recognise combination without non-empty children' , ( ) => { let emptyChild1 = new CombinationConstraint ( ) ; let emptyChild2 = new CombinationConstraint ( ) ; let combination = new CombinationConstraint ( ) ; expect ( ConstraintHelper . hasNonEmptyChildren ( combination ) ) . toEqual ( false ) ; combination . addChild ( emptyChild1 ) ; combination . addChild ( emptyChild2 ) ; expect ( ConstraintHelper . hasNonEmptyChildren ( combination ) ) . toEqual ( false ) ; } ) ; } ) ; describe ( 'ConstraintHelper.ensurePatientLevelConstraint' , ( ) => { it ( 'should wrap combination selecting samples into patient subselection' , ( ) => { let sampleLevelCombination = new CombinationConstraint ( ) ; sampleLevelCombination . dimension = 'sample-dimension' ; let resultConstraint = ConstraintHelper . ensurePatientLevelConstraint ( sampleLevelCombination ) ; expect ( resultConstraint ) . toEqual ( jasmine . any ( CombinationConstraint ) ) ; expect ( ( resultConstraint as CombinationConstraint ) . dimension ) . toEqual ( 'patient' ) ; expect ( ( ( resultConstraint as CombinationConstraint ) . children [ 0 ] as CombinationConstraint ) . dimension ) . toEqual ( 'sample-dimension' ) ; } ) ; it ( 'should wrap non-combination constraint into patient subselection' , ( ) => { let conceptConstraint = new ConceptConstraint ( ) ; let resultConstraint = ConstraintHelper . ensurePatientLevelConstraint ( conceptConstraint ) ; expect ( resultConstraint ) . toEqual ( jasmine . any ( CombinationConstraint ) ) ; expect ( ( resultConstraint as CombinationConstraint ) . dimension ) . toEqual ( 'patient' ) ; expect ( ( resultConstraint as CombinationConstraint ) . children [ 0 ] ) . toEqual ( jasmine . any ( ConceptConstraint ) ) ; } ) ; it ( 'should not wrap combination selecting patients into patient subselection' , ( ) => { let patientLevelCombination = new CombinationConstraint ( ) ; patientLevelCombination . dimension = 'patient' ; patientLevelCombination . addChild ( new ConceptConstraint ( ) ) ; let resultConstraint = ConstraintHelper . ensurePatientLevelConstraint ( patientLevelCombination ) ; expect ( resultConstraint ) . toEqual ( jasmine . any ( CombinationConstraint ) ) ; expect ( ( resultConstraint as CombinationConstraint ) . dimension ) . toEqual ( 'patient' ) ; expect ( ( resultConstraint as CombinationConstraint ) . children [ 0 ] ) . toEqual ( jasmine . any ( ConceptConstraint ) ) ; } ) ; } ) ;