1 | from django.test import TestCase, override_settings
|
---|
2 | from django.conf import settings
|
---|
3 |
|
---|
4 | TEST_SETTINGS = {
|
---|
5 | 'MY_DATA': {
|
---|
6 | 'foo': 1,
|
---|
7 | 'bar': 2
|
---|
8 | }
|
---|
9 | }
|
---|
10 |
|
---|
11 |
|
---|
12 | class Dummy:
|
---|
13 | def foo(self):
|
---|
14 | return settings.MY_DATA.get('foo')
|
---|
15 |
|
---|
16 | def bar(self):
|
---|
17 | return settings.MY_DATA.get('bar')
|
---|
18 |
|
---|
19 |
|
---|
20 | @override_settings(**TEST_SETTINGS)
|
---|
21 | class SimpleTest(TestCase):
|
---|
22 | def setUp(self):
|
---|
23 | self.dummy = Dummy()
|
---|
24 |
|
---|
25 | def test_foobar(self):
|
---|
26 | self.assertEqual(self.dummy.foo(), 1)
|
---|
27 | self.assertEqual(self.dummy.bar(), 2)
|
---|
28 |
|
---|
29 | @override_settings()
|
---|
30 | def test_delete_my_data(self):
|
---|
31 | del settings.MY_DATA
|
---|
32 | with self.assertRaises(AttributeError):
|
---|
33 | self.dummy.foo()
|
---|
34 | with self.assertRaises(AttributeError):
|
---|
35 | self.dummy.bar()
|
---|
36 |
|
---|
37 | @override_settings()
|
---|
38 | def test_delete_foo(self):
|
---|
39 | del settings.MY_DATA['foo']
|
---|
40 | self.assertIsNone(self.dummy.foo())
|
---|
41 | self.assertEqual(self.dummy.bar(), 2)
|
---|
42 |
|
---|
43 | def test_foobar_again(self):
|
---|
44 | self.assertEqual(self.dummy.foo(), 1)
|
---|
45 | self.assertEqual(self.dummy.bar(), 2)
|
---|