def _assert_db_updated(self, mock_model, mock_sa_or): '''Checks if the Package schema's type attributes were updated''' # only look if a subset (i.e. filter was used) of the package schema # gets dataset as type mock_model.Session.query.assert_called_with(mock_model.Package) self.assertEquals(mock_model.Session.query.call_count, 2, 'Expecting 2 calls to Session.query') self.assertTrue(call.query().filter(mock_sa_or).update( {'type': u'dataset'}) in mock_model.Session.mock_calls) self.assertTrue(call.query().filter(mock_sa_or).filter(True) in mock_model.Session.mock_calls) mock_model.repo.commit.assert_any_call()
def test_num_stale_accounts(self): accts = [ Mock(is_stale=True, is_active=True), Mock(is_stale=False, is_active=True) ] with patch('%s.db_session' % pbm) as mock_db: mock_db.query.return_value.filter.return_value\ .all.return_value = accts res = NotificationsController.num_stale_accounts() assert res == 1 assert mock_db.mock_calls[0] == call.query(Account) assert mock_db.mock_calls[2] == call.query().filter().all()
def test_unreconciled(self): m_db = Mock() m_q = Mock(spec_set=Query) m_filt = Mock(spec_set=Query) m_db.query.return_value = m_q m_q.filter.return_value = m_filt res = OFXTransaction.unreconciled(m_db) assert res == m_filt assert len(m_db.mock_calls) == 2 assert m_db.mock_calls[0] == call.query(OFXTransaction) kall = m_db.mock_calls[1] assert kall[0] == 'query().filter' expected1 = OFXTransaction.reconcile.__eq__(null()) cutoff = datetime(2017, 3, 17, 0, 0, 0, tzinfo=UTC) expected2 = OFXTransaction.date_posted.__ge__(cutoff) expected3 = OFXTransaction.account.has(reconcile_trans=True) assert len(kall[1]) == 8 assert str(expected1) == str(kall[1][0]) assert binexp_to_dict(expected2) == binexp_to_dict(kall[1][1]) assert str(kall[1][2]) == str(expected3) assert str(OFXTransaction.is_payment.__ne__(True)) == str(kall[1][3]) assert str(OFXTransaction.is_late_fee.__ne__(True)) == str(kall[1][4]) assert str(OFXTransaction.is_interest_charge.__ne__(True)) == str( kall[1][5]) assert str(OFXTransaction.is_other_fee.__ne__(True)) == str(kall[1][6]) assert str(OFXTransaction.is_interest_payment.__ne__(True)) == str( kall[1][7])
def _assert_db_not_updated(self, mock_model, mock_sa_or): '''Asserts that the datbase logic was not called''' mock_model.Session.query.assert_called_once_with(mock_model.Package) self.assertTrue( call.query().filter(mock_sa_or).filter(True) in mock_model.Session.mock_calls ) mock_model.repo.commit.assert_not_called()
def test_init(self): with patch('airflow.settings.Session') as mock_session: mock_session.return_value = UnifiedAlchemyMagicMock( data=[([ call.query(Connection), call.filter(Connection.conn_id == AZURE_CONN_ID) ], [AZURE_CONN]), ([ call.query(Connection), call.filter(Connection.conn_id == HDI_CONN_ID) ], [HDI_CONN])]) op = ConnectedAzureHDInsightCreateClusterOperator( cluster_name=CLUSTER_NAME, azure_conn_id=AZURE_CONN_ID, hdi_conn_id=HDI_CONN_ID, task_id='foo', dag=self.dag) self.assertEqual(op.cluster_params, CLUSTER_PARAMS) self.assertEqual(op.params, AZURE_CONN.extra_dejson)
def test_unreconciled(self): Transaction() m_db = Mock() m_q = Mock(spec_set=Query) m_filt = Mock(spec_set=Query) m_db.query.return_value = m_q m_q.filter.return_value = m_filt res = Transaction.unreconciled(m_db) assert res == m_filt assert len(m_db.mock_calls) == 2 assert m_db.mock_calls[0] == call.query(Transaction) kall = m_db.mock_calls[1] assert kall[0] == 'query().filter' expected1 = Transaction.reconcile.__eq__(null()) expected2 = Transaction.date.__ge__(date(2017, 3, 17)) expected3 = Transaction.account.has(reconcile_trans=True) assert len(kall[1]) == 3 assert str(expected1) == str(kall[1][0]) assert binexp_to_dict(expected2) == binexp_to_dict(kall[1][1]) assert str(expected3) == str(kall[1][2])
def test_execute(self): with patch('airflow.settings.Session') as mock_session: with patch('airflowhdi.operators.azure_hdinsight_ssh_operator.SSHHook') as ssh_hook: with patch('airflowhdi.operators.azure_hdinsight_ssh_operator.AzureHDInsightHook') as mock_hdi_hook: with patch.object(SSHOperator, 'execute') as mock_ssh_op_exec: ssh_endpoint = 'loc' ssh_port = 523 mock_session.return_value = UnifiedAlchemyMagicMock(data=[ ( [call.query(Connection), call.filter(Connection.conn_id == AZURE_CONN_ID)], [AZURE_CONN] ) ]) mock_hdi_hook.return_value.get_cluster_state.return_value = ClusterGetProperties( cluster_definition=None, connectivity_endpoints=[ConnectivityEndpoint( name='SSH', location=ssh_endpoint, port=ssh_port )] ) mock_hdi_hook.return_value.get_connection.return_value = AZURE_CONN op = AzureHDInsightSshOperator( command='date', cluster_name=CLUSTER_NAME, azure_conn_id=AZURE_CONN_ID, task_id='foo', dag=self.dag) op.execute(None) ssh_hook.assert_called_once_with( remote_host=ssh_endpoint, port=ssh_port, username=AZURE_CONN.extra_dejson['SSH_USER_NAME'], password=AZURE_CONN.extra_dejson['SSH_PASSWORD'] ) mock_ssh_op_exec.assert_called_once()