def testEmptyConfigFileKeyRaises(self): with patch('rubbish_geo_common.db_ops.APPDIR', new=get_app_dir()): with open(pathlib.Path(TEST_APP_DIR_TMPDIR) / 'config', 'w') as f: f.write( "[nonlocal]\nconnstr = foo\nconntype = local\nconname = unset" ) with pytest.raises(ValueError): return get_db('local')
def testPresentConfigFileKeyWorks(self): with patch('rubbish_geo_common.db_ops.APPDIR', new=get_app_dir()): with open(pathlib.Path(TEST_APP_DIR_TMPDIR) / 'config', 'w') as f: f.write( "[local]\nconnstr = foo\nconntype = local\nconname = unset" ) expected = ('foo', 'local', 'unset') result = get_db('local') assert result == expected
def POST_pickups(request): """ This function services a POST request writing one or more Rubbish runs into the database. Expects a JSON payload with the following shape: ``` { "firebase_run_id": [ { "firebase_run_id": <int>, "firebase_id": <int>, "type": <str; from {'tobacco', 'paper', 'plastic', 'other', 'food', 'glass'}>, "timestamp": <int; UTC UNIX timestamp>, "curb": <{'left', 'right', 'middle', None}; side of the street>, "geometry": <str; POINT in WKT format> } ] } ``` """ try: get_db(RUBBISH_GEO_ENV) except: logger.log_struct({ "level": "error", "message": "Could not connect to the database." }) abort(400) request = request.get_json() logger.log_struct({ "level": "info", "message": f"Processing POST_pickups({list(request.keys())})." }) for firebase_run_id in request: run = request[firebase_run_id] for pickup in run: pickup['geometry'] = shapely.wkt.loads(pickup['geometry']) # TODO: support custom pickup types. pickup_type = pickup['type'] pickup_id = pickup['firebase_id'] if pickup_type not in RUBBISH_TYPES: logger.log_struct({ "level": "warning", "message": (f"Pickup {pickup_id!r} has custom pickup type {pickup_type!r}. " f"rubbish-geo does not support custom types yet. Replacing with 'other'." ) }) pickup['type'] = 'other' try: write_pickups(run, RUBBISH_GEO_ENV, logger=logger) except: logger.log_struct({ "level": "error", "message": "Run write did not succeed." }) abort(400) return {"status": 200}
def testEmptyConfigFileRaises(self): with patch('rubbish_geo_common.db_ops.APPDIR', new=get_app_dir()): (pathlib.Path(TEST_APP_DIR_TMPDIR) / 'config').touch() with pytest.raises(ValueError): return get_db('local')
def testNonexistantConfigFileRaises(self): with patch('rubbish_geo_common.db_ops.APPDIR', new=get_app_dir()): with pytest.raises(ValueError): return get_db('local')