-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
103 lines (84 loc) · 2.91 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Tests for the datastore-orm library
"""
from datastore_orm import BaseModel, initialize, CustomKey
from typing import List
from dataclasses import dataclass
from google.cloud import datastore
from google.oauth2 import service_account
from google.cloud.datastore import Key
from datetime import datetime
from uuid import uuid4
@dataclass
class Brand(BaseModel):
name: str
description: str
@classmethod
def _factory(cls):
cls._class_mapping = cls("Sample Brand", "Hello from sample brand")
return cls._class_mapping
_exclude_from_indexes_ = ('description',)
@dataclass
class Price(BaseModel):
value: float
last_revised: datetime
currency: str
@classmethod
def _factory(cls):
cls._class_mapping = cls(9999.9, datetime.utcnow(), "USD")
return cls._class_mapping
@dataclass
class Car(BaseModel):
uid: str
created_by: Key
brand: Brand = None
prices: List[Price] = None
@classmethod
def _factory(cls):
cls._class_mapping = Car(str(uuid4()), CustomKey('Car', '[email protected]', project=CustomKey._client.project),
Brand._factory(), [Price._factory()])
return cls._class_mapping
def test_dotted_dict_to_object():
"""
Trigger BaseModel._dotted_dict_to_object and test its functionality
"""
uid = str(uuid4())
time1 = datetime.utcnow()
time2 = datetime.utcnow()
dict_ = {
"uid": uid,
"brand.name": "Mercedes",
"brand.description": "Generic luxury car",
"prices.value": [9888, 6785],
"prices.last_revised": [time1, time2],
"prices.currency": ['USD', 'EUR'],
"created_by": CustomKey('User', '[email protected]')
}
car = Car._dotted_dict_to_object(dict_)
assert car == Car(uid, brand=Brand(name='Mercedes', description='Generic luxury car',),
prices=[Price(9888, time1, 'USD'), Price(6785, time2, 'EUR')],
created_by=CustomKey('User', '[email protected]'))
def test_put():
car = Car(str(uuid4()), CustomKey("Car", "[email protected]"),
prices=[Price(9888, datetime.utcnow(), "USD"), Price(6899, datetime.utcnow(), "GBP")])
car_key = car.put()
print(car_key.id)
car_from_ds = car_key.get()
def test_query(token=None):
query = Car.query()
query_iter = query.fetch(start_cursor=token, limit=1)
for page in query_iter.pages:
print(list(page))
return query_iter.next_page_token
if __name__ == '__main__':
# credentials1 = service_account.Credentials.from_service_account_file('./keys/credentials.json')
# client1 = datastore.Client(project='project-name', namespace='Beta', credentials=credentials1)
client = datastore.Client(namespace='Beta')
initialize(clients=[client])
test_put()
_token = None
while True:
_token = test_query(_token)
if not _token:
break
# test_dotted_dict_to_object()