Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests] Change configuration to a config file #115

Merged
merged 1 commit into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ before_install:
- | # MySQLdb is not available in Python 3.x
if [[ $TRAVIS_PYTHON_VERSION == 2.7 ]]; then
pip install MYSQL-python
pip install configparser
else
pip install PyMySQL>=0.7.0
fi
Expand All @@ -24,6 +25,7 @@ install:

before_script:
- mysql -e 'create database testhat;'
- cp tests/tests.conf.sample tests/tests.conf

script:
- cd tests
Expand Down
23 changes: 11 additions & 12 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import configparser
import sys
import unittest

Expand All @@ -30,11 +31,7 @@

from sortinghat.db.database import Database

from tests.config import (DB_USER,
DB_PASSWORD,
DB_NAME,
DB_HOST,
DB_PORT)
CONFIG_FILE = 'tests.conf'


class TestDatabaseCaseBase(unittest.TestCase):
Expand All @@ -51,7 +48,14 @@ class TestDatabaseCaseBase(unittest.TestCase):
"""
@classmethod
def setUpClass(cls):
cls.db = Database(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST, DB_PORT)
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
cls.db_kwargs = {'user': config['Database']['user'],
'password': config['Database']['password'],
'database': config['Database']['name'],
'host': config['Database']['host'],
'port': config['Database']['port']}
cls.db = Database(**cls.db_kwargs)
cls.db.clear()

def setUp(self):
Expand Down Expand Up @@ -81,12 +85,7 @@ class TestCommandCaseBase(TestDatabaseCaseBase):
def setUpClass(cls):
super(TestCommandCaseBase, cls).setUpClass()

kwargs = {'user' : DB_USER,
'password' : DB_PASSWORD,
'database' :DB_NAME,
'host' : DB_HOST,
'port' : DB_PORT}
cls.cmd = cls.cmd_klass(**kwargs)
cls.cmd = cls.cmd_klass(**cls.db_kwargs)

def setUp(self):
if not hasattr(sys.stdout, 'getvalue') and not hasattr(sys.stderr, 'getvalue'):
Expand Down
29 changes: 0 additions & 29 deletions tests/config.py

This file was deleted.

16 changes: 10 additions & 6 deletions tests/test_cmd_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import configparser
import sys
import unittest
import uuid
Expand All @@ -37,12 +38,12 @@
from sortinghat.db.database import Database
from sortinghat.exceptions import CODE_DATABASE_ERROR, CODE_VALUE_ERROR

from tests.config import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT


DB_ACCESS_ERROR = r".+Access denied for user '%(user)s'@'localhost' \(using password: YES\)"
DB_EXISTS_ERROR = r".+Can't create database '%(database)s'; database exists \(err: 1007\)"

CONFIG_FILE = 'tests.conf'


class TestInitCaseBase(unittest.TestCase):
"""Defines common setup and teardown methods on init unit tests"""
Expand All @@ -54,12 +55,15 @@ def setUp(self):
# Create a temporal name for the registry
self.name = 'tmp' + uuid.uuid4().hex

config = configparser.ConfigParser()
config.read(CONFIG_FILE)

# Create command
self.kwargs = {'user' : DB_USER,
'password' : DB_PASSWORD,
self.kwargs = {'user' : config['Database']['user'],
'password' : config['Database']['password'],
'database' : self.name,
'host' : DB_HOST,
'port' : DB_PORT}
'host' : config['Database']['host'],
'port' : config['Database']['port']}
self.cmd = Init(**self.kwargs)

def tearDown(self):
Expand Down
13 changes: 10 additions & 3 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import configparser
import datetime
import sys
import unittest
Expand All @@ -38,13 +39,13 @@
from sortinghat.db.model import ModelBase, Organization, Domain, Country,\
UniqueIdentity, Identity, Profile, Enrollment, MatchingBlacklist

from tests.config import DB_USER, DB_PASSWORD, DB_NAME, DB_HOST, DB_PORT


DUP_CHECK_ERROR = 'Duplicate entry'
NULL_CHECK_ERROR = 'cannot be null'
INVALID_DATATYPE_ERROR = 'ValueError'

CONFIG_FILE = 'tests.conf'


class MockDatabase(object):

Expand Down Expand Up @@ -73,7 +74,13 @@ class TestCaseBase(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.db = MockDatabase(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST, DB_PORT)
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
cls.db = MockDatabase(config['Database']['user'],
config['Database']['password'],
config['Database']['name'],
config['Database']['host'],
config['Database']['port'])

def setUp(self):
self.session = self.db.session()
Expand Down
8 changes: 8 additions & 0 deletions tests/tests.conf.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Database]
name=testhat

host=127.0.0.1
port=3306

user=
password=