forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate_datastore.py
More file actions
90 lines (82 loc) · 2.5 KB
/
populate_datastore.py
File metadata and controls
90 lines (82 loc) · 2.5 KB
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
"""Script to populate datastore with regression test data."""
from gcloud import datastore
# This assumes the command is being run via tox hence the
# repository root is the current directory.
from regression import regression_utils
ANCESTOR = {'kind': 'Book', 'name': 'GoT'}
RICKARD = {'kind': 'Character', 'name': 'Rickard'}
EDDARD = {'kind': 'Character', 'name': 'Eddard'}
KEY_PATHS = [
[ANCESTOR, RICKARD],
[ANCESTOR, RICKARD, EDDARD],
[ANCESTOR,
{'kind': 'Character', 'name': 'Catelyn'}],
[ANCESTOR, RICKARD, EDDARD,
{'kind': 'Character', 'name': 'Arya'}],
[ANCESTOR, RICKARD, EDDARD,
{'kind': 'Character', 'name': 'Sansa'}],
[ANCESTOR, RICKARD, EDDARD,
{'kind': 'Character', 'name': 'Robb'}],
[ANCESTOR, RICKARD, EDDARD,
{'kind': 'Character', 'name': 'Bran'}],
[ANCESTOR, RICKARD, EDDARD,
{'kind': 'Character', 'name': 'Jon Snow'}],
]
CHARACTERS = [
{
'name': u'Rickard',
'family': u'Stark',
'appearances': 0,
'alive': False,
}, {
'name': u'Eddard',
'family': u'Stark',
'appearances': 9,
'alive': False,
}, {
'name': u'Catelyn',
'family': [u'Stark', u'Tully'],
'appearances': 26,
'alive': False,
}, {
'name': u'Arya',
'family': u'Stark',
'appearances': 33,
'alive': True,
}, {
'name': u'Sansa',
'family': u'Stark',
'appearances': 31,
'alive': True,
}, {
'name': u'Robb',
'family': u'Stark',
'appearances': 22,
'alive': False,
}, {
'name': u'Bran',
'family': u'Stark',
'appearances': 25,
'alive': True,
}, {
'name': u'Jon Snow',
'family': u'Stark',
'appearances': 32,
'alive': True,
},
]
def add_characters():
dataset = regression_utils.get_dataset()
with dataset.transaction():
for key_path, character in zip(KEY_PATHS, CHARACTERS):
if key_path[-1]['name'] != character['name']:
raise ValueError(('Character and key don\'t agree',
key_path, character))
key = datastore.key.Key(path=key_path)
entity = datastore.entity.Entity(dataset=dataset).key(key)
entity.update(character)
entity.save()
print 'Adding Character %s %s' % (character['name'],
character['family'])
if __name__ == '__main__':
add_characters()