#
# @file    TestSBMLConvertStrict.py
# @brief   SBMLConvert unit tests for strict conversion
#
# @author  Akiya Jouraku (Python conversion)
# @author  Sarah Keating
 
#
# $Id$
# $HeadURL$
#
# This test file was converted from src/sbml/test/TestSBMLConvertStrict.c
# with the help of conversion sciprt (ctest_converter.pl).
#
#<!---------------------------------------------------------------------------
# This file is part of libSBML.  Please visit http://sbml.org for more
# information about SBML, and the latest version of libSBML.
#
# Copyright 2005-2009 California Institute of Technology.
# Copyright 2002-2005 California Institute of Technology and
#                     Japan Science and Technology Corporation.
# 
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation.  A copy of the license agreement is provided
# in the file named "LICENSE.txt" included with this software distribution
# and also available online as http://sbml.org/software/libsbml/license.html
#--------------------------------------------------------------------------->*/
import sys
import unittest
import libsbml

class TestSBMLConvertStrict(unittest.TestCase):


  def test_SBMLConvertStrict_convertL1ParamRule(self):
    d = libsbml.SBMLDocument(1,2)
    m = d.createModel()
    c = m.createCompartment()
    c.setId( "c")
    p = m.createParameter()
    p.setId( "p")
    p1 = m.createParameter()
    p1.setId( "p1")
    math = libsbml.parseFormula("p")
    ar = m.createAssignmentRule()
    ar.setVariable( "p1")
    ar.setMath(math)
    ar.setUnits( "mole")
    success = d.setLevelAndVersion(2,1,True)
    self.assert_( success == 1 )
    self.assert_( d.getLevel() == 2 )
    self.assert_( d.getVersion() == 1 )
    r1 = d.getModel().getRule(0)
    self.assert_( r1.getUnits() == "" )
    d = None
    pass  

  def test_SBMLConvertStrict_convertNonStrictSBO(self):
    d = libsbml.SBMLDocument(2,4)
    m = d.createModel()
    c = m.createCompartment()
    c.setId( "c")
    c.setConstant(False)
    (c).setSBOTerm(64)
    success = d.setLevelAndVersion(2,3,True)
    self.assert_( success == 0 )
    self.assert_( d.getLevel() == 2 )
    self.assert_( d.getVersion() == 4 )
    success = d.setLevelAndVersion(2,2,True)
    self.assert_( success == 0 )
    self.assert_( d.getLevel() == 2 )
    self.assert_( d.getVersion() == 4 )
    success = d.setLevelAndVersion(2,1,True)
    self.assert_( success == 1 )
    self.assert_( d.getLevel() == 2 )
    self.assert_( d.getVersion() == 1 )
    c1 = d.getModel().getCompartment(0)
    self.assert_( (c1).getSBOTerm() == -1 )
    success = d.setLevelAndVersion(1,2,True)
    self.assert_( success == 1 )
    self.assert_( d.getLevel() == 1 )
    self.assert_( d.getVersion() == 2 )
    c2 = d.getModel().getCompartment(0)
    self.assert_( (c2).getSBOTerm() == -1 )
    d = None
    pass  

  def test_SBMLConvertStrict_convertNonStrictUnits(self):
    d = libsbml.SBMLDocument(2,4)
    m = d.createModel()
    c = m.createCompartment()
    c.setId( "c")
    c.setConstant(False)
    p = m.createParameter()
    p.setId( "p")
    p.setUnits( "mole")
    math = libsbml.parseFormula("p")
    ar = m.createAssignmentRule()
    ar.setVariable( "c")
    ar.setMath(math)
    success = d.setLevelAndVersion(2,1,True)
    self.assert_( success == 0 )
    self.assert_( d.getLevel() == 2 )
    self.assert_( d.getVersion() == 4 )
    success = d.setLevelAndVersion(2,2,True)
    self.assert_( success == 0 )
    self.assert_( d.getLevel() == 2 )
    self.assert_( d.getVersion() == 4 )
    success = d.setLevelAndVersion(2,3,True)
    self.assert_( success == 0 )
    self.assert_( d.getLevel() == 2 )
    self.assert_( d.getVersion() == 4 )
    success = d.setLevelAndVersion(1,2,True)
    self.assert_( success == 0 )
    self.assert_( d.getLevel() == 2 )
    self.assert_( d.getVersion() == 4 )
    d = None
    pass  

  def test_SBMLConvertStrict_convertSBO(self):
    d = libsbml.SBMLDocument(2,4)
    m = d.createModel()
    c = m.createCompartment()
    c.setId( "c")
    (c).setSBOTerm(240)
    success = d.setLevelAndVersion(2,3,True)
    self.assert_( success == 1 )
    self.assert_( d.getLevel() == 2 )
    self.assert_( d.getVersion() == 3 )
    success = d.setLevelAndVersion(2,2,True)
    self.assert_( success == 1 )
    self.assert_( d.getLevel() == 2 )
    self.assert_( d.getVersion() == 2 )
    c1 = d.getModel().getCompartment(0)
    self.assert_( (c1).getSBOTerm() == -1 )
    d = None
    pass  

  def test_SBMLConvertStrict_convertToL1(self):
    d = libsbml.SBMLDocument(2,4)
    m = d.createModel()
    (m).setMetaId( "_m")
    c = m.createCompartment()
    c.setId( "c")
    (c).setSBOTerm(240)
    s = m.createSpecies()
    s.setId( "s")
    s.setCompartment( "c")
    s.setHasOnlySubstanceUnits(True)
    success = d.setLevelAndVersion(1,2,True)
    self.assert_( success == 1 )
    self.assert_( d.getLevel() == 1 )
    self.assert_( d.getVersion() == 2 )
    m1 = d.getModel()
    self.assert_( (m1).getMetaId() == "" )
    c1 = m1.getCompartment(0)
    self.assert_( (c1).getSBOTerm() == -1 )
    s1 = m1.getSpecies(0)
    self.assert_( s1.getHasOnlySubstanceUnits() == False )
    d = None
    pass  

def suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestSBMLConvertStrict))

  return suite

if __name__ == "__main__":
  if unittest.TextTestRunner(verbosity=1).run(suite()).wasSuccessful() :
    sys.exit(0)
  else:
    sys.exit(1)
