def test_remove_user_by_username(self): UsersDb.remove_user_by_username(TEST_USER_NAME) snap = firestore_client().document(u'users', TEST_USER_ID).get() assert snap.exists == False col = firestore_client().collection(u'users', TEST_USER_ID, u'records') contains_sth = False for ref in col.list_documents(): contains_sth = True break assert contains_sth == False
def setUp(self): # create a user for testing firestore_client().collection(u'users').add( document_data=TEST_USER_STORED.dict(), document_id=TEST_USER_ID ) rec_ref = firestore_client().document(u'users', TEST_USER_ID).collection(u'records').document(convert_date_to_str(TEST_USER_RECORD_1.date)) rec_ref.set(TEST_USER_RECORD_1.symptoms.dict()) firestore_client().document(u'users', TEST_USER_ID).set( document_data={'anamnesis' : TEST_USER_ANAMNESIS.dict()}, merge=True )
def __init__(self, *args, **kwargs): self._bin = [] super().__init__(*args, **kwargs) list_purps = firestore_client().collection(u'usage_purposes').list_documents() self.all_purps = [] for purp in list_purps: self.all_purps.append(purp.get().id)
def test_set_anamanesis(self): anamnesis_to_update = { 'gender': 'w', 'positive_tested': False } updated_anamnesis = {**TEST_USER_ANAMNESIS.dict(), **anamnesis_to_update} ret_a = AnamnesisDb.set_anamnesis(TEST_USER_ID, Anamnesis.parse_obj(anamnesis_to_update)) self.assertDictEqual(ret_a.dict(), updated_anamnesis) doc_ref = firestore_client().document(u'users', TEST_USER_ID) ret_b = doc_ref.get().get(u'anamnesis') self.assertDictEqual(ret_b, updated_anamnesis)
def test_save_new_user_succ(self): UsersDb.save_new_user( user_id=TEST_USER_ID+'NEW', user=UserStored( username=TEST_USER_NAME+'NEW', password=TEST_USER_STORED.password, usage_purpose=TEST_USER_STORED.usage_purpose, time_created=TEST_USER_STORED.time_created ) ) doc: DocumentSnapshot = firestore_client().document(u'users', TEST_USER_ID+'NEW').get() assert doc.exists == True assert doc.id == TEST_USER_ID+'NEW' assert doc.get(u'username') == TEST_USER_NAME+'NEW'
def test_set_record_merge(self): to_update_symptoms = { 'sore_throat': 1, 'diarrhoea': False } to_update = Record( date=TEST_USER_RECORD_1.date, symptoms=Symptoms.parse_obj(to_update_symptoms) ) RecordsDb.set_record(TEST_USER_ID, to_update) x = TEST_USER_RECORD_1.dict()['symptoms'] updated_symptoms = {**TEST_USER_RECORD_1.dict()['symptoms'], **to_update_symptoms} rec_ref: DocumentReference = firestore_client().document(u'users', TEST_USER_ID, u'records', convert_date_to_str(TEST_USER_RECORD_1.date)) self.assertDictEqual(rec_ref.get().to_dict(), updated_symptoms)
def test_set_record_new(self): RecordsDb.set_record(TEST_USER_ID, TEST_USER_RECORD_3) all_recs = firestore_client().collection(u'users', TEST_USER_ID, u'records').list_documents() i = 0 for rec in all_recs: if convert_str_to_date(rec.get().id) == TEST_USER_RECORD_1.date: self.assertDictEqual(rec.get().to_dict(), TEST_USER_RECORD_1.symptoms.dict()) i += 1 if convert_str_to_date(rec.get().id) == TEST_USER_RECORD_2.date: self.assertDictEqual(rec.get().to_dict(), TEST_USER_RECORD_2.symptoms.dict()) i += 1 if convert_str_to_date(rec.get().id) == TEST_USER_RECORD_3.date: self.assertDictEqual(rec.get().to_dict(), TEST_USER_RECORD_3.symptoms.dict()) i += 1 assert i == 3
def test_update_user(self): anamnesis_to_update = { 'gender': 'w', 'positive_tested': False } ret_a = UsersDb.update_user(TEST_USER_ID, Anamnesis.parse_obj(anamnesis_to_update)) assert ret_a.gender == 'w' assert ret_a.positive_tested == False assert ret_a.residence == 12345 doc_ref = firestore_client().document(u'users', TEST_USER_ID) db_rec = doc_ref.get().to_dict() assert db_rec['anamnesis']['gender'] == 'w' assert db_rec['anamnesis']['positive_tested'] == False assert db_rec['anamnesis']['residence'] == 12345