|
| 1 | +"""Script to populate datastore with regression test data.""" |
| 2 | + |
| 3 | + |
| 4 | +# This assumes the command is being run via tox hence the |
| 5 | +# repository root is the current directory. |
| 6 | +from regression import regression_utils |
| 7 | + |
| 8 | + |
| 9 | +FETCH_MAX = 20 |
| 10 | +ALL_KINDS = [ |
| 11 | + 'Character', |
| 12 | + 'Company', |
| 13 | + 'Kind', |
| 14 | + 'Person', |
| 15 | + 'Post', |
| 16 | +] |
| 17 | + |
| 18 | + |
| 19 | +def remove_kind(dataset, kind): |
| 20 | + dataset_id = dataset.id() |
| 21 | + connection = dataset.connection() |
| 22 | + |
| 23 | + with dataset.transaction(): |
| 24 | + query = dataset.query(kind=kind).limit( |
| 25 | + FETCH_MAX).projection(['__key__']) |
| 26 | + results = [] |
| 27 | + more_results = True |
| 28 | + while more_results: |
| 29 | + # Make new query. |
| 30 | + if query._cursor is not None: |
| 31 | + query = query.with_cursor(query._cursor) |
| 32 | + |
| 33 | + curr_results = query.fetch() |
| 34 | + results.extend(curr_results) |
| 35 | + |
| 36 | + more_results = len(curr_results) == FETCH_MAX |
| 37 | + |
| 38 | + # Now that we have all results, we seek to delete. |
| 39 | + key_pbs = [entity.key().to_protobuf() for entity in results] |
| 40 | + connection.delete_entities(dataset_id, key_pbs) |
| 41 | + |
| 42 | + |
| 43 | +def remove_all_entities(): |
| 44 | + print 'This command will remove all entities for the following kinds:' |
| 45 | + print '\n'.join(['- ' + val for val in ALL_KINDS]) |
| 46 | + response = raw_input('Is this OK [y/n]? ') |
| 47 | + if response.lower() != 'y': |
| 48 | + print 'Doing nothing.' |
| 49 | + return |
| 50 | + |
| 51 | + dataset = regression_utils.get_dataset() |
| 52 | + for kind in ALL_KINDS: |
| 53 | + remove_kind(dataset, kind) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + remove_all_entities() |
0 commit comments