File: test_Singleton.py

package info (click to toggle)
yapsy 1.12.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 888 kB
  • sloc: python: 2,684; makefile: 82
file content (135 lines) | stat: -rw-r--r-- 4,061 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t; python-indent: 4 -*-

from . import test_settings

import os 
import unittest

from yapsy.ConfigurablePluginManager import ConfigurablePluginManager
from yapsy.VersionedPluginManager import VersionedPluginManager
from yapsy.PluginManager import PluginManagerSingleton
from configparser import ConfigParser


"""
There can be only one series of tests for the singleton, guess why ...
"""

class ConfigSingletonTestsCase(unittest.TestCase):
	"""
	Test the correct loading of a simple plugin as well as basic
	commands, use the Singleton version of the ConfigurablePluginManager.
	"""
	
	CONFIG_FILE = test_settings.TEMP_CONFIG_FILE_NAME

	def setUp(self):
		"""
		init
		"""
		# create a config file
		self.config_file = self.CONFIG_FILE
		self.config_parser = ConfigParser()
		self.plugin_info = None

		# create the plugin manager
		PluginManagerSingleton.setBehaviour([ConfigurablePluginManager,VersionedPluginManager])
		pluginManager = PluginManagerSingleton.get()
		pluginManager.setPluginPlaces(directories_list=[os.path.dirname(os.path.abspath(__file__))])
		pluginManager.setPluginInfoExtension("yapsy-config-plugin")
		pluginManager.setConfigParser(self.config_parser,self.update_config)
		# load the plugins that may be found
		pluginManager.collectPlugins()

	def tearDown(self):
		"""
		When the test has been performed erase the temp file.
		"""
		if os.path.isfile(self.config_file):
			os.remove(self.config_file)


	def testConfigurationFileExistence(self):
		"""
		Test if the configuration file has been properly written.
		"""
		# activate the only loaded plugin
		self.plugin_activate()
		# get rid of the plugin manager and create a new one
		self.config_parser.read(self.config_file)
		self.assertTrue(self.config_parser.has_section("Plugin Management"))
		self.assertTrue(self.config_parser.has_option("Plugin Management", 
												   "default_plugins_to_load"))


	def testLoaded(self):
		"""
		Test if the correct plugin has been loaded.
		"""
		self.plugin_loading_check()
		
	def testActivationAndDeactivation(self):
		"""
		Test if the activation/deactivaion procedures work.
		"""
		self.plugin_activate()
		PluginManagerSingleton.get().deactivatePluginByName(self.plugin_info.name,
															self.plugin_info.category)
		self.assertTrue(not self.plugin_info.plugin_object.is_activated)

	def testPluginOptions(self):
		"""
		Test is the plugin can register and access options from the
		ConfigParser.
		"""
		self.plugin_activate()
		plugin = self.plugin_info.plugin_object
		plugin.choseTestOption("voila")
		self.assertTrue(plugin.checkTestOption())
		self.assertEqual(plugin.getTestOption(),"voila")


	#--- UTILITIES

	def plugin_loading_check(self):
		"""
		Test if the correct plugin has been loaded.
		"""
		if self.plugin_info is None:
			pluginManager = PluginManagerSingleton.get()
			# check nb of categories
			self.assertEqual(len(pluginManager.getCategories()),1)
			sole_category = pluginManager.getCategories()[0]
			# check the number of plugins
			self.assertEqual(len(pluginManager.getPluginsOfCategory(sole_category)),1)
			self.plugin_info = pluginManager.getPluginsOfCategory(sole_category)[0]
			# test that the name of the plugin has been correctly defined
			self.assertEqual(self.plugin_info.name,"Config Plugin")
			self.assertEqual(sole_category,self.plugin_info.category)
		else:
			self.assertTrue(True)
		
	def plugin_activate(self):
		"""
		Activate the plugin with basic checking
		"""
		self.plugin_loading_check()
		if not self.plugin_info.plugin_object.is_activated:
			PluginManagerSingleton.get().activatePluginByName(self.plugin_info.name,
															  self.plugin_info.category)
		self.assertTrue(self.plugin_info.plugin_object.is_activated)
		

	def update_config(self):
		"""
		Write the content of the ConfigParser in a file.
		"""
		cf = open(self.config_file,"a")
		self.config_parser.write(cf)
		cf.close()



suite = unittest.TestSuite([
		unittest.TestLoader().loadTestsFromTestCase(ConfigSingletonTestsCase),
		])