@@ -111,3 +111,127 @@ var _ = Describe("Runs Table Integration", func() {
111111 Expect (err .Error ()).To (ContainSubstring ("violates foreign key constraint" ))
112112 })
113113})
114+
115+ var _ = Describe ("Planned Runs Table Integration" , func () {
116+ var userID uuid.UUID
117+
118+ BeforeEach (func () {
119+ userID = uuid .New ()
120+ now := time .Now ()
121+ // Insert into credentials first (to satisfy users FK)
122+ _ , err := testDbInstance .Exec (`
123+ INSERT INTO credentials (id, email, password, created_at, last_login)
124+ VALUES ($1, $2, $3, $4, $5)
125+ ` , userID , "planuser@example.com" , "hashedpassword" , now , now )
126+ Expect (err ).To (BeNil ())
127+
128+ // Insert into users
129+ _ , err = testDbInstance .Exec (`
130+ INSERT INTO users (id, username, email, rocketpoints)
131+ VALUES ($1, $2, $3, $4)
132+ ` , userID , "planuser" , "planuser@example.com" , 0 )
133+ Expect (err ).To (BeNil ())
134+ })
135+
136+ AfterEach (func () {
137+ testDbInstance .Exec ("DELETE FROM planned_runs" )
138+ testDbInstance .Exec ("DELETE FROM users" )
139+ testDbInstance .Exec ("DELETE FROM credentials" )
140+ })
141+
142+ It ("should insert and retrieve a planned run for a user" , func () {
143+ route := "LINESTRING(2 2,3 3)"
144+ name := "Morning Plan"
145+ distance := 5.0
146+
147+ _ , err := testDbInstance .Exec (`
148+ INSERT INTO planned_runs (user_id, route, name, distance)
149+ VALUES ($1, ST_GeomFromText($2, 4326), $3, $4)
150+ ` , userID , route , name , distance )
151+ Expect (err ).To (BeNil ())
152+
153+ rows , err := testDbInstance .Query (`
154+ SELECT id, ST_AsText(route), name, created_at, distance
155+ FROM planned_runs
156+ WHERE user_id = $1
157+ ` , userID )
158+ Expect (err ).To (BeNil ())
159+ defer rows .Close ()
160+
161+ var found bool
162+ for rows .Next () {
163+ var id uuid.UUID
164+ var gotRoute , gotName string
165+ var createdAt time.Time
166+ var gotDistance float64
167+ err := rows .Scan (& id , & gotRoute , & gotName , & createdAt , & gotDistance )
168+ Expect (err ).To (BeNil ())
169+ Expect (gotRoute ).To (Equal (route ))
170+ Expect (gotName ).To (Equal (name ))
171+ Expect (gotDistance ).To (BeNumerically ("~" , distance , 0.01 ))
172+ found = true
173+ }
174+ Expect (found ).To (BeTrue ())
175+ })
176+
177+ It ("should delete a planned run" , func () {
178+ route := "LINESTRING(2 2,3 3)"
179+ name := "Delete Plan"
180+ distance := 3.0
181+
182+ var planID uuid.UUID
183+ err := testDbInstance .QueryRow (`
184+ INSERT INTO planned_runs (user_id, route, name, distance)
185+ VALUES ($1, ST_GeomFromText($2, 4326), $3, $4)
186+ RETURNING id
187+ ` , userID , route , name , distance ).Scan (& planID )
188+ Expect (err ).To (BeNil ())
189+
190+ _ , err = testDbInstance .Exec (`
191+ DELETE FROM planned_runs WHERE id = $1
192+ ` , planID )
193+ Expect (err ).To (BeNil ())
194+
195+ row := testDbInstance .QueryRow (`
196+ SELECT COUNT(*) FROM planned_runs WHERE id = $1
197+ ` , planID )
198+ var count int
199+ err = row .Scan (& count )
200+ Expect (err ).To (BeNil ())
201+ Expect (count ).To (Equal (0 ))
202+ })
203+
204+ It ("should enforce unique constraint on (user_id, name)" , func () {
205+ route := "LINESTRING(2 2,3 3)"
206+ name := "Unique Plan"
207+ distance := 4.0
208+
209+ _ , err := testDbInstance .Exec (`
210+ INSERT INTO planned_runs (user_id, route, name, distance)
211+ VALUES ($1, ST_GeomFromText($2, 4326), $3, $4)
212+ ` , userID , route , name , distance )
213+ Expect (err ).To (BeNil ())
214+
215+ // Try to insert another planned run with the same user_id and name
216+ _ , err = testDbInstance .Exec (`
217+ INSERT INTO planned_runs (user_id, route, name, distance)
218+ VALUES ($1, ST_GeomFromText($2, 4326), $3, $4)
219+ ` , userID , route , name , distance )
220+ Expect (err ).ToNot (BeNil ())
221+ Expect (err .Error ()).To (ContainSubstring ("duplicate key" ))
222+ })
223+
224+ It ("should enforce foreign key constraint on user_id" , func () {
225+ nonExistentUserID := uuid .New ()
226+ route := "LINESTRING(2 2,3 3)"
227+ name := "FK Plan"
228+ distance := 2.5
229+
230+ _ , err := testDbInstance .Exec (`
231+ INSERT INTO planned_runs (user_id, route, name, distance)
232+ VALUES ($1, ST_GeomFromText($2, 4326), $3, $4)
233+ ` , nonExistentUserID , route , name , distance )
234+ Expect (err ).ToNot (BeNil ())
235+ Expect (err .Error ()).To (ContainSubstring ("violates foreign key constraint" ))
236+ })
237+ })
0 commit comments