~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/jobs/tests/test_utils.py

  • Committer: Benji York
  • Date: 2013-11-18 20:38:47 UTC
  • mto: This revision was merged to the branch mainline in revision 465.
  • Revision ID: benji@benjiyork.com-20131118203847-2mfs1w7b8aqy64mr
checkpoint

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
import logging
5
 
import pymongo
 
5
from pymongo import Connection
6
6
from pymongo.errors import DuplicateKeyError
7
7
from time import time
8
8
 
9
 
from charmworld.jobs.utils import (
10
 
    BadDBConnection,
11
 
    lock,
12
 
    LockHeld,
13
 
    parse_branch,
14
 
)
15
 
from charmworld.testing import (
16
 
    MONGO_DATABASE,
17
 
    MONGO_URL,
18
 
    MongoTestBase,
19
 
    TestCase,
20
 
)
 
9
from charmworld.jobs.utils import BadDBConnection
 
10
from charmworld.jobs.utils import lock
 
11
from charmworld.jobs.utils import LockHeld
 
12
from charmworld.testing import MONGO_DATABASE
 
13
from charmworld.testing import MONGO_URL
 
14
from charmworld.testing import MongoTestBase
21
15
 
22
16
 
23
17
LOCK_NAME = u'test_lock'
132
126
 
133
127
    def test_bad_connection_setup(self):
134
128
        # If the DB connection is created without the parameter fsync,
135
 
        # DuplicateKeyError is raised.
136
 
        conn = pymongo.MongoClient(MONGO_URL)
137
 
        self.duplicate_insert(conn, expect_duplicate_key_error=True)
 
129
        # no DuplicateKeyError is raised.
 
130
        conn = Connection(MONGO_URL)
 
131
        self.duplicate_insert(conn, expect_duplicate_key_error=False)
138
132
 
139
133
        # A DuplicateKeyError is raised if the parameter fsync is specified.
140
 
        conn = pymongo.MongoClient(MONGO_URL, fsync=True)
 
134
        conn = Connection(MONGO_URL, fsync=True)
141
135
        self.duplicate_insert(conn, expect_duplicate_key_error=True)
142
136
        # Note that the value of fsync does not matter...
143
 
        conn = pymongo.MongoClient(MONGO_URL, fsync=False)
 
137
        conn = Connection(MONGO_URL, fsync=False)
144
138
        self.duplicate_insert(conn, expect_duplicate_key_error=True)
145
139
 
146
 
        # Alternatively, using w=1 (formerly safe=True) ensures also that a
147
 
        # DuplicateKeyError is raised.  w=0 inhibits the DuplicateKeyError.
148
 
        conn = pymongo.MongoClient(MONGO_URL, w=0)
 
140
        # Alternatively, using safe=True ensures also that a
 
141
        # DuplicateKeyError is raised.
 
142
        conn = Connection(MONGO_URL, safe=False)
149
143
        self.duplicate_insert(conn, expect_duplicate_key_error=False)
150
 
        conn = pymongo.MongoClient(MONGO_URL, w=1)
 
144
        conn = Connection(MONGO_URL, safe=True)
151
145
        self.duplicate_insert(conn, expect_duplicate_key_error=True)
152
146
 
153
 
    def test_lock_creation_with_w0(self):
154
 
        # A DB connection that was created with w=0
155
 
        # cannot be used to create a lock.  So don't do that.
156
 
        conn = pymongo.MongoClient(MONGO_URL, w=0)
 
147
    def test_lock_creation_without_fsync_or_safe_param(self):
 
148
        # A DB connection that was created without the fsync parameter
 
149
        # or safe=True cannot be used to create a lock.
 
150
        conn = Connection(MONGO_URL)
157
151
        db = conn['locks']
158
152
        self.assertRaises(BadDBConnection, self.use_lock, db=db)
159
 
 
160
 
 
161
 
class TestParse(TestCase):
162
 
 
163
 
    def test_parse_branch_parse_failure(self):
164
 
        # If the charm cannot be parsed, an error is raised.
165
 
        repo, commit, series = [
166
 
            u'~charmers/charms/precise/junk/someproject/trunk',
167
 
            u'ja@appflower.com-20120329093714-s2m9e28dwotmijqc',
168
 
            [],
169
 
        ]
170
 
        self.assertRaises(ValueError, parse_branch, repo, commit, series)