Example #1
0
 def test_corrupted_cookie_should_raise(self):
     with open(self.tf.name, 'w') as f:
         f.write('{{{')
     c = Cookie(self.tf.name)
     with self.assertRaises(ValueError):
         c.open()
     c.close()
Example #2
0
 def test_wrong_cookie_format(self):
     with open(self.tf.name, 'w') as f:
         f.write('[1, 2, 3]\n')
     c = Cookie(self.tf.name)
     with self.assertRaises(ValueError):
         c.open()
     c.close()
Example #3
0
 def test_close_removes_file_if_no_value_set(self):
     try:
         os.unlink("cookietest")
     except OSError:
         pass
     c = Cookie("cookietest")
     c.close()
     self.failIf(os.path.exists("cookietest"), u"file 'cookietest' does exist but shouldn't")
Example #4
0
 def test_cookie_format_exception_truncates_file(self):
     with codecs.open(self.tf.name, 'w', 'utf-8') as f:
         f.write('{slö@@ä')
     c = Cookie(self.tf.name)
     try:
         c.open()
     except ValueError:
         pass
     finally:
         c.close()
     self.assertEqual(0, os.stat(self.tf.name).st_size)
Example #5
0
 def test_multiple_commit(self):
     c = Cookie(self.tf.name)
     c.open()
     c['key'] = 1
     c.commit()
     with open(self.tf.name) as f:
         self.assertIn('"key": 1', f.read())
     c['key'] = 2
     c.commit()
     with open(self.tf.name) as f:
         self.assertIn('"key": 2', f.read())
     c.close()
Example #6
0
 def test_oblivious_cookie(self):
     c = Cookie('')
     # the following method calls are not expected to perfom any function
     c.open()
     c['key'] = 1
     c.commit()
     c.close()
     self.assertEqual(c['key'], 1)
Example #7
0
 def test_multiple_commit(self):
     c = Cookie(self.tf.name)
     c.open()
     c['key'] = 1
     c.commit()
     with open(self.tf.name) as f:
         self.assertIn('"key": 1', f.read())
     c['key'] = 2
     c.commit()
     with open(self.tf.name) as f:
         self.assertIn('"key": 2', f.read())
     c.close()
Example #8
0
 def test_should_not_commit_on_exception(self):
     try:
         with Cookie(self.tf.name) as c:
             c['foo'] = True
             raise RuntimeError()
     except RuntimeError:
         pass
     with open(self.tf.name) as f:
         self.assertEqual('', f.read())
Example #9
0
 def test_oblivious_cookie(self):
     c = Cookie('')
     # the following method calls are not expected to perfom any function
     c.open()
     c['key'] = 1
     c.commit()
     c.close()
     self.assertEqual(c['key'], 1)
Example #10
0
 def test_corrupted_cookie_should_raise(self):
     with open(self.tf.name, 'w') as f:
         f.write('{{{')
     c = Cookie(self.tf.name)
     with self.assertRaises(ValueError):
         c.open()
     c.close()
Example #11
0
 def test_wrong_cookie_format(self):
     with open(self.tf.name, 'w') as f:
         f.write('[1, 2, 3]\n')
     c = Cookie(self.tf.name)
     with self.assertRaises(ValueError):
         c.open()
     c.close()
Example #12
0
 def test_cookie_format_exception_truncates_file(self):
     with codecs.open(self.tf.name, 'w', 'utf-8') as f:
         f.write('{slö@@ä')
     c = Cookie(self.tf.name)
     try:
         c.open()
     except ValueError:
         pass
     finally:
         c.close()
     self.assertEqual(0, os.stat(self.tf.name).st_size)
Example #13
0
 def test_get_file_contents(self):
     with open(self.tf.name, 'w') as f:
         f.write('{"hello": "world"}\n')
     with Cookie(self.tf.name) as c:
         self.assertEqual('world', c['hello'])
Example #14
0
 def test_get_without_open_should_raise_keyerror(self):
     c = Cookie(self.tf.name)
     with self.assertRaises(KeyError):
         c['foo']
Example #15
0
 def test_double_close_raises_no_exception(self):
     c = Cookie(self.tf.name)
     c.open()
     c.close()
     c.close()
     self.assertTrue(True)
Example #16
0
 def test_exit_should_write_content(self):
     os.unlink(self.tf.name)
     with Cookie(self.tf.name) as c:
         c['hello'] = 'wörld'
     with open(self.tf.name) as f:
         self.assertEqual('{"hello": "w\\u00f6rld"}\n', f.read())
Example #17
0
 def test_get_default_value_if_empty(self):
     with Cookie(self.tf.name) as c:
         self.assertEqual('default value', c.get('key', 'default value'))
Example #18
0
 def test_close_within_with_block_fails(self):
     with self.assertRaises(IOError):
         with Cookie(self.tf.name) as c:
             c.close()
Example #19
0
 def test_double_close_raises_no_exception(self):
     c = Cookie(self.tf.name)
     c.open()
     c.close()
     c.close()
     self.assertTrue(True)