2024-01-29 03:41:35 +03:00
# -*- coding: utf-8 -*-
#
# Copyright (c) 2024 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
Author : Adolfo Gómez , dkmaster at dkmon dot com
"""
import typing
from uds . core import environment
from uds . core . util . cache import Cache
from uds . core . util . storage import Storage
2024-01-29 04:52:27 +03:00
from . . utils . test import UDSTransactionTestCase
2024-01-29 03:41:35 +03:00
2024-01-29 04:52:27 +03:00
class TestEnvironment ( UDSTransactionTestCase ) :
def _check_environment (
self ,
env : environment . Environment ,
expected_key : ' str|None ' ,
is_persistent : bool ,
recreate_fnc : typing . Optional [ typing . Callable [ [ ] , environment . Environment ] ] = None ,
) - > None :
2024-01-29 03:41:35 +03:00
self . assertIsInstance ( env , environment . Environment )
self . assertIsInstance ( env . cache , Cache )
self . assertIsInstance ( env . storage , Storage )
2024-01-29 04:52:27 +03:00
self . assertIsInstance ( env . _id_generators , dict )
if expected_key is not None :
self . assertEqual ( env . key , expected_key )
env . storage . put ( ' test ' , ' test ' )
2024-03-15 01:20:44 +03:00
self . assertEqual ( env . storage . read ( ' test ' ) , ' test ' )
2024-01-29 04:52:27 +03:00
2024-10-25 21:40:04 +03:00
env . cache . put ( ' test ' , ' test ' )
2024-01-29 04:52:27 +03:00
self . assertEqual ( env . cache . get ( ' test ' ) , ' test ' )
# Recreate environment
env = environment . Environment ( env . key ) if not recreate_fnc else recreate_fnc ( )
2024-01-29 03:41:35 +03:00
self . assertIsInstance ( env , environment . Environment )
self . assertIsInstance ( env . cache , Cache )
self . assertIsInstance ( env . storage , Storage )
2024-01-29 04:52:27 +03:00
self . assertIsInstance ( env . _id_generators , dict )
if expected_key is not None :
self . assertEqual ( env . key , expected_key )
if is_persistent :
2024-03-15 01:20:44 +03:00
self . assertEqual ( env . storage . read ( ' test ' ) , ' test ' )
2024-01-29 04:52:27 +03:00
self . assertEqual ( env . cache . get ( ' test ' ) , ' test ' )
else :
2024-03-15 01:20:44 +03:00
self . assertEqual ( env . storage . read ( ' test ' ) , None )
2024-01-29 04:52:27 +03:00
self . assertEqual ( env . cache . get ( ' test ' ) , None )
def test_global_environment ( self ) - > None :
2024-02-18 18:48:20 +03:00
env = environment . Environment . common_environment ( )
2024-01-29 04:52:27 +03:00
self . _check_environment ( env , environment . COMMON_ENV , True )
def test_temporary_environment ( self ) - > None :
env = environment . Environment . testing_environment ( )
self . _check_environment ( env , environment . TEST_ENV , False , recreate_fnc = environment . Environment . testing_environment )
2024-01-29 03:41:35 +03:00
def test_table_record_environment ( self ) - > None :
2024-01-29 04:52:27 +03:00
env = environment . Environment . environment_for_table_record ( ' test_table ' )
self . _check_environment ( env , ' t-test_table- ' , True )
def test_table_record_environment_with_id ( self ) - > None :
env = environment . Environment . environment_for_table_record ( ' test_table ' , 123 )
self . _check_environment ( env , ' t-test_table-123 ' , True )
2024-01-29 03:41:35 +03:00
def test_environment_for_type ( self ) - > None :
2024-02-18 18:48:20 +03:00
env = environment . Environment . type_environment ( TestEnvironment )
self . _check_environment ( env , ' type- ' + str ( TestEnvironment ) , True )
2024-01-29 04:52:27 +03:00
def test_exclusive_temporary_environment ( self ) - > None :
unique_key : str = ' '
with environment . Environment . temporary_environment ( ) as env :
self . assertIsInstance ( env , environment . Environment )
self . assertIsInstance ( env . cache , Cache )
self . assertIsInstance ( env . storage , Storage )
self . assertIsInstance ( env . _id_generators , dict )
unique_key = env . key # store for later test
env . storage . put ( ' test ' , ' test ' )
2024-03-15 01:20:44 +03:00
self . assertEqual ( env . storage . read ( ' test ' ) , ' test ' )
2024-01-29 04:52:27 +03:00
2024-10-25 21:40:04 +03:00
env . cache . put ( ' test ' , ' test ' )
2024-01-29 04:52:27 +03:00
self . assertEqual ( env . cache . get ( ' test ' ) , ' test ' )
# Environment is cleared after exit, ensure it
env = environment . Environment ( unique_key )
with env as env :
self . assertIsInstance ( env , environment . Environment )
self . assertIsInstance ( env . cache , Cache )
self . assertIsInstance ( env . storage , Storage )
self . assertIsInstance ( env . _id_generators , dict )
self . assertEqual ( env . key , unique_key )
2024-03-15 01:20:44 +03:00
self . assertEqual ( env . storage . read ( ' test ' ) , None )
2024-01-29 04:52:27 +03:00
self . assertEqual ( env . cache . get ( ' test ' ) , None )