def test_investment_projects_get_updated():
    """Tests that investment projects get updated when running load again."""
    investment_projects = InvestmentProjectFactory.create_batch(10)
    extract = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = extract.load()
    assert (0, 10) == (updated, created)

    dashboard = MIInvestmentProject.objects.values(*extract.COLUMNS).all()
    for row in dashboard:
        source_row = extract.get_rows().get(pk=row['dh_fdi_project_id'])
        assert source_row == row

    for investment_project in investment_projects:
        investment_project.number_new_jobs = 100000000
        investment_project.save()

    updated, created = extract.load()
    assert (10, 0) == (updated, created)

    dashboard = MIInvestmentProject.objects.values(*extract.COLUMNS).all()
    for row in dashboard:
        source_row = extract.get_rows().get(pk=row['dh_fdi_project_id'])
        assert row['number_new_jobs'] == 100000000
        assert source_row == row
def test_load_investment_projects():
    """Tests that investment projects are loaded to FDIDashboard table."""
    InvestmentProjectFactory.create_batch(10)
    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 10) == (updated, created)

    dashboard = MIInvestmentProject.objects.values(*etl.COLUMNS).all()
    for row in dashboard:
        source_row = etl.get_rows().get(pk=row['dh_fdi_project_id'])
        assert source_row == row
def test_run_mi_investment_project_etl_pipeline():
    """Tests that run_mi_investment_project_etl_pipeline copy data to MIInvestmentProject table."""
    InvestmentProjectFactory.create_batch(5, actual_land_date=date(2018, 4, 1))
    InvestmentProjectFactory.create_batch(5,
                                          actual_land_date=date(2018, 3, 31))

    updated, created = run_mi_investment_project_etl_pipeline()
    assert (0, 10) == (updated, created)

    etl = ETLInvestmentProjects(destination=MIInvestmentProject)
    dashboard = MIInvestmentProject.objects.values(
        *ETLInvestmentProjects.COLUMNS).all()
    for row in dashboard:
        source_row = etl.get_rows().get(pk=row['dh_fdi_project_id'])
        assert source_row == row
def test_run_pipeline(caplog):
    """Tests that run_pipeline command copy investment projects to MIInvestmentProject table."""
    caplog.set_level('INFO')

    InvestmentProjectFactory.create_batch(5)

    management.call_command(run_pipeline.Command())

    assert 'Updated "0" and created "5" investment projects.' in caplog.text
    assert len(caplog.records) == 1

    etl = ETLInvestmentProjects(destination=MIInvestmentProject)
    dashboard = MIInvestmentProject.objects.values(
        *ETLInvestmentProjects.COLUMNS)
    for row in dashboard:
        source_row = etl.get_rows().get(pk=row['dh_fdi_project_id'])
        assert source_row == row