Ejemplo n.º 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()
Ejemplo n.º 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()
Ejemplo n.º 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")
Ejemplo n.º 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)
Ejemplo n.º 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()
Ejemplo n.º 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)
Ejemplo n.º 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()
Ejemplo n.º 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())
Ejemplo n.º 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)
Ejemplo n.º 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()
Ejemplo n.º 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()
Ejemplo n.º 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)
Ejemplo n.º 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'])
Ejemplo n.º 14
0
 def test_get_without_open_should_raise_keyerror(self):
     c = Cookie(self.tf.name)
     with self.assertRaises(KeyError):
         c['foo']
Ejemplo n.º 15
0
 def test_double_close_raises_no_exception(self):
     c = Cookie(self.tf.name)
     c.open()
     c.close()
     c.close()
     self.assertTrue(True)
Ejemplo n.º 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())
Ejemplo n.º 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'))
Ejemplo n.º 18
0
 def test_close_within_with_block_fails(self):
     with self.assertRaises(IOError):
         with Cookie(self.tf.name) as c:
             c.close()
Ejemplo n.º 19
0
 def test_double_close_raises_no_exception(self):
     c = Cookie(self.tf.name)
     c.open()
     c.close()
     c.close()
     self.assertTrue(True)