Coverage for anonymise/tests/factories.py: 94%

17 statements  

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

1""" 

2crate_anon/anonymise/tests/factories.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**Factory Boy SQL Alchemy test factories for anonymisation.** 

27 

28""" 

29 

30from typing import TYPE_CHECKING 

31 

32from cardinal_pythonlib.hash import HashMethods, make_hasher 

33import factory 

34 

35from crate_anon.anonymise.models import PatientInfo 

36from crate_anon.testing.factories import SecretBaseFactory, Fake 

37 

38if TYPE_CHECKING: 

39 from factory.builder import Resolver 

40 

41 

42class PatientInfoFactory(SecretBaseFactory): 

43 class Meta: 

44 exclude = ("hasher",) 

45 model = PatientInfo 

46 

47 hasher = make_hasher(HashMethods.HMAC_MD5, "encryptionphrase") 

48 pid = factory.Sequence(lambda n: n + 1) 

49 mpid = factory.LazyFunction(Fake.en_gb.nhs_number) 

50 

51 @factory.lazy_attribute 

52 def rid(obj: "Resolver") -> str: 

53 return obj.hasher.hash(obj.pid)