Refactor record processor

Move user-related and Launchpad-related functions from record
processor to corresponding modules.

Change-Id: I9bd08fec4bc074373255118ab1be702113c56a6f
This commit is contained in:
Ilya Shakhat
2017-08-24 18:01:01 +02:00
parent afce3ea987
commit 6c7f19e011
6 changed files with 313 additions and 189 deletions

View File

@@ -0,0 +1,46 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import testtools
from stackalytics.processor import launchpad_utils
class TestLaunchpadUtils(testtools.TestCase):
@mock.patch('stackalytics.processor.launchpad_utils._lp_profile_by_email')
def test_get_lp_info(self, lp_mock):
lp_mock.return_value = dict(name='john', display_name='smith')
observed = launchpad_utils.query_lp_info('john@smith.to')
self.assertEqual(('john', 'smith'), observed)
lp_mock.assert_called_once_with('john@smith.to')
@mock.patch('stackalytics.processor.launchpad_utils._lp_profile_by_email')
def test_get_lp_info_not_found(self, lp_mock):
lp_mock.return_value = None
observed = launchpad_utils.query_lp_info('john@smith.to')
self.assertEqual((None, None), observed)
lp_mock.assert_called_once_with('john@smith.to')
@mock.patch('stackalytics.processor.launchpad_utils._lp_profile_by_email')
def test_get_lp_info_invalid_email(self, lp_mock):
observed = launchpad_utils.query_lp_info('error.root')
self.assertEqual((None, None), observed)
lp_mock.assert_not_called()

View File

@@ -24,6 +24,7 @@ from stackalytics.processor import config
from stackalytics.processor import record_processor
from stackalytics.processor import runtime_storage
from stackalytics.processor import user_processor
from stackalytics.processor.user_processor import get_company_by_email
from stackalytics.processor import utils
@@ -62,12 +63,12 @@ class TestRecordProcessor(testtools.TestCase):
self.read_launchpad = self.read_json_from_uri_patch.start()
self.lp_profile_by_launchpad_id_patch = mock.patch(
'stackalytics.processor.launchpad_utils.'
'lp_profile_by_launchpad_id')
'_lp_profile_by_launchpad_id')
self.lp_profile_by_launchpad_id = (
self.lp_profile_by_launchpad_id_patch.start())
self.lp_profile_by_launchpad_id.return_value = None
self.lp_profile_by_email_patch = mock.patch(
'stackalytics.processor.launchpad_utils.lp_profile_by_email')
'stackalytics.processor.launchpad_utils._lp_profile_by_email')
self.lp_profile_by_email = (
self.lp_profile_by_email_patch.start())
self.lp_profile_by_email.return_value = None
@@ -86,7 +87,7 @@ class TestRecordProcessor(testtools.TestCase):
companies=[{'company_name': 'IBM', 'domains': ['ibm.com']}]
)
email = 'jdoe@ibm.com'
res = record_processor_inst._get_company_by_email(email)
res = get_company_by_email(record_processor_inst.domains_index, email)
self.assertEqual('IBM', res)
def test_get_company_by_email_with_long_suffix_mapped(self):
@@ -94,7 +95,7 @@ class TestRecordProcessor(testtools.TestCase):
companies=[{'company_name': 'NEC', 'domains': ['nec.co.jp']}]
)
email = 'man@mxw.nes.nec.co.jp'
res = record_processor_inst._get_company_by_email(email)
res = get_company_by_email(record_processor_inst.domains_index, email)
self.assertEqual('NEC', res)
def test_get_company_by_email_with_long_suffix_mapped_2(self):
@@ -103,23 +104,15 @@ class TestRecordProcessor(testtools.TestCase):
'domains': ['nec.co.jp', 'nec.com']}]
)
email = 'man@mxw.nes.nec.com'
res = record_processor_inst._get_company_by_email(email)
res = get_company_by_email(record_processor_inst.domains_index, email)
self.assertEqual('NEC', res)
def test_get_company_by_email_not_mapped(self):
record_processor_inst = self.make_record_processor()
email = 'foo@boo.com'
res = record_processor_inst._get_company_by_email(email)
res = get_company_by_email(record_processor_inst.domains_index, email)
self.assertIsNone(res)
# get_lp_info
def test_get_lp_info_invalid_email(self):
self.read_launchpad.return_value = None
record_processor_inst = self.make_record_processor(users=[])
self.assertEqual((None, None),
record_processor_inst._get_lp_info('error.root'))
# commit processing
def test_process_commit_existing_user(self):

View File

@@ -85,3 +85,34 @@ class TestUserProcessor(testtools.TestCase):
updated_user = user_processor.update_user_profile(stored_user, user)
self.assertTrue(updated_user.get('static'))
def test_are_users_same(self):
users = [
dict(seq=1),
dict(seq=1),
dict(seq=1),
]
self.assertTrue(user_processor.are_users_same(users))
def test_are_users_same_none(self):
users = [
{},
{},
]
self.assertFalse(user_processor.are_users_same(users))
def test_are_users_not_same(self):
users = [
dict(seq=1),
dict(seq=2),
dict(seq=1),
]
self.assertFalse(user_processor.are_users_same(users))
def test_are_users_not_same_2(self):
users = [
dict(seq=1),
dict(seq=1),
{}
]
self.assertFalse(user_processor.are_users_same(users))