def test_partitioned_table_basic_insert(self, hge_ctx, evts_webhook): if hge_ctx.pg_version < 110000: pytest.skip( 'Event triggers on partioned tables are not supported in Postgres versions < 11' ) return st_code, resp = hge_ctx.v1q_f(self.dir() + '/partition_table_setup.yaml') assert st_code == 200, resp table = {"schema": "hge_tests", "name": "measurement"} init_row = { "city_id": 1, "logdate": "2006-02-02", "peaktemp": 1, "unitsales": 1 } exp_ev_data = {"old": None, "new": init_row} st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "measurement_all", table, "INSERT", exp_ev_data) st_code, resp = hge_ctx.v1q_f(self.dir() + '/partition_table_teardown.yaml') assert st_code == 200, resp
def test_basic(self, hge_ctx): table = {"schema" : "hge_tests", "name": "test_t1"} init_row = {"c1" : 1, "c2" : "hello"} exp_ev_data = { "old": None, "new": init_row } headers = {"X-Header-From-Value": "MyValue", "X-Header-From-Env": "MyEnvValue"} st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp check_event(hge_ctx, "t1_all", table, "INSERT", exp_ev_data, headers, "/")
def test_timeout_long(self, hge_ctx, evts_webhook): table = {"schema": "hge_tests", "name": "test_t3"} init_row = {"c1": 1, "c2": "hello"} exp_ev_data = { "old": None, "new": init_row } st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp time.sleep(15) check_event(hge_ctx, evts_webhook, "t3_timeout_long", table, "INSERT", exp_ev_data, webhook_path = "/timeout_long")
def test_selected_payload(self, hge_ctx): table = {"schema": "hge_tests", "name": "test_t1"} init_row = {"c1": 1, "c2": "hello"} exp_ev_data = {"old": None, "new": {"c1": 1, "c2": "hello"}} st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp check_event(hge_ctx, "t1_payload", table, "INSERT", exp_ev_data) where_exp = {"c1": 1} set_exp = {"c2": "world"} exp_ev_data = {"old": {"c1": 1}, "new": {"c1": 1}} st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp check_event(hge_ctx, "t1_payload", table, "UPDATE", exp_ev_data) where_exp = {"c1": 1} set_exp = {"c1": 2} exp_ev_data = {"old": {"c1": 1}, "new": {"c1": 2}} st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp check_event(hge_ctx, "t1_payload", table, "UPDATE", exp_ev_data) where_exp = {"c1": 2} exp_ev_data = {"old": {"c2": "world"}, "new": None} st_code, resp = delete(hge_ctx, table, where_exp) assert st_code == 200, resp check_event(hge_ctx, "t1_payload", table, "DELETE", exp_ev_data)
def test_update_basic(self, hge_ctx): table = {"schema": "hge_tests", "name": "test_t1"} init_row = {"c1": 1, "c2": "hello"} exp_ev_data = {"old": None, "new": {"c1": 1, "c2": "hello"}} st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp with pytest.raises(queue.Empty): check_event(hge_ctx, "t1_cols", table, "INSERT", exp_ev_data, webhook_path="/new") where_exp = {"c1": 1} set_exp = {"c2": "world"} # expected no event hence previous expected data st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp with pytest.raises(queue.Empty): check_event(hge_ctx, "t1_cols", table, "UPDATE", exp_ev_data, webhook_path="/new") where_exp = {"c1": 1} set_exp = {"c1": 2} exp_ev_data = { "old": { "c1": 1, "c2": "world" }, "new": { "c1": 2, "c2": "world" } } st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp check_event(hge_ctx, "t1_cols", table, "UPDATE", exp_ev_data, webhook_path="/new") where_exp = {"c1": 2} exp_ev_data = {"old": {"c1": 2, "c2": "world"}, "new": None} st_code, resp = delete(hge_ctx, table, where_exp) assert st_code == 200, resp check_event(hge_ctx, "t1_cols", table, "DELETE", exp_ev_data, webhook_path="/new")
def test_bigint(self, hge_ctx, evts_webhook): table = {"schema": "hge_tests", "name": "test_bigint"} init_row = {"id": 50755254975729665, "name": "hello"} exp_ev_data = { "old": None, "new": { "id": "50755254975729665", "name": "hello" } } st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "bigint_all", table, "INSERT", exp_ev_data)
def test_basic(self, hge_ctx, evts_webhook): table = {"schema": "hge_tests", "name": "test_t1"} init_row = {"c1": 1, "c2": "hello"} exp_ev_data = { "old": None, "new": init_row } st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "t1_retry", table, "INSERT", exp_ev_data, webhook_path = "/fail", retry = 0) check_event(hge_ctx, evts_webhook, "t1_retry", table, "INSERT", exp_ev_data, webhook_path = "/fail", retry = 1) check_event(hge_ctx, evts_webhook, "t1_retry", table, "INSERT", exp_ev_data, webhook_path = "/fail", retry = 2) check_event(hge_ctx, evts_webhook, "t1_retry", table, "INSERT", exp_ev_data, webhook_path = "/fail", retry = 3) check_event(hge_ctx, evts_webhook, "t1_retry", table, "INSERT", exp_ev_data, webhook_path = "/fail", retry = 4)
def test_update_basic(self, hge_ctx, evts_webhook): table = {"schema": "hge_tests", "name": "test_t1"} # Expect that inserting a row (which would have triggered in original # create_event_trigger) does not trigger init_row = {"c1": 1, "c2": "hello", "c3": {"name": "clarke"}} st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp with pytest.raises(queue.Empty): check_event(hge_ctx, evts_webhook, "t1_cols", table, "INSERT", {}, webhook_path = "/new", get_timeout = 0) # Likewise for an update on c2: where_exp = {"c1": 1} set_exp = {"c2": "world"} st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp with pytest.raises(queue.Empty): check_event(hge_ctx, evts_webhook, "t1_cols", table, "UPDATE", {}, webhook_path = "/new", get_timeout = 0) where_exp = {"c1": 1} set_exp = {"c3": {"name": "bellamy"}} exp_ev_data = { "old": {"c1": 1, "c2": "world", "c3": {"name": "clarke"}}, "new": {"c1": 1, "c2": "world", "c3": {"name": "bellamy"}} } st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "t1_cols", table, "UPDATE", exp_ev_data, webhook_path ="/new") where_exp = {"c1": 1} set_exp = {"c1": 2} exp_ev_data = { "old": {"c1": 1, "c2": "world", "c3": {"name": "bellamy"}}, "new": {"c1": 2, "c2": "world", "c3": {"name": "bellamy"}} } st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "t1_cols", table, "UPDATE", exp_ev_data, webhook_path ="/new") where_exp = {"c1": 2} exp_ev_data = { "old": {"c1": 2, "c2": "world", "c3": {"name": "bellamy"}}, "new": None } st_code, resp = delete(hge_ctx, table, where_exp) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "t1_cols", table, "DELETE", exp_ev_data, webhook_path = "/new")
def test_insert_only(self, hge_ctx): table = {"schema": "hge_tests", "name": "test_t1"} init_row = {"c1": 1, "c2": "hello"} exp_ev_data = {"old": None, "new": init_row} headers = {} st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp check_event(hge_ctx, "t1_insert", table, "INSERT", exp_ev_data, headers, "/") where_exp = {"c1": 1} set_exp = {"c2": "world"} exp_ev_data = {"old": init_row, "new": {"c1": 1, "c2": "world"}} st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp with pytest.raises(queue.Empty): check_event(hge_ctx, "t1_insert", table, "UPDATE", exp_ev_data, headers, "/") exp_ev_data = {"old": {"c1": 1, "c2": "world"}, "new": None} st_code, resp = delete(hge_ctx, table, where_exp) assert st_code == 200, resp with pytest.raises(queue.Empty): check_event(hge_ctx, "t1_insert", table, "DELETE", exp_ev_data, headers, "/")
def test_delete_basic(self, hge_ctx, evts_webhook): table = {"schema": "hge_tests", "name": "test_t1"} init_row = {"c1": 1, "c2": "hello"} exp_ev_data = {"old": None, "new": init_row} st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp with pytest.raises(queue.Empty): check_event(hge_ctx, evts_webhook, "t1_all", table, "INSERT", exp_ev_data) where_exp = {"c1": 1} set_exp = {"c2": "world"} exp_ev_data = {"old": init_row, "new": {"c1": 1, "c2": "world"}} st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp with pytest.raises(queue.Empty): check_event(hge_ctx, evts_webhook, "t1_all", table, "UPDATE", exp_ev_data) exp_ev_data = {"old": {"c1": 1, "c2": "world"}, "new": None} st_code, resp = delete(hge_ctx, table, where_exp) assert st_code == 200, resp with pytest.raises(queue.Empty): check_event(hge_ctx, evts_webhook, "t1_all", table, "DELETE", exp_ev_data)
def test_empty_cols(self, hge_ctx): table = {"schema": "hge_tests", "name": "test_t1"} init_row = {"c1": 1, "c2": "hello"} exp_ev_data = {"old": None, "new": {}} headers = {} st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp check_event(hge_ctx, "t1_empty", table, "INSERT", exp_ev_data, headers, "/") where_exp = {"c1": 1} set_exp = {"c2": "world"} exp_ev_data = {"old": {}, "new": {}} st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp check_event(hge_ctx, "t1_empty", table, "UPDATE", exp_ev_data, headers, "/") exp_ev_data = {"old": {}, "new": None} st_code, resp = delete(hge_ctx, table, where_exp) assert st_code == 200, resp check_event(hge_ctx, "t1_empty", table, "DELETE", exp_ev_data, headers, "/")
def test_timeout_short(self, hge_ctx, evts_webhook): table = {"schema": "hge_tests", "name": "test_t2"} init_row = {"c1": 1, "c2": "hello"} exp_ev_data = {"old": None, "new": init_row} st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "t2_timeout_short", table, "INSERT", exp_ev_data, webhook_path="/sleep_2s", retry=0, get_timeout=5) check_event(hge_ctx, evts_webhook, "t2_timeout_short", table, "INSERT", exp_ev_data, webhook_path="/sleep_2s", retry=1, get_timeout=5) check_event(hge_ctx, evts_webhook, "t2_timeout_short", table, "INSERT", exp_ev_data, webhook_path="/sleep_2s", retry=2, get_timeout=5)
def test_basic(self, hge_ctx, evts_webhook): table = {"schema": "hge_tests", "name": "test_t1"} init_row = {"c1": 1, "c2": "hello"} exp_ev_data = { "old": None, "new": init_row } session_variables = { 'x-hasura-role': 'admin', 'x-hasura-allowed-roles': "['admin','user']", 'x-hasura-user-id': '1'} st_code, resp = insert(hge_ctx, table, init_row, headers = session_variables) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "t1_all", table, "INSERT", exp_ev_data, session_variables = session_variables) where_exp = {"c1": 1} set_exp = {"c2": "world"} exp_ev_data = { "old": init_row, "new": {"c1": 1, "c2": "world"} } session_variables = { 'x-hasura-role': 'admin', 'x-hasura-random': 'some_random_info', 'X-Random-Header': 'not_session_variable'} st_code, resp = update(hge_ctx, table, where_exp, set_exp, headers = session_variables) assert st_code == 200, resp session_variables.pop('X-Random-Header') check_event(hge_ctx, evts_webhook, "t1_all", table, "UPDATE", exp_ev_data, session_variables = session_variables) exp_ev_data = { "old": {"c1": 1, "c2": "world"}, "new": None } st_code, resp = delete(hge_ctx, table, where_exp) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "t1_all", table, "DELETE", exp_ev_data)
def test_insert_only(self, hge_ctx, evts_webhook): table = {"schema": "hge_tests", "name": "test_t1"} init_row = {"c1": 1, "c2": "hello"} exp_ev_data = { "old": None, "new": init_row } st_code, resp = insert(hge_ctx, table, init_row) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "t1_insert", table, "INSERT", exp_ev_data) where_exp = {"c1": 1} set_exp = {"c2": "world"} exp_ev_data = { "old": init_row, "new": {"c1": 1, "c2": "world"} } st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp with pytest.raises(queue.Empty): check_event(hge_ctx, evts_webhook, "t1_insert", table, "UPDATE", exp_ev_data, get_timeout=0) exp_ev_data = { "old": {"c1": 1, "c2": "world"}, "new": None } st_code, resp = delete(hge_ctx, table, where_exp) assert st_code == 200, resp with pytest.raises(queue.Empty): # NOTE: use a bit of a delay here, to catch any stray events generated above check_event(hge_ctx, evts_webhook, "t1_insert", table, "DELETE", exp_ev_data, get_timeout=2)
def test_geojson(self, hge_ctx, evts_webhook): table = {"schema": "hge_tests", "name": "test_geojson"} exp_ev_data = { "old": { "id": 1, "location": { "coordinates": [-43.77, 45.64], "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::4326" } }, "type": "Point" } }, "new": { "id": 2, "location": { "coordinates": [-43.77, 45.64], "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::4326" } }, "type": "Point" } } } where_exp = {"id": 1} set_exp = {"id": 2} st_code, resp = update(hge_ctx, table, where_exp, set_exp) assert st_code == 200, resp check_event(hge_ctx, evts_webhook, "geojson_all", table, "UPDATE", exp_ev_data)