"""Tests for ows-salessheets history model.""" from datetime import datetime, timedelta from flexmock import flexmock from sqlalchemy.exc import SQLAlchemyError from salessheets.connectors import mysql from salessheets.constants import error from salessheets.constants import field_const from salessheets.constants import models from salessheets.constants import salessheets from salessheets.models import history from tests.factories import history_job from tests.test_utils import db @db.test_schema def test_create_job(): """Test inserting single job into table.""" user_id = 'oa:1234' upcs = ['123415123', '12315136713'] generation_method = salessheets.ALLOWED_GENERATION_METHODS[1] context_type = salessheets.DISPLAY_UPC result = history.create_job( user_id=user_id, context=upcs, context_type=context_type, output_format=generation_method) assert result with mysql.ss_db_session() as session: obj = session.query(history.Job).filter_by( job_id=result.message).first() assert obj.user_id == user_id assert obj.context_type == context_type assert obj.output_format == generation_method def test_create_job_exception(): """Test create job for exception.""" user_id = 'oa:1234' upcs = ['123415123', '12315136713'] generation_method = salessheets.ALLOWED_GENERATION_METHODS[1] context_type = salessheets.DISPLAY_UPC (flexmock(mysql) .should_receive('ss_db_session') .and_raise(SQLAlchemyError)) (flexmock(mysql) .should_receive('capture_exception') .once()) result = history.create_job( user_id=user_id, context=upcs, context_type=context_type, output_format=generation_method) assert result.status == 500 def test_get_dict_from_job_object(): """Test getting dict from Job object data.""" user_id = 'oa:1234' upcs = ['123415123', '12315136713'] generation_method = salessheets.ALLOWED_GENERATION_METHODS[1] timestamp = datetime.utcnow() expected_result = { 'status': None, 'timestamp': timestamp.isoformat(), 'user_id': user_id, 'output_format': generation_method, 'id': None, 'context': upcs, 'context_type': models.UPC, } job = history.Job( user_id=user_id, context=upcs, context_type=models.UPC, output_format=generation_method, timestamp=timestamp) job_dict = job.as_dict() assert job_dict == expected_result @db.test_schema def test_get_job_by_id(): """Test getting single job by id from table.""" job = history_job.JobFactory(user_id='42', context='24') db.seed_ss_models([job]) result = history.get_job_by_id(job.job_id) assert result.status == 200 assert result.message.user_id == job.user_id assert result.message.output_format == job.output_format assert result.message.context == job.context @db.test_schema def test_get_job_by_id_no_such_job(): """Test getting job by invalid id from table.""" result = history.get_job_by_id(1) assert result.status == 404 assert result.errors['message'] == error.JOB_DOES_NOT_EXIST def test_get_job_by_id_exception(): """Test get job for exception.""" job_id = 1 (flexmock(mysql) .should_receive('ss_db_session') .and_raise(SQLAlchemyError)) (flexmock(mysql) .should_receive('capture_exception') .once()) result = history.get_job_by_id(job_id) assert result.status == 500 @db.test_schema def test_get_all_jobs_by_user_id(): """Test getting all jobs by user id from table.""" jobs = [ history_job.JobFactory(), history_job.JobFactory(), history_job.JobFactory(user_id='other') ] db.seed_ss_models(jobs) result = history.get_all_jobs_by_user_id(history_job.DEFAULT_USER) assert result.status == 200 assert len(result.message) == 2 @db.test_schema def test_get_all_jobs_by_user_id_no_such_id(): """Test getting jobs by invalid user id from table.""" result = history.get_all_jobs_by_user_id('foobar') assert result.status == 404 assert result.errors['message'] == error.JOB_DOES_NOT_EXIST def test_get_all_jobs_by_user_id_exception(): """Test get jobs by user id for exception.""" (flexmock(mysql) .should_receive('ss_db_session') .and_raise(SQLAlchemyError)) (flexmock(mysql) .should_receive('capture_exception') .once()) result = history.get_all_jobs_by_user_id('1') assert result.status == 500 @db.test_schema def test_get_all_jobs_by_user_id_timestamp_ordering(): """Test get jobs by user id with asc and desc order by timestamp.""" jobs = [ history_job.JobFactory(timestamp=datetime.utcnow()), history_job.JobFactory( timestamp=datetime.utcnow() - timedelta(days=2)), history_job.JobFactory( timestamp=datetime.utcnow() - timedelta(days=1)) ] db.seed_ss_models(jobs) sort_by = field_const.TIMESTAMP result = history.get_all_jobs_by_user_id( history_job.DEFAULT_USER, sort_by=sort_by, sort_order=field_const.ASC) assert result.status == 200 assert result.message[0].job_id == jobs[1].job_id assert result.message[1].job_id == jobs[2].job_id assert result.message[2].job_id == jobs[0].job_id result = history.get_all_jobs_by_user_id( history_job.DEFAULT_USER, sort_by=sort_by, sort_order=field_const.DESC) assert result.status == 200 assert result.message[0].job_id == jobs[0].job_id assert result.message[1].job_id == jobs[2].job_id assert result.message[2].job_id == jobs[1].job_id @db.test_schema def test_get_all_jobs_by_user_id_output_format_order(): """Test get jobs by user id with asc and desc order by output_format.""" jobs = [ history_job.JobFactory( output_format=salessheets.ALLOWED_GENERATION_METHODS[0]), history_job.JobFactory( output_format=salessheets.ALLOWED_GENERATION_METHODS[1]), ] db.seed_ss_models(jobs) sort_by = field_const.OUTPUT_FORMAT result = history.get_all_jobs_by_user_id( history_job.DEFAULT_USER, sort_by=sort_by, sort_order=field_const.ASC) assert result.status == 200 assert result.message[0].job_id == jobs[0].job_id assert result.message[1].job_id == jobs[1].job_id result = history.get_all_jobs_by_user_id( history_job.DEFAULT_USER, sort_by=sort_by, sort_order=field_const.DESC) assert result.status == 200 assert result.message[0].job_id == jobs[1].job_id assert result.message[1].job_id == jobs[0].job_id @db.test_schema def test_get_jobs_count_by_user_id(): """Test getting jobs count by user id.""" jobs = [ history_job.JobFactory(), history_job.JobFactory(user_id='other') ] db.seed_ss_models(jobs) result = history.get_jobs_count(history_job.DEFAULT_USER) assert result.status == 200 assert result.message == 1 def test_get_jobs_count_by_user_id_exception(): """Test get job for exception.""" user_id = '1234' (flexmock(mysql) .should_receive('ss_db_session') .and_raise(SQLAlchemyError)) (flexmock(mysql) .should_receive('capture_exception') .once()) result = history.get_jobs_count(user_id) assert result.status == 500 @db.test_schema def test_get_job_by_upc(): """Test getting single job by upc from table.""" upc = 2 jobs = [ history_job.JobFactory(), history_job.JobFactory( status=salessheets.COMPLETED, context=str(upc)) ] db.seed_ss_models(jobs) result = history.get_job_by_upc(upc) assert result.status == 200 assert result.message.job_id == jobs[1].job_id @db.test_schema def test_get_job_by_upc_no_such_job(): """Test getting job by upc no such job.""" result = history.get_job_by_upc(1) assert result.status == 404 assert result.errors['message'] == error.JOB_DOES_NOT_EXIST def test_get_job_by_upc_exception(): """Test get job by upc for exception.""" upc = 1 (flexmock(mysql) .should_receive('ss_db_session') .and_raise(SQLAlchemyError)) (flexmock(mysql) .should_receive('capture_exception') .once()) result = history.get_job_by_upc(upc) assert result.status == 500 @db.test_schema def test_get_job_by_release_id(): """Test getting single job by release_id from table.""" release_id = 2 jobs = [ history_job.JobFactory(), history_job.JobFactory( status=salessheets.COMPLETED, context=str(release_id)) ] db.seed_ss_models(jobs) result = history.get_job_by_release_id(release_id) assert result.status == 200 assert result.message.job_id == jobs[1].job_id @db.test_schema def test_get_job_by_release_id_job_not_found(): """Test getting job by release_id no such job.""" result = history.get_job_by_release_id(1) assert result.status == 404 assert result.errors['message'] == error.JOB_DOES_NOT_EXIST def test_get_job_by_release_id_exception(): """Test get job by release_id for exception.""" release_id = 1 (flexmock(mysql) .should_receive('ss_db_session') .and_raise(SQLAlchemyError)) (flexmock(mysql) .should_receive('capture_exception') .once()) result = history.get_job_by_release_id(release_id) assert result.status == 500