Coverage for linkage/tests/person_tests.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-08-27 10:34 -0500

1r""" 

2crate_anon/linkage/tests/person_tests.py 

3 

4=============================================================================== 

5 

6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry. 

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

8 

9 This file is part of CRATE. 

10 

11 CRATE is free software: you can redistribute it and/or modify 

12 it under the terms of the GNU General Public License as published by 

13 the Free Software Foundation, either version 3 of the License, or 

14 (at your option) any later version. 

15 

16 CRATE is distributed in the hope that it will be useful, 

17 but WITHOUT ANY WARRANTY; without even the implied warranty of 

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

19 GNU General Public License for more details. 

20 

21 You should have received a copy of the GNU General Public License 

22 along with CRATE. If not, see <https://www.gnu.org/licenses/>. 

23 

24=============================================================================== 

25 

26**Fuzzy matching tests for person representations.** 

27 

28""" 

29 

30from crate_anon.linkage.helpers import standardize_name, standardize_postcode 

31from crate_anon.linkage.matchconfig import mk_dummy_match_config 

32from crate_anon.linkage.person import Person 

33from crate_anon.testing.classes import CrateTestCase 

34 

35 

36class PersonTests(CrateTestCase): 

37 def test_person_created_from_plaintext_csv(self) -> None: 

38 local_id = "12345" 

39 gender = self.fake.sex() 

40 forename_1 = self.fake.forename(gender) 

41 forename_2 = self.fake.forename(gender) 

42 surname_1 = self.fake.last_name() 

43 surname_2 = self.fake.last_name() 

44 dob = self.fake.consistent_date_of_birth().strftime("%Y-%m-%d") 

45 postcode = self.fake.postcode() 

46 nhs_number = str(self.fake.nhs_number()) 

47 

48 rowdict = { 

49 "local_id": local_id, 

50 "forenames": f"{forename_1}; {forename_2}", 

51 "surnames": f"{surname_1}; {surname_2}", 

52 "dob": dob, 

53 "gender": gender, 

54 "postcodes": f"{postcode}", 

55 "perfect_id": f"nhsnum:{nhs_number}", 

56 "other_info": "", 

57 } 

58 

59 config = mk_dummy_match_config() 

60 

61 person = Person.from_plaintext_csv(config, rowdict) 

62 standardized_forenames = [f.name for f in person.forenames] 

63 raw_surnames = [s.raw_surname for s in person.surnames] 

64 

65 self.assertEqual(person.local_id, local_id) 

66 self.assertIn(standardize_name(forename_1), standardized_forenames) 

67 self.assertIn(standardize_name(forename_2), standardized_forenames) 

68 self.assertIn(surname_1, raw_surnames) 

69 self.assertIn(surname_2, raw_surnames) 

70 self.assertEqual(person.dob.dob_str, dob) 

71 self.assertEqual(person.gender.gender_str, gender) 

72 self.assertEqual( 

73 person.postcodes[0].postcode_unit, standardize_postcode(postcode) 

74 ) 

75 self.assertEqual(person.perfect_id.identifiers, {"nhsnum": nhs_number})