def test_insert_many(site_dao): site1 = Site(id=1, capacity=10.0, panels=100, address="100 SE Pine St.", city="Portland", state="OR", postal_code="97202") site2 = Site(id=2, capacity=25.0, panels=110, address="101 SW Ankeny", city="Portland", state="OR", postal_code="97203") site3 = Site(id=3, capacity=100.0, panels=155, address="201 SE Burnside", city="Portland", state="OR", postal_code="97204") site_dao.insert_many(site1, site2, site3) assert site_dao.find_by_id(1) == site1 assert site_dao.find_by_id(2) == site2 assert site_dao.find_by_id(3) == site3
def test_find_all(site_geo_dao): site1 = Site(id=1, capacity=10.0, panels=100, address="100 SE Pine St.", city="Portland", state="OR", postal_code="97202", coordinate=PORTLAND) site2 = Site(id=2, capacity=25.0, panels=110, address="101 SW Ankeny", city="Portland", state="OR", postal_code="97203", coordinate=PORTLAND) site3 = Site(id=3, capacity=100.0, panels=155, address="201 SE Burnside", city="Portland", state="OR", postal_code="97204", coordinate=PORTLAND) site_geo_dao.insert_many(site1, site2, site3) assert site_geo_dao.find_all() == {site1, site2, site3}
def test_find_sites_by_geo(site_geo_dao): site1 = Site(id=1, capacity=10.0, panels=100, address="100 SE Pine St.", city="Portland", state="OR", postal_code="97202", coordinate=PORTLAND) site2 = Site(id=2, capacity=25.0, panels=110, address="101 SW Ankeny", city="Portland", state="OR", postal_code="97203", coordinate=PORTLAND) site3 = Site(id=3, capacity=100.0, panels=155, address="9585 SW Washington Sq.", city="Beaverton", state="OR", postal_code="97223", coordinate=BEAVERTON) site_geo_dao.insert_many(site1, site2, site3) query = GeoQuery(coordinate=PORTLAND, radius=1, radius_unit=GeoUnit.MI) assert site_geo_dao.find_by_geo(query) == {site1, site2}
def sites(redis, key_schema): site_dao = SiteDaoRedis(redis, key_schema) site1 = Site(id=1, capacity=10.0, panels=100, address="100 SE Pine St.", city="Portland", state="OR", postal_code="97202", coordinate=Coordinate(lat=1, lng=1)) site2 = Site(id=2, capacity=25.0, panels=110, address="101 SW Ankeny", city="Portland", state="OR", postal_code="97203", coordinate=Coordinate(lat=1, lng=1)) site3 = Site(id=3, capacity=100.0, panels=155, address="201 SE Burnside", city="Portland", state="OR", postal_code="97204", coordinate=Coordinate(lat=1, lng=1)) site_dao.insert_many(site1, site2, site3) yield [site1, site2, site3]
def sites(redis, key_schema): site_dao = SiteGeoDaoRedis(redis, key_schema) site1 = Site(id=1, capacity=10.0, panels=100, address="100 SE Pine St.", city="Portland", state="OR", postal_code="97202", coordinate=PORTLAND) site2 = Site(id=2, capacity=25.0, panels=110, address="101 SW Ankeny", city="Portland", state="OR", postal_code="97203", coordinate=PORTLAND) site3 = Site(id=3, capacity=100.0, panels=155, address="9585 SW Washington Sq.", city="Beaverton", state="OR", postal_code="97223", coordinate=BEAVERTON) site_dao.insert_many(site1, site2, site3) yield [site1, site2, site3]
def test_insert(redis, site_dao): site = Site(id=1, capacity=10.0, panels=100, address="100 SE Pine St.", city="Portland", state="OR", postal_code="97202") site_dao.insert(site) assert site_dao.find_by_id(1) == site
def test_find_by_id_existing_site(site_dao): site_id = 1 site = Site(id=site_id, capacity=10.0, panels=100, address="100 SE Pine St.", city="Portland", state="OR", postal_code="97202") site_dao.insert(site) found_site = site_dao.find_by_id(site_id) assert found_site == site
def test_find_by_geo(sites, client, geo_enabled_app): resp = client.get( f"/sites/geo?lat={PORTLAND.lat}&lng={PORTLAND.lng}&radius=1&radius_unit={GeoUnit.M.value}" ).json resp_sites = SiteSchema(many=True).load(resp) portland_sites = { Site(id=1, capacity=10.0, panels=100, address="100 SE Pine St.", city="Portland", state="OR", postal_code="97202", coordinate=PORTLAND), Site(id=2, capacity=25.0, panels=110, address="101 SW Ankeny", city="Portland", state="OR", postal_code="97203", coordinate=PORTLAND) } assert set(resp_sites) == set(portland_sites)
def test_find_by_geo_with_excess_capacity(site_geo_dao, capacity_dao): site1 = Site(id=1, capacity=10.0, panels=100, address="100 SE Pine St.", city="Portland", state="OR", postal_code="97202", coordinate=PORTLAND) site_geo_dao.insert(site1) # Check that this site is returned when we're not looking for excess capacity. query = GeoQuery(coordinate=PORTLAND, radius=1, radius_unit=GeoUnit.MI) assert site_geo_dao.find_by_geo(query) == {site1} # Simulate changing a meter reading with no excess capacity. reading = MeterReading(site_id=site1.id, wh_used=1.0, wh_generated=0.0, temp_c=10, timestamp=datetime.datetime.now()) capacity_dao.update(reading) # In this case, no sites are returned for an excess capacity query. query = GeoQuery(coordinate=PORTLAND, radius=1, radius_unit=GeoUnit.MI, only_excess_capacity=True) sites = site_geo_dao.find_by_geo(query) assert len(sites) == 0 # Simulate changing a meter reading indicating excess capacity. reading = MeterReading(site_id=site1.id, wh_used=1.0, wh_generated=2.0, temp_c=10, timestamp=datetime.datetime.now()) capacity_dao.update(reading) # Add more Sites -- none with excess capacity -- to make the test more # realistic. for i in range(2, 20, 1): # Site 1 is our expected site - skip it! site = Site(id=i, capacity=10, panels=100, address=f"100{i} SE Pine St.", city="Portland", state="OR", postal_code="97202", coordinate=PORTLAND) site_geo_dao.insert(site) reading = MeterReading(site_id=i, wh_used=i, wh_generated=0, temp_c=10, timestamp=datetime.datetime.now()) capacity_dao.update(reading) # In this case, one site is returned on the excess capacity query sites = site_geo_dao.find_by_geo(query) assert site1 in sites