def test_json_waveclient_deserialization(self):
        '''
        '''
        import psysmon.core.waveclient

        packages_path = os.path.dirname(os.path.abspath(__file__))
        packages_path = os.path.join(packages_path, 'waveclient_packages')
        psybase = test_util.create_psybase(package_directory=[
            packages_path,
        ])
        project = test_util.create_dbtest_project(psybase)
        project.createDatabaseStructure(psybase.packageMgr.packages)

        # Set the maxDiff attribute to None to enable long output of
        # non-equal strings tested with assertMultiLineEqual.
        self.maxDiff = None

        # Set the createTime of the project to a known value.
        project.createTime = UTCDateTime('2013-01-01T00:00:00')

        # Add a waveclient to the project.
        waveclient = psysmon.core.waveclient.PsysmonDbWaveClient(
            'db client', project)
        project.addWaveClient(waveclient)
        project.defaultWaveclient = 'db client'

        encoder = util.ProjectFileEncoder()
        decoder = util.ProjectFileDecoder()
        json_project = encoder.encode(project)
        project_obj = decoder.decode(json_project)

        # TODO: Test the project_obj for validity.
        print project_obj.waveclient['db client'].mode

        psybase.stop_project_server()
        base_dir = project.base_dir
        test_util.drop_project_database_tables(project)
        shutil.rmtree(base_dir)
Exemplo n.º 2
0
    def test_json_serialization(self):
        '''
        '''
        # Set the maxDiff attribute to None to enable long output of 
        # non-equal strings tested with assertMultiLineEqual.
        self.maxDiff = None

        # Set the createTime of the project to a known value.
        self.db_project.createTime = UTCDateTime('2013-01-01T00:00:00')

        encoder = util.ProjectFileEncoder()

        # Test empty project.
        expected_result = '''{
    "__baseclass__": [], 
    "__class__": "Project", 
    "__module__": "psysmon.core.project", 
    "createTime": {
        "__baseclass__": [
            "object"
        ], 
        "__class__": "UTCDateTime", 
        "__module__": "obspy.core.utcdatetime", 
        "utcdatetime": "2013-01-01T00:00:00"
    }, 
    "dbDialect": "mysql", 
    "dbDriver": null, 
    "dbHost": "localhost", 
    "dbName": "psysmon_unit_test", 
    "db_version": {}, 
    "defaultWaveclient": null, 
    "name": "Unit Test", 
    "pkg_version": {
        "test_package_1": "0.1.1"
    }, 
    "scnlDataSources": {}, 
    "user": [
        {
            "__baseclass__": [], 
            "__class__": "User", 
            "__module__": "psysmon.core.project", 
            "activeCollection": null, 
            "agency_name": "University of Test", 
            "agency_uri": "at.uot", 
            "author_name": "Stefan Test", 
            "author_uri": "stest", 
            "collection": {}, 
            "mode": "admin", 
            "name": "unit_test"
        }
    ], 
    "waveclient": {}
}'''

        self.assertMultiLineEqual(encoder.encode(self.db_project), expected_result)

        # Test project with empty collection
        self.db_project.createDirectoryStructure()
        self.db_project.addCollection('Test Collection')
        expected_result = '''{
    "__baseclass__": [], 
    "__class__": "Project", 
    "__module__": "psysmon.core.project", 
    "createTime": {
        "__baseclass__": [
            "object"
        ], 
        "__class__": "UTCDateTime", 
        "__module__": "obspy.core.utcdatetime", 
        "utcdatetime": "2013-01-01T00:00:00"
    }, 
    "dbDialect": "mysql", 
    "dbDriver": null, 
    "dbHost": "localhost", 
    "dbName": "psysmon_unit_test", 
    "db_version": {}, 
    "defaultWaveclient": null, 
    "name": "Unit Test", 
    "pkg_version": {
        "test_package_1": "0.1.1"
    }, 
    "scnlDataSources": {}, 
    "user": [
        {
            "__baseclass__": [], 
            "__class__": "User", 
            "__module__": "psysmon.core.project", 
            "activeCollection": "Test Collection", 
            "agency_name": "University of Test", 
            "agency_uri": "at.uot", 
            "author_name": "Stefan Test", 
            "author_uri": "stest", 
            "collection": {
                "Test Collection": {
                    "__baseclass__": [], 
                    "__class__": "Collection", 
                    "__module__": "psysmon.core.base", 
                    "name": "Test Collection", 
                    "nodes": []
                }
            }, 
            "mode": "admin", 
            "name": "unit_test"
        }
    ], 
    "waveclient": {}
}'''
        self.assertMultiLineEqual(encoder.encode(self.db_project), expected_result)


        # Test project with a collection with one collection node.
        node_template = self.psybase.packageMgr.getCollectionNodeTemplate('json plain testnode')
        self.db_project.addNode2Collection(node_template)
        expected_result = '''{
    "__baseclass__": [], 
    "__class__": "Project", 
    "__module__": "psysmon.core.project", 
    "createTime": {
        "__baseclass__": [
            "object"
        ], 
        "__class__": "UTCDateTime", 
        "__module__": "obspy.core.utcdatetime", 
        "utcdatetime": "2013-01-01T00:00:00"
    }, 
    "dbDialect": "mysql", 
    "dbDriver": null, 
    "dbHost": "localhost", 
    "dbName": "psysmon_unit_test", 
    "db_version": {}, 
    "defaultWaveclient": null, 
    "name": "Unit Test", 
    "pkg_version": {
        "test_package_1": "0.1.1"
    }, 
    "scnlDataSources": {}, 
    "user": [
        {
            "__baseclass__": [], 
            "__class__": "User", 
            "__module__": "psysmon.core.project", 
            "activeCollection": "Test Collection", 
            "agency_name": "University of Test", 
            "agency_uri": "at.uot", 
            "author_name": "Stefan Test", 
            "author_uri": "stest", 
            "collection": {
                "Test Collection": {
                    "__baseclass__": [], 
                    "__class__": "Collection", 
                    "__module__": "psysmon.core.base", 
                    "name": "Test Collection", 
                    "nodes": [
                        {
                            "__baseclass__": [
                                "CollectionNode"
                            ], 
                            "__class__": "JsonPlainTestNode", 
                            "__module__": "test_package.test_node", 
                            "enabled": true, 
                            "pref_manager": {
                                "__baseclass__": [], 
                                "__class__": "PreferencesManager", 
                                "__module__": "psysmon.core.preferences_manager", 
                                "pages": {
                                    "preferences": []
                                }
                            }, 
                            "provides": null, 
                            "requires": null
                        }
                    ]
                }
            }, 
            "mode": "admin", 
            "name": "unit_test"
        }
    ], 
    "waveclient": {}
}'''
        self.assertMultiLineEqual(encoder.encode(self.db_project), expected_result)

        # Test project with a collection with two collection nodes 
        # and one of them containing preferences in the pref_manager.
        node_template = self.psybase.packageMgr.getCollectionNodeTemplate('json preferences testnode')
        self.db_project.addNode2Collection(node_template)
        expected_result = '''{
    "__baseclass__": [], 
    "__class__": "Project", 
    "__module__": "psysmon.core.project", 
    "createTime": {
        "__baseclass__": [
            "object"
        ], 
        "__class__": "UTCDateTime", 
        "__module__": "obspy.core.utcdatetime", 
        "utcdatetime": "2013-01-01T00:00:00"
    }, 
    "dbDialect": "mysql", 
    "dbDriver": null, 
    "dbHost": "localhost", 
    "dbName": "psysmon_unit_test", 
    "db_version": {}, 
    "defaultWaveclient": null, 
    "name": "Unit Test", 
    "pkg_version": {
        "test_package_1": "0.1.1"
    }, 
    "scnlDataSources": {}, 
    "user": [
        {
            "__baseclass__": [], 
            "__class__": "User", 
            "__module__": "psysmon.core.project", 
            "activeCollection": "Test Collection", 
            "agency_name": "University of Test", 
            "agency_uri": "at.uot", 
            "author_name": "Stefan Test", 
            "author_uri": "stest", 
            "collection": {
                "Test Collection": {
                    "__baseclass__": [], 
                    "__class__": "Collection", 
                    "__module__": "psysmon.core.base", 
                    "name": "Test Collection", 
                    "nodes": [
                        {
                            "__baseclass__": [
                                "CollectionNode"
                            ], 
                            "__class__": "JsonPlainTestNode", 
                            "__module__": "test_package.test_node", 
                            "enabled": true, 
                            "pref_manager": {
                                "__baseclass__": [], 
                                "__class__": "PreferencesManager", 
                                "__module__": "psysmon.core.preferences_manager", 
                                "pages": {
                                    "preferences": []
                                }
                            }, 
                            "provides": null, 
                            "requires": null
                        }, 
                        {
                            "__baseclass__": [
                                "CollectionNode"
                            ], 
                            "__class__": "JsonPreferencesTestNode", 
                            "__module__": "test_package.test_node", 
                            "enabled": true, 
                            "pref_manager": {
                                "__baseclass__": [], 
                                "__class__": "PreferencesManager", 
                                "__module__": "psysmon.core.preferences_manager", 
                                "pages": {
                                    "preferences": [
                                        {
                                            "__baseclass__": [
                                                "PreferenceItem"
                                            ], 
                                            "__class__": "TextEditPrefItem", 
                                            "__module__": "psysmon.core.preferences_manager", 
                                            "default": "test filter", 
                                            "group": null, 
                                            "label": "filter name", 
                                            "limit": null, 
                                            "name": "filter_name", 
                                            "value": "test filter"
                                        }, 
                                        {
                                            "__baseclass__": [
                                                "PreferenceItem"
                                            ], 
                                            "__class__": "DirBrowsePrefItem", 
                                            "__module__": "psysmon.core.preferences_manager", 
                                            "default": "", 
                                            "group": null, 
                                            "label": "browse", 
                                            "limit": null, 
                                            "name": "directory_browse", 
                                            "start_directory": "/home", 
                                            "value": ""
                                        }, 
                                        {
                                            "__baseclass__": [
                                                "PreferenceItem"
                                            ], 
                                            "__class__": "FloatSpinPrefItem", 
                                            "__module__": "psysmon.core.preferences_manager", 
                                            "default": "4.5", 
                                            "digits": 3, 
                                            "group": null, 
                                            "increment": 0.1, 
                                            "label": "filter cutoff", 
                                            "limit": null, 
                                            "name": "filter_cutoff", 
                                            "value": "4.5"
                                        }
                                    ]
                                }
                            }, 
                            "provides": null, 
                            "requires": null
                        }
                    ]
                }
            }, 
            "mode": "admin", 
            "name": "unit_test"
        }
    ], 
    "waveclient": {}
}'''
        self.assertMultiLineEqual(encoder.encode(self.db_project), expected_result)
Exemplo n.º 3
0
 def test_default(self):
     '''
     '''
     encoder = util.ProjectFileEncoder()
     d = encoder.default(self.db_project)
     print d
Exemplo n.º 4
0
    def test_json_waveclient_serialization(self):
        '''
        '''
        import psysmon.core.waveclient

        packages_path = os.path.dirname(os.path.abspath(__file__))
        packages_path = os.path.join(packages_path, 'waveclient_packages')
        psybase = test_util.create_psybase(package_directory = [packages_path, ])
        project = test_util.create_dbtest_project(psybase)
        project.createDatabaseStructure(psybase.packageMgr.packages)

        # Set the maxDiff attribute to None to enable long output of 
        # non-equal strings tested with assertMultiLineEqual.
        self.maxDiff = None

        # Set the createTime of the project to a known value.
        project.createTime = UTCDateTime('2013-01-01T00:00:00')

        # Add a waveclient to the project.
        waveclient = psysmon.core.waveclient.PsysmonDbWaveClient('db client', project)
        project.addWaveClient(waveclient)
        project.defaultWaveclient = 'db client'

        encoder = util.ProjectFileEncoder()

        # Test empty project.
        expected_result = '''{
    "__baseclass__": [], 
    "__class__": "Project", 
    "__module__": "psysmon.core.project", 
    "createTime": {
        "__baseclass__": [
            "object"
        ], 
        "__class__": "UTCDateTime", 
        "__module__": "obspy.core.utcdatetime", 
        "utcdatetime": "2013-01-01T00:00:00"
    }, 
    "dbDialect": "mysql", 
    "dbDriver": null, 
    "dbHost": "localhost", 
    "dbName": "psysmon_unit_test", 
    "db_version": {
        "geometry": "0.1.1", 
        "obspyImportWaveform": "0.1.1"
    }, 
    "defaultWaveclient": "db client", 
    "name": "Unit Test", 
    "pkg_version": {
        "geometry": "0.1.1", 
        "obspyImportWaveform": "0.1.1"
    }, 
    "scnlDataSources": {}, 
    "user": [
        {
            "__baseclass__": [], 
            "__class__": "User", 
            "__module__": "psysmon.core.project", 
            "activeCollection": null, 
            "agency_name": "University of Test", 
            "agency_uri": "at.uot", 
            "author_name": "Stefan Test", 
            "author_uri": "stest", 
            "collection": {}, 
            "mode": "admin", 
            "name": "unit_test"
        }
    ], 
    "waveclient": {
        "db client": {
            "__baseclass__": [
                "WaveClient"
            ], 
            "__class__": "PsysmonDbWaveClient", 
            "__module__": "psysmon.core.waveclient", 
            "name": "db client", 
            "options": {}, 
            "stock_window": 3600
        }
    }
}'''

        try:
            self.assertMultiLineEqual(encoder.encode(project), expected_result)
        finally:
            base_dir = project.base_dir
            psybase.stop_project_server()
            test_util.drop_project_database_tables(project)
            shutil.rmtree(base_dir)
    def test_json_deserialization(self):
        '''
        '''
        # Set the maxDiff attribute to None to enable long output of
        # non-equal strings tested with assertMultiLineEqual.
        self.maxDiff = None

        # Set the createTime of the project to a known value.
        self.db_project.createTime = UTCDateTime('2013-01-01T00:00:00')

        # Add an empty collection.
        self.db_project.createDirectoryStructure()
        self.db_project.addCollection('Test Collection')

        # Add a collection node to the collection.
        node_template = self.psybase.packageMgr.getCollectionNodeTemplate(
            'json plain testnode')
        self.db_project.addNode2Collection(node_template)

        # Add a collection node containing preferences.
        node_template = self.psybase.packageMgr.getCollectionNodeTemplate(
            'json preferences testnode')
        self.db_project.addNode2Collection(node_template)
        # Change a value in the filter_cutoff pref_item.
        self.db_project.activeUser.activeCollection.nodes[
            1].pref_manager.set_value('filter_cutoff', 10)

        encoder = util.ProjectFileEncoder()
        decoder = util.ProjectFileDecoder()
        json_project = encoder.encode(self.db_project)
        project_obj = decoder.decode(json_project)

        self.assertIsInstance(project_obj, psysmon.core.project.Project)
        self.assertEqual(project_obj.name, 'Unit Test')
        self.assertEqual(project_obj.dbHost, 'localhost')
        self.assertIsInstance(project_obj.activeUser,
                              psysmon.core.project.User)
        self.assertEqual(len(project_obj.user), 1)
        self.assertIsInstance(project_obj.user[0], psysmon.core.project.User)
        self.assertEqual(project_obj.activeUser.name, 'unit_test')
        self.assertEqual(project_obj.rid, 'smi:at.uot.stest/psysmon/unit_test')
        self.assertEqual(len(project_obj.activeUser.collection), 1)
        self.assertIsInstance(
            project_obj.activeUser.collection['Test Collection'],
            psysmon.core.base.Collection)
        self.assertEqual(project_obj.activeUser.activeCollection,
                         project_obj.activeUser.collection['Test Collection'])
        collection = project_obj.activeUser.collection['Test Collection']
        self.assertEqual(len(collection.nodes), 2)
        self.assertEqual(collection.nodes[0].__class__.__name__,
                         'JsonPlainTestNode')
        self.assertEqual(collection.nodes[1].__class__.__name__,
                         'JsonPreferencesTestNode')
        c_node = collection.nodes[0]
        self.assertIsInstance(
            c_node.pref_manager,
            psysmon.core.preferences_manager.PreferencesManager)
        self.assertEqual(len(c_node.pref_manager.pages), 1)
        self.assertEqual(len(c_node.pref_manager.pages['preferences']), 0)
        c_node = collection.nodes[1]
        self.assertIsInstance(
            c_node.pref_manager,
            psysmon.core.preferences_manager.PreferencesManager)
        self.assertEqual(len(c_node.pref_manager.pages), 1)
        self.assertEqual(len(c_node.pref_manager.pages['preferences']), 3)
        filter_item = c_node.pref_manager.get_item('filter_cutoff',
                                                   'preferences')[0]
        self.assertEqual(filter_item.value, 10)