diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index b588d1ec8c97..8ae1e45d06b1 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -209,6 +209,12 @@ Running Regression Tests $ python regression/populate_datastore.py +- If you make a mistake during development (i.e. a failing test that + prevents clean-up) you can clear all regression data from your + datastore instance via:: + + $ python regression/clear_datastore.py + Test Coverage ------------- diff --git a/regression/clear_datastore.py b/regression/clear_datastore.py new file mode 100644 index 000000000000..19d294e43b53 --- /dev/null +++ b/regression/clear_datastore.py @@ -0,0 +1,57 @@ +"""Script to populate datastore with regression test data.""" + + +# This assumes the command is being run via tox hence the +# repository root is the current directory. +from regression import regression_utils + + +FETCH_MAX = 20 +ALL_KINDS = [ + 'Character', + 'Company', + 'Kind', + 'Person', + 'Post', +] + + +def remove_kind(dataset, kind): + dataset_id = dataset.id() + connection = dataset.connection() + + with dataset.transaction(): + query = dataset.query(kind=kind).limit( + FETCH_MAX).projection(['__key__']) + results = [] + more_results = True + while more_results: + # Make new query. + if query._cursor is not None: + query = query.with_cursor(query._cursor) + + curr_results = query.fetch() + results.extend(curr_results) + + more_results = len(curr_results) == FETCH_MAX + + # Now that we have all results, we seek to delete. + key_pbs = [entity.key().to_protobuf() for entity in results] + connection.delete_entities(dataset_id, key_pbs) + + +def remove_all_entities(): + print 'This command will remove all entities for the following kinds:' + print '\n'.join(['- ' + val for val in ALL_KINDS]) + response = raw_input('Is this OK [y/n]? ') + if response.lower() != 'y': + print 'Doing nothing.' + return + + dataset = regression_utils.get_dataset() + for kind in ALL_KINDS: + remove_kind(dataset, kind) + + +if __name__ == '__main__': + remove_all_entities()