forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AIRFLOW-2427] Add tests to named hive sensor
Closes apache#3323 from gglanzani/AIRFLOW-2427 (cherry picked from commit b18b437) Signed-off-by: Fokko Driesprong <[email protected]>
- Loading branch information
Showing
2 changed files
with
169 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you 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 random | ||
import unittest | ||
from datetime import timedelta | ||
|
||
from airflow import configuration, DAG, operators | ||
from airflow.sensors.named_hive_partition_sensor import NamedHivePartitionSensor | ||
from airflow.utils.timezone import datetime | ||
from airflow.hooks.hive_hooks import HiveMetastoreHook | ||
|
||
configuration.load_test_config() | ||
|
||
DEFAULT_DATE = datetime(2015, 1, 1) | ||
DEFAULT_DATE_ISO = DEFAULT_DATE.isoformat() | ||
DEFAULT_DATE_DS = DEFAULT_DATE_ISO[:10] | ||
|
||
|
||
class NamedHivePartitionSensorTests(unittest.TestCase): | ||
def setUp(self): | ||
configuration.load_test_config() | ||
args = {'owner': 'airflow', 'start_date': DEFAULT_DATE} | ||
self.dag = DAG('test_dag_id', default_args=args) | ||
self.next_day = (DEFAULT_DATE + | ||
timedelta(days=1)).isoformat()[:10] | ||
self.database = 'airflow' | ||
self.partition_by = 'ds' | ||
self.table = 'static_babynames_partitioned' | ||
self.hql = """ | ||
CREATE DATABASE IF NOT EXISTS {{ params.database }}; | ||
USE {{ params.database }}; | ||
DROP TABLE IF EXISTS {{ params.table }}; | ||
CREATE TABLE IF NOT EXISTS {{ params.table }} ( | ||
state string, | ||
year string, | ||
name string, | ||
gender string, | ||
num int) | ||
PARTITIONED BY ({{ params.partition_by }} string); | ||
ALTER TABLE {{ params.table }} | ||
ADD PARTITION({{ params.partition_by }}='{{ ds }}'); | ||
""" | ||
self.hook = HiveMetastoreHook() | ||
t = operators.hive_operator.HiveOperator( | ||
task_id='HiveHook_' + str(random.randint(1, 10000)), | ||
params={ | ||
'database': self.database, | ||
'table': self.table, | ||
'partition_by': self.partition_by | ||
}, | ||
hive_cli_conn_id='beeline_default', | ||
hql=self.hql, dag=self.dag) | ||
t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, | ||
ignore_ti_state=True) | ||
|
||
def tearDown(self): | ||
hook = HiveMetastoreHook() | ||
with hook.get_conn() as metastore: | ||
metastore.drop_table(self.database, self.table, deleteData=True) | ||
|
||
def test_parse_partition_name_correct(self): | ||
schema = 'default' | ||
table = 'users' | ||
partition = 'ds=2016-01-01/state=IT' | ||
name = '{schema}.{table}/{partition}'.format(schema=schema, | ||
table=table, | ||
partition=partition) | ||
parsed_schema, parsed_table, parsed_partition = ( | ||
NamedHivePartitionSensor.parse_partition_name(name) | ||
) | ||
self.assertEqual(schema, parsed_schema) | ||
self.assertEqual(table, parsed_table) | ||
self.assertEqual(partition, parsed_partition) | ||
|
||
def test_parse_partition_name_incorrect(self): | ||
name = 'incorrect.name' | ||
with self.assertRaises(ValueError): | ||
NamedHivePartitionSensor.parse_partition_name(name) | ||
|
||
def test_parse_partition_name_default(self): | ||
table = 'users' | ||
partition = 'ds=2016-01-01/state=IT' | ||
name = '{table}/{partition}'.format(table=table, | ||
partition=partition) | ||
parsed_schema, parsed_table, parsed_partition = ( | ||
NamedHivePartitionSensor.parse_partition_name(name) | ||
) | ||
self.assertEqual('default', parsed_schema) | ||
self.assertEqual(table, parsed_table) | ||
self.assertEqual(partition, parsed_partition) | ||
|
||
def test_poke_existing(self): | ||
partitions = ["{}.{}/{}={}".format(self.database, | ||
self.table, | ||
self.partition_by, | ||
DEFAULT_DATE_DS)] | ||
sensor = NamedHivePartitionSensor(partition_names=partitions, | ||
task_id='test_poke_existing', | ||
poke_interval=1, | ||
hook=self.hook, | ||
dag=self.dag) | ||
self.assertTrue(sensor.poke(None)) | ||
|
||
def test_poke_non_existing(self): | ||
partitions = ["{}.{}/{}={}".format(self.database, | ||
self.table, | ||
self.partition_by, | ||
self.next_day)] | ||
sensor = NamedHivePartitionSensor(partition_names=partitions, | ||
task_id='test_poke_non_existing', | ||
poke_interval=1, | ||
hook=self.hook, | ||
dag=self.dag) | ||
self.assertFalse(sensor.poke(None)) |