def update_guest_stat( self, host_id: str, guest_id: str, old_updated_at: int, new_updated_at: int, new_total_messages: int, ): # find old record old_key = { "Key": { "itemType": "guest", "itemID": "#".join([host_id, str(old_updated_at), guest_id]), } } old_guest = self.table.get_item(**old_key)["Item"]["itemData"] # delete old record self.table.delete_item(**old_key) # update stat new_guest = GuestModel(**old_guest) new_guest.updated_at = new_updated_at new_guest.total_msgs = new_total_messages # add new record self.table.put_item( Item={ "itemType": "guest", "itemID": "#".join([host_id, str(new_updated_at), guest_id]), "itemData": new_guest.dict(), })
def add_guest(self, host_id: str, guest: GuestModel): updated_at, guest_id = guest.updated_at, guest.guest_id key = "#".join([host_id, str(updated_at), guest_id]) self.table.put_item(Item={ "itemType": "guest", "itemID": key, "itemData": guest.dict(), })
def test_create_guest(sync, mock_thread_one_step_one): expected_guest = GuestModel( guest_id=mock_thread_one_step_one.guest_id(), updated_at=mock_thread_one_step_one.updated_at(), total_msgs=len(mock_thread_one_step_one.messages()), name=mock_thread_one_step_one.guest_name(), ) sync._create_guest(mock_thread_one_step_one) sync.db.add_guest.assert_called_once_with("001", expected_guest)
def test_update_guest_new_guest_empty_db(sync, mock_thread_one_step_one): sync.db.guests_by_host.return_value = [] sync._update_guest(mock_thread_one_step_one) expected_guest = GuestModel( guest_id=mock_thread_one_step_one.guest_id(), updated_at=mock_thread_one_step_one.updated_at(), total_msgs=len(mock_thread_one_step_one.messages()), name=mock_thread_one_step_one.guest_name(), ) sync.db.add_guest.assert_called_once_with("001", expected_guest)
def test_update_guest_existing_guest_same_stat(sync, mock_thread_one_step_one): existing_guest = GuestModel( guest_id=mock_thread_one_step_one.guest_id(), updated_at=mock_thread_one_step_one.updated_at(), total_msgs=len(mock_thread_one_step_one.messages()), name=mock_thread_one_step_one.guest_name(), ) sync.db.guests_by_host.return_value = [existing_guest] sync._update_guest(mock_thread_one_step_one) assert not (sync.db.add_guest.called) assert not (sync.db.update_guest_stat.called)
def _create_guest(self, thread): guest_id = str(thread.guest_id()) host_id = str(thread.host_id()) new_guest = GuestModel( guest_id=guest_id, updated_at=thread.updated_at(), name=thread.guest_name(), total_msgs=len(thread.messages()), ) self.db.add_guest(host_id, new_guest)
def test_update_guest_new_guest(sync, mock_thread_one_step_two, mock_thread_two_step_three): existing_guest = GuestModel( guest_id=mock_thread_one_step_two.guest_id(), updated_at=mock_thread_one_step_two.updated_at(), total_msgs=len(mock_thread_one_step_two.messages()), name=mock_thread_one_step_two.guest_name(), ) sync.db.guests_by_host.return_value = [existing_guest] sync._update_guest(mock_thread_two_step_three) expected_guest = GuestModel( guest_id=mock_thread_two_step_three.guest_id(), updated_at=mock_thread_two_step_three.updated_at(), total_msgs=len(mock_thread_two_step_three.messages()), name=mock_thread_two_step_three.guest_name(), ) sync.db.add_guest.assert_called_once_with("001", expected_guest) assert not (sync.db.update_guest_stat.called)
def guests(self): result = {} data = self._query_table("guest") data = [(GuestModel(**item["itemData"]), item["itemID"]) for item in data] for guest, key in data: host_id, _, guest_id = key.split("#") if host_id not in result: result[host_id] = {} result[host_id][guest_id] = guest return result
def test_update_guest_existing_guest_different_stat(sync, mock_thread_one_step_one, mock_thread_one_step_two): existing_guest = GuestModel( guest_id=mock_thread_one_step_one.guest_id(), updated_at=mock_thread_one_step_one.updated_at(), total_msgs=len(mock_thread_one_step_one.messages()), name=mock_thread_one_step_one.guest_name(), ) sync.db.guests_by_host.return_value = [existing_guest] sync._update_guest(mock_thread_one_step_two) expected_args = [ "001", mock_thread_one_step_one.guest_id(), mock_thread_one_step_one.updated_at(), mock_thread_one_step_two.updated_at(), len(mock_thread_one_step_two.messages()), ] assert not (sync.db.add_guest.called) sync.db.update_guest_stat.assert_called_once_with(*expected_args)
def guests_by_host(self, host_id: str): data = self._query_table("guest", get_attributes=["itemData"]) data = [item["itemData"] for item in data] data = [GuestModel(**guest) for guest in data] return data