def test_upsert_by_id(db: TinyDB): assert len(db) == 3 # Single document existing extant_doc = Document({'char': 'v'}, doc_id=1) assert db.upsert(extant_doc) == [1] doc = db.get(where('char') == 'v') assert doc is not None assert doc.doc_id == 1 assert len(db) == 3 # Single document missing missing_doc = Document({'int': 5, 'char': 'w'}, doc_id=5) assert db.upsert(missing_doc) == [5] doc = db.get(where('char') == 'w') assert doc is not None assert doc.doc_id == 5 assert len(db) == 4 # Missing doc_id and condition with pytest.raises(ValueError, match=r"(?=.*\bdoc_id\b)(?=.*\bquery\b)"): db.upsert({'no_Document': 'no_query'}) # Make sure we didn't break anything assert db.insert({'check': '_next_id'}) == 6
def document(object): if isinstance(object, dict): return Document(object, object['id']) else: dico = dict((attr[1:], value) for (attr, value) in object.__dict__.items() if not isinstance(value, list)) return Document(dico, object.id)
def handle_union(response, profiles, unions, lock): '''Handle union response. ''' results = response.get('results') or [response] get_child_ids = [] for result in results: doc_id = int(result['id'].split('-')[1]) document = Document(result, doc_id=doc_id) with lock: # If the union is already in the database then end here if unions.contains(doc_id=doc_id): logging.debug(f'Union {doc_id} already present!') continue # Store it in the database unions.insert(document) logging.info(f'Added union #{unions.__len__()}') # Construct query to unseen children profiles. These will either be # the brothers/sisters or the children of the originating profile. for c in result.get('children', []): geni_id = c.split('/')[-1] doc_id = int(geni_id.split('-')[1]) with lock: present = profiles.contains(doc_id=doc_id) if not present: get_child_ids.append(geni_id) # Split into batches for i in range(0, len(get_child_ids), MAX_IDS): ids = get_child_ids[i:i + MAX_IDS] yield f'{BASE}/profile?ids={",".join(ids)}&fields={PROFILE_FIELDS}'
def addToQueue(player: Member, mins_to_queue_for: int = 60) -> None: new_player = BallChaser(id=player.id, name=str(player), mmr=Leaderboard.getPlayerMMR(player), queueTime=(datetime.now() + timedelta(minutes=mins_to_queue_for))) currQueue.insert(Document(new_player.toJSON(), doc_id=new_player.id))
def handle_profile(response, profiles, unions, lock): '''Handle profile response. ''' results = response.get('results') or [response] get_union_ids = [] for result in results: # Split id from geni id doc_id = int(result['id'].split('-')[1]) document = Document(result, doc_id=doc_id) name = result.get('name') with lock: # If the profile is already in the database then end here if profiles.contains(doc_id=doc_id): logging.debug(f'Profile {name} already present!') continue # Store it in the database profiles.insert(document) logging.info(f'Added profile #{profiles.__len__()}: {name}') # Construct query to unseen unions for u in result.get('unions', []): geni_id = u.split('/')[-1] doc_id = int(geni_id.split('-')[1]) with lock: present = unions.contains(doc_id=doc_id) if not present: get_union_ids.append(geni_id) # Split into batches for i in range(0, len(get_union_ids), MAX_IDS): ids = get_union_ids[i:i + MAX_IDS] yield f'{BASE}/union?ids={",".join(ids)}&fields={UNION_FIELDS}'
def test_insert_multiple_with_doc_ids(db: TinyDB): db.drop_tables() assert db.insert_multiple([ Document({ 'int': 1, 'char': 'a' }, 12), Document({ 'int': 1, 'char': 'b' }, 77) ]) == [12, 77] assert db.get(doc_id=12) == {'int': 1, 'char': 'a'} assert db.get(doc_id=77) == {'int': 1, 'char': 'b'} with pytest.raises(ValueError): db.insert_multiple([Document({'int': 1, 'char': 'a'}, 12)])
def store_user_settings(user_id, settings): print('settings_arr') try: current_settings = settings_db.get(doc_id=user_id) if current_settings is not None: settings_db.update(settings, doc_ids=[user_id]) else: settings_db.insert(Document(settings, doc_id=user_id)) print('returning') utils.user_settings_updated(settings) return settings except: print("Error Occurred while storing settings")
def saveActualStates(self): db = TinyDB(self.dbPath) for states in self.statesList: # Changing np.arrays to lists, so we can serialize them states['prev_state']['image'] = states['prev_state'][ 'image'].tolist() states['curr_state']['image'] = states['curr_state'][ 'image'].tolist() date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: states['update_date'] = date db.update(Document(states, doc_id=states.doc_id)) except AttributeError: states['create_date'] = date db.insert(states)
def save_item(self, id: PositiveInt): item = self.read_by_id(id) self.table.upsert(Document(json.loads(item.json()), doc_id=id))
def test_insert_with_duplicate_doc_id(db: TinyDB): db.drop_tables() assert db.insert({'int': 1, 'char': 'a'}) == 1 with pytest.raises(AssertionError): db.insert(Document({'int': 1, 'char': 'a'}, 1))
def test_insert_with_doc_id(db: TinyDB): db.drop_tables() assert db.insert({'int': 1, 'char': 'a'}) == 1 assert db.insert(Document({'int': 1, 'char': 'a'}, 12)) == 12 assert db.insert(Document({'int': 1, 'char': 'a'}, 77)) == 77 assert db.insert({'int': 1, 'char': 'a'}) == 78
def upsert_op(self, req_dict): ##self._db.upsert(req_dict, GNGraphDBConfigModel.query.serverIP == req_dict['serverIP']) self._db.upsert(Document(req_dict, doc_id=1))
def reportMatch(player: Union[Member, str], whoWon: Team, adminOverride: bool = False) -> bool: global sorted_lb match = getActiveMatch(player) if (not match): return False if (isinstance(player, Member)): foundPlayer = match[str(player.id)] elif (isinstance(player, str)): foundPlayer = match[player] player = BallChaser( name=foundPlayer[MatchKey.NAME], id=foundPlayer[MatchKey.ID], mmr=foundPlayer[MatchKey.MMR], team=foundPlayer[MatchKey.TEAM] ) report_confirm_success = reportConfirm(player, match, whoWon) # report confirmed matchMMR = Points.calculateMMR(match) if (report_confirm_success or adminOverride == True): for key in match: if (key != MatchKey.REPORTED_WINNER): teamMember = match[key] if ( (whoWon == Team.BLUE and teamMember["team"] == Team.BLUE) or (whoWon == Team.ORANGE and teamMember["team"] == Team.ORANGE) ): win = 1 loss = 0 mmr = matchMMR else: win = 0 loss = 1 mmr = -matchMMR player = leaderboard.get(doc_id=teamMember[MatchKey.ID]) if (not player): leaderboard.insert(Document({ LbKey.ID: teamMember[MatchKey.ID], LbKey.NAME: teamMember[MatchKey.NAME], LbKey.MMR: 100 + mmr, LbKey.WINS: win, LbKey.LOSSES: loss, LbKey.MATCHES: 1, LbKey.WIN_PERC: float(win), }, doc_id=teamMember[MatchKey.ID])) else: updated_player = { LbKey.NAME: teamMember[MatchKey.NAME], LbKey.MMR: max(player[LbKey.MMR] + mmr, 0), LbKey.WINS: player[LbKey.WINS] + win, LbKey.LOSSES: player[LbKey.LOSSES] + loss, LbKey.MATCHES: player[LbKey.MATCHES] + 1, LbKey.WIN_PERC: player[LbKey.WIN_PERC], } total_wins = int(updated_player[LbKey.WINS]) total_matches = int(updated_player[LbKey.MATCHES]) updated_player[LbKey.WIN_PERC] = float("{:.2f}".format(total_wins / total_matches)) leaderboard.update(updated_player, doc_ids=[player.doc_id]) elif (not report_confirm_success): return report_confirm_success activeMatches.remove(doc_ids=[match.doc_id]) sorted_lb = sorted(leaderboard.all(), key=lambda x: (x[LbKey.MMR], x[LbKey.WINS], x[LbKey.WIN_PERC]), reverse=True) with concurrent.futures.ThreadPoolExecutor() as executor: executor.submit(AWS.writeRemoteLeaderboard, dumps(sorted_lb)) return True
def resetFromRemote(remoteData: dict) -> None: leaderboard.truncate() for p in remoteData: leaderboard.insert(Document(p, doc_id=p["id"]))
def _throwing_save(self, reading_entry_struct: ReadingEntryStruct) -> bool: """Examples: >>> from unittest.mock import MagicMock, PropertyMock, patch >>> di = dict() >>> test_input_entry_struct = dict(title='foo', link='bar') >>> def setup_mock_db(mock_db): ... mock_db_instance = MagicMock() ... mock_db.return_value = mock_db_instance ... return mock_db_instance 1. TinyDbDriver::_throwing_save saves the reading entry struct as new Document with custom id >>> with patch.object(TinyDbDriver, '_db', new_callable=PropertyMock) as mock_db: ... with patch.object(TinyDbDriver, '_get_document_id') as mock_get_document_id: ... with patch( ... 'reading_list.core.persistency.tinydb_driver.Document' ... ) as mock_document: ... mock_db_instance = setup_mock_db(mock_db) ... expected_document = 'some_document' ... expected_document_id = 'some_document_id' ... mock_get_document_id.return_value = expected_document_id ... driver = TinyDbDriver(di) ... _ = driver._throwing_save(test_input_entry_struct) ... mock_document.assert_called_once_with( ... test_input_entry_struct, expected_document_id) 2. TinyDbDriver::_throwing_save saves the struct of the reading entry >>> with patch.object(TinyDbDriver, '_db', new_callable=PropertyMock) as mock_db: ... with patch.object(TinyDbDriver, '_get_document_id') as mock_get_document_id: ... with patch( ... 'reading_list.core.persistency.tinydb_driver.Document' ... ) as mock_document: ... mock_db_instance = setup_mock_db(mock_db) ... mock_document.return_value = expected_document ... driver = TinyDbDriver(di) ... _ = driver._throwing_save(test_input_entry_struct) ... mock_db_instance.insert.assert_called_once_with(expected_document) 3. TinyDbDriver::_throwing_save returns True if the result of insert is a valid document id >>> with patch.object(TinyDbDriver, '_db', new_callable=PropertyMock) as mock_db: ... with patch.object(TinyDbDriver, '_get_document_id') as mock_get_document_id: ... with patch( ... 'reading_list.core.persistency.tinydb_driver.Document' ... ) as mock_document: ... mock_db_instance = setup_mock_db(mock_db) ... mock_db_instance.insert.return_value = "some_valid_document_id" ... driver = TinyDbDriver(di) ... driver._throwing_save(test_input_entry_struct) True 4. TinyDbDriver::_throwing_save returns False if the result of insert is not a valid document id >>> with patch.object(TinyDbDriver, '_db', new_callable=PropertyMock) as mock_db: ... with patch.object(TinyDbDriver, '_get_document_id') as mock_get_document_id: ... with patch( ... 'reading_list.core.persistency.tinydb_driver.Document' ... ) as mock_document: ... mock_db_instance = setup_mock_db(mock_db) ... mock_db_instance.insert.return_value = None ... driver = TinyDbDriver(di) ... driver._throwing_save(test_input_entry_struct) False """ new_doc_id = self._get_document_id(reading_entry_struct) document_to_store = Document(reading_entry_struct, new_doc_id) entry_id = self._db.insert(document_to_store) return True if entry_id else False