Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Commit

Permalink
More test re-working.
Browse files Browse the repository at this point in the history
  • Loading branch information
gtaylor committed Sep 28, 2013
1 parent 4c007e8 commit 7be4b44
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 40 deletions.
2 changes: 2 additions & 0 deletions settings.py
Expand Up @@ -15,6 +15,8 @@

DATABASE_NAME = 'dott'
DATABASE_USERNAME = 'dott'
TEST_DATABASE_USERNAME = 'dott'
TEST_DATABASE_NAME = 'dott_test'

# Amazon Web Services credentials.
AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXXXXXX'
Expand Down
15 changes: 12 additions & 3 deletions src/accounts/account_store.py
Expand Up @@ -5,7 +5,7 @@
from twisted.internet.defer import inlineCallbacks, returnValue

from src.accounts.db_io import DBManager
from src.accounts.exceptions import AccountNotFoundException
from src.accounts.exceptions import AccountNotFoundException, UsernameTakenException
from src.accounts.account import PlayerAccount


Expand Down Expand Up @@ -45,6 +45,13 @@ def create_account(self, username, password, email):
a duplicate account.
"""

try:
yield self.get_account_by_username(username)
raise UsernameTakenException(
"User with that username already exists.")
except AccountNotFoundException:
pass

# Create the PlayerAccount, pointed at the PlayerObject's _id.
#noinspection PyTypeChecker
account = PlayerAccount(
Expand Down Expand Up @@ -92,7 +99,8 @@ def get_account_count(self):
:returns: A total count of active accounts.
"""

yield self.db_manager.get_account_count()
num_accounts = yield self.db_manager.get_account_count()
returnValue(num_accounts)

@inlineCallbacks
def get_account_by_id(self, account_id):
Expand All @@ -106,6 +114,7 @@ def get_account_by_id(self, account_id):
ID exists.
"""

account_id = int(account_id)
account = yield self.db_manager.get_account_by_id(account_id)
if not account:
raise AccountNotFoundException(
Expand All @@ -131,6 +140,6 @@ def get_account_by_username(self, username):
account = yield self.db_manager.get_account_by_username(lowered_username)
if not account:
raise AccountNotFoundException(
'Invalid account username requested: %s' % lowered_username)
'No account with username "%s" exists.' % lowered_username)
else:
returnValue(account)
4 changes: 2 additions & 2 deletions src/accounts/db_io.py
Expand Up @@ -69,7 +69,7 @@ def get_account_count(self):
"""

results = yield self._db.runQuery("SELECT count(*) as count FROM dott_accounts")
returnValue(results["count"])
returnValue(results[0][0])

@inlineCallbacks
def get_account_by_id(self, account_id):
Expand Down Expand Up @@ -99,7 +99,7 @@ def get_account_by_username(self, account_username):
:rtype: PlayerAccount
"""

modified_query = "{base_query} WHERE username=%s".format(
modified_query = "{base_query} WHERE username ILIKE %s".format(
base_query=self.BASE_ACCOUNT_SELECT
)
results = yield self._db.runQuery(modified_query, (account_username,))
Expand Down
6 changes: 6 additions & 0 deletions src/accounts/exceptions.py
@@ -1,12 +1,18 @@
"""
Account related exceptions.
"""

from src.utils.exceptions import BaseException


class AccountNotFoundException(BaseException):
"""
Raise this when the user tries to query for an account that does not exist.
"""

pass


class UsernameTakenException(BaseException):
"""
Raise this when someone attempts to create an account with a username
Expand Down
45 changes: 16 additions & 29 deletions src/accounts/tests.py
@@ -1,3 +1,7 @@
"""
Account store tests.
"""

from twisted.internet.defer import inlineCallbacks

from src.utils.test_utils import DottTestCase
Expand All @@ -8,38 +12,18 @@
class DBAccountStoreTests(DottTestCase):

@inlineCallbacks
def test_empty_db_creation(self):
"""
Makes sure the default DB is created.
"""
num_objects = yield self.account_store.get_account_count()
# The DB should be reachable, but empty.
self.assertEqual(num_objects, 0)

def _test_create_account(self):
def test_create_account(self):
"""
Tests the creation and querying of an account.
TODO: RESTORE THIS TEST TO WORKING CONDITION
"""
self.skipTest()
account = self.account_store.create_account('TestGuy', 'yay', 'some@guy.com')

account = yield self.account_store.create_account('TestGuy', 'yay', 'some@guy.com')

# These two values should be the same. username is just a property
# that maps to _id.
self.assertEqual('TestGuy', account.username)

# This should be the ID of the object this account is
# currently controlling.
self.assertIsInstance(account.currently_controlling.id, basestring)
# The object the account is controlling.
obj = account.currently_controlling
# If this returns a BaseObject, we should have an ID attribute.
self.assertIsInstance(obj.id, basestring)
# This should equal the username.
self.assertEqual(obj.name, account.username)
# This should return the account we created.
self.assertIs(obj.controlled_by, account)
self.assertEqual(account.username, 'TestGuy')

# Make sure the password given at creation is valid.
self.assertEqual(account.check_password('yay'), True)
Expand All @@ -50,17 +34,20 @@ def _test_create_account(self):
# Try the old original value and make sure it doesn't match.
self.assertEqual(account.check_password('yay'), False)

num_accounts = len(self.account_store._db)
num_accounts = yield self.account_store.get_account_count()
self.assertEqual(num_accounts, 1)

# Just make sure it works.
self.account_store.get_account('TestGuy')
q_account = yield self.account_store.get_account_by_username('TestGuy')
self.assertEqual(q_account.username, 'TestGuy')

# Trying to create an account with a username that is already
# in use.
self.assertRaises(UsernameTakenException,
self.account_store.create_account,
'TestGuy', 'yay', 'some@guy.com')
try:
yield self.account_store.create_account('TestGuy', 'yay', 'some@guy.com')
self.fail("This should have failed due to duplication.")
except UsernameTakenException:
pass


class ValidatorTests(DottTestCase):
Expand Down
4 changes: 2 additions & 2 deletions src/daemons/server/objects/tests.py
Expand Up @@ -36,8 +36,8 @@ def test_create_room(self):
"""

room = yield self.object_store.create_object(settings.ROOM_PARENT, name='Another room')
# The _id attribute should have a value from CouchDB.
self.assertIsInstance(room.id, basestring)
# The id attribute should have a value from the DB.
self.assertIsInstance(room.id, int)
# The room name should match what was given during creation.
self.assertEqual('Another room', room.name)

Expand Down
3 changes: 2 additions & 1 deletion src/utils/db.py
Expand Up @@ -20,7 +20,8 @@ class txPGDictConnection(txpostgres.Connection):
@staticmethod
def dict_connect(*args, **kwargs):
kwargs['connection_factory'] = psycopg2.extras.DictConnection
return psycopg2.connect(*args, **kwargs)
conn = psycopg2.connect(*args, **kwargs)
return conn

# Overriding the default connection factory.
connectionFactory = dict_connect
33 changes: 30 additions & 3 deletions src/utils/test_utils.py
Expand Up @@ -2,8 +2,12 @@
Assorted utilities for unit testing.
"""

import os
import psycopg2
from twisted.trial import unittest
from twisted.internet.defer import inlineCallbacks

import settings
from src.game.commands.global_cmdtable import GlobalCommandTable, GlobalAdminCommandTable
from src.daemons.server.commands.handler import CommandHandler
from src.daemons.server.objects.object_store import ObjectStore
Expand Down Expand Up @@ -41,6 +45,11 @@ def prep_and_load(self):
yield self.object_store.prep_and_load()
yield self.account_store.prep_and_load()

#noinspection PyProtectedMember
def unload(self):
self.object_store.db_manager._db.close()
self.account_store.db_manager._db.close()


#noinspection PyPep8Naming
class DottTestCase(unittest.TestCase):
Expand Down Expand Up @@ -69,6 +78,26 @@ def create_clean_game_env(self):
Creates a fresh set of stores, DBs, and etc.
"""

conn = psycopg2.connect(user=settings.DATABASE_USERNAME)
conn.set_session(autocommit=True)
cur = conn.cursor()
cur.execute("DROP DATABASE IF EXISTS %s;" % settings.TEST_DATABASE_NAME)
cur.execute("CREATE DATABASE %s WITH OWNER=%s;" % (
settings.TEST_DATABASE_NAME, settings.TEST_DATABASE_USERNAME
))
cur.close()
conn.close()

conn = psycopg2.connect(
user=settings.DATABASE_USERNAME, database=settings.TEST_DATABASE_NAME)
conn.set_session(autocommit=True)
cur = conn.cursor()
schema_file = os.path.join(settings.BASE_PATH, 'misc', 'dott-schema.sql')
schema = open(schema_file).read()
cur.execute(schema)
cur.close()
conn.close()

self.mud_service = MockMudService()
yield self.mud_service.prep_and_load()
self.global_cmd_table = self.mud_service.global_cmd_table
Expand All @@ -82,6 +111,4 @@ def cleanup_game_env(self):
Cleans up the created environment.
"""

pass
#del self.object_store._server['dott_objects_test']
#del self.account_store._server['dott_accounts_test']
self.mud_service.unload()

0 comments on commit 7be4b44

Please sign in to comment.