test_application.py
175 lines
| 5.8 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r3796 | """ | ||
Tests for IPython.config.application.Application | ||||
Authors: | ||||
* Brian Granger | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (C) 2008-2011 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r4977 | import logging | ||
Brian Granger
|
r3796 | from unittest import TestCase | ||
from IPython.config.configurable import Configurable | ||||
MinRK
|
r4977 | from IPython.config.loader import Config | ||
Brian Granger
|
r3796 | |||
from IPython.config.application import ( | ||||
Application | ||||
) | ||||
from IPython.utils.traitlets import ( | ||||
MinRK
|
r3861 | Bool, Unicode, Int, Float, List, Dict | ||
Brian Granger
|
r3796 | ) | ||
#----------------------------------------------------------------------------- | ||||
# Code | ||||
#----------------------------------------------------------------------------- | ||||
class Foo(Configurable): | ||||
MinRK
|
r3852 | i = Int(0, config=True, help="The integer i.") | ||
j = Int(1, config=True, help="The integer j.") | ||||
name = Unicode(u'Brian', config=True, help="First name.") | ||||
Brian Granger
|
r3796 | |||
class Bar(Configurable): | ||||
MinRK
|
r3957 | b = Int(0, config=True, help="The integer b.") | ||
MinRK
|
r3852 | enabled = Bool(True, config=True, help="Enable bar.") | ||
Brian Granger
|
r3796 | |||
class MyApp(Application): | ||||
Brian Granger
|
r3942 | name = Unicode(u'myapp') | ||
MinRK
|
r3861 | running = Bool(False, config=True, | ||
Brian Granger
|
r3796 | help="Is the app running?") | ||
classes = List([Bar, Foo]) | ||||
MinRK
|
r3861 | config_file = Unicode(u'', config=True, | ||
Brian Granger
|
r3796 | help="Load this config file") | ||
MinRK
|
r4214 | aliases = Dict({ | ||
'i' : 'Foo.i', | ||||
'j' : 'Foo.j', | ||||
'name' : 'Foo.name', | ||||
'enabled' : 'Bar.enabled', | ||||
MinRK
|
r4977 | 'log-level' : 'Application.log_level', | ||
MinRK
|
r4214 | }) | ||
MinRK
|
r3861 | |||
flags = Dict(dict(enable=({'Bar': {'enabled' : True}}, "Set Bar.enabled to True"), | ||||
MinRK
|
r4977 | disable=({'Bar': {'enabled' : False}}, "Set Bar.enabled to False"), | ||
crit=({'Application' : {'log_level' : logging.CRITICAL}}, | ||||
"set level=CRITICAL"), | ||||
)) | ||||
MinRK
|
r3852 | |||
Brian Granger
|
r3796 | def init_foo(self): | ||
self.foo = Foo(config=self.config) | ||||
def init_bar(self): | ||||
self.bar = Bar(config=self.config) | ||||
class TestApplication(TestCase): | ||||
def test_basic(self): | ||||
app = MyApp() | ||||
Brian Granger
|
r3942 | self.assertEquals(app.name, u'myapp') | ||
Brian Granger
|
r3796 | self.assertEquals(app.running, False) | ||
self.assertEquals(app.classes, [MyApp,Bar,Foo]) | ||||
self.assertEquals(app.config_file, u'') | ||||
def test_config(self): | ||||
app = MyApp() | ||||
MinRK
|
r4214 | app.parse_command_line(["--i=10","--Foo.j=10","--enabled=False","--log-level=50"]) | ||
Brian Granger
|
r3796 | config = app.config | ||
self.assertEquals(config.Foo.i, 10) | ||||
self.assertEquals(config.Foo.j, 10) | ||||
self.assertEquals(config.Bar.enabled, False) | ||||
MinRK
|
r3948 | self.assertEquals(config.MyApp.log_level,50) | ||
Brian Granger
|
r3796 | |||
def test_config_propagation(self): | ||||
app = MyApp() | ||||
MinRK
|
r4214 | app.parse_command_line(["--i=10","--Foo.j=10","--enabled=False","--log-level=50"]) | ||
Brian Granger
|
r3796 | app.init_foo() | ||
app.init_bar() | ||||
self.assertEquals(app.foo.i, 10) | ||||
self.assertEquals(app.foo.j, 10) | ||||
self.assertEquals(app.bar.enabled, False) | ||||
MinRK
|
r3957 | def test_flags(self): | ||
MinRK
|
r3852 | app = MyApp() | ||
MinRK
|
r4197 | app.parse_command_line(["--disable"]) | ||
MinRK
|
r3852 | app.init_bar() | ||
self.assertEquals(app.bar.enabled, False) | ||||
app.parse_command_line(["--enable"]) | ||||
app.init_bar() | ||||
self.assertEquals(app.bar.enabled, True) | ||||
MinRK
|
r3957 | def test_aliases(self): | ||
app = MyApp() | ||||
MinRK
|
r4197 | app.parse_command_line(["--i=5", "--j=10"]) | ||
MinRK
|
r3957 | app.init_foo() | ||
self.assertEquals(app.foo.i, 5) | ||||
app.init_foo() | ||||
self.assertEquals(app.foo.j, 10) | ||||
def test_flag_clobber(self): | ||||
"""test that setting flags doesn't clobber existing settings""" | ||||
app = MyApp() | ||||
MinRK
|
r4197 | app.parse_command_line(["--Bar.b=5", "--disable"]) | ||
MinRK
|
r3957 | app.init_bar() | ||
self.assertEquals(app.bar.enabled, False) | ||||
self.assertEquals(app.bar.b, 5) | ||||
MinRK
|
r4197 | app.parse_command_line(["--enable", "--Bar.b=10"]) | ||
MinRK
|
r3957 | app.init_bar() | ||
self.assertEquals(app.bar.enabled, True) | ||||
self.assertEquals(app.bar.b, 10) | ||||
MinRK
|
r4977 | def test_flatten_flags(self): | ||
cfg = Config() | ||||
cfg.MyApp.log_level = logging.WARN | ||||
app = MyApp() | ||||
app.update_config(cfg) | ||||
self.assertEquals(app.log_level, logging.WARN) | ||||
self.assertEquals(app.config.MyApp.log_level, logging.WARN) | ||||
app.initialize(["--crit"]) | ||||
self.assertEquals(app.log_level, logging.CRITICAL) | ||||
# this would be app.config.Application.log_level if it failed: | ||||
self.assertEquals(app.config.MyApp.log_level, logging.CRITICAL) | ||||
def test_flatten_aliases(self): | ||||
cfg = Config() | ||||
cfg.MyApp.log_level = logging.WARN | ||||
app = MyApp() | ||||
app.update_config(cfg) | ||||
self.assertEquals(app.log_level, logging.WARN) | ||||
self.assertEquals(app.config.MyApp.log_level, logging.WARN) | ||||
app.initialize(["--log-level", "CRITICAL"]) | ||||
self.assertEquals(app.log_level, logging.CRITICAL) | ||||
# this would be app.config.Application.log_level if it failed: | ||||
self.assertEquals(app.config.MyApp.log_level, "CRITICAL") | ||||
MinRK
|
r3958 | def test_extra_args(self): | ||
app = MyApp() | ||||
MinRK
|
r4197 | app.parse_command_line(["--Bar.b=5", 'extra', "--disable", 'args']) | ||
MinRK
|
r3958 | app.init_bar() | ||
MinRK
|
r4211 | self.assertEquals(app.bar.enabled, False) | ||
self.assertEquals(app.bar.b, 5) | ||||
self.assertEquals(app.extra_args, ['extra', 'args']) | ||||
app = MyApp() | ||||
app.parse_command_line(["--Bar.b=5", '--', 'extra', "--disable", 'args']) | ||||
app.init_bar() | ||||
MinRK
|
r4189 | self.assertEquals(app.bar.enabled, True) | ||
MinRK
|
r3958 | self.assertEquals(app.bar.b, 5) | ||
MinRK
|
r4211 | self.assertEquals(app.extra_args, ['extra', '--disable', 'args']) | ||
MinRK
|
r3958 | |||
MinRK
|
r3957 | |||