2015-03-10 01:43:23 +03:00
|
|
|
"""Config provider examples."""
|
|
|
|
|
2015-03-23 02:04:18 +03:00
|
|
|
from objects.catalog import AbstractCatalog
|
2015-03-10 01:43:23 +03:00
|
|
|
|
|
|
|
from objects.providers import Config
|
|
|
|
from objects.providers import NewInstance
|
|
|
|
|
2015-03-23 02:04:18 +03:00
|
|
|
from objects.injections import KwArg
|
2015-01-28 14:08:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ObjectA(object):
|
2015-03-10 01:43:23 +03:00
|
|
|
|
|
|
|
"""Example class ObjectA, that has dependencies on some setting values."""
|
|
|
|
|
|
|
|
def __init__(self, fee, price, timezone):
|
|
|
|
"""Initializer."""
|
|
|
|
self.fee = fee
|
|
|
|
self.price = price
|
|
|
|
self.timezone = timezone
|
2015-01-28 14:08:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Catalog(AbstractCatalog):
|
2015-03-10 01:43:23 +03:00
|
|
|
|
|
|
|
"""Catalog of objects providers."""
|
2015-01-28 14:08:54 +03:00
|
|
|
|
|
|
|
config = Config()
|
2015-03-10 01:43:23 +03:00
|
|
|
""":type: (objects.Config)"""
|
2015-01-28 14:08:54 +03:00
|
|
|
|
|
|
|
object_a = NewInstance(ObjectA,
|
2015-03-23 02:04:18 +03:00
|
|
|
KwArg('fee', config.FEE),
|
|
|
|
KwArg('price', config.PRICE),
|
|
|
|
KwArg('timezone', config.GLOBAL.TIMEZONE))
|
2015-03-10 01:43:23 +03:00
|
|
|
""":type: (objects.Provider) -> ObjectA"""
|
2015-01-28 14:08:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Setting config value and making some tests.
|
|
|
|
Catalog.config.update_from({
|
2015-03-10 01:43:23 +03:00
|
|
|
'FEE': 1.25,
|
|
|
|
'PRICE': 2.99,
|
2015-01-28 14:08:54 +03:00
|
|
|
'GLOBAL': {
|
2015-03-10 01:43:23 +03:00
|
|
|
'TIMEZONE': 'US/Eastern'
|
2015-01-28 14:08:54 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
object_a1 = Catalog.object_a()
|
|
|
|
|
2015-03-10 01:43:23 +03:00
|
|
|
assert object_a1.fee == 1.25
|
|
|
|
assert object_a1.price == 2.99
|
|
|
|
assert object_a1.timezone == 'US/Eastern'
|
2015-01-28 14:08:54 +03:00
|
|
|
|
|
|
|
# Changing config value one more time and making some tests.
|
|
|
|
Catalog.config.update_from({
|
2015-03-10 01:43:23 +03:00
|
|
|
'FEE': 5.25,
|
|
|
|
'PRICE': 19.99,
|
2015-01-28 14:08:54 +03:00
|
|
|
'GLOBAL': {
|
2015-03-10 01:43:23 +03:00
|
|
|
'TIMEZONE': 'US/Western'
|
2015-01-28 14:08:54 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
object_a2 = Catalog.object_a()
|
|
|
|
|
2015-03-10 01:43:23 +03:00
|
|
|
# New one ObjectA has new config values.
|
|
|
|
assert object_a2.fee == 5.25
|
|
|
|
assert object_a2.price == 19.99
|
|
|
|
assert object_a2.timezone == 'US/Western'
|
2015-01-28 14:08:54 +03:00
|
|
|
|
2015-03-10 01:43:23 +03:00
|
|
|
# And old one has old ones.
|
|
|
|
assert object_a1.fee == 1.25
|
|
|
|
assert object_a1.price == 2.99
|
|
|
|
assert object_a1.timezone == 'US/Eastern'
|