def step_impl(context):
    """
    :type context behave.runner.Context
    """

    perm_list = list()
    perm_cnts = dict()

    perm = mysql.select(context.dev_xmart, "select sourcelistingid from xmart.listing;")
    mysql.close_connection(context.dev_xmart)

    for listid in perm:
        listid = str(listid)
        first_quot = listid.find("'")
        comma = listid.find(",")
        perm_list.append(listid[first_quot+1:comma-1])

    for li in perm_list:
        perm_cnts[li] = perm_cnts.get(li, 0) + 1

    # print(perm_cnts)
    for key,val in perm_cnts.items():
        assert_that(val, is_(1))

    print("Assertion complete (3 of 3). Listing Table contains only unique source listing id's. \n")
def step_impl(context):
    """
    :type context behave.runner.Context
    """

    stg_list_cnts = dict()
    stg_list = list()

    stg = mysql.select(context.dev_xmart, "select sourcelistingid from xmart.stg_listing;")
    mysql.close_connection(con=context.dev_xmart)

    for row in stg:
        row = str(row)
        # print("========", id)
        first_quot = row.find("'")
        comma = row.find(",")
        stg_list.append(row[first_quot+1:comma-1])

    for aa in context.id_exception:
        assert_that(aa, is_in(stg_list))
        # if aa in stg_list:
        #     print(aa)

    for bb in stg_list:
        stg_list_cnts[bb] = stg_list_cnts.get(bb, 0) + 1
    # print(stg_list_cnts)

    for key,val in stg_list_cnts.items():
        assert_that(val, is_(1))

    print("Assertion complete (2 of 3). Listings that are in the Exceptions table appear only once in Stg table. \n")
def step_impl(context, dup_id1, dup_id2):
    """
    :type context behave.runner.Context
    """

    del_query = "delete from xmart.load_exception where sourcelistingid in (%s, %s);" % (dup_id1, dup_id2)
    del_exec = mysql.execute(context.dev_xmart, del_query)
    mysql.close_connection(context.dev_xmart)
def step_impl(context, agent_test1, agent_test2, office_test1, office_test2):
    """
    :type context behave.runner.Context
    """
    print("\n **** Scenario 3 **** \n\n")

    context.agent_test1 = agent_test1
    context.agent_test2 = agent_test2
    context.office_test1 = office_test1
    context.office_test2 = office_test2

    del_agent = "delete from xmart.agent where sourceagentid in ('%s', '%s');" % (agent_test1, agent_test2)
    da = mysql.execute(mysql.dev_mart, del_agent)

    del_office = "delete from xmart.office where sourceofficeid in ('%s', '%s');" % (office_test1, office_test2)
    do = mysql.execute(mysql.dev_mart, del_office)

    mysql.close_connection(mysql.dev_mart)
def step_impl(context, db_attr_cnt, s3_attr_cnt):
    """
    :type context behave.runner.Context
    """

    db_attr_cnt = str(mysql.select(context.conn, "Select count(*) from xmart.listing_attribute_value;"))

    first_range = db_attr_cnt.find("(")
    sec_range = db_attr_cnt.find("L")
    db_attr_cnt = int(db_attr_cnt[first_range +2 : sec_range])
    mysql.close_connection(context.conn)
    # print(db_attr_cnt)

    s3_attr_cnt = len(s3_connect.cc_attr)
    # print(s3_attr_cnt)

    assert_that(db_attr_cnt, equal_to(s3_attr_cnt))

    print("Assertion complete (2 of 3). Attribute Counts Match between S3 attributes file and Database. \n")
def step_impl(context, mart_ids, s3_ids):
    """
    :type context behave.runner.Context
    """

    mart_ids = list()
    m_ids = mysql.select(context.conn, "select * from xmart.listing")
    for line in m_ids:
        mart_ids.append(line[3])

    mart_ids.sort()
    # print(mart_ids)
    mysql.close_connection(context.conn)

    s3_ids = s3_connect.listing_id
    s3_ids.sort()
    # print(s3_ids, "\n")

    assert_that(mart_ids, equal_to(s3_ids))

    print("Assertion complete (3 of 3). Source Listing ID's Match between S3 feed file and the DB listing table. \n")
def step_impl(context):
    """
    :type context behave.runner.Context
    """
    n_office = list()

    notfound = 0

    for office in context.new_office:
        off_query = "select sourceofficeid from xmart.office nolock where sourceofficeid = '%s';" % office
        off_select = mysql.select(mysql.dev_mart, off_query)
        mysql.close_connection(mysql.dev_mart)

        n_office.append(office)

        if str(off_select) == '()':
            print("** Fail. Office not found: ", office)
            notfound = notfound + 1

    assert_that(notfound, is_(0))

    print("Assertion complete (2 of 2). New Office(s) appear in Office Table... ID's:", n_office, "\n")
def step_impl(context):
    """
    :type context behave.runner.Context
    """
    n_agent = list()

    notfound = 0

    for agent in context.new_agent:
        agt_query = "select sourceagentid from xmart.agent nolock where sourceagentid = '%s';" % agent
        agt_select = mysql.select(mysql.dev_mart, agt_query)
        mysql.close_connection(mysql.dev_mart)

        n_agent.append(agent)

        if str(agt_select) == '()':
            print("** Fail. Agent not found: ", agent)
            notfound = notfound + 1

    assert_that(notfound, is_(0))

    print("Assertion complete (1 of 2). New Agent(s) appear in Agent Table... ID's:", n_agent, "\n")
def step_impl(context):
    """
    :type context behave.runner.Context
    """

    exception = mysql.select(context.dev_xmart, "select distinct sourcelistingid from xmart.load_exception;")
    mysql.close_connection(con=context.dev_xmart)

    context.id_exception = list()

    for row in exception:
        row = str(row)
        first_quot = row.find("'")
        comma = row.find(",")
        context.id_exception.append(row[first_quot+1:comma-1])

    context.id_exception.sort()
    # print("=== ID's from Exceptions table: ", context.id_exception, "\n")

    assert_that(context.id_exception, equal_to(context.dup_feed_id))

    print("Assertion complete (1 of 3). Duplicate ID's from the Feed File have been found in the Exceptions table. \n")
def step_impl(context, stg_attr_del, stg_list_del, perm_attr_del, perm_list_del):
    """
    :type context behave.runner.Context
    """
    cur_count = str(mysql.select(context.conn, "Select count(*) from xmart.listing;"))
    cur_count = cur_count[2]

    stg_attr_del = "delete from xmart.stg_listing_attribute_value;"
    stg_list_del = "delete from xmart.stg_listing;"
    perm_attr_del = "delete from xmart.listing_attribute_value;"
    perm_list_del = "delete from xmart.listing;"

    if int(cur_count) >= 1:
        mysql.execute(con=context.conn, statement= stg_attr_del)
        mysql.execute(con=context.conn, statement= stg_list_del)
        mysql.execute(con=context.conn, statement= perm_attr_del)
        mysql.execute(con=context.conn, statement= perm_list_del)


        mysql.close_connection(context.conn)

    else:
        mysql.close_connection(context.conn)
        pass