var coding = require('../../lib/assert/coding');

var should = require('chai').should();

describe('assert.coding', function () {
    describe('includesCode', function () {
        it('should throw exception if code undefined', function () {
            should.Throw(function () {
                coding.includesCode();
            });
        });

        it('should return a function', function () {
            var code = {
                system: 'utest',
                code: 'foo'
            };

            var result = coding.includesCode(code);

            should.exist(result);
            result.should.be.a('Function');
        });

        it('should return false when coding does not contain code', function () {
            var candidate = [
                {
                    system: 'READ2',
                    code: '6144.',
                    display: 'Oral contraceptive repeat'
                },
                {
                    system: 'SNOMED',
                    code: '169461009',
                    display: 'Oral contraceptive repeat'
                }
            ];
            var code = {
                system: 'utest',
                code: 'foo'
            };

            var fn = coding.includesCode(code);
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(false);
        });

        it('should return false when coding does not contain code within system', function () {
            var candidate = [
                {
                    system: 'READ2',
                    code: '6144.',
                    display: 'Oral contraceptive repeat'
                },
                {
                    system: 'SNOMED',
                    code: '169461009',
                    display: 'Oral contraceptive repeat'
                }
            ];
            var code = {
                system: 'utest',
                code: '6144.'
            };

            var fn = coding.includesCode(code);
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(false);
        });

        it('should return true when coding contains code', function () {
            var candidate = [
                {
                    system: 'READ2',
                    code: '6144.',
                    display: 'Oral contraceptive repeat'
                },
                {
                    system: 'SNOMED',
                    code: '169461009',
                    display: 'Oral contraceptive repeat'
                }
            ];
            var code = {
                system: 'READ2',
                code: '6144.'
            };

            var fn = coding.includesCode(code);
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(true);
        });

        it('should return true when coding does contain match for regex code', function () {
            var candidate = [
                {
                    system: 'READ2',
                    code: '6144.',
                    display: 'Oral contraceptive repeat'
                },
                {
                    system: 'SNOMED',
                    code: '169461009',
                    display: 'Oral contraceptive repeat'
                }
            ];
            var code = {
                system: 'READ2',
                code: /^614/
            };

            var fn = coding.includesCode(code);
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(true);
        });

        it('should return false when coding does not contain match for regex code', function () {
            var candidate = [
                {
                    system: 'READ2',
                    code: '6144.',
                    display: 'Oral contraceptive repeat'
                },
                {
                    system: 'SNOMED',
                    code: '169461009',
                    display: 'Oral contraceptive repeat'
                }
            ];
            var code = {
                system: 'READ2',
                code: /^416/
            };

            var fn = coding.includesCode(code);
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(false);
        });
    });

    describe('includesCodeFromSet', function () {
        it('should throw exception if codes undefined', function () {
            should.Throw(function () {
                coding.includesCodeFromSet();
            });
        });

        it('should return a function', function () {
            var codes = [
                {
                    system: 'utest',
                    code: 'foo'
                }
            ];

            var result = coding.includesCodeFromSet(codes);

            should.exist(result);
            result.should.be.a('Function');
        });

        it('should return false when coding does not contain code', function () {
            var candidate = [
                {
                    system: 'READ2',
                    code: '6144.',
                    display: 'Oral contraceptive repeat'
                },
                {
                    system: 'SNOMED',
                    code: '169461009',
                    display: 'Oral contraceptive repeat'
                }
            ];
            var codes = [
                {
                    system: 'utest',
                    code: 'foo'
                },
                {
                    system: 'READ2',
                    code: 'NOMATCH'
                }
            ];

            var fn = coding.includesCodeFromSet(codes);
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(false);
        });

        it('should return true when coding contains code', function () {
            var candidate = [
                {
                    system: 'READ2',
                    code: '6144.',
                    display: 'Oral contraceptive repeat'
                },
                {
                    system: 'SNOMED',
                    code: '169461009',
                    display: 'Oral contraceptive repeat'
                }
            ];
            var codes = [
                {
                    system: 'READ2',
                    code: '6144.'
                },
                {
                    system: 'READ2',
                    code: 'NOMATCH'
                }
            ];

            var fn = coding.includesCodeFromSet(codes);
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(true);
        });
    });
});
