66from gene2phenotype_app .models import (
77 User ,
88 LGDVariantType ,
9+ LGDVariantTypePublication ,
10+ LGDVariantTypeComment ,
911 LocusGenotypeDisease ,
1012 OntologyTerm ,
1113)
@@ -33,6 +35,7 @@ class LGDEditVariantTypesTests(TestCase):
3335 "gene2phenotype_app/fixtures/source.json" ,
3436 "gene2phenotype_app/fixtures/lgd_publication.json" ,
3537 "gene2phenotype_app/fixtures/lgd_variant_type.json" ,
38+ "gene2phenotype_app/fixtures/lgd_variant_type_publication.json" ,
3639 ]
3740
3841 def setUp (self ):
@@ -233,6 +236,10 @@ def test_readd_soft_deleted_variant_updates_inheritance_flags(self):
233236 inherited = True ,
234237 de_novo = True ,
235238 unknown_inheritance = True ,
239+ is_deleted = 1 ,
240+ )
241+ LGDVariantTypePublication .objects .create (
242+ lgd_variant_type = lgd_variant ,
236243 publication_id = 3 ,
237244 is_deleted = 1 ,
238245 )
@@ -263,11 +270,16 @@ def test_readd_soft_deleted_variant_updates_inheritance_flags(self):
263270 self .assertFalse (lgd_variant .inherited )
264271 self .assertFalse (lgd_variant .de_novo )
265272 self .assertFalse (lgd_variant .unknown_inheritance )
273+ self .assertTrue (
274+ LGDVariantTypePublication .objects .filter (
275+ lgd_variant_type = lgd_variant , publication_id = 3 , is_deleted = 0
276+ ).exists ()
277+ )
266278
267279 def test_reuse_existing_row_with_new_publication_updates_inheritance_flags (self ):
268280 """
269- Test that converting an existing publication-less row into a
270- publication-linked row replaces inheritance flags with the latest payload.
281+ Test that adding a publication link to an existing publication-less
282+ variant type replaces inheritance flags with the latest payload.
271283 """
272284 user = User .objects .get (email = "john@test.ac.uk" )
273285 refresh = RefreshToken .for_user (user )
@@ -281,7 +293,6 @@ def test_reuse_existing_row_with_new_publication_updates_inheritance_flags(self)
281293 inherited = True ,
282294 de_novo = True ,
283295 unknown_inheritance = True ,
284- publication = None ,
285296 is_deleted = 0 ,
286297 )
287298
@@ -307,7 +318,69 @@ def test_reuse_existing_row_with_new_publication_updates_inheritance_flags(self)
307318 self .assertEqual (response .status_code , 201 )
308319
309320 lgd_variant .refresh_from_db ()
310- self .assertEqual (lgd_variant .publication_id , 3 )
321+ self .assertTrue (
322+ LGDVariantTypePublication .objects .filter (
323+ lgd_variant_type = lgd_variant , publication_id = 3 , is_deleted = 0
324+ ).exists ()
325+ )
311326 self .assertFalse (lgd_variant .inherited )
312327 self .assertFalse (lgd_variant .de_novo )
313328 self .assertFalse (lgd_variant .unknown_inheritance )
329+
330+ def test_add_variant_type_with_multiple_publications_no_duplication (self ):
331+ """
332+ Test that adding a variant type supported by more than one publication
333+ creates exactly one LGDVariantType row, one LGDVariantTypePublication
334+ row per publication, and does not duplicate the comment.
335+ """
336+ user = User .objects .get (email = "john@test.ac.uk" )
337+ refresh = RefreshToken .for_user (user )
338+ access_token = str (refresh .access_token )
339+ self .client .cookies [settings .SIMPLE_JWT ["AUTH_COOKIE" ]] = access_token
340+
341+ payload = {
342+ "variant_types" : [
343+ {
344+ "comment" : "supported by two papers" ,
345+ "de_novo" : False ,
346+ "inherited" : True ,
347+ "primary_type" : "protein_changing" ,
348+ "secondary_type" : "stop_gained" ,
349+ "supporting_papers" : ["12451214" , "20512146" ],
350+ "unknown_inheritance" : False ,
351+ }
352+ ]
353+ }
354+
355+ response = self .client .post (
356+ self .url_add_variant ,
357+ payload ,
358+ content_type = "application/json" ,
359+ )
360+ self .assertEqual (response .status_code , 201 )
361+
362+ stop_gained = OntologyTerm .objects .get (term = "stop_gained" )
363+ lgd_variant_qs = LGDVariantType .objects .filter (
364+ lgd__stable_id__stable_id = "G2P00002" ,
365+ variant_type_ot = stop_gained ,
366+ is_deleted = 0 ,
367+ )
368+ self .assertEqual (len (lgd_variant_qs ), 1 )
369+
370+ lgd_variant = lgd_variant_qs .first ()
371+ lgd_variant_publications = LGDVariantTypePublication .objects .filter (
372+ lgd_variant_type = lgd_variant , is_deleted = 0
373+ )
374+ self .assertEqual (len (lgd_variant_publications ), 2 )
375+ self .assertEqual (
376+ set (lgd_variant_publications .values_list ("publication__pmid" , flat = True )),
377+ {12451214 , 20512146 },
378+ )
379+
380+ lgd_variant_comments = LGDVariantTypeComment .objects .filter (
381+ lgd_variant_type = lgd_variant , is_deleted = 0
382+ )
383+ self .assertEqual (len (lgd_variant_comments ), 1 )
384+ self .assertEqual (
385+ lgd_variant_comments .first ().comment , "supported by two papers"
386+ )
0 commit comments