#!/usr/bin/python
# vim: et :

import logging
import unittest
import sys
sys.path.append('../napd/')

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
log_format = "%(levelname)-8s %(message)s"

import nap


class NapTest(unittest.TestCase):
    """ Tests the schema features of NAP
    """

    logger = logging.getLogger()
    nap = nap.Nap()

    def test_add_schema(self):
        """ Add a schema and check it's there using list functions
        """
        schema_attrs = {
                'name': 'test-schema-wrong',
                'description': 'A simple test schema with incorrect name!'
                }
        schema_id = self.nap.add_schema(schema_attrs)
        schema = self.nap.list_schema({ 'id': schema_id })
        self.assertEqual(schema[0]['id'], schema_id, 'Add operations returned id differ from listed id')
        self.assertEqual(schema[0]['name'], schema_attrs['name'], 'Added name differ from listed name')
        self.assertEqual(schema[0]['description'], schema_attrs['description'], 'Added description differ from listed description')



    def test_dupe_check(self):
        """ Check so we can't create duplicate schemas
        """
        schema_attrs = {
                'name': 'test-schema-wrong',
                'description': 'A simple test schema with incorrect name!'
                }
        # TODO: this should raise a better exception, something like non-unique or duplicate
        self.assertRaises(nap.NapError, self.nap.add_schema, schema_attrs)



    def test_modify_schema(self):
        """ Modify schema
        """
        spec = { 'name': 'test-schema-wrong' }
        attrs = {
                'name': 'test-schema',
                'description': 'A simple test schema with correct name!'
                }
        # FIXME: edit_schema is actually broken!?
        #self.nap.edit_schema(spec, attrs)



    def test_pool(self):
        """ Simple test of address-pool functions

            1. Add address-schema named 'global'
            1. Add address-pool named 'test-pool' with default_type of
               reservation
            2. Verify 'test-pool' exists and default_type is reservation
            3. Modify 'test-pool' default_type to assignment
            4. Remove 'test-pool'
            5. Verify 'test-pool' does not exist
        """
        pool_attrs = {
                'name': 'test-pool',
                'schema': 'test-schema',
                'description': 'A test pool!'
                }
#        self.nap.add_pool(pool_attrs)

    def test_add_prefix(self):
        """ Simply test of adding a prefix and verifying it is there

            Will try to add prefix 192.0.2.0/24
        """
#        nap.add_prefix()
        self.assertEqual(1, 1)





def clean_db():
    """ Better start from a clean slate!
    """
    # local nap object to avoid fscking up something
    n = nap.Nap()
    n._execute("DELETE FROM ip_net_plan")
    n._execute("DELETE FROM ip_net_pool")
    n._execute("DELETE FROM ip_net_schema")


def main():
    clean_db()
    if sys.version_info >= (2,7):
        unittest.main(verbosity=2)
    else:
        unittest.main()




if __name__ == '__main__':
    main()

@@ -460,39 +460,32 @@
     %22%22%22%0A        
-schema_
 attrs = %7B%0A      
@@ -470,32 +470,32 @@
       attrs = %7B%0A
+
                 
@@ -656,23 +656,16 @@
 _schema(
-schema_
 attrs)%0A 
@@ -867,31 +867,24 @@
 0%5D%5B'name'%5D, 
-schema_
 attrs%5B'name'
@@ -975,23 +975,16 @@
 tion'%5D, 
-schema_
 attrs%5B'd
@@ -1763,17 +1763,17 @@
     
-# FIXME: 
+self.nap.
 edit
@@ -1783,71 +1783,487 @@
 hema
- is actually broken!?%0A        #self.nap.edit_schema(spec, attrs
+(spec, attrs)%0A        # check that search for old record doesn't return anything%0A        schema = self.nap.list_schema(spec)%0A        self.assertEqual(schema, %5B%5D, 'Old entry still exists')%0A        schema = self.nap.list_schema(%7B 'name': 'test-schema' %7D)%0A        self.assertEqual(schema%5B0%5D%5B'name'%5D, attrs%5B'name'%5D, 'Modified name differ from listed name')%0A        self.assertEqual(schema%5B0%5D%5B'description'%5D, attrs%5B'description'%5D, 'Modified description differ from listed description'
 )%0A%0A%0A

Add test for modify_schema