#---------------------------------------------------------------------------
# Copyright 2013 PwC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#---------------------------------------------------------------------------

## @package PL_Suite001
## Problem List Package Test (Suite)

'''
These are the Problem List package Suite001 tests, implemented as Python functions.

Each test has a unique name derived from the test method name.
Each test has a unique result log with a filename derived from the testname and a datestamp.
There is a parent resultlog that is also used for pass/fail logging.
In general each test establishes a connection to the target application (VistA),
signs on as a user, provider, or programmer and then performs a set of test functions.
When testing is complete the connection is closed and a pass/fail indicate is written
to the resultlog.

Created on Mar 9, 2012
@author: pbradley
@copyright PwC
@license http://www.apache.org/licenses/LICENSE-2.0
'''

import sys
sys.path = ['./Functional/RAS/lib'] + ['./dataFiles'] + ['./Python/vista'] + sys.path
from PLActions import PLActions
from ORActions import ORActions
from CPRSActions import CPRSActions
import TestHelper

def pl_test001(test_suite_details):
    '''
    This performs the NIST Inpatient Test; add problem to problem list, edit problem list,
    verify, and remove problem from problem list.

    The test scripts for testing of the Problem List adhere to the approved
    guidelines as described in the NIST Test Procedure for 170.302 (c) Maintain up-to-date
    problem list found at: http://healthcare.nist.gov/docs/170.302.c_problemlist_v1.1.pdf .
    '''
    testname = sys._getframe().f_code.co_name
    test_driver = TestHelper.TestDriver(testname)

    test_driver.pre_test_run(test_suite_details)
    try:
        VistA1 = test_driver.connect_VistA(test_suite_details)
        pl = PLActions(VistA1, user='fakedoc1', code='1Doc!@#$')
        pl.signon()
        pl.addcsv(ssn='333224444', pfile='./Functional/dataFiles/NISTinpatientdata0.csv')
        pl.editinactivate(ssn='333224444', probnum='4', resdate='08/29/2010')
        pl.editinactivate(ssn='333224444', probnum='3', resdate='08/29/2010')
        pl.verplist(ssn='333224444', vlist=['Essential Hypertension',
                                            'Chronic airway obstruction'])
        pl.verify(ssn='333224444', probnum='1', itemnum='1',
                     evalue='Essential Hypertension')
        pl.verify(ssn='333224444', probnum='2', itemnum='1',
                     evalue='Chronic airway obstruction')
        pl.verify(ssn='333224444', probnum='1', itemnum='1',
                     evalue='Acute myocardial', view='IA')
        pl.verify(ssn='333224444', probnum='2', itemnum='1',
                     evalue='Congestive Heart Failure', view='IA')
        for i in range(4):
            pl.rem(ssn='333224444')
        pl.signoff()

        test_driver.post_test_run(test_suite_details)
    except TestHelper.TestError, e:
        test_driver.exception_handling(test_suite_details, e)
    else:
        test_driver.try_else_handling(test_suite_details)
    finally:
        test_driver.finally_handling(test_suite_details)

def pl_test002(test_suite_details):
    '''This test restores previously removed problems '''
    testname = sys._getframe().f_code.co_name
    test_driver = TestHelper.TestDriver(testname)

    test_driver.pre_test_run(test_suite_details)
    try:
        VistA1 = test_driver.connect_VistA(test_suite_details)
        pl = PLActions(VistA1, user='fakedoc1', code='1Doc!@#$')
        pl.signon()
        pl.addcsv(ssn='888776666', pfile='./Functional/dataFiles/NISTinpatientdata0.csv')
        pl.editinactivate(ssn='888776666', probnum='4', resdate='08/29/2010')
        pl.editinactivate(ssn='888776666', probnum='3', resdate='08/29/2010')
        pl.verplist(ssn='888776666', vlist=['Essential Hypertension',
                                            'Chronic airway obstruction'])
        pl.verify(ssn='888776666', probnum='1', itemnum='1',
                     evalue='Essential Hypertension')
        pl.verify(ssn='888776666', probnum='2', itemnum='1',
                     evalue='Chronic airway obstruction')
        pl.verify(ssn='888776666', probnum='1', itemnum='1',
                     evalue='Acute myocardial', view='IA')
        pl.verify(ssn='888776666', probnum='2', itemnum='1',
                     evalue='Congestive Heart Failure', view='IA')
        for i in range(4):
            pl.rem(ssn='888776666')
        pl.checkempty(ssn='888776666')
        pl.replace(ssn='888776666', probnum='1')
        pl.verify(ssn='888776666', probnum='1', itemnum='1',
                     evalue='Essential Hypertension')
        pl.rem(ssn='888776666')
        pl.signoff()

        test_driver.post_test_run(test_suite_details)
    except TestHelper.TestError, e:
        test_driver.exception_handling(test_suite_details, e)
    else:
        test_driver.try_else_handling(test_suite_details)
    finally:
        test_driver.finally_handling(test_suite_details)


def pl_test003(test_suite_details):
    '''This test changes problem data '''
    testname = sys._getframe().f_code.co_name
    test_driver = TestHelper.TestDriver(testname)

    test_driver.pre_test_run(test_suite_details)
    try:
        VistA1 = test_driver.connect_VistA(test_suite_details)
        pl = PLActions(VistA1, user='fakedoc1', code='1Doc!@#$')
        pl.signon()
        pl.addcsv(ssn='656771234',
                     pfile='./Functional/dataFiles/NISTinpatientdata0.csv')
        pl.editsimple(ssn='656771234', probnum='1', itemnum='1',
                        chgval='787.1')
        pl.editsimple(ssn='656771234', probnum='1', itemnum='2',
                        chgval='3/26/12')
        pl.editsimple(ssn='656771234', probnum='1', itemnum='4',
                        chgval='MANAGER')
        pl.editsimple(ssn='656771234', probnum='1', itemnum='5',
                        chgval='VISTA')
        pl.verify(ssn='656771234', probnum='1', itemnum='1',
                     evalue='Heartburn')
        pl.verify(ssn='656771234', probnum='1', itemnum='2',
                     evalue='3/26/12')
        pl.verify(ssn='656771234', probnum='1', itemnum='4',
                     evalue='MANAGER,SYSTEM')
        pl.verify(ssn='656771234', probnum='1', itemnum='5',
                     evalue='VISTA')
        for i in range(4):
            pl.rem(ssn='656771234')
        pl.checkempty(ssn='656771234')
        pl.signoff()

        test_driver.post_test_run(test_suite_details)
    except TestHelper.TestError, e:
        test_driver.exception_handling(test_suite_details, e)
    else:
        test_driver.try_else_handling(test_suite_details)
    finally:
        test_driver.finally_handling(test_suite_details)

def pl_test004(test_suite_details):
    ''' This test creates a Problem Selection List, and then adds/modifies/removes categories and problems '''
    testname = sys._getframe().f_code.co_name
    test_driver = TestHelper.TestDriver(testname)

    test_driver.pre_test_run(test_suite_details)
    try:
        VistA1 = test_driver.connect_VistA(test_suite_details)
        pl = PLActions(VistA1)
        pl.signon()
        pl.createsellist(listname="List001", clinic='VISTA')
        pl.createcat(listname='List001', catname='cat001')
        pl.createcat(listname='List001', catname='cat002')
        pl.catad(listname='List001', catname='cat001', icd='787.1')
        pl.catad(listname='List001', catname='cat001', icd='786.50')
        pl.catad(listname='List001', catname='cat001', icd='100.0')
        pl.catad(listname='List001', catname='cat002', icd='780.50')
        pl.catad(listname='List001', catname='cat002', icd='292.0')
        pl.catad(listname='List001', catname='cat002', icd='304.90')
        pl.sellistad(listname='List001', catname='cat001')
        pl.sellistad(listname='List001', catname='cat002')
        pl.versellist(ssn='656454321', clinic='VISTA',
                      vlist=['List001', 'cat001', 'Heartburn', 'chest pain',
                             'Leptospirosis', 'cat002', 'Sleep Disturbance',
                             'Drug withdrawal', 'drug dependence'])
        pl.add(ssn='656454321', clinic='VISTA', probnum='1',
                  comment='this is a test', onsetdate='t', status='Active',
                  acutechronic='A', service='N', evalue='Heartburn')
        pl.verify(ssn='656454321', probnum='1', itemnum='1',
                     evalue='Heartburn')
        pl.rem(ssn='656454321')
        pl.sellistrm(listname='List001')
        pl.sellistrm(listname='List001')
        pl.catdl(listname='List001', catname='cat001')
        pl.catdl(listname='List001', catname='cat002')
        pl.sellistrfu(listname='List001', username='Alexander')
        pl.sellistdl(listname='List001')
        pl.checkRMsellist(ssn='656454321', clinic='VISTA')
        pl.signoff()

        test_driver.post_test_run(test_suite_details)
    except TestHelper.TestError, e:
        test_driver.exception_handling(test_suite_details, e)
    else:
        test_driver.try_else_handling(test_suite_details)
    finally:
        test_driver.finally_handling(test_suite_details)

def pl_test005(test_suite_details):
    '''This test creates a Problem Selection List, assigns it to user, and adds problem. '''
    '''This test uses separate VistA Instances to allow concurrent connections in case of
    future use of tstart and trollback when these features are available.'''
    testname = sys._getframe().f_code.co_name
    test_driver = TestHelper.TestDriver(testname)

    test_driver.pre_test_run(test_suite_details)
    try:
        test_driver.testname = testname + '_01'
        VistA1 = test_driver.connect_VistA(test_suite_details)
        pl1 = PLActions(VistA1)
        pl1.signon()
        pl1.createsellist(listname="List002", clinic='')
        pl1.sellistgal(listname="List002", username='Alexander')
        pl1.createcat(listname='List002', catname='cat011')
        pl1.createcat(listname='List002', catname='cat022')
        pl1.catad(listname='List002', catname='cat011', icd='787.1')
        pl1.catad(listname='List002', catname='cat011', icd='786.50')
        pl1.catad(listname='List002', catname='cat011', icd='100.0')
        pl1.catad(listname='List002', catname='cat022', icd='780.50')
        pl1.catad(listname='List002', catname='cat022', icd='292.0')
        pl1.catad(listname='List002', catname='cat022', icd='304.90')
        pl1.sellistad(listname='List002', catname='cat011')
        pl1.sellistad(listname='List002', catname='cat022')

        test_driver.testname = testname + '_02'
        VistA2 = test_driver.connect_VistA(test_suite_details)
        pl2 = PLActions(VistA2, user='fakedoc1', code='1Doc!@#$')
        pl2.signon()
        pl2.versellist(ssn='354623902', clinic='',
                      vlist=['List002', 'cat011', 'Heartburn', 'chest pain',
                             'Leptospirosis', 'cat022', 'Sleep Disturbance',
                             'Drug withdrawal', 'drug dependence'])
        pl2.add(ssn='354623902', clinic='', probnum='1',
                   comment='this is a test', onsetdate='t',
                   status='Active', acutechronic='A', service='N',
                   evalue='Heartburn')
        pl2.verify(ssn='354623902', probnum='1', itemnum='1',
                      evalue='Heartburn')
        pl2.rem(ssn='354623902')
        pl1.sellistrm(listname='List002')
        pl1.sellistrm(listname='List002')
        pl1.catdl(listname='List002', catname='cat011')
        pl1.catdl(listname='List002', catname='cat022')
        pl1.sellistrfu(listname='List002', username='Alexander')
        pl1.sellistdl(listname='List002')
        pl2.signoff()
        pl1.signoff()

        test_driver.post_test_run(test_suite_details)
    except TestHelper.TestError, e:
        test_driver.exception_handling(test_suite_details, e)
    else:
        test_driver.try_else_handling(test_suite_details)
    finally:
        test_driver.finally_handling(test_suite_details)

def pl_test006 (test_suite_details):
    '''This test creates a Selection List from IB Encounter Form'''
    testname = sys._getframe().f_code.co_name
    test_driver = TestHelper.TestDriver(testname)

    test_driver.pre_test_run(test_suite_details)
    try:
        VistA1 = test_driver.connect_VistA(test_suite_details)
        pl = PLActions(VistA1, user='fakenurse1', code='1Nur!@#$')
        pl.signon()
        pl.createibform('LAB', 'FORM1', 'Group1', ['428.0', '410.90', '401.9'])
        pl.sellistib('FORM1', 'List003', 'LAB')
        pl.versellist(ssn='345238901', clinic='LAB',
                   vlist=['List003', 'Group1', 'Congestive Heart Failure', 'Acute myocardial', 'Essential Hypertension'])
        pl.add(ssn='345238901', clinic='LAB', probnum='1',
                  comment='this is a test', onsetdate='t', status='Active',
                  acutechronic='A', service='N', evalue='Congestive')
        pl.verify(ssn='345238901', probnum='1', itemnum='1',
                     evalue='Congestive')
        pl.rem('345238901')
        pl.sellistrm(listname='List003')
        pl.catdl(listname='List003', catname='Group1')
        pl.sellistrfu(listname='List003', username='Alexander')
        pl.sellistdl(listname='List003')
        pl.signoff()

        test_driver.post_test_run(test_suite_details)
    except TestHelper.TestError, e:
        test_driver.exception_handling(test_suite_details, e)
    else:
        test_driver.try_else_handling(test_suite_details)
    finally:
        test_driver.finally_handling(test_suite_details)

def pl_test007 (test_suite_details):
    '''This test adds problems and then views patients by problem via Problem List menu items 4 & 5'''
    testname = sys._getframe().f_code.co_name
    test_driver = TestHelper.TestDriver(testname)

    test_driver.pre_test_run(test_suite_details)
    try:
        VistA1 = test_driver.connect_VistA(test_suite_details)
        pl = PLActions(VistA1, user='fakedoc1', code='1Doc!@#$')
        pl.signon()
        pl.addcsv(ssn='655447777', pfile='./Functional/dataFiles/NISTinpatientdata0.csv')
        pl.addcsv(ssn='543236666', pfile='./Functional/dataFiles/NISTinpatientdata0.csv')
        pl.addcsv(ssn='345678233', pfile='./Functional/dataFiles/NISTinpatientdata0.csv')
        pl.verlistpats(vlist=['EIGHT,PATIENT', 'ONE,PATIENT', 'TWELVE,PATIENT'])
        pl.verpatsrch(prob='428.0', vlist=['EIGHT,PATIENT', 'ONE,PATIENT', 'TWELVE,PATIENT'])
        for i in range(4):
            pl.rem('655447777')
            pl.rem('543236666')
            pl.rem('345678233')
        pl.signoff()

        test_driver.post_test_run(test_suite_details)
    except TestHelper.TestError, e:
        test_driver.exception_handling(test_suite_details, e)
    else:
        test_driver.try_else_handling(test_suite_details)
    finally:
        test_driver.finally_handling(test_suite_details)

def pl_test008 (test_suite_details):
    '''This test adds a problem via data entry as clerk and changes the problem as doctor'''
    '''Multiple VistA instances to allow concurrent logins for when
    tstart and trollback become available and implemented'''
    testname = sys._getframe().f_code.co_name
    test_driver = TestHelper.TestDriver(testname)

    test_driver.pre_test_run(test_suite_details)
    try:
        test_driver.testname = testname + '_01'
        VistA1 = test_driver.connect_VistA(test_suite_details)
        pl1 = PLActions(VistA1, user='fakeclerk1', code='1Cle!@#$')
        pl1.signon()
        pl1.dataentry(ssn='666551234', provider='Alexander', clinic='', problem='chest pain',
                      comment='test', onsetdate='t', status='Active', acutechronic='A',
                      service='N')
        pl1.signoff()

        test_driver.testname = testname + '_02'
        VistA2 = test_driver.connect_VistA(test_suite_details)
        pl2 = PLActions(VistA2, user='fakedoc1', code='1Doc!@#$')
        pl2.signon()
        pl2.editsimple(ssn='666551234', probnum='1', itemnum='1', chgval='786.50')
        pl2.verplist(ssn='666551234', vlist=['Unspecified chest pain'])
        pl2.rem('666551234')
        pl2.signoff()

        test_driver.post_test_run(test_suite_details)
    except TestHelper.TestError, e:
        test_driver.exception_handling(test_suite_details, e)
    else:
        test_driver.try_else_handling(test_suite_details)
    finally:
        test_driver.finally_handling(test_suite_details)

def pl_test009 (test_suite_details):
    '''This test verifies problems entered into the Problem List through Order Entry package'''
    '''Multiple VistA instances to allow concurrent logins for when
    tstart and trollback become available and implemented'''
    testname = sys._getframe().f_code.co_name
    test_driver = TestHelper.TestDriver(testname)

    test_driver.pre_test_run(test_suite_details)
    try:
        test_driver.testname = testname + '_01'
        VistA1 = test_driver.connect_VistA(test_suite_details)
        pl = PLActions(VistA1, user='fakedoc1', code='1Doc!@#$')
        pl.signon()
        pl.addcsv(ssn='323678904', pfile='./Functional/dataFiles/NISTinpatientdata0.csv')
        pl.verplist(ssn='323678904', vlist=['Essential Hypertension',
                                            'Chronic airway obstruction',
                                            'Acute myocardial',
                                            'Congestive Heart Failure'])
        pl.signoff()

        test_driver.testname = testname + '_02'
        VistA2 = test_driver.connect_VistA(test_suite_details)
        oentry = ORActions(VistA2)
        oentry.signon()
        oentry.verproblems(ssn='323678904', vlist=['Essential Hypertension',
                                            'Chronic airway obstruction',
                                            'Acute myocardial',
                                            'Congestive Heart Failure'])
        oentry.signoff()
        test_driver.testname = testname + '_03'
        VistA3 = test_driver.connect_VistA(test_suite_details)
        pl = PLActions(VistA3, user='fakedoc1', code='1Doc!@#$')
        pl.signon()
        for i in range(4):
            pl.rem('323678904')
        pl.signoff()

        test_driver.post_test_run(test_suite_details)
    except TestHelper.TestError, e:
        test_driver.exception_handling(test_suite_details, e)
    else:
        test_driver.try_else_handling(test_suite_details)
    finally:
        test_driver.finally_handling(test_suite_details)

def pl_test010(test_suite_details):
    '''This tests adds problems to the Problem List and then removes them. '''
    testname = sys._getframe().f_code.co_name
    test_driver = TestHelper.TestDriver(testname)

    test_driver.pre_test_run(test_suite_details)
    try:
        VistA1 = test_driver.connect_VistA(test_suite_details)
        pl = PLActions(VistA1, user='fakedoc1', code='1Doc!@#$')
        pl.signon()
        pl.addcsv(ssn='323123456', pfile='./Functional/dataFiles/probdata0.csv')
        pl.verplist(ssn='323123456', vlist=['drug abuse', 'Arterial embolism'])
        pl.rem(ssn='323123456')
        pl.rem(ssn='323123456')
        pl.checkempty(ssn='323123456')
        pl.signoff()

        test_driver.post_test_run(test_suite_details)
    except TestHelper.TestError, e:
        test_driver.exception_handling(test_suite_details, e)