def test_empty_to_dict(self): """FlashScope: Should export the flash data to a dict even if it's empty. """ self.flash = FlashScope() expected_data = {_SESSION_KEY: {}, _USED_KEY: {}} data = self.flash.to_dict() self.assertEqual(expected_data, data)
class JSONCodecTestCase(TestCase): """Tests the JSON-based serialization codec implementation. """ def setUp(self): """Creates a JSON-based codec and a sample flash. """ self.expected = '{"_session": {"info": "Info"}, ' \ '"_used": {"info": null}}' self.codec = json_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update() def test_encode(self): """Codec: JSON-based codec should return a JSON version of the flash. """ self.assertEqual(self.expected, self.codec.encode(self.flash)) def test_decode(self): """Codec: JSON-based codec should restore the flash from a JSON string. """ flash = self.codec.decode(self.expected) self.assertEqual('Info', flash['info']) flash.update() self.assertFalse('info' in flash)
class ImmediateFlashScope(TestCase): """Tests the ``Flashscope.now``. """ def setUp(self): """Create a FlashScope object to be used by the test methods. """ self.flash = FlashScope() self.flash.now['info'] = 'Info' def test_now(self): """FlashScope.now: "flash.now[key] = value" syntax should be supported. """ self.assertEqual('Info', self.flash['info']) self.flash.update() self.assertFalse('info' in self.flash) def test_alternative_now(self): """FlashScope.now: Immediate values (flash.now) should be supported. """ self.flash.now.put(error='Error') self.assertEqual('Error', self.flash['error']) self.flash.update() self.assertFalse('error' in self.flash) def test_contains(self): """FlashScope.now: "key in flash.now" syntax should be supported. """ self.assertFalse('error' in self.flash.now) self.flash.now['error'] = 'Error' self.assertTrue('error' in self.flash.now) def test_get_invalid_item(self): """FlashScope.now: Should raise KeyError if trying to get an invalid item. """ self.assertRaises(KeyError, lambda: self.flash.now['error']) def test_add_with_non_existing_value(self): """FlashScope.now: Should append an immediate value even if the given key doesn't exists. """ self.flash.now.add('error', 'Error 1') self.flash.now.add('error', 'Error 2', 'Error 3') self.assertEqual(['Error 1', 'Error 2', 'Error 3'], self.flash['error']) def test_add_with_existing_non_list_value(self): """FlashScope.now: Should append immediate values to a key even if the current value is not a list. """ self.flash.now.add('info', 'Error 1') self.flash.now.add('info', 'Error 2', 'Error 3') self.assertEqual(['Info', 'Error 1', 'Error 2', 'Error 3'], self.flash['info']) def test_add_with_existing_list_value(self): """FlashScope.now: Should append an immediate value if the current value is a list. """ self.flash.now['error'] = ['Error 1'] self.flash.now.add('error', 'Error 2') self.flash.now.add('error', 'Error 3', 'Error 4') self.assertEqual(['Error 1', 'Error 2', 'Error 3', 'Error 4'], self.flash['error'])
class PickleCodecTestCase(TestCase): """Tests the Pickle-based serialization codec implementation. """ def setUp(self): """Creates a Pickle-based codec and a sample flash. """ self.codec = pickle_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update() self.expected = '\x80\x02cdjangoflash.models\nFlashScope\nq\x01)\x81q' \ '\x02}q\x03(U\x03nowq\x04cdjangoflash.models\n_Immedi' \ 'ateFlashScopeAdapter\nq\x05)\x81q\x06}q\x07U\x08dele' \ 'gateq\x08h\x02sbU\x08_sessionq\t}q\nU\x04infoq\x0bU' \ '\x04Infoq\x0csU\x05_usedq\r}q\x0eh\x0bNsub.' def test_encode(self): """Codec: Pickle-based codec should return a Pickle dump of the flash. """ self.assertEqual(self.expected, self.codec.encode(self.flash)) def test_decode(self): """Codec: Pickle-based codec should restore the flash from a Pickle dump string. """ flash = self.codec.decode(self.expected) self.assertEqual('Info', flash['info']) flash.update() self.assertFalse('info' in flash)
class JSONZlibCodecTestCase(TestCase): """Tests the JSON/zlib-based serialization codec implementation. """ def setUp(self): """Creates a JSON\zlib-based codec and a sample flash. """ self.expected = 'x\x9c\xabV\x8a/N-.\xce\xcc\xcfS\xb2R\xa8V\xca\xccK' \ '\xcb\x072\x94<At\xad\x8e\x82R|iqj\n\xb2T^iNNm-\x00' \ '\xf5\xa2\x12\x03' self.codec = json_zlib_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update() def test_encode(self): """Codec: JSON\zlib-based codec should return a zlib compressed JSON version of the flash. """ self.assertEqual(self.expected, self.codec.encode(self.flash)) def test_decode(self): """Codec: JSON\zlib-based codec should restore the flash from a zlib compressed JSON string. """ flash = self.codec.decode(self.expected) self.assertEqual('Info', flash['info']) flash.update() self.assertFalse('info' in flash)
def test_restore_with_invalid_values(self): """FlashScope: Should not restore the flash using a dict with invalid values. """ data = {_SESSION_KEY: {}, _USED_KEY: None} self.assertRaises(ValueError, lambda: FlashScope(data)) data = {_SESSION_KEY: None, _USED_KEY: {}} self.assertRaises(ValueError, lambda: FlashScope(data))
class ImmediateFlashScope(TestCase): """Tests the ``Flashscope.now``. """ def setUp(self): """Create a FlashScope object to be used by the test methods. """ self.flash = FlashScope() self.flash.now['info'] = 'Info' def test_now(self): """FlashScope.now: "flash.now[key] = value" syntax should be supported. """ self.assertEqual('Info', self.flash['info']) self.flash.update() self.assertFalse('info' in self.flash) def test_alternative_now(self): """FlashScope.now: Immediate values (flash.now) should be supported. """ self.flash.now.put(error='Error') self.assertEqual('Error', self.flash['error']) self.flash.update() self.assertFalse('error' in self.flash) def test_contains(self): """FlashScope.now: "key in flash.now" syntax should be supported. """ self.assertFalse('error' in self.flash.now) self.flash.now['error'] = 'Error' self.assertTrue('error' in self.flash.now) def test_get_invalid_item(self): """FlashScope.now: Should raise KeyError if trying to get an invalid item. """ self.assertRaises(KeyError, lambda: self.flash.now['error']); def test_add_with_non_existing_value(self): """FlashScope.now: Should append an immediate value even if the given key doesn't exists. """ self.flash.now.add('error', 'Error 1') self.flash.now.add('error', 'Error 2', 'Error 3') self.assertEqual(['Error 1', 'Error 2', 'Error 3'], self.flash['error']) def test_add_with_existing_non_list_value(self): """FlashScope.now: Should append immediate values to a key even if the current value is not a list. """ self.flash.now.add('info', 'Error 1') self.flash.now.add('info', 'Error 2', 'Error 3') self.assertEqual(['Info', 'Error 1', 'Error 2', 'Error 3'], self.flash['info']) def test_add_with_existing_list_value(self): """FlashScope.now: Should append an immediate value if the current value is a list. """ self.flash.now['error'] = ['Error 1'] self.flash.now.add('error', 'Error 2') self.flash.now.add('error', 'Error 3', 'Error 4') self.assertEqual(['Error 1', 'Error 2', 'Error 3', 'Error 4'], self.flash['error'])
def setUp(self): """Creates a JSON-based codec and a sample flash. """ self.expected = '{"_session": {"info": "Info"}, ' \ '"_used": {"info": null}}' self.codec = json_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update()
def get_context_from_session(request): """Gets the :class:`FlashScope` object from the user's session and :meth:`update` it. If this object couldn't be found, this method returns a brand new :class:`FlashScope` object. """ context = FlashScope() if hasattr(request, 'session') and _SESSION_KEY in request.session: context = request.session[_SESSION_KEY] context.update() return context
def setUp(self): """Creates a codec and a sample flash. """ self.codec = json_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update() self.expected = 'eyJfc2Vzc2lvbiI6IHsiaW5mbyI6ICJJbmZvIn0sICJfdXNlZCI6' \ 'IHsiaW5mbyI6IG51bGx9fWZk\nNDViYTljMmU3MWJlZjBjYjcxOW' \ 'EwYjdlYzJlZjUx\n'
def get_context_from_session(request): """Gets the :class:`FlashScope` object from the user's session and :meth:`update` it. If this object couldn't be found, this method returns a brand new :class:`FlashScope` object. """ context = FlashScope() if hasattr(request, "session") and _SESSION_KEY in request.session: context = request.session[_SESSION_KEY] context.update() return context
def setUp(self): """Creates a JSON\zlib-based codec and a sample flash. """ self.expected = 'x\x9c\xabV\x8a/N-.\xce\xcc\xcfS\xb2R\xa8V\xca\xccK' \ '\xcb\x072\x94<At\xad\x8e\x82R|iqj\n\xb2T^iNNm-\x00' \ '\xf5\xa2\x12\x03' self.codec = json_zlib_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update()
def setUp(self): """Creates a Pickle-based codec and a sample flash. """ self.codec = pickle_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update() self.expected = '\x80\x02cdjangoflash.models\nFlashScope\nq\x01)\x81q' \ '\x02}q\x03(U\x03nowq\x04cdjangoflash.models\n_Immedi' \ 'ateFlashScopeAdapter\nq\x05)\x81q\x06}q\x07U\x08dele' \ 'gateq\x08h\x02sbU\x08_sessionq\t}q\nU\x04infoq\x0bU' \ '\x04Infoq\x0csU\x05_usedq\r}q\x0eh\x0bNsub.'
def _get_flash_from_storage(request): """Gets the flash from the storage, adds it to the given request and returns it. A new :class:`FlashScope` is used if the storage is empty. """ flash = storage.get(request) or FlashScope() setattr(request, CONTEXT_VAR, flash) return flash
def test_restore_immutability(self): """FlashScope: Should restore the flash using a shallow copy of a dict. """ data = { _SESSION_KEY: { 'info': 'Info', 'error': 'Error' }, _USED_KEY: { 'error': None } } self.flash = FlashScope(data) self.assertEqual('Info', self.flash['info']) del data[_SESSION_KEY]['info'] self.assertTrue('info' in self.flash)
def setUp(self): """Creates a cookie-based flash storage for testing. """ self.request = HttpRequest() self.response = HttpResponse('') self.flash = FlashScope() self.storage = cookie.FlashStorageClass()
def setUp(self): """Create a request with an used message inside the flash. """ self.request = HttpRequest() self.request.flash = self.flash = FlashScope() self.flash['message'] = 'Message' self.flash.update()
def test_empty_to_dict(self): """FlashScope: Should export the flash data to a dict even if it's empty. """ self.flash = FlashScope() expected_data = {_SESSION_KEY: {}, _USED_KEY:{}} data = self.flash.to_dict() self.assertEqual(expected_data, data)
def test_restore_immutability(self): """FlashScope: Should restore the flash using a shallow copy of a dict. """ data = {_SESSION_KEY: {'info' : 'Info', 'error': 'Error'}, _USED_KEY : {'error': None}} self.flash = FlashScope(data) self.assertEqual('Info', self.flash['info']) del data[_SESSION_KEY]['info'] self.assertTrue('info' in self.flash)
def test_restore(self): """FlashScope: Should restore the flash using a dict. """ data = { _SESSION_KEY: { 'info': 'Info', 'error': 'Error' }, _USED_KEY: { 'error': None } } self.flash = FlashScope(data) self.assertEqual(2, len(self.flash)) self.assertEqual('Info', self.flash['info']) self.assertEqual('Error', self.flash['error']) self.flash.update() self.assertEqual('Info', self.flash['info']) self.assertFalse('error' in self.flash)
def get_context_from_request(request): """Gets the :class:`FlashScope` object from the *request* and returns it. If it couldn't be found, the method returns a brand new :class:`FlashScope` object. """ context = FlashScope() if hasattr(request, CONTEXT_VAR): context = getattr(request, CONTEXT_VAR) if not isinstance(context, FlashScope): raise TypeError('Invalid Flash scope object: %s' % \ repr(context)) return context
def test_restore(self): """FlashScope: Should restore the flash using a dict. """ data = {_SESSION_KEY: {'info' : 'Info', 'error': 'Error'}, _USED_KEY : {'error': None}} self.flash = FlashScope(data) self.assertEqual(2, len(self.flash)) self.assertEqual('Info', self.flash['info']) self.assertEqual('Error', self.flash['error']) self.flash.update() self.assertEqual('Info', self.flash['info']) self.assertFalse('error' in self.flash)
class BaseCodecTestCase(TestCase): """Tests the tampered checks and signing logic. """ def setUp(self): """Creates a codec and a sample flash. """ self.codec = json_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update() self.expected = 'eyJfc2Vzc2lvbiI6IHsiaW5mbyI6ICJJbmZvIn0sICJfdXNlZCI6' \ 'IHsiaW5mbyI6IG51bGx9fWZk\nNDViYTljMmU3MWJlZjBjYjcxOW' \ 'EwYjdlYzJlZjUx\n' def test_encode_and_sign(self): """Codec: BaseCodec should return an encoded and signed version of the flash. """ encoded_and_signed = self.codec.encode_and_sign(self.flash) self.assertEqual(self.expected, encoded_and_signed) def test_decoded_signed(self): """Codec: BaseCodec should decode an encoded and signed version of the flash. """ flash = self.codec.decode_signed(self.expected) self.assertEqual('Info', flash['info']) flash.update() self.assertFalse('info' in flash) def test_decoded_tampered(self): """Codec: BaseCodec should not decode a tampered version of the flash. """ tampered = 'eyJfc2Vzc2lvbiI6IHsiaW6mbyI6ICJJbmZvIn0sICJfdXNlZCI6IHsia' \ 'W5mbyI6IG51bGx9fWZk\nNDViYTljMmU3MWJlZjBjYjcxOWEwYjdlYzJl' \ 'ZjUx\n' operation = lambda: self.codec.decode_signed(tampered) self.assertRaises(SuspiciousOperation, operation)
def flash(request): """This context processor gets the :class:`FlashScope` object from the current *request* and adds it to the template context: .. code-block:: html+django <html> <head></head> <body> request.flash['message'] = {{ flash.message }} </body> </html> """ flash_scope = None try: flash_scope = getattr(request, CONTEXT_VAR) if not isinstance(flash_scope, FlashScope): raise SuspiciousOperation('Invalid flash: %s' % repr(flash_scope)) except AttributeError: # Exposes an empty flash when none is available flash_scope = FlashScope() return {CONTEXT_VAR: flash_scope}
class MixedFlashScope(TestCase): """Tests mixing regular and immediate values. """ def setUp(self): """Create a FlashScope object to be used by the test methods. """ self.flash = FlashScope() def test_replace_with_immediate_value(self): """FlashScope: Should replace a regular value by an immediate value. """ self.flash['info'] = 'Info' self.flash.update() self.assertEqual('Info', self.flash['info']) self.flash.now['info'] = 'Error' self.assertEqual('Error', self.flash['info']) self.flash.update() self.assertFalse('info' in self.flash) def test_replace_immediate_with_regular_value(self): """FlashScope: Should replace an immediate value with a regular value. """ self.flash.now['info'] = 'Info' self.assertEqual('Info', self.flash['info']) self.flash['info'] = 'Error' self.flash.update() self.assertEqual('Error', self.flash['info']) self.flash.update() self.assertFalse('info' in self.flash) def test_add_immediate_with_existing_regular_value(self): """FlashScope.now: Should add an immediate value to a regular key, expiring on the current request. """ self.flash['error'] = 'Error 1' self.flash.now.add('error', 'Error 2') self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) self.flash.update() self.assertFalse('error' in self.flash) def test_add_immediate_with_existing_regular_list(self): """FlashScope.now: Should add an immediate value to a regular list, expiring on the current request. """ self.flash['error'] = ['Error 1'] self.flash.now.add('error', 'Error 2') self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) self.flash.update() self.assertFalse('error' in self.flash) def test_add_regular_with_existing_immediate_value(self): """FlashScope: Should add a regular value to an immediate key, expiring on the next request. """ self.flash.now['error'] = 'Error 1' self.flash.add('error', 'Error 2') self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) self.flash.update() self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) self.flash.update() self.assertFalse('error' in self.flash) def test_add_regular_with_existing_immediate_list(self): """FlashScope: Should add a regular value to an immediate list, expiring on the next request. """ self.flash.now['error'] = ['Error 1'] self.flash.add('error', 'Error 2') self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) self.flash.update() self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) self.flash.update() self.assertFalse('error' in self.flash)
def setUp(self): """Create a FlashScope object to be used by the test methods. """ self.flash = FlashScope() self.flash.now['info'] = 'Info'
def setUp(self): """Create a FlashScope object to be used by the test methods. """ self.flash = FlashScope()
def decode(self, encoded_flash): """Restores the *flash* from the given JSON string. """ return FlashScope(json.loads(encoded_flash))
class FlashScopeTestCase(TestCase): """Tests the FlashScope object. """ def setUp(self): """Create a FlashScope object to be used by the test methods. """ self.flash = FlashScope() self.flash['info'] = 'Info' def test_restore(self): """FlashScope: Should restore the flash using a dict. """ data = { _SESSION_KEY: { 'info': 'Info', 'error': 'Error' }, _USED_KEY: { 'error': None } } self.flash = FlashScope(data) self.assertEqual(2, len(self.flash)) self.assertEqual('Info', self.flash['info']) self.assertEqual('Error', self.flash['error']) self.flash.update() self.assertEqual('Info', self.flash['info']) self.assertFalse('error' in self.flash) def test_restore_immutability(self): """FlashScope: Should restore the flash using a shallow copy of a dict. """ data = { _SESSION_KEY: { 'info': 'Info', 'error': 'Error' }, _USED_KEY: { 'error': None } } self.flash = FlashScope(data) self.assertEqual('Info', self.flash['info']) del data[_SESSION_KEY]['info'] self.assertTrue('info' in self.flash) def test_restore_with_invalid_type(self): """FlashScope: Should not restore the flash using an invalid object. """ self.assertRaises(TypeError, lambda: FlashScope('invalid_data')) def test_restore_with_invalid_keys(self): """FlashScope: Should not restore the flash using a dict with invalid keys. """ data = {_SESSION_KEY: None} self.assertRaises(ValueError, lambda: FlashScope(data)) data = {_USED_KEY: None} self.assertRaises(ValueError, lambda: FlashScope(data)) def test_restore_with_invalid_values(self): """FlashScope: Should not restore the flash using a dict with invalid values. """ data = {_SESSION_KEY: {}, _USED_KEY: None} self.assertRaises(ValueError, lambda: FlashScope(data)) data = {_SESSION_KEY: None, _USED_KEY: {}} self.assertRaises(ValueError, lambda: FlashScope(data)) def test_contains(self): """FlashScope: "key in flash" syntax should be supported. """ self.assertFalse('error' in self.flash) self.assertEqual('Info', self.flash['info']) def test_get_invalid_item(self): """FlashScope: Should raise KeyError if trying to get an invalid value. """ self.assertRaises(KeyError, lambda: self.flash['error']) def test_set_item(self): """FlashScope: flash[key] = value" syntax should be supported. """ self.flash['error'] = 'Error' self.assertEqual('Error', self.flash['error']) def test_del_item(self): """FlashScope: "del flash[key]" syntax should be supported. """ self.assertEqual('Info', self.flash['info']) del self.flash['info'] self.assertFalse('info' in self.flash) def test_clear(self): """FlashScope: flash.clear() should remove all items from the flash scope. """ self.flash['error'] = 'Error' self.assertEqual(2, len(self.flash)) self.flash.clear() self.assertEqual(0, len(self.flash)) def test_len(self): """FlashScope: "len(flash)" syntax should be supported. """ self.assertEqual(1, len(self.flash)) def test_keys(self): """FlashScope: Should return the list of keys stored in the flash scope. """ self.assertEqual(['info'], self.flash.keys()) def test_values(self): """FlashScope: Should return the list of values stored in the flash scope. """ self.assertEqual(['Info'], self.flash.values()) def test_items(self): """FlashScope: Should return the list of items stored in the flash scope. """ self.assertEqual([('info', 'Info')], self.flash.items()) def test_iterkeys(self): """FlashScope: Should return an iterator to the keys stored in the flash scope. """ iterator = self.flash.iterkeys() self.assertEqual('info', iterator.next()) self.assertRaises(StopIteration, iterator.next) def test_itervalues(self): """FlashScope: Should return an iterator to the values stored in the flash scope. """ iterator = self.flash.itervalues() self.assertEqual('Info', iterator.next()) self.assertRaises(StopIteration, iterator.next) def test_iteritems(self): """FlashScope: Should return an iterator to the items stored in the flash scope. """ iterator = self.flash.iteritems() self.assertEqual(('info', 'Info'), iterator.next()) self.assertRaises(StopIteration, iterator.next) def test_add_with_existing_non_list_value(self): """FlashScope: Should append a value to a key even if the current value is not a list. """ self.flash.add('info', 'Error') self.assertEqual(['Info', 'Error'], self.flash['info']) def test_add_with_existing_list_value(self): """FlashScope: Should append a value if the current value is a list. """ self.flash['error'] = ['Error 1'] self.flash.add('error', 'Error 2') self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) def test_add_with_non_existing_value(self): """FlashScope: Should add a value even if the given key doesn't exists. """ self.flash.add('error', 'Error') self.assertEqual(['Error'], self.flash['error']) def test_add_across_requests(self): """FlashScope: Should keep a key when a value is appended to it. """ self.flash['error'] = 'Error 1' self.flash.update() self.flash.add('error', 'Error 2') self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) self.flash.update() self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) self.flash.update() self.assertFalse('error' in self.flash) def test_get(self): """FlashScope: Should return a default value if the given key doesn' exists. """ self.assertEqual('Oops', self.flash.get('error', 'Oops')) self.assertEqual('Info', self.flash.get('info', 'Something')) self.assertEqual(None, self.flash.get('error')) def test_pop(self): """FlashScope: Should pop a value from the flash scope. """ self.assertEqual(None, self.flash.pop('error')) self.assertEqual('Info', self.flash.pop('info')) self.assertFalse('info' in self.flash) def test_pop_used_value(self): """FlashScope: Should pop a used value from the flash scope. """ self.flash.update() self.assertEqual('Info', self.flash.pop('info')) self.assertFalse('info' in self.flash) def test_put(self): """FlashScope: Should put several keys into the flash scope at the same time. """ self.flash.put(warn='Warning', error='Error') self.assertEqual('Warning', self.flash['warn']) self.assertEqual('Error', self.flash['error']) def test_discard(self): """FlashScope: Should mark a value for removal. """ self.flash.discard() self.flash.update() self.assertFalse('info' in self.flash) def test_keep(self): """FlashScope: Should avoid the removal of specific values. """ self.flash.update() self.flash.keep('info') self.flash.update() self.assertEqual('Info', self.flash['info']) self.flash.update() self.assertFalse('info' in self.flash) def test_keep_all(self): """FlashScope: Should avoid the removal of all values. """ self.flash.update() self.flash.keep() self.flash.update() self.assertEqual('Info', self.flash['info']) self.flash.update() self.assertFalse('info' in self.flash) def test_replace_used_value(self): """FlashScope: Should keep a key when its value is replaced. """ self.flash.update() self.assertEqual('Info', self.flash['info']) self.flash['info'] = 'Error' self.assertEqual('Error', self.flash['info']) self.flash.update() self.assertEqual('Error', self.flash['info']) self.flash.update() self.assertFalse('info' in self.flash) def test_empty_to_dict(self): """FlashScope: Should export the flash data to a dict even if it's empty. """ self.flash = FlashScope() expected_data = {_SESSION_KEY: {}, _USED_KEY: {}} data = self.flash.to_dict() self.assertEqual(expected_data, data) def test_to_dict(self): """FlashScope: Should export the flash data to a dict. """ self.flash.update() self.flash['error'] = 'Error' expected_data = { _SESSION_KEY: { 'info': 'Info', 'error': 'Error' }, _USED_KEY: { 'info': None } } data = self.flash.to_dict() self.assertEqual(expected_data, data) def test_to_dict_immutability(self): """FlashScope: Should export a copy of the flash data as a dict. """ data = self.flash.to_dict() del self.flash['info'] self.assertEqual('Info', data[_SESSION_KEY]['info'])
class FlashScopeTestCase(TestCase): """Tests the FlashScope object. """ def setUp(self): """Create a FlashScope object to be used by the test methods. """ self.flash = FlashScope() self.flash['message'] = 'Message' def test_contains(self): """FlashScope: "key in flash" syntax should be supported. """ self.assertFalse('another_message' in self.flash) self.assertTrue('message' in self.flash) def test_get_item(self): """FlashScope: flash[key]" syntax should be supported. """ self.assertRaises(KeyError, lambda: self.flash['another_message']); self.assertEqual('Message', self.flash['message']); def test_set_item(self): """FlashScope: flash[key] = value" syntax should be supported. """ self.flash['another_message'] = 'Another message' self.assertEqual('Another message', self.flash['another_message']); def test_del_item(self): """FlashScope: "del flash[key]" syntax should be supported. """ self.assertEqual('Message', self.flash['message']) del self.flash['message'] self.assertFalse('message' in self.flash) def test_clear(self): """FlashScope: flash.clear() should remove all items from the flash scope. """ self.flash['anotherMessage'] = 'Another message' self.assertEqual(2, len(self.flash)) self.flash.clear() self.assertEqual(0, len(self.flash)) def test_len(self): """FlashScope: "len(flash)" syntax should be supported. """ self.assertEqual(1, len(self.flash)) def test_keys(self): """FlashScope: Should return the list of keys stored in the flash scope. """ self.assertEqual(['message'], self.flash.keys()) def test_values(self): """FlashScope: Should return the list of values stored in the flash scope. """ self.assertEqual(['Message'], self.flash.values()) def test_items(self): """FlashScope: Should return the list of items stored in the flash scope. """ self.assertEqual([('message', 'Message')], self.flash.items()) def test_iterkeys(self): """FlashScope: Should return an iterator to the keys stored in the flash scope. """ iterator = self.flash.iterkeys() self.assertEqual('message', iterator.next()) self.assertRaises(StopIteration, iterator.next) def test_itervalues(self): """FlashScope: Should return an iterator to the values stored in the flash scope. """ iterator = self.flash.itervalues() self.assertEqual('Message', iterator.next()) self.assertRaises(StopIteration, iterator.next) def test_iteritems(self): """FlashScope: Should return an iterator to the items stored in the flash scope. """ iterator = self.flash.iteritems() self.assertEqual(('message', 'Message'), iterator.next()) self.assertRaises(StopIteration, iterator.next) def test_add_with_existing_non_list_value(self): """FlashScope: Should add values to a key even if the current value is not a list. """ self.flash.add('message', 'Another Message') self.assertEqual(['Message', 'Another Message'], self.flash['message']) def test_add_with_existing_list_value(self): """FlashScope: Should add a new value if the current value is a list. """ self.flash['another_message'] = ['Message 1'] self.flash.add('another_message', 'Message 2') self.assertEqual(['Message 1', 'Message 2'], self.flash['another_message']) def test_add_with_no_existing_value(self): """FlashScope: Should add a value even if the given key doesn't exists. """ self.flash.add('another_message', 'Another Message') self.assertEqual(['Another Message'], self.flash['another_message']) def test_get(self): """FlashScope: Should return a default value if the given key doesn' exists. """ self.assertEqual('Oops', self.flash.get('another_message', 'Oops')) self.assertEqual('Message', self.flash.get('message', 'Something')) self.assertEqual(None, self.flash.get('another_message')) def test_pop(self): """FlashScope: Should pop an item from the flash scope. """ self.assertEqual(None,self.flash.pop('another_message')) self.assertEqual('Message', self.flash.pop('message')) self.assertFalse('message' in self.flash) def test_pop_used_value(self): """FlashScope: Should pop an used item from the flash scope. """ self.flash.update() self.assertEqual('Message', self.flash.pop('message')) self.assertFalse('message' in self.flash) def test_has_key(self): """FlashScope: Should check if there's a value related with the given key. """ self.assertFalse(self.flash.has_key('another_message')) self.assertTrue(self.flash.has_key('message')) def test_put(self): """FlashScope: Should put several values into the flash scope at the same time. """ self.flash.put(msg1='Message1', msg2='Message2') self.assertEqual('Message1', self.flash['msg1']) self.assertEqual('Message2', self.flash['msg2']) def test_discard(self): """FlashScope: Should mark a flash-scoped value for removal. """ self.flash.discard() self.flash.update() self.assertFalse('message' in self.flash) def test_keep(self): """FlashScope: Should avoid the removal of specific flash-scoped values. """ self.flash.update() self.flash.keep('message') self.flash.update() self.assertEqual('Message', self.flash['message']) self.flash.update() self.assertFalse('message' in self.flash) def test_keep_all(self): """FlashScope: Should avoid the removal of all flash-scoped values. """ self.flash.update() self.flash.keep() self.flash.update() self.assertEqual('Message', self.flash['message']) self.flash.update() self.assertFalse('message' in self.flash) def test_alternative_now(self): """FlashScope: Immediate values (flash.now) should be supported. """ self.flash.now(another_message='Another message') self.assertEqual('Another message', self.flash['another_message']) self.flash.update() self.assertFalse('another_message' in self.flash) def test_now(self): """FlashScope: "flash.now[key] = value" syntax should be supported. """ self.flash.now['another_message'] = 'Another message' self.assertEqual('Another message', self.flash['another_message']) self.flash.update() self.assertFalse('another_message' in self.flash) def test_now_get_item(self): """FlashScope: "FlashScope: "flash.now[key]" syntax should be supported. """ self.flash.now['another_message'] = 'Another message' self.assertEqual('Another message', self.flash.now['another_message']) def test_now_contains(self): """FlashScope: "key in flash.now" syntax should be supported. """ self.assertFalse('another_message' in self.flash.now) self.flash.now['another_message'] = 'Another message' self.assertTrue('another_message' in self.flash.now) def test_replace_value(self): """FlashScope: Should replace a value properly. """ self.flash['message'] = 'Message' self.flash.update() self.assertEqual('Message', self.flash['message']) self.flash['message'] = 'Another Message' self.assertEqual('Another Message', self.flash['message']) self.flash.update() self.assertEqual('Another Message', self.flash['message']) self.flash.update() self.assertFalse('message' in self.flash) def test_replace_with_immediage_value(self): """FlashScope: Should replace a value properly by a immediate value. """ self.flash['message'] = 'Message' self.flash.update() self.assertEqual('Message', self.flash['message']) self.flash.now['message'] = 'Another Message' self.assertEqual('Another Message', self.flash['message']) self.flash.update() self.assertFalse('message' in self.flash)
def setUp(self): """Create a FlashScope object to be used by the test methods. """ self.flash = FlashScope() self.flash['message'] = 'Message'
class FlashScopeTestCase(TestCase): """Tests the FlashScope object. """ def setUp(self): """Create a FlashScope object to be used by the test methods. """ self.flash = FlashScope() self.flash['info'] = 'Info' def test_restore(self): """FlashScope: Should restore the flash using a dict. """ data = {_SESSION_KEY: {'info' : 'Info', 'error': 'Error'}, _USED_KEY : {'error': None}} self.flash = FlashScope(data) self.assertEqual(2, len(self.flash)) self.assertEqual('Info', self.flash['info']) self.assertEqual('Error', self.flash['error']) self.flash.update() self.assertEqual('Info', self.flash['info']) self.assertFalse('error' in self.flash) def test_restore_immutability(self): """FlashScope: Should restore the flash using a shallow copy of a dict. """ data = {_SESSION_KEY: {'info' : 'Info', 'error': 'Error'}, _USED_KEY : {'error': None}} self.flash = FlashScope(data) self.assertEqual('Info', self.flash['info']) del data[_SESSION_KEY]['info'] self.assertTrue('info' in self.flash) def test_restore_with_invalid_type(self): """FlashScope: Should not restore the flash using an invalid object. """ self.assertRaises(TypeError, lambda: FlashScope('invalid_data')) def test_restore_with_invalid_keys(self): """FlashScope: Should not restore the flash using a dict with invalid keys. """ data = {_SESSION_KEY: None} self.assertRaises(ValueError, lambda: FlashScope(data)) data = {_USED_KEY: None} self.assertRaises(ValueError, lambda: FlashScope(data)) def test_restore_with_invalid_values(self): """FlashScope: Should not restore the flash using a dict with invalid values. """ data = {_SESSION_KEY: {}, _USED_KEY: None} self.assertRaises(ValueError, lambda: FlashScope(data)) data = {_SESSION_KEY: None, _USED_KEY: {}} self.assertRaises(ValueError, lambda: FlashScope(data)) def test_contains(self): """FlashScope: "key in flash" syntax should be supported. """ self.assertFalse('error' in self.flash) self.assertEqual('Info', self.flash['info']) def test_get_invalid_item(self): """FlashScope: Should raise KeyError if trying to get an invalid value. """ self.assertRaises(KeyError, lambda: self.flash['error']); def test_set_item(self): """FlashScope: flash[key] = value" syntax should be supported. """ self.flash['error'] = 'Error' self.assertEqual('Error', self.flash['error']); def test_del_item(self): """FlashScope: "del flash[key]" syntax should be supported. """ self.assertEqual('Info', self.flash['info']) del self.flash['info'] self.assertFalse('info' in self.flash) def test_clear(self): """FlashScope: flash.clear() should remove all items from the flash scope. """ self.flash['error'] = 'Error' self.assertEqual(2, len(self.flash)) self.flash.clear() self.assertEqual(0, len(self.flash)) def test_len(self): """FlashScope: "len(flash)" syntax should be supported. """ self.assertEqual(1, len(self.flash)) def test_keys(self): """FlashScope: Should return the list of keys stored in the flash scope. """ self.assertEqual(['info'], self.flash.keys()) def test_values(self): """FlashScope: Should return the list of values stored in the flash scope. """ self.assertEqual(['Info'], self.flash.values()) def test_items(self): """FlashScope: Should return the list of items stored in the flash scope. """ self.assertEqual([('info', 'Info')], self.flash.items()) def test_iterkeys(self): """FlashScope: Should return an iterator to the keys stored in the flash scope. """ iterator = self.flash.iterkeys() self.assertEqual('info', iterator.next()) self.assertRaises(StopIteration, iterator.next) def test_itervalues(self): """FlashScope: Should return an iterator to the values stored in the flash scope. """ iterator = self.flash.itervalues() self.assertEqual('Info', iterator.next()) self.assertRaises(StopIteration, iterator.next) def test_iteritems(self): """FlashScope: Should return an iterator to the items stored in the flash scope. """ iterator = self.flash.iteritems() self.assertEqual(('info', 'Info'), iterator.next()) self.assertRaises(StopIteration, iterator.next) def test_add_with_existing_non_list_value(self): """FlashScope: Should append a value to a key even if the current value is not a list. """ self.flash.add('info', 'Error') self.assertEqual(['Info', 'Error'], self.flash['info']) def test_add_with_existing_list_value(self): """FlashScope: Should append a value if the current value is a list. """ self.flash['error'] = ['Error 1'] self.flash.add('error', 'Error 2') self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) def test_add_with_non_existing_value(self): """FlashScope: Should add a value even if the given key doesn't exists. """ self.flash.add('error', 'Error') self.assertEqual(['Error'], self.flash['error']) def test_add_across_requests(self): """FlashScope: Should keep a key when a value is appended to it. """ self.flash['error'] = 'Error 1' self.flash.update() self.flash.add('error', 'Error 2') self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) self.flash.update() self.assertEqual(['Error 1', 'Error 2'], self.flash['error']) self.flash.update() self.assertFalse('error' in self.flash) def test_get(self): """FlashScope: Should return a default value if the given key doesn' exists. """ self.assertEqual('Oops', self.flash.get('error', 'Oops')) self.assertEqual('Info', self.flash.get('info', 'Something')) self.assertEqual(None, self.flash.get('error')) def test_pop(self): """FlashScope: Should pop a value from the flash scope. """ self.assertEqual(None, self.flash.pop('error')) self.assertEqual('Info', self.flash.pop('info')) self.assertFalse('info' in self.flash) def test_pop_used_value(self): """FlashScope: Should pop a used value from the flash scope. """ self.flash.update() self.assertEqual('Info', self.flash.pop('info')) self.assertFalse('info' in self.flash) def test_put(self): """FlashScope: Should put several keys into the flash scope at the same time. """ self.flash.put(warn='Warning', error='Error') self.assertEqual('Warning', self.flash['warn']) self.assertEqual('Error', self.flash['error']) def test_discard(self): """FlashScope: Should mark a value for removal. """ self.flash.discard() self.flash.update() self.assertFalse('info' in self.flash) def test_keep(self): """FlashScope: Should avoid the removal of specific values. """ self.flash.update() self.flash.keep('info') self.flash.update() self.assertEqual('Info', self.flash['info']) self.flash.update() self.assertFalse('info' in self.flash) def test_keep_all(self): """FlashScope: Should avoid the removal of all values. """ self.flash.update() self.flash.keep() self.flash.update() self.assertEqual('Info', self.flash['info']) self.flash.update() self.assertFalse('info' in self.flash) def test_replace_used_value(self): """FlashScope: Should keep a key when its value is replaced. """ self.flash.update() self.assertEqual('Info', self.flash['info']) self.flash['info'] = 'Error' self.assertEqual('Error', self.flash['info']) self.flash.update() self.assertEqual('Error', self.flash['info']) self.flash.update() self.assertFalse('info' in self.flash) def test_empty_to_dict(self): """FlashScope: Should export the flash data to a dict even if it's empty. """ self.flash = FlashScope() expected_data = {_SESSION_KEY: {}, _USED_KEY:{}} data = self.flash.to_dict() self.assertEqual(expected_data, data) def test_to_dict(self): """FlashScope: Should export the flash data to a dict. """ self.flash.update() self.flash['error'] = 'Error' expected_data = {_SESSION_KEY: {'info' : 'Info', 'error': 'Error'}, _USED_KEY : {'info' : None}} data = self.flash.to_dict() self.assertEqual(expected_data, data) def test_to_dict_immutability(self): """FlashScope: Should export a copy of the flash data as a dict. """ data = self.flash.to_dict() del self.flash['info'] self.assertEqual('Info', data[_SESSION_KEY]['info'])
def setUp(self): self.request = HttpRequest() self.scope = FlashScope(); setattr(self.request, CONTEXT_VAR, self.scope);
def test_restore_with_invalid_type(self): """FlashScope: Should not restore the flash using an invalid object. """ self.assertRaises(TypeError, lambda: FlashScope('invalid_data'))