def update_brew(event: dict, context, service=None): if not service: service = make_brew_service() try: brew = BrewRemote.from_json(event["body"]) except (ValueError, JSONDecodeError, KeyError) as e: return handle_error(e) service.put(brew) return {"statusCode": 200}
def test_dict(self): time_str = "2021-03-25T17:24:10.829080" incoming = json.dumps({ "name": "brew2", "id": "2", "remote_id": "d2e85707-9fcf-4ff3-9d1b-bdb2419da675", "active": False, "start_time": time_str, "current_stage": FERMENTING, }) b2 = BrewRemote.from_json(incoming) self.assertEqual(type(b2.start_time), datetime.datetime) b2dict = b2.to_dict() self.assertEqual(time_str, b2dict["start_time"]) b2_final = BrewRemote.from_dict(b2dict) self.assertEqual(b2.start_time, b2_final.start_time) self.assertEqual(b2.remote_id, b2_final.remote_id) self.assertEqual(b2.id, b2_final.id) self.assertEqual(b2.active, b2_final.active)
def test_brew(self): incoming = json.dumps({ "name": "brew1", "id": "1", "remote_id": "d2e85707-9fcf-4ff3-9d1b-bdb2419da674", "active": True, "stages": [{ "start_time": "2021-05-15T19:30:54.100701", "stage_name": "FERMENTING", "estimated_end_time": "2021-05-22T19:30:54.100701" }] }) b = BrewRemote.from_json(incoming) self.assertEqual(b.name, "brew1") incoming2 = json.dumps({ "name": "brew2", "id": "2", "remote_id": "d2e85707-9fcf-4ff3-9d1b-bdb2419da675", "active": False, "stages": [{ "start_time": "2021-05-15T19:30:54.100701", "stage_name": "FERMENTING", "estimated_end_time": "2021-05-22T19:30:54.100701" }] }) b2 = BrewRemote.from_json(incoming2) self.assertEqual(b2.name, "brew2") self.assertEqual(b2.active, False) self.assertEqual(b2.current_stage.stage_name, "FERMENTING") self.assertEqual(type(b2.current_stage.estimated_end_time), datetime.datetime)
def test_delete(self): service = BrewServiceRemote(StubDynamo()) brew = BrewRemote.from_json( json.dumps({ "name": "brew2", "id": "2", "active": False, "remote_id": "xyz1", "current_stage": FERMENTING, })) service.create(brew) brews = service.getAll() self.assertEqual(1, len(brews)) self.assertEqual(brews[0].name, "brew2") service.delete("xyz1") brews = service.getAll() self.assertEqual(0, len(brews))
def test_put(self): mock_dynamo = StubDynamo() service = BrewServiceRemote(mock_dynamo) brew = BrewRemote.from_json( json.dumps({ "name": "brew2", "id": "2", "active": False, "remote_id": "xyz1", "current_stage": FERMENTING, })) service.create(brew) brews = service.getAll() self.assertEqual(1, len(brews)) self.assertEqual(brews[0].name, "brew2") brew.name = "newbrew" service.put(brew) brews = service.getAll() self.assertEqual(1, len(brews)) self.assertEqual(brews[0].name, "newbrew")
def get(self, id_to_get: int) -> BrewRemote: brew_dict = self.db.get(BREWS_DYNAMO_TABLE, str(id_to_get), BREW_TABLE_ID) if brew_dict: return BrewRemote.from_dict(brew_dict) else: return None
def put(self, brew: BrewRemote): self.db.put(BREWS_DYNAMO_TABLE, brew.to_dict())
def getAll(self) -> List[BrewRemote]: return [ BrewRemote.from_dict(json_brew) for json_brew in self.db.get_all(BREWS_DYNAMO_TABLE) ]