#
#  Copyright (C) 2002-2006  greg Landrum and Rational Discovery LLC
#
#   @@ All Rights Reserved @@
#  This file is part of the RDKit.
#  The contents are covered by the terms of the BSD license
#  which is included in the file license.txt, found at the root
#  of the RDKit source tree.
#
"""
unit testing code for calculations in rdkit.Chem.MolSurf
"""


from collections import namedtuple
import os.path
import unittest

from rdkit import Chem
from rdkit import RDConfig
from rdkit.Chem import MolSurf

doLong = False
TestData = namedtuple('TestData', 'lineNo,smiles,mol,expected')


class TestCase(unittest.TestCase):
  dataNCI200 = os.path.join(RDConfig.RDDataDir, 'NCI', 'first_200.tpsa.csv')
  dataNCI5000 = os.path.join(RDConfig.RDCodeDir, 'Chem', 'test_data', 'NCI_5K_TPSA.csv')
  dataTPSAregr = os.path.join(RDConfig.RDCodeDir, 'Chem', 'test_data', 'tpsa_regr.csv')

  @classmethod
  def readNCI_200(cls):
    for data in readPSAtestData(cls.dataNCI200):
      yield data

  @classmethod
  def readNCI_5000(cls):
    for data in readPSAtestData(cls.dataNCI5000):
      yield data

  @classmethod
  def readTPSAregres(cls):
    for data in readPSAtestData(cls.dataTPSAregr):
      yield data

  def testTPSAShort(self):
    for data in self.readNCI_200():
      calc = MolSurf.TPSA(data.mol)
      self.assertAlmostEqual(
        calc, data.expected, delta=1e-4,
        msg='bad TPSA for SMILES {0.smiles} ({1:.2f} != {0.expected:.2f})'.format(data, calc))

  def testTPSALong(self):
    if not doLong:
      raise unittest.SkipTest('long test')
    for data in self.readNCI_5000():
      try:
        calc = MolSurf.TPSA(data.mol)
      except Exception:
        raise AssertionError(
          'Line {0.lineNo}: TPSA Calculation failed for SMILES {0.smiles}'.format(data))
      self.assertAlmostEqual(
        calc, data.expected, delta=1e-4,
        msg='bad TPSA for SMILES {0.smiles} ({1:.2f} != {0.expected:.2f})'.format(data, calc))

  def testTPSALongNCI(self):
    if not doLong:
      raise unittest.SkipTest('long test')
    for data in self.readTPSAregres():
      try:
        calc = MolSurf.TPSA(data.mol)
      except Exception:
        raise AssertionError(
          'Line {0.lineNo}: TPSA Calculation failed for SMILES {0.smiles}'.format(data))