
import sys
sys.path.insert(0, '..')

import unittest

import search_xml

import test_util

###

class TestSearchResult ( unittest.TestCase ):

    def setUp ( self ):
        self.test_request = 'http://nova.infrae:82/rrr/service/search/unowned'
        self.res_1 = u'''<?xml version="1.0" encoding="utf-8"?>
<search-result xmlns="http://www.infrae.com/namespaces/search">
  <request>http://nova.infrae:82/rrr/service/search/unowned</request>
  <size>0</size>
  <resources>

  </resources>
</search-result>
'''.encode('utf-8')
        self.res_2 = u'''<?xml version="1.0" encoding="utf-8"?>
<search-result xmlns="http://www.infrae.com/namespaces/search">
  <request>http://nova.infrae:82/rrr/service/search/unowned</request>
  <size>1</size>
  <resources>

    <resource id="6b9cc5f3-339a-414c-96ee-414726a55687">
      <url>http://nova.infrae:82/rrr/repo/image/jpeg/000/8892-1.jpg</url>
      <mime-type>image/jpeg</mime-type>
      <size>35342</size>
      <lastmodified>2005-02-03T13:24:41</lastmodified>
    </resource>

  </resources>
</search-result>
'''
        return

    def tearDown ( self ):
        pass

    def test_01_create_01 ( self ):
        """Test creation of SearchResult instances.
        """
        res = search_xml.SearchResult(self.test_request)
        xml = res.as_string()
        self.assertEquals(self.res_1, xml)
        return

    def test_02_add_record_01 ( self ):
        res = search_xml.SearchResult(self.test_request)
        dt = test_util.parse_timestamp('2005-02-03T13:24:41Z')
        d = {'resource_id': '6b9cc5f3-339a-414c-96ee-414726a55687',
             'resource_url': 'http://nova.infrae:82/rrr/repo/image/jpeg/000/8892-1.jpg',
             'resource_type': 'image/jpeg',
             'resource_size': 35342,
             'resource_date': dt.isoformat(),
             }
        res.append_resource(d)
        xml = res.as_string()
        self.assertEquals(self.res_2, xml)
        return
#
###


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