Skip to content

Commit

Permalink
bug 775631 - Revert "refactored DRY in various tests"
Browse files Browse the repository at this point in the history
This reverts commit dfeff50.
  • Loading branch information
rhelmer committed Jul 19, 2012
1 parent ccec57d commit 35da8e3
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 15 deletions.
67 changes: 64 additions & 3 deletions socorro/unittest/cron/jobs/test_bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import datetime
import shutil
import os
import json
import unittest
import tempfile
import mock
import psycopg2
from psycopg2.extensions import TRANSACTION_STATUS_IDLE
from nose.plugins.attrib import attr
from socorro.cron import crontabber
from .base import TestCaseBase, DSN
from socorro.unittest.config.commonconfig import (
databaseHost, databaseName, databaseUserName, databasePassword)
from configman import ConfigurationManager

DSN = {
"database_host": databaseHost.default,
"database_name": databaseName.default,
"database_user": databaseUserName.default,
"database_password": databasePassword.default
}


SAMPLE_CSV = [
Expand All @@ -25,14 +38,62 @@
]


class _TestCaseBase(unittest.TestCase):

def setUp(self):
self.tempdir = tempfile.mkdtemp()

def tearDown(self):
if os.path.isdir(self.tempdir):
shutil.rmtree(self.tempdir)

def _setup_config_manager(self, jobs_string, extra_value_source=None):
if not extra_value_source:
extra_value_source = {}
mock_logging = mock.Mock()
required_config = crontabber.CronTabber.required_config
required_config.add_option('logger', default=mock_logging)

json_file = os.path.join(self.tempdir, 'test.json')
assert not os.path.isfile(json_file)

config_manager = ConfigurationManager(
[required_config,
#logging_required_config(app_name)
],
app_name='crontabber',
app_description=__doc__,
values_source_list=[{
'logger': mock_logging,
'jobs': jobs_string,
'database': json_file,
}, DSN, extra_value_source]
)
return config_manager, json_file


#==============================================================================
class TestBugzilla(_TestCaseBase):

def setUp(self):
super(TestBugzilla, self).setUp()
self.psycopg2_patcher = mock.patch('psycopg2.connect')
self.mocked_connection = mock.Mock()
self.psycopg2 = self.psycopg2_patcher.start()

def tearDown(self):
super(TestBugzilla, self).tearDown()
self.psycopg2_patcher.stop()


#==============================================================================
@attr(integration='postgres') # for nosetests
class TestFunctionalBugzilla(TestCaseBase):
class TestFunctionalBugzilla(_TestCaseBase):

def setUp(self):
super(TestFunctionalBugzilla, self).setUp()
# prep a fake table
assert 'test' in DSN['database_name'], DSN['database_name']
assert 'test' in databaseName.default, databaseName.default
dsn = ('host=%(database_host)s dbname=%(database_name)s '
'user=%(database_user)s password=%(database_password)s' % DSN)
self.conn = psycopg2.connect(dsn)
Expand Down
65 changes: 53 additions & 12 deletions socorro/unittest/cron/jobs/test_ftpscraper.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import shutil
import os
import json
import datetime
import unittest
import tempfile
from functools import wraps
from cStringIO import StringIO
import mock
import psycopg2
from nose.plugins.attrib import attr
from socorro.cron import crontabber
from socorro.unittest.config.commonconfig import (
databaseHost, databaseName, databaseUserName, databasePassword)
from socorro.lib.datetimeutil import utc_now
from configman import ConfigurationManager
from socorro.cron.jobs import ftpscraper
from .base import TestCaseBase, DSN

DSN = {
"database_host": databaseHost.default,
"database_name": databaseName.default,
"database_user": databaseUserName.default,
"database_password": databasePassword.default
}


def stringioify(func):
Expand All @@ -22,8 +31,42 @@ def wrapper(*a, **k):
return wrapper


class _TestCaseBase(unittest.TestCase):

def setUp(self):
self.tempdir = tempfile.mkdtemp()

def tearDown(self):
if os.path.isdir(self.tempdir):
shutil.rmtree(self.tempdir)

def _setup_config_manager(self, jobs_string, extra_value_source=None):
if not extra_value_source:
extra_value_source = {}
mock_logging = mock.Mock()
required_config = crontabber.CronTabber.required_config
required_config.add_option('logger', default=mock_logging)

json_file = os.path.join(self.tempdir, 'test.json')
assert not os.path.isfile(json_file)

config_manager = ConfigurationManager(
[required_config,
#logging_required_config(app_name)
],
app_name='crontabber',
app_description=__doc__,
values_source_list=[{
'logger': mock_logging,
'jobs': jobs_string,
'database': json_file,
}, DSN, extra_value_source]
)
return config_manager, json_file


#==============================================================================
class TestFTPScraper(TestCaseBase):
class TestFTPScraper(_TestCaseBase):

def setUp(self):
super(TestFTPScraper, self).setUp()
Expand Down Expand Up @@ -194,12 +237,12 @@ def mocked_urlopener(url):

#==============================================================================
@attr(integration='postgres') # for nosetests
class TestIntegrationFTPScraper(TestCaseBase):
class TestIntegrationFTPScraper(_TestCaseBase):

def setUp(self):
super(TestIntegrationFTPScraper, self).setUp()
# prep a fake table
assert 'test' in DSN['database_name'], DSN['database_name']
assert 'test' in databaseName.default, databaseName.default
dsn = ('host=%(database_host)s dbname=%(database_name)s '
'user=%(database_user)s password=%(database_password)s' % DSN)
self.conn = psycopg2.connect(dsn)
Expand All @@ -212,7 +255,6 @@ def setUp(self):
sunset_date = now + datetime.timedelta(days=30)

cursor.execute("""
TRUNCATE products CASCADE;
INSERT INTO products
(product_name, sort, release_name)
VALUES
Expand All @@ -224,7 +266,6 @@ def setUp(self):
""")

cursor.execute("""
TRUNCATE product_versions CASCADE;
INSERT INTO product_versions
(product_version_id, product_name, major_version, release_version,
version_string, version_sort, build_date, sunset_date,
Expand All @@ -245,7 +286,6 @@ def setUp(self):
""" % {"build_date": build_date, "sunset_date": sunset_date})

cursor.execute("""
TRUNCATE release_channels CASCADE;
INSERT INTO release_channels
(release_channel, sort)
VALUES
Expand All @@ -256,7 +296,6 @@ def setUp(self):
""")

cursor.execute("""
TRUNCATE product_release_channels CASCADE;
INSERT INTO product_release_channels
(product_name, release_channel, throttle)
VALUES
Expand Down Expand Up @@ -286,6 +325,8 @@ def tearDown(self):
cursor.execute("""
UPDATE crontabber_state SET state='{}';
TRUNCATE TABLE releases_raw CASCADE;
""")
cursor.execute("""
TRUNCATE product_versions CASCADE;
TRUNCATE products CASCADE;
TRUNCATE releases_raw CASCADE;
Expand Down
1 change: 1 addition & 0 deletions socorro/unittest/cron/jobs/test_matviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import mock
from socorro.cron import crontabber

from .base import TestCaseBase


Expand Down

0 comments on commit 35da8e3

Please sign in to comment.