def test_update(): tmp_first_name = "Aa" # get doc user = User.get_user_by_telegram_id(telegram_id='tgid1') real_first_name = user.first_name # update the doc user.first_name = tmp_first_name user.update_db() # upload updated version user = User.get_user_by_telegram_id(telegram_id='tgid1') assert user.first_name == tmp_first_name # restore initial value user.first_name = real_first_name user.update_db()
def get_user_state(user: User) -> Optional[State]: user_id = user.get_id() state_record = DatastoreClient().get_client().collection( USERS_STATES).document(user_id).get().to_dict() print(f'user_id: {user_id}, state_record: {state_record}') state = None if state_record: state = State(**state_record) print(f'parsed_state: {state}') return state
def update_user_state(user: User, new_state: QuestStateType) -> bool: try: user_id = user.get_id() state_ref = DatastoreClient().get_client().collection( USERS_STATES).document(user_id) state_ref.update({u'state_type': new_state.name}) return True except Exception as e: print(f'Update of state failed for user {user} to state: {new_state}. ' f'Exception: {e}') return False
def get_telegram_user(message, create_new_user: bool) -> Optional[User]: telegram_id = message.from_user.id user = User.get_user_by_telegram_id(telegram_id=telegram_id) if not user: if create_new_user: first_name = message.from_user.first_name last_name = message.from_user.last_name user = User( user_id='', telegram_id=telegram_id, first_name=first_name, last_name=last_name, ) user_id = user.add_to_db() init_user_state(user_id=user_id) else: return None return user
def test_from_dict(): web_dict = { 'user_name': 'u1', 'email': 'e1', 'password_hash': 'ph1', 'registration_date': '2020.02.12 12:11:01' } user_dict = { 'user_id': "id1", 'first_name': "aa", 'last_name': "bb", 'telegram_id': 'tgid_1', 'web_credentials': web_dict } user = User(**user_dict) assert asdict(user) == user_dict
def save_user_state(user: User, state: State): user_id = user.get_id() DatastoreClient().get_client().collection(USERS_STATES).document( user_id).set(asdict(state))
def test_delete_and_add(): u = User(user_id='', first_name='aa', last_name='bb', telegram_id='cc') u.add_to_db() print(u.user_id) assert DatastoreClient().get_client().collection( USERS_COLLECTION).document(u.user_id)
def test_get_user(): user = User.get_user_by_telegram_id(telegram_id='tgid1') assert user, 'User with all fields was not set properly' user = User.get_user_by_telegram_id(telegram_id='tgid2') assert user, 'User without web credentials was not set properly'