class Example(object): def __init__(self): self.app = Flask(__name__) self.app.debug = True self.basic_example_tree_create() self.handler = Handler(self.tree) self.set_up() def basic_example_tree_create(self): self.tree = SyncTree(name="root_node") root = self.tree.root CSE = self.tree.add_node(root, category_name="CSE events") ECE = self.tree.add_node(root, category_name="ECE events") hackathon = self.tree.add_node(CSE, event_name="Esya Hackathon", hours=16) prosort = self.tree.add_node(CSE, event_name="Foobar Prosort", prizes=10000) hackOn = self.tree.add_node(CSE, event_name="HackOn", organisers=["a", "b"]) IOT_hackathon = self.tree.add_node(ECE, event_name="IOT", food=True) self.tree.refresh_tree() def set_up(self): @self.app.route('/api/sync/node') def end_point(): request_type = request.args.get('type', 'check') handle_request = {'check': self.handler.check, 'fetch': self.handler.fetch, 'get_parents': self.handler.get_parents} if request_type not in handle_request: return jsonify(success=False, error_message="Unknown API call type.") return handle_request[request_type](request) @self.app.route('/api/sync') def refresh_point(): DEFAULT_STARTING_TIME = 0 try: client_time = float(request.args.get( 'updated_time', DEFAULT_STARTING_TIME)) except ValueError: client_time = DEFAULT_STARTING_TIME nodes = self.tree.get_nodes_after_time(client_time) return jsonify(success=True, data={node._pk: { "hash": node.get_sync_hash(), "updated_time": node.get_update_time()} for node in nodes})
def test_get_nodes_after_time(self): now = time_now() tree = SyncTree(**temp_info) root = tree.root # pk 0 root_child1 = tree.add_node(root, **temp_info) # pk 1 root_child2 = tree.add_node(root, **temp_info) # pk 2 root_child1_child1 = tree.add_node(root_child1, **temp_info) # pk 3 root_child2_child1 = tree.add_node(root_child2, **temp_info) # pk 4 root_child2_child1_child1 = tree.add_node(root_child2_child1, **temp_info) # pk 5 set_all = set([root, root_child1, root_child2, root_child1_child1, root_child2_child1, root_child2_child1_child1]) empty = set() tree.refresh_tree() changed_nodes = tree.get_nodes_after_time(now) self.assertSetEqual(changed_nodes, set_all) now = time_now() changed_nodes = tree.get_nodes_after_time(now) self.assertSetEqual(changed_nodes, empty) root_child1_child1.abc = "asg" tree.refresh_tree() changed_nodes = tree.get_nodes_after_time(now) self.assertSetEqual(changed_nodes, set([root_child1_child1, root_child1, root])) root.abd = "asg" tree.refresh_tree() changed_nodes = tree.get_nodes_after_time(now) self.assertSetEqual(changed_nodes, set([root_child1_child1, root_child1, root])) now = time_now() changed_nodes = tree.get_nodes_after_time(now) self.assertSetEqual(changed_nodes, empty) root_child2_child1_child1.abc = "def" root_child1.abc = "def" tree.refresh_tree() self.assertEqual(tree.get_nodes_after_time(now), set([root, root_child1, root_child2, root_child2_child1, root_child2_child1_child1]))