Skip to content

Commit

Permalink
updtae trigger (#68)
Browse files Browse the repository at this point in the history
* updtae trigger

* update

* test_migration update

* Update function trigger calcul_point_voie

* Adding tests

* test_migration update

* Apply suggestions from code review

Co-authored-by: rldhont <rldhont@gmail.com>

* test_migration update

* 0.5.0

Co-authored-by: rldhont <rldhont@gmail.com>
  • Loading branch information
pdrillin and rldhont authored Jul 21, 2020
1 parent 5050d2e commit 22fe244
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### 0.5.0 - 21/07/2020
* Update Trigger for calculate number of point by road
* Adding test for this trigger

### 0.4.0 - 19/06/2020
* Fix bugs
* Adding tests
Expand Down
17 changes: 16 additions & 1 deletion gestion_base_adresse/install/sql/adresse/10_FUNCTION.sql
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,26 @@ DECLARE
nb integer;
BEGIN
IF TG_OP = 'DELETE' THEN
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = OLD.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = OLD.id_voie;
ELSIF TG_OP = 'INSERT' THEN
IF NEW.id_voie IS NOT NULL THEN
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = NEW.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = NEW.id_voie;
END IF;
ELSIF TG_OP = 'UPDATE' AND NEW.id_voie IS DISTINCT FROM OLD.id_voie THEN
IF NEW.id_voie IS NULL THEN
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = OLD.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = OLD.id_voie;
ELSIF TG_OP = 'INSERT' THEN
ELSIF OLD.id_voie IS NULL THEN
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = NEW.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = NEW.id_voie;
ELSE
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = NEW.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = NEW.id_voie;
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = OLD.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = OLD.id_voie;
END IF;
END IF;

RETURN NEW;
Expand Down
2 changes: 1 addition & 1 deletion gestion_base_adresse/install/sql/adresse/50_TRIGGER.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CREATE TRIGGER get_commune AFTER INSERT ON adresse.voie FOR EACH ROW EXECUTE PRO


-- point_adresse nb_point
CREATE TRIGGER nb_point AFTER INSERT ON adresse.point_adresse FOR EACH ROW EXECUTE PROCEDURE adresse.calcul_point_voie();
CREATE TRIGGER nb_point AFTER INSERT OR UPDATE ON adresse.point_adresse FOR EACH ROW EXECUTE PROCEDURE adresse.calcul_point_voie();


-- point_adresse nb_point_delete
Expand Down
43 changes: 43 additions & 0 deletions gestion_base_adresse/install/sql/upgrade/upgrade_to_0.5.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
BEGIN;

CREATE OR REPLACE FUNCTION adresse.calcul_point_voie() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
nb integer;
BEGIN
IF TG_OP = 'DELETE' THEN
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = OLD.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = OLD.id_voie;
ELSIF TG_OP = 'INSERT' THEN
IF NEW.id_voie IS NOT NULL THEN
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = NEW.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = NEW.id_voie;
END IF;
ELSIF TG_OP = 'UPDATE' AND NEW.id_voie IS DISTINCT FROM OLD.id_voie THEN
IF NEW.id_voie IS NULL THEN
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = OLD.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = OLD.id_voie;
ELSIF OLD.id_voie IS NULL THEN
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = NEW.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = NEW.id_voie;
ELSE
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = NEW.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = NEW.id_voie;
SELECT COUNT(id_point) into nb FROM adresse.point_adresse WHERE id_voie = OLD.id_voie;
UPDATE adresse.voie SET nb_point = nb WHERE id_voie = OLD.id_voie;
END IF;
END IF;

RETURN NEW;
END;
$$;

DROP TRIGGER IF EXISTS nb_point ON adresse.point_adresse;
CREATE TRIGGER nb_point
AFTER INSERT OR UPDATE
ON adresse.point_adresse
FOR EACH ROW
EXECUTE PROCEDURE adresse.calcul_point_voie();

COMMIT;
40 changes: 40 additions & 0 deletions gestion_base_adresse/test/test_execute_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,3 +1068,43 @@ def test_add_and_delete_road(self):
)
self.cursor.execute(sql)
self.assertEqual(None, self.cursor.fetchone())

def test_count_number_point(self):
""" Test count number of point for a road """
# Suppression des points pour pouvoir tout tester
sql = "TRUNCATE TABLE adresse.point_adresse RESTART IDENTITY"
self.cursor.execute(sql)

# Disable trigger
sql = (
"ALTER TABLE adresse.point_adresse "
"DISABLE TRIGGER trigger_point_adr"
)
self.cursor.execute(sql)

# Adding One point
sql=(
"INSERT INTO adresse.point_adresse(id_point, numero, geom, valide)"
"values(1, 20, St_geomfromtext('POINT(429162 6920851)', 2154), false) "
)
self.cursor.execute(sql)

# Set point valide = true
sql=(
"UPDATE adresse.point_adresse SET valide = true WHERE id_point = 1"
)
self.cursor.execute(sql)

# select id_voie from point
sql= (
"SELECT id_voie FROM adresse.point_adresse WHERE id_point = 1"
)
self.cursor.execute(sql)
self.assertEqual((3,), self.cursor.fetchone())

# test nb_point = 1
sql= (
"SELECT nb_point FROM adresse.voie WHERE id_voie = 3"
)
self.cursor.execute(sql)
self.assertEqual((1,), self.cursor.fetchone())

0 comments on commit 22fe244

Please sign in to comment.