def test_explicit_namespace(self): # create two departments with the same id which will be stored in different namespaces department1 = Department2( id="dept_id", namespace="namespace1", name="department in 'namespace1'", ) department2 = Department2( id="dept_id", namespace="namespace2", name="department in 'namespace2'", ) department3 = Department2( # no namespace here, so the default should be taken (supplied with ndb.enable_use_with_gcd()) id="dept_id", name="department in 'namespace3'", ) if USE_DATASTORE: from google.cloud import datastore from .gcloud_credentials import EmulatorCredentials client = datastore.Client( project=PROJECT, credentials=EmulatorCredentials(), namespace='namespace_xxx') # set default namespace client.put(department1) client.put(department2) client.put(department3) key1 = ndb.Key(Department2, "dept_id", project=PROJECT, namespace="namespace1") key2 = ndb.Key(Department2, "dept_id", project=PROJECT, namespace="namespace2") key3 = ndb.Key(Department2, "dept_id", project=PROJECT, namespace="namespace3") department1_from_db = client.get(key1) department2_from_db = client.get(key2) department3_from_db = client.get(key3) self.assertEqual(department1_from_db.name, "department in 'namespace1'") self.assertEqual(department2_from_db.name, "department in 'namespace2'") self.assertEqual(department3_from_db.name, "department in 'namespace3'") else: print("skipping test without datastore")
def get(cls, urlsafe_or_key): """Retrieves an entity object based on an URL safe Key string or Key object.""" cls._get_client() if isinstance(urlsafe_or_key, (str, bytes)): key = ndb.Key(cls, **NDB_KWARGS) complete_key = key.from_legacy_urlsafe(urlsafe_or_key) else: complete_key = urlsafe_or_key item = complete_key.get() if not item: raise Exception("item doesn't exist") return item
def test_complex_model(self): import datetime import binascii namespace = "your-namespace" ndb.model.ENABLE_PICKLE_LOADS = True # might be dangerous in production person = person_pb2.Person() person.id = 1234 person.name = "John Doe" person.email = "*****@*****.**" phone = person.phones.add() phone.number = "555-4321" phone.type = person_pb2.Person.HOME department = Department(id="dept_id", department_id=123, name="department") if USE_DATASTORE: from google.cloud import datastore from .gcloud_credentials import EmulatorCredentials client = datastore.Client(project=PROJECT, credentials=EmulatorCredentials()) client.put(department) human = Human2( name='Arthur Dent', gender=Gender.male, age=42, items=Items(has_hat=True, number_of_socks=3, namespace=namespace), numbers=[12, 13, 14], description="a real man", description2="a real man, oh yeah", meters_tall=1.82, datetime_of_birth=datetime.datetime(2017, 8, 26, 15, 10, 42, 123456), date_of_birth=datetime.datetime(2017, 8, 26, 15, 10, 42, 123456).date(), time_of_birth=datetime.datetime(2017, 8, 26, 15, 10, 42, 123456).time(), hobbies=[u"football", u"tv"], pickle=[{ "football_at": datetime.datetime(2017, 8, 26, 15, 10, 42, 123456) }], binary=binascii.unhexlify("61626300"), home=ndb.GeoPt("52.37, 4.88"), generic=7, #model=Items(has_hat=True, number_of_socks=3, namespace=namespace).to_dict(), model=Items(has_hat=True, number_of_socks=3, namespace=namespace), person_details=person, key_prop=department.key, key_prop2=ndb.Key(DepartmentRoot, "root", Department, "dept_id2"), namespace=namespace) human_recovered = entity_to_binary_to_entity(human) # now do the tests self.assertEqual(human_recovered.name, 'Arthur Dent') self.assertEqual(human_recovered.gender, Gender.male) self.assertEqual(human_recovered.age, 42) self.assertEqual(human_recovered.items.has_hat, True) self.assertEqual(human_recovered.items.number_of_socks, 3) self.assertEqual(human_recovered.numbers, [12, 13, 14]) self.assertEqual(human_recovered.description, "a real man") self.assertEqual(human_recovered.description2, "a real man, oh yeah") self.assertAlmostEqual(human_recovered.meters_tall, 1.82) # DateTime always have a timezone attached self.assertEqual( human_recovered.datetime_of_birth.replace(tzinfo=None), datetime.datetime(2017, 8, 26, 15, 10, 42, 123456)) self.assertEqual( human_recovered.date_of_birth, datetime.datetime(2017, 8, 26, 15, 10, 42, 123456).date()) self.assertEqual( human_recovered.time_of_birth, datetime.datetime(2017, 8, 26, 15, 10, 42, 123456).time()) self.assertEqual(human_recovered.hobbies, [u"football", u"tv"]) self.assertEqual(human_recovered.pickle[0]["football_at"], datetime.datetime(2017, 8, 26, 15, 10, 42, 123456)) self.assertEqual(human_recovered.binary, binascii.unhexlify("61626300")) self.assertAlmostEqual(human_recovered.home.lat, 52.37) self.assertAlmostEqual(human_recovered.home.lon, 4.88) self.assertEqual(human_recovered.generic, 7) self.assertEqual(human_recovered.model.has_hat, True) self.assertEqual(human_recovered.model.number_of_socks, 3) self.assertEqual(human_recovered.person_details.phones[0].number, "555-4321") self.assertEqual(human_recovered.key_prop, ndb.Key(Department, "dept_id")) self.assertEqual( human_recovered.key_prop2, ndb.Key(DepartmentRoot, "root", Department, "dept_id2")) # these were set automatically self.assertEqual(human_recovered.number_of_hobbies, 2) self.assertEqual(human_recovered.default_info, "unknown") self.assertEqual(isinstance(human_recovered.update, datetime.date), True) if USE_DATASTORE: from google.cloud import datastore from .gcloud_credentials import EmulatorCredentials client = datastore.Client(project=PROJECT, credentials=EmulatorCredentials()) department_db = client.get(human_recovered.key_prop) self.assertEqual(department_db.department_id, 123) self.assertEqual(department_db.name, "department") ndb.model.ENABLE_PICKLE_LOADS = False