""" Unit tests for self.jssanimal.py - the Jacob Sheep library. """

import os
import sys
import unittest


# Add the test directory to pythonpath
pkg_dir = os.path.dirname(os.path.abspath('./'))
sys.path.append(pkg_dir)

import cherrypy

from app_jss import jssanimal, jssflock, jssmember
from lib import genlib


class ParseAnimalID(unittest.TestCase):
    """ Test self.jssanimal.parse_id.
    
    Test that the (large number) of possible variations of sheep 
    id are correctly parsed and 'corrected'.
    
    """
    def setUp(self):
        uriconf = {}
        cfgfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../app_jss/jss.cfg')
        cherrypy._cpconfig.merge(uriconf, cfgfile)
        
        # Separate the bspd configuration from the URI configuration.
        appconf = uriconf.pop('app_conf')
        appconf['approot'] = genlib.get_approot(appconf['bspd_root'])
        appconf['version'] = genlib.get_version()
        self.jssanimal = jssanimal.JSSSheep(appconf)
        
    def testExactlyOneArgument(self):
        self.assertRaises(TypeError, self.jssanimal.parse_id)
        self.assertRaises(TypeError, self.jssanimal.parse_id, 'J10192', 'J5424')
    
    def testMustBeString(self):
        self.assertRaises((AttributeError, TypeError), self.jssanimal.parse_id, 1)
        
    def testMalformed(self):
        self.assertRaises(genlib.ParseError, self.jssanimal.parse_id, '@CL123@-12345')
        
    def testJSSRetag(self):
        self.assertRaises(genlib.ParseError, self.jssanimal.parse_id, 'JSS-J12345')
        self.assertRaises(genlib.ParseError, self.jssanimal.parse_id, 'JSS-R12345')
        for arg in ('1234', '23'):
            self.assertEqual(self.jssanimal.parse_id('%s-%s' % ('JSS', arg)), 
                ('', {'idmode': self.jssanimal.ID_MODE_TAG, 'idtype': self.jssanimal.ID_TYPE_JSS_RETAG, 'leader': 'JSS', 'trailer': arg}))

        
    def testJSSPrefix(self):
        for arg in ('1J', '01J', '001J', '0001J'):
            self.assertEqual(self.jssanimal.parse_id(arg),
                ('', {'idmode': self.jssanimal.ID_MODE_PREFIX, 'idtype': self.jssanimal.ID_TYPE_JSS, 'leader': '0001J', 'trailer': ''}))
        for arg in ('22J', '022J', '0022J'):
            self.assertEqual(self.jssanimal.parse_id(arg),
                ('', {'idmode': self.jssanimal.ID_MODE_PREFIX, 'idtype': self.jssanimal.ID_TYPE_JSS, 'leader': '0022J', 'trailer': ''}))
        for arg in ('333J', '0333J'):
            self.assertEqual(self.jssanimal.parse_id(arg),
                ('', {'idmode': self.jssanimal.ID_MODE_PREFIX, 'idtype': self.jssanimal.ID_TYPE_JSS, 'leader': '0333J', 'trailer': ''}))
        self.assertEqual(self.jssanimal.parse_id('4444J'),
            ('', {'idmode': self.jssanimal.ID_MODE_PREFIX, 'idtype': self.jssanimal.ID_TYPE_JSS, 'leader': '4444J', 'trailer': ''}))

    def testEUPrefix(self):
        for arg in ('UK123456', 'FR123456'):
            self.assertEqual(self.jssanimal.parse_id(arg),
                ('', {'idmode': self.jssanimal.ID_MODE_PREFIX, 'idtype': self.jssanimal.ID_TYPE_EU, 'leader': arg, 'trailer': ''}))

        self.assertRaises(genlib.ParseError, self.jssanimal.parse_id, 'PB1234567')
        
    def testNoSeparator(self):
        self.assertRaises(genlib.ParseError, self.jssanimal.parse_id, 'CL0002J1234')
        

    def testJSSTag(self):
        prefix = '1189J'
        for tag in ('4', '04', '004', '0004', '00004', '000004'):
            for sep in (' ', '-', '+', ''):
                id = '%s%s%s' % (prefix, sep, tag)
                self.assertEqual(self.jssanimal.parse_id(id),
                ('', {'idmode': self.jssanimal.ID_MODE_TAG, 'idtype': self.jssanimal.ID_TYPE_JSS, 'leader': '1189J', 'trailer': tag}))
            
    def testEUTag(self):
        self.assertRaises(genlib.ParseError, self.jssanimal.parse_id, 'UK123456-Z12345')
        self.assertRaises(genlib.ParseError, self.jssanimal.parse_id, 'UK123456-1234567')
        self.assertEqual(self.jssanimal.parse_id('UK456789-00032'),
                ('', {'idmode': self.jssanimal.ID_MODE_TAG, 'idtype': self.jssanimal.ID_TYPE_EU, 'leader': 'UK456789', 'trailer': '00032'}))
        self.assertEqual(self.jssanimal.parse_id('UK456789-33'),
                ('', {'idmode': self.jssanimal.ID_MODE_TAG, 'idtype': self.jssanimal.ID_TYPE_EU, 'leader': 'UK456789', 'trailer': '33'}))

        
if __name__ == '__main__':
    unittest.main()