Skip to content

Commit

Permalink
Make targetEdit work like observationEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
cquiroz committed Oct 30, 2024
1 parent 1006c28 commit ec0e113
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4775,11 +4775,12 @@ type TargetEdit {
"""
editType: EditType!

targetId: TargetId!

"""
Edited object
"""
value: Target!
id: Long! @deprecated(reason: "id is no longer computed; a constant value is returned")
value: Target
}

"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

CREATE OR REPLACE FUNCTION ch_target_edit()
RETURNS trigger AS $$
DECLARE
BEGIN
IF (TG_OP = 'DELETE') THEN
PERFORM pg_notify('ch_target_edit', OLD.c_target_id || ',' || OLD.c_program_id || ',' || TG_OP);
END IF;
IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
PERFORM pg_notify('ch_target_edit', NEW.c_target_id || ',' || NEW.c_program_id || ',' || TG_OP);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS ch_target_insert_update_trigger on t_target;

CREATE CONSTRAINT TRIGGER ch_target_insert_update_trigger
AFTER INSERT OR UPDATE OR DELETE ON t_target
DEFERRABLE
FOR EACH ROW
EXECUTE PROCEDURE ch_target_edit();



Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ trait SubscriptionMapping[F[_]] extends Predicates[F] {
.map { e =>
Result(
Environment(
Env("editType" -> e.editType),
Env(
"editType" -> e.editType,
"targetId" -> e.targetId
),
Unique(Filter(Predicates.targetEdit.value.id.eql(e.targetId), child))
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package lucuma.odb.graphql
package mapping


import grackle.Result
import lucuma.core.model.Target
import lucuma.odb.data.EditType
import lucuma.odb.graphql.table.TargetView

Expand All @@ -16,8 +16,8 @@ trait TargetEditMapping[F[_]] extends TargetView[F] {
lazy val TargetEditMapping: ObjectMapping =
ObjectMapping(TargetEditType)(
SqlField("synthetic-id", TargetView.TargetId, key = true, hidden = true),
CursorField("id", _ => Result(0L), List("synthetic-id")),
CursorField("editType", _.envR[EditType]("editType"), List("synthetic-id")),
CursorField("targetId", _.envR[Target.Id]("targetId"), List("synthetic-id")),
SqlObject("value")
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import lucuma.core.model.User
import lucuma.odb.data.EditType

import scala.concurrent.duration.*
import cats.effect.kernel.Ref

class targetEdit extends OdbSuite {

Expand Down Expand Up @@ -76,6 +77,7 @@ class targetEdit extends OdbSuite {
subscription {
targetEdit$args {
editType
targetId
value {
name
}
Expand All @@ -86,115 +88,135 @@ class targetEdit extends OdbSuite {

def targetEdit(
editType: EditType,
name: String
name: String,
tid: Target.Id
): Json =
Json.obj(
"targetEdit" -> Json.obj(
"editType" -> Json.fromString(editType.tag.toUpperCase),
"targetId" -> Json.fromString(tid.show),
"value" -> Json.obj(
"name" -> Json.fromString(name)
)
)
)

def created(name: String): Json =
targetEdit(EditType.Created, name)
def created(name: String, tid: Target.Id): Json =
targetEdit(EditType.Created, name, tid)

def updated(name: String): Json =
targetEdit(EditType.Updated, name)
def updated(name: String, tid: Target.Id): Json =
targetEdit(EditType.Updated, name, tid)

test("trigger for a new target in any program") {
subscriptionExpect(
user = pi,
query = nameSubscription(None, None),
mutations =
Right(
createProgramAs(pi).flatMap(createTarget(pi, _, "target 1")) >>
createProgramAs(pi).flatMap(createTarget(pi, _, "target 2"))
),
expected = List(created("target 1"), created("target 2"))
)
}

test("trigger for an updated target in any program") {
subscriptionExpect(
user = pi,
query = nameSubscription(None, None),
mutations =
Right(
for {
pid <- createProgramAs(pi)
tid <- createTarget(pi, pid, "old name")
_ <- updateTarget(pi, tid, "new name")
} yield ()
),
expected = List(created("old name"), updated("new name"))
)
}

test("trigger for a new target in [only] a specific program") {
createProgramAs(pi).flatMap { pid =>
subscriptionExpect(
Ref.of[IO, List[Target.Id]](List.empty[Target.Id]).flatMap { ref =>
subscriptionExpectF(
user = pi,
query = nameSubscription(Some(pid), None),
query = nameSubscription(None, None),
mutations =
Right(
createTarget(pi, pid, "should see this") >>
createProgramAs(pi).flatMap(createTarget(pi, _, "should not see this"))
createProgramAs(pi).flatMap(createTarget(pi, _, "target 1")
.flatTap(i => ref.update(i :: _))) >>
createProgramAs(pi).flatMap(createTarget(pi, _, "target 2")
.flatTap(i => ref.update(i :: _)))
),
expected = List(created("should see this"))
expectedF = ref.get.map(l => List(created("target 1", l(1)), created("target 2", l(0))))
)
}
}

test("trigger for an updated target in [only] a specific program") {
createProgramAs(pi).flatMap { pid =>
subscriptionExpect(
test("trigger for an updated target in any program") {
Ref.of[IO, List[Target.Id]](List.empty[Target.Id]).flatMap { ref =>
subscriptionExpectF(
user = pi,
query = nameSubscription(Some(pid), None),
query = nameSubscription(None, None),
mutations =
Right(
createTarget(pi, pid, "should see this").flatMap(updateTarget(pi, _, "and this")) >>
createProgramAs(pi).flatMap(createTarget(pi, _, "should not see this").flatMap(updateTarget(pi, _, "or this")))
for {
pid <- createProgramAs(pi)
tid <- createTarget(pi, pid, "old name").flatTap(i => ref.set(List(i)))
_ <- updateTarget(pi, tid, "new name")
} yield ()
),
expected = List(created("should see this"), updated("and this"))
expectedF = ref.get.map(l => List(created("old name", l(0)), updated("new name", l(0))))
)
}
}

test("trigger for [only] a specific updated target") {
createProgramAs(pi).flatMap { pid =>
createTarget(pi, pid, "old name").flatMap { tid =>
subscriptionExpect(
test("trigger for a new target in [only] a specific program") {
Ref.of[IO, List[Target.Id]](List.empty[Target.Id]).flatMap { ref =>
createProgramAs(pi).flatMap { pid =>
subscriptionExpectF(
user = pi,
query = nameSubscription(None, Some(tid)),
query = nameSubscription(Some(pid), None),
mutations =
Right(
updateTarget(pi, tid, "new name") >>
createTarget(pi, pid, "should not see this").flatMap(updateTarget(pi, _, "or this"))
createTarget(pi, pid, "should see this").flatTap(i => ref.set(List(i))) >>
createProgramAs(pi).flatMap(createTarget(pi, _, "should not see this"))
),
expected = List(updated("new name"))
expectedF = ref.get.map(l => List(created("should see this", l(0))))
)
}
}
}

test("trigger for an updated target in [only] a specific program") {
Ref.of[IO, List[Target.Id]](List.empty[Target.Id]).flatMap { ref =>
createProgramAs(pi).flatMap { pid =>
subscriptionExpectF(
user = pi,
query = nameSubscription(Some(pid), None),
mutations =
Right(
createTarget(pi, pid, "should see this")
.flatTap(i => ref.update(i :: _))
.flatMap(updateTarget(pi, _, "and this")) >>
createProgramAs(pi).flatMap(
createTarget(pi, _, "should not see this")
.flatTap(i => ref.update(i :: _))
.flatMap(updateTarget(pi, _, "or this")))
),
expectedF = ref.get.map(l => List(created("should see this", l(1)), updated("and this", l(1))))
)
}
}
}

test("trigger for [only] a specific updated target") {
Ref.of[IO, List[Target.Id]](List.empty[Target.Id]).flatMap { ref =>
createProgramAs(pi).flatMap { pid =>
createTarget(pi, pid, "old name")
.flatTap(i => ref.set(List(i)))
.flatMap { tid =>
subscriptionExpectF(
user = pi,
query = nameSubscription(None, Some(tid)),
mutations =
Right(
updateTarget(pi, tid, "new name") >>
createTarget(pi, pid, "should not see this").flatMap(updateTarget(pi, _, "or this"))
),
expectedF = ref.get.map(l => List(updated("new name", l(0))))
)
}
}
}
}

test("work even if no database fields are selected") {
subscriptionExpect(
user = pi,
query = s"""
subscription {
targetEdit {
editType
id
}
}
""",
mutations =
Right(
createProgramAs(pi).flatMap(createTarget(pi, _, "t")).replicateA(2)
),
expected = List.fill(2)(json"""{"targetEdit":{"editType":"CREATED","id":0}}""")
expected = List.fill(2)(json"""{"targetEdit":{"editType":"CREATED"}}""")
)
}

Expand Down

0 comments on commit ec0e113

Please sign in to comment.