Ejemplo n.º 1
0
def test_checkNewRowNeededFail():
    """Verify and exception is thrown because both rows have all
    the same values except for the Deal Registration Group"""

    oldRow = DealRegRow({
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'IBM Brand',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ACTIVE_FLAG': 'Y',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    newRow = DealRegRow({
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ACTIVE_FLAG': 'Y',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    with pytest.raises(ValueError):
        process_deal_reg_data.checkNewRowNeeded(oldRow, newRow)
Ejemplo n.º 2
0
def test_checkNewRowNeededPass_useActiveFlag():
    """Verify that the function returns true because both rows
    have the same end date and add date but the new row has an
    active flag of Y while the old one has N."""

    oldRow = DealRegRow({
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ACTIVE_FLAG': 'N',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    newRow = DealRegRow({
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ACTIVE_FLAG': 'Y',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    assert process_deal_reg_data.checkNewRowNeeded(oldRow, newRow) == True
def addNewRow(validRow, allRows):
    """Create new DealRegRow object from valid row, check to see if
    a duplicate already exists, determine which row to keep if it does or
    add the new row if it doesn't"""

    newRow = DealRegRow(validRow)

    # If a duplicate exists only one should be found because this check is preformed each time
    # a new row needs to be added
    duplicateRow = next((item for item in allRows if item.partNum == newRow.partNum), None)

    if duplicateRow == None:
        allRows.append(newRow)
    else:
        if duplicateRow.areEqual(newRow):
            # New row does not need to be added because an exact match already exists
            pass
        else:
            newRowNeeded = checkNewRowNeeded(duplicateRow, newRow)

            if newRowNeeded:
                #Remove old Row first then add new one
                allRows.remove(duplicateRow)
                allRows.append(newRow)
            else:
                # New row is not needed and old row needs to stay
                pass
def test_updateDealRegTableWithRowsFail_InvalidRowData():
    """Value Error should be thrown if invalid part number is passed in"""

    newRow = DealRegRow({'PART_NUM': '000796', 'DEAL_REG_GROUP': 'Service Action',
                          'START_DATE': datetime(2018, 10, 9, 0, 0), 'END_DATE': datetime(9999, 12, 31, 0, 0),
                          'ADD_DATE': datetime(2019, 12, 5, 21, 30, 21), 'MODIFIED_DATE': datetime(2019, 12, 5, 21, 30, 21)})

    with pytest.raises(ValueError):
        deal_reg_db.updateDealRegTableWithRows([newRow])
Ejemplo n.º 5
0
def test_addNewRowPass_addNew():
    """Verify that the new row is added to the list
    because no duplicate rows were found."""

    row1 = DealRegRow({
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(2019, 12, 9, 0, 0),
        'ACTIVE_FLAG': 'N',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    row2 = DealRegRow({
        'PART_NUM': 'SAPT000797',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ACTIVE_FLAG': 'Y',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    row3 = {
        'PART_NUM': 'SAPT000798',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ACTIVE_FLAG': 'Y',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    }

    allRows = [row1, row2]

    process_deal_reg_data.addNewRow(row3, allRows)

    assert allRows[0] == row1 and allRows[1] == row2 and DealRegRow(
        row3).areEqual(allRows[2])


#endregion
Ejemplo n.º 6
0
def test_addNewRowPass_RowsDiffReplace():
    """Verify that the old row is deleted from the list
    and the new row is added to the end because the duplicate
    row has an end date that comes before the new one."""

    row1 = DealRegRow({
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(2019, 12, 9, 0, 0),
        'ACTIVE_FLAG': 'N',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    row2 = DealRegRow({
        'PART_NUM': 'SAPT000797',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ACTIVE_FLAG': 'Y',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    row3 = {
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ACTIVE_FLAG': 'Y',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    }

    allRows = [row1, row2]

    process_deal_reg_data.addNewRow(row3, allRows)

    assert row2.areEqual(allRows[0]) and DealRegRow(row3).areEqual(allRows[1])
Ejemplo n.º 7
0
def test_addNewRowPass_RowsDiffKeep():
    """Verify that the existing list of items isn't modified because
    the duplicate row has an end date greater than that of the new row."""

    row1 = DealRegRow({
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ACTIVE_FLAG': 'Y',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    row2 = DealRegRow({
        'PART_NUM': 'SAPT000797',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ACTIVE_FLAG': 'Y',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    row3 = {
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(2019, 12, 9, 0, 0),
        'ACTIVE_FLAG': 'N',
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2019, 12, 5, 21, 30, 21)
    }

    allRows = [row1, row2]

    process_deal_reg_data.addNewRow(row3, allRows)

    assert allRows == [row1, row2]
def test_updateDealRegTableWithRowsFail_InvalidDatabase():
    """Exception should be thrown if invalid connections is set in environment"""

    # Set up deal_reg_db file globals
    os.environ['DEAL_REG_DB_URL'] = 'postgresql://postgres@localhost:5000/TestDB'
    deal_reg_db.engine = create_engine(os.getenv('DEAL_REG_DB_URL'))
    deal_reg_db.Session = sessionmaker(bind=deal_reg_db.engine)

    newRow = DealRegRow({'PART_NUM': 'SWPT000796', 'DEAL_REG_GROUP': 'Service Action',
                          'START_DATE': datetime(2018, 10, 9, 0, 0), 'END_DATE': datetime(9999, 12, 31, 0, 0),
                          'ADD_DATE': datetime(2019, 12, 5, 21, 30, 21), 'MODIFIED_DATE': datetime(2019, 12, 5, 21, 30, 21)})

    with pytest.raises(Exception):
        deal_reg_db.updateDealRegTableWithRows([newRow])
Ejemplo n.º 9
0
def test_checkNewRowNeededPass_useEndDate():
    """Verify that the function returns false because the old row
    has and end date that comes after the new row."""

    oldRow = DealRegRow({
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(9999, 12, 31, 0, 0),
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2018, 12, 27, 21, 30, 21)
    })

    newRow = DealRegRow({
        'PART_NUM': 'SAPT000796',
        'DEAL_REG_GROUP': 'Service Action',
        'START_DATE': datetime(2018, 10, 9, 0, 0),
        'END_DATE': datetime(2019, 10, 31, 0, 0),
        'ADD_DATE': datetime(2018, 12, 27, 21, 30, 21),
        'MODIFIED_DATE': datetime(2019, 10, 27, 21, 30, 21)
    })

    assert process_deal_reg_data.checkNewRowNeeded(oldRow, newRow) == False
def test_updateDealRegTableWithRowsPass_EmptyTblRowAdd():
    """New Row Should be added to empty/missing table"""

    # Set up deal_reg_db file globals
    os.environ['DEAL_REG_DB_URL'] = 'postgresql://postgres@localhost:5432/TestDB'
    deal_reg_db.engine = create_engine(os.getenv('DEAL_REG_DB_URL'))
    deal_reg_db.Session = sessionmaker(bind=deal_reg_db.engine)

    # Create Local session info
    testEngine = create_engine(os.getenv('DEAL_REG_DB_URL'))
    testSession = sessionmaker(bind=testEngine)

    # Access DB and drop table if it exists
    if not database_exists(testEngine.url):
        create_database(testEngine.url)

    activeSession = testSession()

    # Drop table if there
    testEngine.execute('DROP TABLE IF EXISTS tbl_deal_reg;')

    activeSession.commit()
    activeSession.close()

    newRow = DealRegRow({'PART_NUM': 'SWPT000796', 'DEAL_REG_GROUP': 'Service Action',
                          'START_DATE': datetime(2018, 10, 9, 0, 0), 'END_DATE': datetime(9999, 12, 31, 0, 0),
                          'ADD_DATE': datetime(2019, 12, 5, 21, 30, 21), 'MODIFIED_DATE': datetime(2019, 12, 5, 21, 30, 21)})

    # Add tble and row to database
    deal_reg_db.updateDealRegTableWithRows([newRow])

    # Verify new row exists
    activeSession = testSession()

    result = activeSession.query(deal_reg_db.DealRegTable) \
                            .filter(deal_reg_db.DealRegTable.part_num == 'SWPT000796').all()

    assert not result == []

    activeSession.commit()
    activeSession.close()
def test_updateDealRegTableWithRowsPass_ExistingTblRowAdd():
    """Existing rows with same part number prefix should be deleted
    and new row should be added to table"""

    # Set up deal_reg_db file globals
    os.environ['DEAL_REG_DB_URL'] = 'postgresql://postgres@localhost:5432/TestDB'
    deal_reg_db.engine = create_engine(os.getenv('DEAL_REG_DB_URL'))
    deal_reg_db.Session = sessionmaker(bind=deal_reg_db.engine)

    # Create Local session info
    testEngine = create_engine(os.getenv('DEAL_REG_DB_URL'))
    testSession = sessionmaker(bind=testEngine)

    # Access DB and drop table if it exists
    if not database_exists(testEngine.url):
        create_database(testEngine.url)

    # Drop table if there
    testEngine.execute('DROP TABLE IF EXISTS tbl_deal_reg;')

    # Add tbl back and start session
    deal_reg_db.Base.metadata.create_all(testEngine)

    activeSession = testSession()

    # Create existing row to be deleted by function
    initialRow = DealRegRow({'PART_NUM': 'SWPT000795', 'DEAL_REG_GROUP': 'Service Action',
                          'START_DATE': datetime(2018, 10, 9, 0, 0), 'END_DATE': datetime(9999, 12, 31, 0, 0),
                          'ADD_DATE': datetime(2019, 12, 5, 21, 30, 21), 'MODIFIED_DATE': datetime(2019, 12, 5, 21, 30, 21)})

    activeSession.add(deal_reg_db.DealRegTable(initialRow))

    initialRowsFound = activeSession.query(deal_reg_db.DealRegTable) \
                               .filter(deal_reg_db.DealRegTable.part_num == 'SWPT000795') \
                               .all()

    if initialRowsFound == []:
        activeSession.commit()
        activeSession.close()
        raise Exception('Error: Setup failed. Inital row could not be added to table')

    activeSession.commit()
    activeSession.close()

    newRow = DealRegRow({'PART_NUM': 'SWPT000796', 'DEAL_REG_GROUP': 'Service Action',
                          'START_DATE': datetime(2018, 10, 9, 0, 0), 'END_DATE': datetime(9999, 12, 31, 0, 0),
                          'ADD_DATE': datetime(2019, 12, 5, 21, 30, 21), 'MODIFIED_DATE': datetime(2019, 12, 5, 21, 30, 21)})

    # Add tble and row to database
    deal_reg_db.updateDealRegTableWithRows([newRow])

    # Verify new row exists
    activeSession = testSession()

    add_result = activeSession.query(deal_reg_db.DealRegTable) \
                            .filter(deal_reg_db.DealRegTable.part_num == 'SWPT000796') \
                            .all()

    delete_result = activeSession.query(deal_reg_db.DealRegTable) \
                                 .filter(deal_reg_db.DealRegTable.part_num == 'SWPT000795') \
                                 .all()
    assert not add_result == [] and delete_result == []

    activeSession.commit()
    activeSession.close()