Skip to content

Instantly share code, notes, and snippets.

@Lunchbox4K
Created January 19, 2018 23:08
Show Gist options
  • Save Lunchbox4K/e792ab7b2a663321b10f1943212ba4a8 to your computer and use it in GitHub Desktop.
Save Lunchbox4K/e792ab7b2a663321b10f1943212ba4a8 to your computer and use it in GitHub Desktop.
FireStore Python Assert Test
# GameProjectsSync FireBase Admin - FireBase Test Script
# Mitchell Pell - 2018/01/18
#
# todo
# + Create constant for collection and document.
#--------------------------------------------------------------------------------
import google.auth
#from google.auth import crypt
#from google.auth import exceptions
#from google.oauth2 import credentials as gcredentials
#from google.oauth2 import service_account
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
# FireBase Credentials
#--------------------
cred = credentials.Certificate('/some-directory/.keys/credentials.json')
# Header Message
#--------------------
print(" ");
print("----------------------------------------");
print("FireBase (Python) Test");
print("Mitchell Pell");
print("-------------------------------");
# FireBase Test
#--------------------
#Assert FireStore Server Timestamp
print("[FireBase Test][Assert]\t Checking FireStore Server TimeStamp");
assert firestore.SERVER_TIMESTAMP is not None # pylint: disable=no-member
#Assert Credentials
print("[FireBase Test][Assert]\t Checking Credentials -> Token");
g_cred = cred.get_credential()
assert g_cred.token is None
assert isinstance(g_cred, google.auth.credentials.ReadOnlyScoped)
assert sorted(credentials._scopes) == sorted(g_cred.scopes)
#
#mock_response = {'access_token': 'mock_access_token', 'expires_in': 3600}
#cred._request = testutils.MockRequest(200, json.dumps(mock_response))
#access_token = cred.get_access_token()
#assert access_token.access_token == 'mock_access_token'
#assert isinstance(access_token.expiry, datetime.datetime)
#Create GameProjectSync Application
gps_app = firebase_admin.initialize_app(cred, {'projectId': 'gameprojectsync'}, name="GameProjectSync")
print("[FireBase Test]\t\t FireBase App Name: "+gps_app.name); # "Game Project Sync"
#Assert Client
print("[FireBase Test][Assert]\t Checking client");
client = firestore.client(app=gps_app)
assert client is not None
#Assert Client Project
print("[FireBase Test][Assert]\t Checking client.project");
assert client.project == 'gameprojectsync'
#Assert Admin Client
print("[FireBase Test][Assert]\t Checking admin");
admin_client = firebase_admin.firestore.client(gps_app)
assert admin_client is not None
#Assert Test Collection
print("[FireBase Test][Assert]\t Checking admin collection reference to [test-collection]");
test_collection_ref = admin_client.collection('test-collection')
assert test_collection_ref is not None
#-----
print("[FireBase Test][Assert]\t Checking admin document reference to [test-collection/Weu2OPlhfRBWuMUkh0Qx]");
test_direct_document_ref = admin_client.document('test-collection/Weu2OPlhfRBWuMUkh0Qx')
assert test_direct_document_ref is not None
print("[FireBase Test]\t\t Checking admin collection read access to [test-collection/Weu2OPlhfRBWuMUkh0Qx]");
try:
test_direct_document = test_direct_document_ref.get()
print(u'[FireBase Test]\t\t Document data from [test-collection/Weu2OPlhfRBWuMUkh0Qx]: \n{}'.format(test_direct_document.to_dict()))
print(u'[FireBase Test][Success] Successfully read at [test-collection/Weu2OPlhfRBWuMUkh0Qx]!')
except google.cloud.exceptions.NotFound:
print(u'[FireBase Test][Failed]\t No such document at [test-collection/Weu2OPlhfRBWuMUkh0Qx]!')
#-----
print("[FireBase Test][Assert]\t Checking admin document reference to [test-collection] from [Weu2OPlhfRBWuMUkh0Qx]");
test_document_ref = test_collection_ref.document('Weu2OPlhfRBWuMUkh0Qx')
assert test_document_ref is not None
print("[FireBase Test]\t\t Checking admin collection read access to [test-collection] from [Weu2OPlhfRBWuMUkh0Qx]");
try:
test_document = test_document_ref.get()
print(u'[FireBase Test]\t\t Document data from [test-collection/Weu2OPlhfRBWuMUkh0Qx]: \n -> {}'.format(test_document.to_dict()))
print(u'[FireBase Test][Success] Successfully read at [test-collection] from [Weu2OPlhfRBWuMUkh0Qx]!')
except google.cloud.exceptions.NotFound:
print(u'[FireBase Test][Failed]\t No such document at [test-collection] from [Weu2OPlhfRBWuMUkh0Qx]!')
#-----
#Assert Geo Point
geo_point = firestore.GeoPoint(10, 20) # pylint: disable=no-member
print("[FireBase Test][Assert]\t Checking firestore.GeoPoint");
assert geo_point.latitude == 10
assert geo_point.longitude == 20
firebase_admin.delete_app(gps_app)
# End of Test Message
#--------------------
print("[FireBase Test] - - - -> Finished!");
print(" ");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment