~larry-e-works/uci-engine/write-exitcode-to-file

« back to all changes in this revision

Viewing changes to nf-stats-service/nfss/tests/unit/test_db_migration.py

  • Committer: Thomi Richards
  • Date: 2014-06-27 20:02:44 UTC
  • mto: (629.2.9 nfss)
  • mto: This revision was merged to the branch mainline in revision 636.
  • Revision ID: thomi.richards@canonical.com-20140627200244-zi7dwxnyw38ypr2f
Initial version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import tempfile
 
2
import unittest
 
3
 
 
4
from mock import patch
 
5
 
 
6
from nfss import db_migrate as dbm
 
7
 
 
8
 
 
9
class DBMigrationTests(unittest.TestCase):
 
10
 
 
11
    def test_get_settings_returns_dict_on_no_file(self):
 
12
        with self.assertRaises(IOError):
 
13
            dbm.get_db_settings('/this/path/does/not/exist')
 
14
 
 
15
    def test_get_settings_returns_dict_on_bad_json(self):
 
16
        with tempfile.NamedTemporaryFile(mode='w') as f:
 
17
            f.write('sdfsdf')
 
18
            f.flush()
 
19
 
 
20
            with self.assertRaises(ValueError):
 
21
                    dbm.get_db_settings(f.name)
 
22
 
 
23
    def test_get_settings_works_on_example_json(self):
 
24
        with tempfile.NamedTemporaryFile(mode='w') as f:
 
25
            f.write(
 
26
                '{"database": "ci-airline-nfss-restish", '
 
27
                '"allowed-units": "ci-airline-nfss-restish/0", '
 
28
                '"state": "standalone", '
 
29
                '"schema_user": "db_2_ci_airline_nfss_restish_schema", '
 
30
                '"schema_password": "secret", '
 
31
                '"private-address": "10.0.3.236", '
 
32
                '"host": "10.0.3.236", '
 
33
                '"user": "db_2_ci_airline_nfss_restish", '
 
34
                '"password": "secret", '
 
35
                '"port": "5432"}'
 
36
            )
 
37
            f.flush()
 
38
 
 
39
            expected = {
 
40
                "database": "ci-airline-nfss-restish",
 
41
                "allowed-units": "ci-airline-nfss-restish/0",
 
42
                "state": "standalone",
 
43
                "schema_user": "db_2_ci_airline_nfss_restish_schema",
 
44
                "schema_password": "secret",
 
45
                "private-address": "10.0.3.236",
 
46
                "host": "10.0.3.236",
 
47
                "user": "db_2_ci_airline_nfss_restish",
 
48
                "password": "secret",
 
49
                "port": "5432"
 
50
            }
 
51
            self.assertEqual(
 
52
                expected,
 
53
                dbm.get_db_settings(f.name)
 
54
            )
 
55
 
 
56
    def test_get_maximum_version_works_with_single_patch(self):
 
57
        with patch.object(dbm, 'get_all_patch_paths') as p:
 
58
            p.return_value = ['000-foo.sql']
 
59
 
 
60
            self.assertEqual(0, dbm.get_maximum_version())
 
61
 
 
62
    def test_get_maximum_version_works_with_many_patches(self):
 
63
        with patch.object(dbm, 'get_all_patch_paths') as p:
 
64
            p.return_value = ['000-foo.sql', '001-bar.sql', '002-baz.sql']
 
65
 
 
66
            self.assertEqual(2, dbm.get_maximum_version())