source: mauRepo/MolesManager/trunk/src/libs/migration/tests/moles3epbmothods.py @ 8205

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/libs/migration/tests/moles3epbmothods.py@8205
Revision 8205, 3.4 KB checked in by mnagni, 9 years ago (diff)

Complete - # 22416: CEDA MOLES3 - search function - by title keyword
 http://team.ceda.ac.uk/trac/ceda/ticket/22416

Line 
1'''
2Created on 8 Mar 2012
3
4@author: mnagni
5'''
6import unittest
7from testconfig import MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT
8from MolesManager.moles3epb import Moles3EPB
9from libs.commons_db import DbManager
10from unittest import TestCase
11import logging
12from logging import StreamHandler
13from test_utils import createObservationCollection, createObservation
14from sqlalchemy.dialects import postgres
15
16
17class Moles3EPBTest(TestCase):
18
19    def setUp(self):   
20        Moles3EPBTest.molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT)
21        Moles3EPB.overrrideDBManager(Moles3EPBTest.molesDB)
22        self._initSearchIndexes(Moles3EPBTest.molesDB)       
23        self.logging = logging.getLogger('Moles3EPBTest')
24        self.logging.addHandler(StreamHandler())
25        self.logging.setLevel(logging.DEBUG)
26
27    def tearDown(self):
28        pass
29        #self._dropAllTables()
30
31    def runTest(self):
32        self.checkExtractCollectionIdentifierByTitle()   
33        self.checkExtractObservationByTitleKeywords() 
34
35    def checkExtractCollectionIdentifierByTitle(self):
36        session = Moles3EPB.getNewMolesSession();
37        observationCollection = createObservationCollection()
38       
39        self.logging.info('Stores an new CEDA_ObservationCollection')
40        Moles3EPB.addCedaObject(observationCollection, session)
41        session.commit()
42
43        my_identifier = Moles3EPB.extractCollectionIdentifierByTitle('test_title', session)           
44        self.assertTrue(my_identifier.count() > 0, "Cannot find MD_Identifier")
45        my_identifier = Moles3EPB.extractCollectionIdentifierByTitle('fake_title', session)       
46        self.assertTrue(my_identifier.count() == 0, "Cannot find MD_Identifier")           
47       
48        self.logging.info('Deletes it')
49        session.delete(observationCollection)
50        session.commit()
51
52    def checkExtractObservationByTitleKeywords(self):
53        session = Moles3EPB.getNewMolesSession();
54        observation = createObservation()
55       
56        self.logging.info('Stores an new CEDA_Observation')
57        Moles3EPB.addCedaObject(observation, session)
58        session.commit()
59       
60        obs = Moles3EPB.extractObservationByTitleKeywords('test_code', session)
61        self.assertTrue(obs.count() > 1, "Cannot find a CEDA_Observation")
62        obs = Moles3EPB.extractObservationByTitleKeywords('dummy_code', session)
63        self.assertTrue(obs.count() == 0, "Should NOT find a CEDA_Observation")       
64
65    def _initSearchIndexes(self, db_manager):
66        #To Be Done - CHECK IF THE COLUMN ALREADY EXISTS!
67        # We don't want sqlalchemy to know about this column so we add it externally.
68        try:
69            db_manager._engine.execute("alter table md_identifier add column code_search_vector tsvector")                 
70
71            # This indexes the tsvector column
72            db_manager._engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)")
73           
74            # This sets up the trigger that keeps the tsvector column up to date.
75            db_manager._engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \
76                for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', 'code')")           
77        except Exception as e:
78            pass
79
80
81
82if __name__ == "__main__":
83    #import sys;sys.argv = ['', 'Test.testName']
84    unittest.main()
Note: See TracBrowser for help on using the repository browser.