def test_get_priority_if_present_with_data_having_no_attribute_priority():
    """
    GIVEN a StoreAFilesMd5MicroService instance
    WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument
    THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db
    collection should be incremented by 1
    """
    data = {}

    common_lib = CommonLib()
    assert common_lib.get_priority_if_present(data) == 100
Пример #2
0
def test_register_to_event_listeners_with_1_url_to_register_having_attributes_transaction_id_and_datetime_start(
        redis_client):
    """
    GIVEN a StoreAFilesMd5MicroService instance
    WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument
    THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db
    collection should be incremented by 1
    """
    event_id = 'sequence_video_clips-melt_ended_event_1'
    url = 'http://some_url_1.com/'
    priority = 100
    datetime_start = '21200518193101.012345'  # YYmmddHHMMSS.nnnnnn datetime format in string
    transaction_id = '20200518193802.123456'  # YYmmddHHMMSS.nnnnnn datetime format in string
    z_url = {url: priority}
    h_url = {url: {
        "datetime_start": datetime_start,
        "transaction_id": transaction_id,
    }}
    response = {}

    common_lib = CommonLib()
    common_lib.CONFIG['TESTING'] = conftest.CONFIG['TESTING']
    common_lib.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST']
    common_lib.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB']
    common_lib.establish_redis_connection()
    common_lib.register_to_event_listeners(event_id, z_url, h_url, response)
    assert redis_client.zcard(sorted_set_events_with_listeners) == 1
    assert redis_client.zcard(sorted_set_event_listeners + ':' + event_id) == 1
    assert redis_client.hlen(hash_event_listeners + ':' + event_id) == 1
    assert redis_client.zrange(
        sorted_set_event_listeners +
        ':' + event_id, 0, -1, withscores=True)[0] == (
               url.encode('UTF-8'),
               priority
           )
    data = json.loads(redis_client.hmget(
        hash_event_listeners +
        ':' + event_id, url)[0].decode('UTF-8'))
    assert data is not None
    expected_data = {
        "datetime_start": datetime_start,
        "transaction_id": transaction_id,
    }
    assert data == expected_data
    assert response == {"registered_for_events": {
        event_id: {
            url: expected_data,
        }
    }}
def test_register_for_retro_event_retrieval_with_1_url_to_register_having_attribute_transaction_id(
        redis_client):
    """
    GIVEN a StoreAFilesMd5MicroService instance
    WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument
    THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db
    collection should be incremented by 1
    """
    event_id = 'sequence_video_clips-melt_ended_event_1'
    url = 'http://some_url_1.com/'
    priority = 100
    transaction_id = '20200518193802.123456'  # YYmmddHHMMSS.nnnnnn datetime format in string
    data = {
        "url": url,
        "transaction_id": transaction_id,
    }
    response = {}
    expected_h_retro_event_listeners = {
        "transaction_id": transaction_id,
        "event_id": event_id,
        "url": url,
    }
    expected_result_data = {"transaction_id": transaction_id}
    expected_response = {
        "registered_for_retro_event_retrieval": {
            event_id: {
                url: expected_result_data,
            }
        }
    }

    common_lib = CommonLib()
    common_lib.CONFIG['TESTING'] = conftest.CONFIG['TESTING']
    common_lib.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST']
    common_lib.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB']
    common_lib.establish_redis_connection()
    common_lib.register_for_retro_event_retrieval(event_id, data, response)
    assert redis_client.zcard(sorted_set_retro_event_listeners) == 1
    assert redis_client.hlen(hash_retro_event_listeners) == 1
    assert redis_client.zrange(sorted_set_retro_event_listeners,
                               0,
                               -1,
                               withscores=True)[0] == (url.encode('UTF-8'),
                                                       priority)
    retrieved_h_retro_event_listeners = json.loads(
        redis_client.hmget(hash_retro_event_listeners, url)[0].decode('UTF-8'))
    assert retrieved_h_retro_event_listeners is not None
    assert retrieved_h_retro_event_listeners == expected_h_retro_event_listeners
    assert response == expected_response
def test_prepare_to_register_for_event_listeners_with_data_having_attribute_datetime_end(
):
    """
    GIVEN a StoreAFilesMd5MicroService instance
    WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument
    THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db
    collection should be incremented by 1
    """
    url = 'http://some_url_4.com/'
    priority = 100
    datetime_end = '21200518193101.012345'  # YYmmddHHMMSS.nnnnnn datetime format in string
    z_urls = {}
    h_urls = {}
    data = {
        "url": url,
        "datetime_end": datetime_end,
    }

    common_lib = CommonLib()
    common_lib.prepare_to_register_for_event_listeners(data, z_urls, h_urls)
    assert z_urls[url] == priority
    assert h_urls[url] == {"datetime_end": datetime_end}
def test_prepare_to_register_for_event_listeners_with_data_having_attribute_count(
):
    """
    GIVEN a StoreAFilesMd5MicroService instance
    WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument
    THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db
    collection should be incremented by 1
    """
    url = 'http://some_url_1.com/'
    priority = 100
    count = 5
    z_urls = {}
    h_urls = {}
    data = {
        "url": url,
        "count": count,
    }

    common_lib = CommonLib()
    common_lib.prepare_to_register_for_event_listeners(data, z_urls, h_urls)
    assert z_urls[url] == priority
    assert h_urls[url] == {"count": count}
def test_reduce_count_if_attribute_is_present_without_any_registered_event_listeners(
        redis_client):
    """
    GIVEN a StoreAFilesMd5MicroService instance
    WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument
    THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db
    collection should be incremented by 1
    """
    common_lib = CommonLib()
    common_lib.CONFIG['TESTING'] = conftest.CONFIG['TESTING']
    common_lib.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST']
    common_lib.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB']
    common_lib.establish_redis_connection()
    result = common_lib.reduce_count_if_attribute_is_present(
        "sequence_video_clips-melt_ended_event", "http://some_url.com/")
    assert len(result) is 0
def test_prepare_to_register_for_event_listeners_with_data_having_attribute_required_data(
):
    """
    GIVEN a StoreAFilesMd5MicroService instance
    WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument
    THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db
    collection should be incremented by 1
    """
    url = 'http://some_url_7.com/'
    priority = 100
    required_data = [
        "data_key_1", "data_key_2"
    ]  # Dictionary data meant for the listener that is set only during registration
    z_urls = {}
    h_urls = {}
    data = {
        "url": url,
        "required_data": required_data,
    }

    common_lib = CommonLib()
    common_lib.prepare_to_register_for_event_listeners(data, z_urls, h_urls)
    assert z_urls[url] == priority
    assert h_urls[url] == {"required_data": required_data}
def test_prepare_to_register_for_event_listeners_with_data_having_attribute_expire_after_set_to_5_seconds(
):
    """
    GIVEN a StoreAFilesMd5MicroService instance
    WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument
    THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db
    collection should be incremented by 1
    """
    url = 'http://some_url_2.com/'
    priority = 100
    expire_after = 1.0 * 5  # expire after 5 secs
    z_urls = {}
    h_urls = {}

    common_lib = CommonLib()
    common_lib.prepare_to_register_for_event_listeners(
        {
            "url": url,
            "expire_after": 0
        }, z_urls, h_urls)
    current_time = h_urls[url]["datetime_end"]
    assert z_urls[url] == priority
    assert h_urls[url] == {"datetime_end": current_time}
    common_lib.prepare_to_register_for_event_listeners(
        {
            "url": url,
            "expire_after": expire_after
        }, z_urls, h_urls)
    datetime_end_after_5_sec = h_urls[url]["datetime_end"]
    assert z_urls[url] == priority
    assert h_urls[url] == {"datetime_end": datetime_end_after_5_sec}
    assert (datetime.strptime(datetime_end_after_5_sec, "%Y%m%d%H%M%S.%f") -
            datetime.strptime(current_time,
                              "%Y%m%d%H%M%S.%f")).total_seconds() >= 5.0
    assert (datetime.strptime(datetime_end_after_5_sec, "%Y%m%d%H%M%S.%f") -
            datetime.strptime(current_time,
                              "%Y%m%d%H%M%S.%f")).total_seconds() < 6.0
 def get_common_lib_instance(self):
     if self.common_lib is None:
         self.common_lib = CommonLib()
     return self.common_lib