Esempio n. 1
0
def test_seed_fake_date(setup):
    """
    This test case tests that the "fake_date" parameter to seed works
    """
    crud.seed(setup["db"], fake_date=datetime(month=8, day=15, year=2020))
    max_date = setup["db"].query(func.max(models.NytLiveCounty.date)).first()
    assert max_date[0].day == 14  # 14 because newest data before 8/15 at 12am
Esempio n. 2
0
def test_seed_fake_date(setup):
    """
    This test case tests that the "fake_date" parameter to seed works
    """
    # crud.seed(fake_date=datetime(month=8, day=29, year=2020))
    today = datetime.now()
    yesterday = datetime.fromtimestamp(datetime.timestamp(today) - 86400)
    crud.seed(fake_date=datetime(
        month=yesterday.month, day=yesterday.day, year=yesterday.year))
    # asyncio.ensure_future(crud.seed(fake_date=datetime(
    # month=8, day=29, year=2020)))
    # time.sleep(360)
    max_date = setup["db"].query(func.max(models.NytLiveCounty.date)).first()
    assert (max_date[0].day == (yesterday.day - 1)
            or max_date[0].day == yesterday.day)
Esempio n. 3
0
def test_async_update(setup):
    """
    Tests that the asynchronous update function correctly updates the data
    """
    # Seed old data
    crud.seed(setup["db"], fake_date=datetime(month=8, day=15, year=2020))

    # Execute a query that causes an async update
    setup["app"].get("/api/data/all")

    # Sleep some time to give update time to work
    time.sleep(120)

    # Check that new max date is today
    today = datetime.now()
    yesterday = datetime.fromtimestamp(datetime.timestamp(today) - 86400)
    max_date = (setup["db"].query(func.max(
        models.NytLiveCounty.date)).first()[0].day)

    assert max_date == today.day or max_date == yesterday.day
Esempio n. 4
0
async def seed_db():
    asyncio.ensure_future(crud.seed())
Esempio n. 5
0
# Populate NYT database
SQLALCHEMY_DATABASE_URL = (
    f"mysql+pymysql://{os.environ['DATABASE_USER']}"
    f":{os.environ['DATABASE_PASSWORD']}"
    f"@{os.environ['DATABASE_HOST']}/{os.environ['DATABASE_NAME']}"
)

engine = sqlalchemy.create_engine(SQLALCHEMY_DATABASE_URL)
Session = sqlalchemy.orm.sessionmaker(
    autocommit=False, autoflush=False, bind=engine
)

# @app.on_event("startup")
# async def seed_db():
#    asyncio.ensure_future(crud.seed())

# asyncio.ensure_future(crud.seed())

db = Session()

crud.seed()

# db.close()


@app.exception_handler(NotFoundException)
async def not_found_exception_handler(
    request: Request, exc: NotFoundException
):
    return JSONResponse(status_code=404, content={"detail": exc.message},)