Best Python code snippet using playwright-python
test_settings.py
Source:test_settings.py
1import unittest2class TestSettingsConfiguratorMixin(unittest.TestCase):3 def _makeOne(self, *arg, **kw):4 from pyramid.config import Configurator5 config = Configurator(*arg, **kw)6 return config7 def test__set_settings_as_None(self):8 config = self._makeOne()9 settings = config._set_settings(None)10 self.assertTrue(settings)11 def test__set_settings_as_dictwithvalues(self):12 config = self._makeOne()13 settings = config._set_settings({'a':'1'})14 self.assertEqual(settings['a'], '1')15 def test_get_settings_nosettings(self):16 from pyramid.registry import Registry17 reg = Registry()18 config = self._makeOne(reg)19 self.assertEqual(config.get_settings(), None)20 def test_get_settings_withsettings(self):21 settings = {'a':1}22 config = self._makeOne()23 config.registry.settings = settings24 self.assertEqual(config.get_settings(), settings)25 def test_add_settings_settings_already_registered(self):26 from pyramid.registry import Registry27 reg = Registry()28 config = self._makeOne(reg)29 config._set_settings({'a':1})30 config.add_settings({'b':2})31 settings = reg.settings32 self.assertEqual(settings['a'], 1)33 self.assertEqual(settings['b'], 2)34 def test_add_settings_settings_not_yet_registered(self):35 from pyramid.registry import Registry36 from pyramid.interfaces import ISettings37 reg = Registry()38 config = self._makeOne(reg)39 config.add_settings({'a':1})40 settings = reg.getUtility(ISettings)41 self.assertEqual(settings['a'], 1)42 def test_add_settings_settings_None(self):43 from pyramid.registry import Registry44 from pyramid.interfaces import ISettings45 reg = Registry()46 config = self._makeOne(reg)47 config.add_settings(None, a=1)48 settings = reg.getUtility(ISettings)49 self.assertEqual(settings['a'], 1)50class TestSettings(unittest.TestCase):51 52 def _getTargetClass(self):53 from pyramid.config.settings import Settings54 return Settings55 def _makeOne(self, d=None, environ=None):56 if environ is None:57 environ = {}58 klass = self._getTargetClass()59 return klass(d, _environ_=environ)60 def test_getattr_success(self):61 import warnings62 with warnings.catch_warnings(record=True) as w:63 warnings.filterwarnings('always')64 settings = self._makeOne({'reload_templates':False})65 self.assertEqual(settings.reload_templates, False)66 self.assertEqual(len(w), 1)67 def test_getattr_fail(self):68 import warnings69 with warnings.catch_warnings(record=True) as w:70 warnings.filterwarnings('always')71 settings = self._makeOne({})72 self.assertRaises(AttributeError, settings.__getattr__, 'wontexist')73 self.assertEqual(len(w), 0)74 def test_getattr_raises_attribute_error(self):75 settings = self._makeOne()76 self.assertRaises(AttributeError, settings.__getattr__, 'mykey')77 def test_noargs(self):78 settings = self._makeOne()79 self.assertEqual(settings['debug_authorization'], False)80 self.assertEqual(settings['debug_notfound'], False)81 self.assertEqual(settings['debug_routematch'], False)82 self.assertEqual(settings['reload_templates'], False)83 self.assertEqual(settings['reload_resources'], False)84 self.assertEqual(settings['pyramid.debug_authorization'], False)85 self.assertEqual(settings['pyramid.debug_notfound'], False)86 self.assertEqual(settings['pyramid.debug_routematch'], False)87 self.assertEqual(settings['pyramid.reload_templates'], False)88 self.assertEqual(settings['pyramid.reload_resources'], False)89 def test_prevent_http_cache(self):90 settings = self._makeOne({})91 self.assertEqual(settings['prevent_http_cache'], False)92 self.assertEqual(settings['pyramid.prevent_http_cache'], False)93 result = self._makeOne({'prevent_http_cache':'false'})94 self.assertEqual(result['prevent_http_cache'], False)95 self.assertEqual(result['pyramid.prevent_http_cache'], False)96 result = self._makeOne({'prevent_http_cache':'t'})97 self.assertEqual(result['prevent_http_cache'], True)98 self.assertEqual(result['pyramid.prevent_http_cache'], True)99 result = self._makeOne({'prevent_http_cache':'1'})100 self.assertEqual(result['prevent_http_cache'], True)101 self.assertEqual(result['pyramid.prevent_http_cache'], True)102 result = self._makeOne({'pyramid.prevent_http_cache':'t'})103 self.assertEqual(result['prevent_http_cache'], True)104 self.assertEqual(result['pyramid.prevent_http_cache'], True)105 result = self._makeOne({}, {'PYRAMID_PREVENT_HTTP_CACHE':'1'})106 self.assertEqual(result['prevent_http_cache'], True)107 self.assertEqual(result['pyramid.prevent_http_cache'], True)108 result = self._makeOne({'prevent_http_cache':'false',109 'pyramid.prevent_http_cache':'1'})110 self.assertEqual(result['prevent_http_cache'], True)111 self.assertEqual(result['pyramid.prevent_http_cache'], True)112 result = self._makeOne({'prevent_http_cache':'false',113 'pyramid.prevent_http_cache':'f'},114 {'PYRAMID_PREVENT_HTTP_CACHE':'1'})115 self.assertEqual(result['prevent_http_cache'], True)116 self.assertEqual(result['pyramid.prevent_http_cache'], True)117 def test_reload_templates(self):118 settings = self._makeOne({})119 self.assertEqual(settings['reload_templates'], False)120 self.assertEqual(settings['pyramid.reload_templates'], False)121 result = self._makeOne({'reload_templates':'false'})122 self.assertEqual(result['reload_templates'], False)123 self.assertEqual(result['pyramid.reload_templates'], False)124 result = self._makeOne({'reload_templates':'t'})125 self.assertEqual(result['reload_templates'], True)126 self.assertEqual(result['pyramid.reload_templates'], True)127 result = self._makeOne({'reload_templates':'1'})128 self.assertEqual(result['reload_templates'], True)129 self.assertEqual(result['pyramid.reload_templates'], True)130 result = self._makeOne({'pyramid.reload_templates':'1'})131 self.assertEqual(result['reload_templates'], True)132 self.assertEqual(result['pyramid.reload_templates'], True)133 result = self._makeOne({}, {'PYRAMID_RELOAD_TEMPLATES':'1'})134 self.assertEqual(result['reload_templates'], True)135 self.assertEqual(result['pyramid.reload_templates'], True)136 result = self._makeOne({'reload_templates':'false',137 'pyramid.reload_templates':'1'})138 self.assertEqual(result['reload_templates'], True)139 self.assertEqual(result['pyramid.reload_templates'], True)140 result = self._makeOne({'reload_templates':'false'},141 {'PYRAMID_RELOAD_TEMPLATES':'1'})142 self.assertEqual(result['reload_templates'], True)143 self.assertEqual(result['pyramid.reload_templates'], True)144 def test_reload_resources(self):145 # alias for reload_assets146 result = self._makeOne({})147 self.assertEqual(result['reload_resources'], False)148 self.assertEqual(result['reload_assets'], False)149 self.assertEqual(result['pyramid.reload_resources'], False)150 self.assertEqual(result['pyramid.reload_assets'], False)151 result = self._makeOne({'reload_resources':'false'})152 self.assertEqual(result['reload_resources'], False)153 self.assertEqual(result['reload_assets'], False)154 self.assertEqual(result['pyramid.reload_resources'], False)155 self.assertEqual(result['pyramid.reload_assets'], False)156 result = self._makeOne({'reload_resources':'t'})157 self.assertEqual(result['reload_resources'], True)158 self.assertEqual(result['reload_assets'], True)159 self.assertEqual(result['pyramid.reload_resources'], True)160 self.assertEqual(result['pyramid.reload_assets'], True)161 result = self._makeOne({'reload_resources':'1'})162 self.assertEqual(result['reload_resources'], True)163 self.assertEqual(result['reload_assets'], True)164 self.assertEqual(result['pyramid.reload_resources'], True)165 self.assertEqual(result['pyramid.reload_assets'], True)166 result = self._makeOne({'pyramid.reload_resources':'1'})167 self.assertEqual(result['reload_resources'], True)168 self.assertEqual(result['reload_assets'], True)169 self.assertEqual(result['pyramid.reload_resources'], True)170 self.assertEqual(result['pyramid.reload_assets'], True)171 result = self._makeOne({}, {'PYRAMID_RELOAD_RESOURCES':'1'})172 self.assertEqual(result['reload_resources'], True)173 self.assertEqual(result['reload_assets'], True)174 self.assertEqual(result['pyramid.reload_resources'], True)175 self.assertEqual(result['pyramid.reload_assets'], True)176 result = self._makeOne({'reload_resources':'false',177 'pyramid.reload_resources':'1'})178 self.assertEqual(result['reload_resources'], True)179 self.assertEqual(result['reload_assets'], True)180 self.assertEqual(result['pyramid.reload_resources'], True)181 self.assertEqual(result['pyramid.reload_assets'], True)182 result = self._makeOne({'reload_resources':'false',183 'pyramid.reload_resources':'false'},184 {'PYRAMID_RELOAD_RESOURCES':'1'})185 self.assertEqual(result['reload_resources'], True)186 self.assertEqual(result['reload_assets'], True)187 self.assertEqual(result['pyramid.reload_resources'], True)188 self.assertEqual(result['pyramid.reload_assets'], True)189 def test_reload_assets(self):190 # alias for reload_resources191 result = self._makeOne({})192 self.assertEqual(result['reload_assets'], False)193 self.assertEqual(result['reload_resources'], False)194 self.assertEqual(result['pyramid.reload_assets'], False)195 self.assertEqual(result['pyramid.reload_resources'], False)196 result = self._makeOne({'reload_assets':'false'})197 self.assertEqual(result['reload_resources'], False)198 self.assertEqual(result['reload_assets'], False)199 self.assertEqual(result['pyramid.reload_assets'], False)200 self.assertEqual(result['pyramid.reload_resources'], False)201 result = self._makeOne({'reload_assets':'t'})202 self.assertEqual(result['reload_assets'], True)203 self.assertEqual(result['reload_resources'], True)204 self.assertEqual(result['pyramid.reload_assets'], True)205 self.assertEqual(result['pyramid.reload_resources'], True)206 result = self._makeOne({'reload_assets':'1'})207 self.assertEqual(result['reload_assets'], True)208 self.assertEqual(result['reload_resources'], True)209 self.assertEqual(result['pyramid.reload_assets'], True)210 self.assertEqual(result['pyramid.reload_resources'], True)211 result = self._makeOne({'pyramid.reload_assets':'1'})212 self.assertEqual(result['reload_assets'], True)213 self.assertEqual(result['reload_resources'], True)214 self.assertEqual(result['pyramid.reload_assets'], True)215 self.assertEqual(result['pyramid.reload_resources'], True)216 result = self._makeOne({}, {'PYRAMID_RELOAD_ASSETS':'1'})217 self.assertEqual(result['reload_assets'], True)218 self.assertEqual(result['reload_resources'], True)219 self.assertEqual(result['pyramid.reload_assets'], True)220 self.assertEqual(result['pyramid.reload_resources'], True)221 result = self._makeOne({'reload_assets':'false',222 'pyramid.reload_assets':'1'})223 self.assertEqual(result['reload_assets'], True)224 self.assertEqual(result['reload_resources'], True)225 self.assertEqual(result['pyramid.reload_assets'], True)226 self.assertEqual(result['pyramid.reload_resources'], True)227 result = self._makeOne({'reload_assets':'false',228 'pyramid.reload_assets':'false'},229 {'PYRAMID_RELOAD_ASSETS':'1'})230 self.assertEqual(result['reload_assets'], True)231 self.assertEqual(result['reload_resources'], True)232 self.assertEqual(result['pyramid.reload_assets'], True)233 self.assertEqual(result['pyramid.reload_resources'], True)234 def test_reload_all(self):235 result = self._makeOne({})236 self.assertEqual(result['reload_templates'], False)237 self.assertEqual(result['reload_resources'], False)238 self.assertEqual(result['reload_assets'], False)239 self.assertEqual(result['pyramid.reload_templates'], False)240 self.assertEqual(result['pyramid.reload_resources'], False)241 self.assertEqual(result['pyramid.reload_assets'], False)242 result = self._makeOne({'reload_all':'false'})243 self.assertEqual(result['reload_templates'], False)244 self.assertEqual(result['reload_resources'], False)245 self.assertEqual(result['reload_assets'], False)246 self.assertEqual(result['pyramid.reload_templates'], False)247 self.assertEqual(result['pyramid.reload_resources'], False)248 self.assertEqual(result['pyramid.reload_assets'], False)249 result = self._makeOne({'reload_all':'t'})250 self.assertEqual(result['reload_templates'], True)251 self.assertEqual(result['reload_resources'], True)252 self.assertEqual(result['reload_assets'], True)253 self.assertEqual(result['pyramid.reload_templates'], True)254 self.assertEqual(result['pyramid.reload_resources'], True)255 self.assertEqual(result['pyramid.reload_assets'], True)256 result = self._makeOne({'reload_all':'1'})257 self.assertEqual(result['reload_templates'], True)258 self.assertEqual(result['reload_resources'], True)259 self.assertEqual(result['reload_assets'], True)260 self.assertEqual(result['pyramid.reload_templates'], True)261 self.assertEqual(result['pyramid.reload_resources'], True)262 self.assertEqual(result['pyramid.reload_assets'], True)263 result = self._makeOne({'pyramid.reload_all':'1'})264 self.assertEqual(result['reload_templates'], True)265 self.assertEqual(result['reload_resources'], True)266 self.assertEqual(result['reload_assets'], True)267 self.assertEqual(result['pyramid.reload_templates'], True)268 self.assertEqual(result['pyramid.reload_resources'], True)269 self.assertEqual(result['pyramid.reload_assets'], True)270 result = self._makeOne({}, {'PYRAMID_RELOAD_ALL':'1'})271 self.assertEqual(result['reload_templates'], True)272 self.assertEqual(result['reload_resources'], True)273 self.assertEqual(result['reload_assets'], True)274 self.assertEqual(result['pyramid.reload_templates'], True)275 self.assertEqual(result['pyramid.reload_resources'], True)276 self.assertEqual(result['pyramid.reload_assets'], True)277 result = self._makeOne({'reload_all':'false',278 'pyramid.reload_all':'1'})279 self.assertEqual(result['reload_templates'], True)280 self.assertEqual(result['reload_resources'], True)281 self.assertEqual(result['reload_assets'], True)282 self.assertEqual(result['pyramid.reload_templates'], True)283 self.assertEqual(result['pyramid.reload_resources'], True)284 self.assertEqual(result['pyramid.reload_assets'], True)285 result = self._makeOne({'reload_all':'false',286 'pyramid.reload_all':'false'},287 {'PYRAMID_RELOAD_ALL':'1'})288 self.assertEqual(result['reload_templates'], True)289 self.assertEqual(result['reload_resources'], True)290 self.assertEqual(result['reload_assets'], True)291 self.assertEqual(result['pyramid.reload_templates'], True)292 self.assertEqual(result['pyramid.reload_resources'], True)293 self.assertEqual(result['pyramid.reload_assets'], True)294 def test_debug_authorization(self):295 result = self._makeOne({})296 self.assertEqual(result['debug_authorization'], False)297 self.assertEqual(result['pyramid.debug_authorization'], False)298 result = self._makeOne({'debug_authorization':'false'})299 self.assertEqual(result['debug_authorization'], False)300 self.assertEqual(result['pyramid.debug_authorization'], False)301 result = self._makeOne({'debug_authorization':'t'})302 self.assertEqual(result['debug_authorization'], True)303 self.assertEqual(result['pyramid.debug_authorization'], True)304 result = self._makeOne({'debug_authorization':'1'})305 self.assertEqual(result['debug_authorization'], True)306 self.assertEqual(result['pyramid.debug_authorization'], True)307 result = self._makeOne({'pyramid.debug_authorization':'1'})308 self.assertEqual(result['debug_authorization'], True)309 self.assertEqual(result['pyramid.debug_authorization'], True)310 result = self._makeOne({}, {'PYRAMID_DEBUG_AUTHORIZATION':'1'})311 self.assertEqual(result['debug_authorization'], True)312 self.assertEqual(result['pyramid.debug_authorization'], True)313 result = self._makeOne({'debug_authorization':'false',314 'pyramid.debug_authorization':'1'})315 self.assertEqual(result['debug_authorization'], True)316 self.assertEqual(result['pyramid.debug_authorization'], True)317 result = self._makeOne({'debug_authorization':'false',318 'pyramid.debug_authorization':'false'},319 {'PYRAMID_DEBUG_AUTHORIZATION':'1'})320 self.assertEqual(result['debug_authorization'], True)321 self.assertEqual(result['pyramid.debug_authorization'], True)322 def test_debug_notfound(self):323 result = self._makeOne({})324 self.assertEqual(result['debug_notfound'], False)325 self.assertEqual(result['pyramid.debug_notfound'], False)326 result = self._makeOne({'debug_notfound':'false'})327 self.assertEqual(result['debug_notfound'], False)328 self.assertEqual(result['pyramid.debug_notfound'], False)329 result = self._makeOne({'debug_notfound':'t'})330 self.assertEqual(result['debug_notfound'], True)331 self.assertEqual(result['pyramid.debug_notfound'], True)332 result = self._makeOne({'debug_notfound':'1'})333 self.assertEqual(result['debug_notfound'], True)334 self.assertEqual(result['pyramid.debug_notfound'], True)335 result = self._makeOne({'pyramid.debug_notfound':'1'})336 self.assertEqual(result['debug_notfound'], True)337 self.assertEqual(result['pyramid.debug_notfound'], True)338 result = self._makeOne({}, {'PYRAMID_DEBUG_NOTFOUND':'1'})339 self.assertEqual(result['debug_notfound'], True)340 self.assertEqual(result['pyramid.debug_notfound'], True)341 result = self._makeOne({'debug_notfound':'false',342 'pyramid.debug_notfound':'1'})343 self.assertEqual(result['debug_notfound'], True)344 self.assertEqual(result['pyramid.debug_notfound'], True)345 result = self._makeOne({'debug_notfound':'false',346 'pyramid.debug_notfound':'false'},347 {'PYRAMID_DEBUG_NOTFOUND':'1'})348 self.assertEqual(result['debug_notfound'], True)349 self.assertEqual(result['pyramid.debug_notfound'], True)350 def test_debug_routematch(self):351 result = self._makeOne({})352 self.assertEqual(result['debug_routematch'], False)353 self.assertEqual(result['pyramid.debug_routematch'], False)354 result = self._makeOne({'debug_routematch':'false'})355 self.assertEqual(result['debug_routematch'], False)356 self.assertEqual(result['pyramid.debug_routematch'], False)357 result = self._makeOne({'debug_routematch':'t'})358 self.assertEqual(result['debug_routematch'], True)359 self.assertEqual(result['pyramid.debug_routematch'], True)360 result = self._makeOne({'debug_routematch':'1'})361 self.assertEqual(result['debug_routematch'], True)362 self.assertEqual(result['pyramid.debug_routematch'], True)363 result = self._makeOne({'pyramid.debug_routematch':'1'})364 self.assertEqual(result['debug_routematch'], True)365 self.assertEqual(result['pyramid.debug_routematch'], True)366 result = self._makeOne({}, {'PYRAMID_DEBUG_ROUTEMATCH':'1'})367 self.assertEqual(result['debug_routematch'], True)368 self.assertEqual(result['pyramid.debug_routematch'], True)369 result = self._makeOne({'debug_routematch':'false',370 'pyramid.debug_routematch':'1'})371 self.assertEqual(result['debug_routematch'], True)372 self.assertEqual(result['pyramid.debug_routematch'], True)373 result = self._makeOne({'debug_routematch':'false',374 'pyramid.debug_routematch':'false'},375 {'PYRAMID_DEBUG_ROUTEMATCH':'1'})376 self.assertEqual(result['debug_routematch'], True)377 self.assertEqual(result['pyramid.debug_routematch'], True)378 def test_debug_templates(self):379 result = self._makeOne({})380 self.assertEqual(result['debug_templates'], False)381 self.assertEqual(result['pyramid.debug_templates'], False)382 result = self._makeOne({'debug_templates':'false'})383 self.assertEqual(result['debug_templates'], False)384 self.assertEqual(result['pyramid.debug_templates'], False)385 result = self._makeOne({'debug_templates':'t'})386 self.assertEqual(result['debug_templates'], True)387 self.assertEqual(result['pyramid.debug_templates'], True)388 result = self._makeOne({'debug_templates':'1'})389 self.assertEqual(result['debug_templates'], True)390 self.assertEqual(result['pyramid.debug_templates'], True)391 result = self._makeOne({'pyramid.debug_templates':'1'})392 self.assertEqual(result['debug_templates'], True)393 self.assertEqual(result['pyramid.debug_templates'], True)394 result = self._makeOne({}, {'PYRAMID_DEBUG_TEMPLATES':'1'})395 self.assertEqual(result['debug_templates'], True)396 self.assertEqual(result['pyramid.debug_templates'], True)397 result = self._makeOne({'debug_templates':'false',398 'pyramid.debug_templates':'1'})399 self.assertEqual(result['debug_templates'], True)400 self.assertEqual(result['pyramid.debug_templates'], True)401 result = self._makeOne({'debug_templates':'false',402 'pyramid.debug_templates':'false'},403 {'PYRAMID_DEBUG_TEMPLATES':'1'})404 self.assertEqual(result['debug_templates'], True)405 self.assertEqual(result['pyramid.debug_templates'], True)406 def test_debug_all(self):407 result = self._makeOne({})408 self.assertEqual(result['debug_notfound'], False)409 self.assertEqual(result['debug_routematch'], False)410 self.assertEqual(result['debug_authorization'], False)411 self.assertEqual(result['debug_templates'], False)412 self.assertEqual(result['pyramid.debug_notfound'], False)413 self.assertEqual(result['pyramid.debug_routematch'], False)414 self.assertEqual(result['pyramid.debug_authorization'], False)415 self.assertEqual(result['pyramid.debug_templates'], False)416 result = self._makeOne({'debug_all':'false'})417 self.assertEqual(result['debug_notfound'], False)418 self.assertEqual(result['debug_routematch'], False)419 self.assertEqual(result['debug_authorization'], False)420 self.assertEqual(result['debug_templates'], False)421 self.assertEqual(result['pyramid.debug_notfound'], False)422 self.assertEqual(result['pyramid.debug_routematch'], False)423 self.assertEqual(result['pyramid.debug_authorization'], False)424 self.assertEqual(result['pyramid.debug_templates'], False)425 result = self._makeOne({'debug_all':'t'})426 self.assertEqual(result['debug_notfound'], True)427 self.assertEqual(result['debug_routematch'], True)428 self.assertEqual(result['debug_authorization'], True)429 self.assertEqual(result['debug_templates'], True)430 self.assertEqual(result['pyramid.debug_notfound'], True)431 self.assertEqual(result['pyramid.debug_routematch'], True)432 self.assertEqual(result['pyramid.debug_authorization'], True)433 self.assertEqual(result['pyramid.debug_templates'], True)434 result = self._makeOne({'debug_all':'1'})435 self.assertEqual(result['debug_notfound'], True)436 self.assertEqual(result['debug_routematch'], True)437 self.assertEqual(result['debug_authorization'], True)438 self.assertEqual(result['debug_templates'], True)439 self.assertEqual(result['pyramid.debug_notfound'], True)440 self.assertEqual(result['pyramid.debug_routematch'], True)441 self.assertEqual(result['pyramid.debug_authorization'], True)442 self.assertEqual(result['pyramid.debug_templates'], True)443 result = self._makeOne({'pyramid.debug_all':'1'})444 self.assertEqual(result['debug_notfound'], True)445 self.assertEqual(result['debug_routematch'], True)446 self.assertEqual(result['debug_authorization'], True)447 self.assertEqual(result['debug_templates'], True)448 self.assertEqual(result['pyramid.debug_notfound'], True)449 self.assertEqual(result['pyramid.debug_routematch'], True)450 self.assertEqual(result['pyramid.debug_authorization'], True)451 self.assertEqual(result['pyramid.debug_templates'], True)452 result = self._makeOne({}, {'PYRAMID_DEBUG_ALL':'1'})453 self.assertEqual(result['debug_notfound'], True)454 self.assertEqual(result['debug_routematch'], True)455 self.assertEqual(result['debug_authorization'], True)456 self.assertEqual(result['debug_templates'], True)457 self.assertEqual(result['pyramid.debug_notfound'], True)458 self.assertEqual(result['pyramid.debug_routematch'], True)459 self.assertEqual(result['pyramid.debug_authorization'], True)460 self.assertEqual(result['pyramid.debug_templates'], True)461 result = self._makeOne({'debug_all':'false',462 'pyramid.debug_all':'1'})463 self.assertEqual(result['debug_notfound'], True)464 self.assertEqual(result['debug_routematch'], True)465 self.assertEqual(result['debug_authorization'], True)466 self.assertEqual(result['debug_templates'], True)467 self.assertEqual(result['pyramid.debug_notfound'], True)468 self.assertEqual(result['pyramid.debug_routematch'], True)469 self.assertEqual(result['pyramid.debug_authorization'], True)470 self.assertEqual(result['pyramid.debug_templates'], True)471 result = self._makeOne({'debug_all':'false',472 'pyramid.debug_all':'false'},473 {'PYRAMID_DEBUG_ALL':'1'})474 self.assertEqual(result['debug_notfound'], True)475 self.assertEqual(result['debug_routematch'], True)476 self.assertEqual(result['debug_authorization'], True)477 self.assertEqual(result['debug_templates'], True)478 self.assertEqual(result['pyramid.debug_notfound'], True)479 self.assertEqual(result['pyramid.debug_routematch'], True)480 self.assertEqual(result['pyramid.debug_authorization'], True)481 self.assertEqual(result['pyramid.debug_templates'], True)482 def test_default_locale_name(self):483 result = self._makeOne({})484 self.assertEqual(result['default_locale_name'], 'en')485 self.assertEqual(result['pyramid.default_locale_name'], 'en')486 result = self._makeOne({'default_locale_name':'abc'})487 self.assertEqual(result['default_locale_name'], 'abc')488 self.assertEqual(result['pyramid.default_locale_name'], 'abc')489 result = self._makeOne({'pyramid.default_locale_name':'abc'})490 self.assertEqual(result['default_locale_name'], 'abc')491 self.assertEqual(result['pyramid.default_locale_name'], 'abc')492 result = self._makeOne({}, {'PYRAMID_DEFAULT_LOCALE_NAME':'abc'})493 self.assertEqual(result['default_locale_name'], 'abc')494 self.assertEqual(result['pyramid.default_locale_name'], 'abc')495 result = self._makeOne({'default_locale_name':'def',496 'pyramid.default_locale_name':'abc'})497 self.assertEqual(result['default_locale_name'], 'abc')498 self.assertEqual(result['pyramid.default_locale_name'], 'abc')499 result = self._makeOne({'default_locale_name':'def',500 'pyramid.default_locale_name':'ghi'},501 {'PYRAMID_DEFAULT_LOCALE_NAME':'abc'})502 self.assertEqual(result['default_locale_name'], 'abc')503 self.assertEqual(result['pyramid.default_locale_name'], 'abc')504 def test_originals_kept(self):505 result = self._makeOne({'a':'i am so a'})...
__init__.py
Source:__init__.py
1# Copyright (c) 2019-present, Facebook, Inc.2# All rights reserved.3#4# This source code is licensed under the license found in the5# LICENSE file in the root directory of this source tree.6#7from logging import getLogger8import os9import torch10from .pretrain import load_embeddings11# , TRANSFORMER_LAYER_PARAMS12from .transformer import DECODER_ONLY_PARAMS, TransformerModel13logger = getLogger()14def check_model_params(params):15 """16 Check models parameters.17 """18 # masked language modeling task parameters19 assert params.bptt >= 120 assert 0 <= params.word_pred < 121 assert 0 <= params.sample_alpha < 122 s = params.word_mask_keep_rand.split(',')23 assert len(s) == 324 s = [float(x) for x in s]25 assert all([0 <= x <= 1 for x in s]) and sum(s) == 126 params.word_mask = s[0]27 params.word_keep = s[1]28 params.word_rand = s[2]29 # input sentence noise for DAE30 if len(params.ae_steps) == 0:31 assert params.word_shuffle == 032 assert params.word_dropout == 033 assert params.word_blank == 034 else:35 assert params.word_shuffle == 0 or params.word_shuffle > 136 assert 0 <= params.word_dropout < 137 assert 0 <= params.word_blank < 138 # model dimensions39 if params.emb_dim_encoder == 0 and params.emb_dim_decoder == 0:40 assert params.emb_dim > 041 params.emb_dim_encoder = params.emb_dim42 params.emb_dim_decoder = params.emb_dim43 else:44 assert params.emb_dim == 045 assert params.emb_dim_encoder > 0 and params.emb_dim_decoder > 046 if params.emb_dim_encoder == params.emb_dim_decoder:47 params.emb_dim = params.emb_dim_decoder48 else:49 assert params.reload_emb == "", 'Pre-trained embeddings are not supported when the embedding size of the ' \50 'encoder and the decoder do not match '51 assert params.emb_dim_encoder % params.n_heads == 052 assert params.emb_dim_decoder % params.n_heads == 053 if params.n_layers_encoder == 0 and params.n_layers_decoder == 0:54 assert params.n_layers > 055 params.n_layers_encoder = params.n_layers56 params.n_layers_decoder = params.n_layers57 else:58 assert params.n_layers == 059 assert params.n_layers_encoder > 0 and params.n_layers_decoder > 060 # reload pretrained word embeddings61 if params.reload_emb != '':62 assert os.path.isfile(params.reload_emb)63 # reload a pretrained model64 if params.reload_model != '':65 if params.encoder_only:66 assert os.path.isfile(params.reload_model)67 else:68 s = params.reload_model.split(',')69 assert len(s) == 270 assert all([x == '' or os.path.isfile(x) for x in s])71 assert not (params.beam_size > 1 and params.number_samples >72 1), 'Cannot sample when already doing beam search'73 assert (params.eval_temperature is None) == (params.number_samples <=74 1), 'Eval temperature should be set if and only if taking several samples at eval time'75def set_pretrain_emb(model, dico, word2id, embeddings):76 """77 Pretrain word embeddings.78 """79 n_found = 080 with torch.no_grad():81 for i in range(len(dico)):82 idx = word2id.get(dico[i], None)83 if idx is None:84 continue85 n_found += 186 model.embeddings.weight[i] = embeddings[idx].cuda()87 model.pred_layer.proj.weight[i] = embeddings[idx].cuda()88 logger.info("Pretrained %i/%i words (%.3f%%)."89 % (n_found, len(dico), 100. * n_found / len(dico)))90def build_model(params, dico):91 """92 Build model.93 """94 if params.encoder_only:95 # build96 model = TransformerModel(97 params, dico, is_encoder=True, with_output=True)98 # reload pretrained word embeddings99 if params.reload_emb != '':100 word2id, embeddings = load_embeddings(params.reload_emb, params)101 set_pretrain_emb(model, dico, word2id, embeddings)102 # reload a pretrained model103 if params.reload_model != '':104 logger.info("Reloading model from %s ..." % params.reload_model)105 reloaded = torch.load(params.reload_model, map_location=lambda storage, loc: storage.cuda(106 params.local_rank))['model']107 if all([k.startswith('module.') for k in reloaded.keys()]):108 reloaded = {k[len('module.'):]: v for k, v in reloaded.items()}109 # # HACK to reload models with less layers110 # for i in range(12, 24):111 # for k in TRANSFORMER_LAYER_PARAMS:112 # k = k % i113 # if k in model.state_dict() and k not in reloaded:114 # logger.warning("Parameter %s not found. Ignoring ..." % k)115 # reloaded[k] = model.state_dict()[k]116 model.load_state_dict(reloaded)117 logger.info("Model: {}".format(model))118 logger.info("Number of parameters (model): %i" % sum(119 [p.numel() for p in model.parameters() if p.requires_grad]))120 return [model.cuda()]121 else:122 # build123 # TODO: only output when necessary - len(params.clm_steps + params.mlm_steps) > 0124 encoder = TransformerModel(125 params, dico, is_encoder=True, with_output=True)126 if params.separate_decoders:127 decoders = [TransformerModel(128 params, dico, is_encoder=False, with_output=True) for _ in params.lang2id.values()]129 else:130 decoders = [TransformerModel(131 params, dico, is_encoder=False, with_output=True)]132 for layer in range(params.n_layers_decoder):133 if layer <= params.n_share_dec - 1:134 assert params.amp == -1, "sharing layers is not supported with AMP"135 logger.info(136 "Sharing decoder attention parameters for layer %i" % layer)137 for i in range(1, len(decoders)):138 decoders[i].attentions[layer] = decoders[0].attentions[layer]139 # reload pretrained word embeddings140 if params.reload_emb != '':141 word2id, embeddings = load_embeddings(params.reload_emb, params)142 set_pretrain_emb(encoder, dico, word2id, embeddings)143 set_pretrain_emb(decoders, dico, word2id, embeddings)144 # reload a pretrained model145 if params.reload_model != '':146 enc_path, dec_path = params.reload_model.split(',')147 assert not (enc_path == '' and dec_path == '')148 # reload encoder149 if enc_path != '':150 logger.info("Reloading encoder from %s ..." % enc_path)151 enc_reload = torch.load(152 enc_path, map_location=lambda storage, loc: storage.cuda(params.local_rank))153 enc_reload = enc_reload['model' if 'model' in enc_reload else 'encoder']154 if all([k.startswith('module.') for k in enc_reload.keys()]):155 enc_reload = {k[len('module.'):]: v for k,156 v in enc_reload.items()}157 # # HACK to reload models trained with less languages158 n_langs = len(params.langs)159 n_langs_reload = enc_reload['lang_embeddings.weight'].size()[0]160 assert n_langs == n_langs_reload or n_langs == 2 * \161 n_langs_reload or n_langs == 2 * n_langs_reload + 1162 if n_langs == 2 * n_langs_reload:163 enc_reload['lang_embeddings.weight'] = enc_reload['lang_embeddings.weight'].transpose(164 0, 1).repeat_interleave(2, 1).transpose(0, 1)165 elif n_langs == 2 * n_langs_reload + 1:166 enc_reload['lang_embeddings.weight'] = enc_reload['lang_embeddings.weight'].transpose(167 0, 1).repeat_interleave(2, 1).transpose(0, 1)168 enc_reload['lang_embeddings.weight'] = torch.cat(169 [enc_reload['lang_embeddings.weight'][0, :].unsqueeze(dim=0), enc_reload['lang_embeddings.weight']])170 if encoder.position_embeddings.weight.size()[0] == 2 * enc_reload['position_embeddings.weight'].size()[0]:171 enc_reload['position_embeddings.weight'] = enc_reload['position_embeddings.weight'].repeat(172 2, 1)173 encoder.load_state_dict(enc_reload)174 # reload decoders175 if dec_path != '':176 for dec in decoders:177 logger.info("Reloading decoders from %s ..." % dec_path)178 dec_reload = torch.load(179 dec_path, map_location=lambda storage, loc: storage.cuda(params.local_rank))180 dec_reload = dec_reload['model' if 'model' in dec_reload else 'decoder']181 if all([k.startswith('module.') for k in dec_reload.keys()]):182 dec_reload = {183 k[len('module.'):]: v for k, v in dec_reload.items()}184 # # HACK to reload models trained with less languages185 n_langs = len(params.langs)186 n_langs_reload = dec_reload['lang_embeddings.weight'].size()[187 0]188 assert n_langs == n_langs_reload or n_langs == 2 * \189 n_langs_reload or n_langs == 2 * n_langs_reload + 1190 if n_langs == 2 * n_langs_reload:191 dec_reload['lang_embeddings.weight'] = dec_reload['lang_embeddings.weight'].transpose(192 0, 1).repeat_interleave(2, 1).transpose(0, 1)193 elif n_langs == 2 * n_langs_reload + 1:194 dec_reload['lang_embeddings.weight'] = dec_reload['lang_embeddings.weight'].transpose(195 0, 1).repeat_interleave(2, 1).transpose(0, 1)196 dec_reload['lang_embeddings.weight'] = torch.cat(197 [dec_reload['lang_embeddings.weight'][0, :].unsqueeze(dim=0), dec_reload['lang_embeddings.weight']])198 if dec.position_embeddings.weight.size()[0] == 2 * dec_reload['position_embeddings.weight'].size()[0]:199 dec_reload['position_embeddings.weight'] = dec_reload['position_embeddings.weight'].repeat(200 2, 1)201 for i in range(params.n_layers_decoder):202 for name in DECODER_ONLY_PARAMS:203 if name % i not in dec_reload:204 logger.warning(205 "Parameter %s not found." % (name % i))206 dec_reload[name % i] = dec.state_dict()[207 name % i]208 dec.load_state_dict(dec_reload)209 logger.debug("Encoder: {}".format(encoder))210 logger.debug("Decoder: {}".format(decoders))211 logger.info("Number of parameters (encoder): %i" % sum(212 [p.numel() for p in encoder.parameters() if p.requires_grad]))213 logger.info("Number of parameters (decoders): %i" % sum(214 [p.numel() for p in decoders[0].parameters() if p.requires_grad]))215 logger.info(f"Number of decoders: {len(decoders)}")...
test_unit_reload.py
Source:test_unit_reload.py
1#!/usr/bin/env python2import os3import unittest2 as unittest4from gppylib.mainUtils import ExceptionNoStackTraceNeeded5from gppylib.operations.reload import GpReload6from mock import MagicMock, Mock, mock_open, patch7class GpReloadTestCase(unittest.TestCase):8 def setUp(self):9 class Options:10 def __init__(self):11 if 'PGPORT' not in os.environ:12 self.port = None13 else:14 self.port = os.environ['PGPORT']15 self.table_file = '/tmp/table_file'16 self.database = 'testdb'17 self.interactive = False18 args = None19 self.reload = GpReload(Options(), args)20 def tearDown(self):21 if self.reload.table_file and os.path.exists(self.reload.table_file):22 os.remove(self.reload.table_file)23 def test_validate_options_table_file_not_specified(self):24 self.reload.table_file = None25 with self.assertRaisesRegexp(ExceptionNoStackTraceNeeded, 'Please specify table file'):26 self.reload.validate_options()27 @patch('os.path.exists', return_value=False)28 def test_validate_options_table_file_does_not_exist(self, mock1):29 with self.assertRaisesRegexp(ExceptionNoStackTraceNeeded, 'Unable to find table file "/tmp/table_file"'):30 self.reload.validate_options()31 @patch('os.path.exists', return_value=True)32 def test_validate_options_database_not_specified(self, mock1):33 self.reload.database = None34 with self.assertRaisesRegexp(ExceptionNoStackTraceNeeded, 'Please specify the correct database'):35 self.reload.validate_options()36 @patch('os.path.exists', return_value=True)37 @patch('os.environ')38 def test_validate_options_no_port(self, mock1, mock2):39 self.reload.port = None40 with self.assertRaisesRegexp(ExceptionNoStackTraceNeeded, 'Please specify PGPORT using -p option or set PGPORT in the environment'):41 self.reload.validate_options()42 @patch('os.path.exists', return_value=True)43 def test_validate_options_valid(self, mock1):44 self.reload.validate_options()45 def test_parse_line_empty_table(self):46 line = 'public.:sort_column'47 with self.assertRaises(Exception):48 self.reload.parse_line(line)49 def test_parse_line_empty_schema(self):50 line = '.t1:sort_column'51 with self.assertRaises(Exception):52 self.reload.parse_line(line)53 def test_parse_line_empty_columns(self):54 line = 'public.t1:'55 with self.assertRaises(Exception):56 self.reload.parse_line(line)57 def test_parse_columns_empty_line(self):58 columns = ''59 with self.assertRaisesRegexp(Exception, 'Empty column'):60 self.reload.parse_columns(columns)61 def test_parse_columns_single_column(self):62 columns = 'single_col'63 self.assertEqual([('single_col', 'asc')], self.reload.parse_columns(columns))64 def test_parse_columns_empty_columns(self):65 columns = ' , c2 foo'66 with self.assertRaisesRegexp(Exception, 'Empty column'):67 self.reload.parse_columns(columns)68 def test_parse_column_single_sort_order(self):69 columns = 'single_col asc'70 self.assertEqual([('single_col', 'asc')], self.reload.parse_columns(columns))71 def test_parse_column_invalid_sort_order1(self):72 columns = 'c1, c2 foo'73 with self.assertRaisesRegexp(Exception, 'Invalid sort order foo'):74 self.reload.parse_columns(columns)75 def test_parse_column_invalid_sort_order2(self):76 columns = 'c1, c2 foo bar'77 with self.assertRaisesRegexp(Exception, 'Invalid sort order specified'):78 self.reload.parse_columns(columns)79 def test_validate_table_file_empty_table_name(self):80 self.reload.table_file = '/tmp/table_file'81 with open(self.reload.table_file, 'w') as fp:82 fp.write('public.t1: a, b\n')83 fp.write('public.: a, b\n')84 try:85 with self.assertRaisesRegexp(ExceptionNoStackTraceNeeded, "Line 'public.: a, b' is not formatted correctly"):86 self.reload.validate_table_file()87 finally:88 if os.path.exists(self.reload.table_file):89 os.remove(self.reload.table_file)90 def test_validate_table_file_empty_schema_name(self):91 self.reload.table_file = '/tmp/table_file'92 with open(self.reload.table_file, 'w') as fp:93 fp.write('public.t1: a, b\n')94 fp.write('.t1: a, b\n')95 try:96 with self.assertRaisesRegexp(ExceptionNoStackTraceNeeded, "Line '.t1: a, b' is not formatted correctly"):97 self.reload.validate_table_file()98 finally:99 if os.path.exists(self.reload.table_file):100 os.remove(self.reload.table_file)101 def test_validate_table_file_empty_column_list(self):102 self.reload.table_file = '/tmp/table_file'103 with open(self.reload.table_file, 'w') as fp:104 fp.write('public.t1:\n')105 try:106 with self.assertRaisesRegexp(ExceptionNoStackTraceNeeded, "Line 'public.t1:' is not formatted correctly"):107 self.reload.validate_table_file()108 finally:109 if os.path.exists(self.reload.table_file):110 os.remove(self.reload.table_file)111 def test_validate_table_file_invalid_sort_order(self):112 self.reload.table_file = '/tmp/table_file'113 with open(self.reload.table_file, 'w') as fp:114 fp.write('public.t1: a, b\n')115 fp.write('public.t2: a, b foo\n')116 try:117 with self.assertRaisesRegexp(ExceptionNoStackTraceNeeded, "Line 'public.t2: a, b foo' is not formatted correctly"):118 self.reload.validate_table_file()119 finally:120 if os.path.exists(self.reload.table_file):121 os.remove(self.reload.table_file)122 def test_validate_table_file_valid_data1(self):123 self.reload.table_file = '/tmp/table_file'124 with open(self.reload.table_file, 'w') as fp:125 fp.write('public.t1: a, b\n')126 fp.write('public.t2: a, b\n')127 try:128 expected_output = [('public', 't1', [('a', 'asc'), ('b', 'asc')]), ('public', 't2', [('a', 'asc'), ('b', 'asc')])]129 self.assertEqual(expected_output, self.reload.validate_table_file())130 finally:131 if os.path.exists(self.reload.table_file):132 os.remove(self.reload.table_file)133 def test_validate_table_file_valid_data2(self):134 self.reload.table_file = '/tmp/table_file'135 with open(self.reload.table_file, 'w') as fp:136 fp.write('public.t1: a, b desc\n')137 fp.write('public.t2: a desc, b\n')138 try:139 expected_output = [('public', 't1', [('a', 'asc'), ('b', 'desc')]), ('public', 't2', [('a', 'desc'), ('b', 'asc')])]140 self.assertEqual(expected_output, self.reload.validate_table_file())141 finally:142 if os.path.exists(self.reload.table_file):143 os.remove(self.reload.table_file)144 @patch('gppylib.operations.reload.dbconn.connect')145 @patch('gppylib.operations.reload.execSQLForSingleton', return_value=0)146 def test_validate_table_invalid_table(self, mock1, mock2):147 with self.assertRaisesRegexp(ExceptionNoStackTraceNeeded, "Table public.t1 does not exist"):148 self.reload.validate_table('public', 't1')149 @patch('gppylib.operations.reload.dbconn.connect')150 @patch('gppylib.operations.reload.execSQLForSingleton', return_value=1)151 def test_validate_table_valid_table(self, mock1, mock2):152 self.reload.validate_table('public', 't1')153 @patch('gppylib.operations.reload.dbconn.connect')154 def test_validate_columns_invalid_column(self, mock1):155 m = Mock()156 m.fetchall.return_value=[['a', 'x'], ['b', 'y']]157 with patch('gppylib.operations.reload.execSQL', return_value=m) as p:158 with self.assertRaisesRegexp(ExceptionNoStackTraceNeeded, 'Table public.t1 does not have column c'):159 self.reload.validate_columns('public', 't1', [('a', 'x'), ('b', 'y'), ('c', 'z')])160 @patch('gppylib.operations.reload.dbconn.connect')161 def test_validate_columns_valid_column1(self, mock1):162 m = Mock()163 m.fetchall.return_value=[['a', 'x'], ['b', 'y'], ['c', 'z']]164 with patch('gppylib.operations.reload.execSQL', return_value=m) as p:165 self.reload.validate_columns('public', 't1', [('a', 'x'), ('b', 'y'), ('c', 'z')])166 @patch('gppylib.operations.reload.dbconn.connect')167 def test_validate_columns_valid_column2(self, mock1):168 m = Mock()169 m.fetchall.return_value=[['a', 'w'], ['b', 'x'], ['c', 'y'], ['d', 'z']]170 with patch('gppylib.operations.reload.execSQL', return_value=m) as p:171 self.reload.validate_columns('public', 't1', [('a', 'w'), ('b', 'x'), ('c', 'y'), ('d', 'z')])172 @patch('gppylib.operations.reload.execSQLForSingleton', return_value=0)173 @patch('gppylib.operations.reload.dbconn.connect')174 def test_check_indexes_no_indexes(self, mock1, mock2):175 self.assertTrue(self.reload.check_indexes('public', 't1'))176 @patch('gppylib.operations.reload.ask_yesno', return_value=True)177 @patch('gppylib.operations.reload.dbconn.connect')178 @patch('gppylib.operations.reload.execSQLForSingleton', return_value=1)179 def test_check_indexes_with_indexes_with_continue(self, mock1, mock2, mock3):180 self.reload.interactive=True181 self.assertTrue(self.reload.check_indexes('public', 't1'))182 @patch('gppylib.operations.reload.ask_yesno', return_value=False)183 @patch('gppylib.operations.reload.dbconn.connect')184 @patch('gppylib.operations.reload.execSQLForSingleton', return_value=1)185 def test_check_indexes_with_indexes_without_continue(self, mock1, mock2, mock3):186 self.reload.interactive=True187 self.assertFalse(self.reload.check_indexes('public', 't1'))188 @patch('gppylib.operations.reload.dbconn.connect')189 @patch('gppylib.operations.reload.execSQL', side_effect=[[('public', 'x1')], [('public', 'x2')]])190 def test_get_parent_partition_map(self, mock1, mock2):191 expected = {('public', 't1'): ('public', 'x1'), ('public', 't2'): ('public', 'x2')}192 self.reload.table_list = [('public', 't1', 'a b c d'), ('public', 't2', 'd e f g')]193 self.assertEqual(expected, self.reload.get_parent_partitions())194 @patch('gppylib.operations.reload.dbconn.connect') 195 @patch('gppylib.operations.reload.execSQL', side_effect=[[('public', 'x1')], []])196 def test_get_parent_partition_map_non_partition(self, mock1, mock2):197 expected = {('public', 't1'): ('public', 'x1'), ('public', 't2'): ('public', 't2')}198 self.reload.table_list = [('public', 't1', 'a b c d'), ('public', 't2', 'd e f g')]...
migrate.py
Source:migrate.py
...128 log.debug('def_ms=%s' % unicode(def_ms))129 html += "</body></html>"130 return HttpResponse(html)131@csrf_exempt132def gitreload(request, reload_dir=None):133 '''134 This can be used as a github WebHook Service Hook, for reloading of the content repo used by the LMS.135 If reload_dir is not None, then instruct the xml loader to reload that course directory.136 '''137 html = "<html><body>"138 ip = getip(request)139 html += '<h3>IP address: %s ' % ip140 html += '<h3>User: %s ' % request.user141 ALLOWED_IPS = [] # allow none by default142 if hasattr(settings, 'ALLOWED_GITRELOAD_IPS'): # allow override in settings143 ALLOWED_IPS = settings.ALLOWED_GITRELOAD_IPS144 if not (ip in ALLOWED_IPS or 'any' in ALLOWED_IPS):145 if request.user and request.user.is_staff:146 log.debug(u'request allowed because user=%s is staff', request.user)...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!