|
| 1 | +""" |
| 2 | +Common strategies and utilities used across multiple test modules. |
| 3 | +
|
| 4 | +Any real-world details or samples used as constants were correct when |
| 5 | +taken on 2024-03-06. |
| 6 | +""" |
| 7 | + |
| 8 | +import datetime as dt |
| 9 | +import string |
| 10 | + |
| 11 | +from dateutil import relativedelta as rd |
| 12 | +from hypothesis import strategies as st |
| 13 | +from langchain_community.chat_models import ChatOllama |
| 14 | + |
| 15 | +from parliai_public.readers.base import BaseReader |
| 16 | + |
| 17 | + |
| 18 | +class ToyReader(BaseReader): |
| 19 | + """A toy class to allow testing our abstract base class.""" |
| 20 | + |
| 21 | + def retrieve_latest_entries(self): |
| 22 | + """Allow testing with toy method.""" |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def _read_metadata(url, soup): |
| 26 | + """Allow testing with toy static method.""" |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def _read_contents(soup): |
| 30 | + """Allow testing with toy static method.""" |
| 31 | + |
| 32 | + def render(self, response, page): |
| 33 | + """Allow testing with toy method.""" |
| 34 | + |
| 35 | + def _summary_template(self): |
| 36 | + """Allow testing with toy method.""" |
| 37 | + |
| 38 | + |
| 39 | +def where_what(reader): |
| 40 | + """Get the right location and class for testing a reader.""" |
| 41 | + |
| 42 | + what = reader |
| 43 | + if reader is ToyReader: |
| 44 | + what = BaseReader |
| 45 | + |
| 46 | + where = ".".join((what.__module__, what.__name__)) |
| 47 | + |
| 48 | + return where, what |
| 49 | + |
| 50 | + |
| 51 | +def default_llm() -> ChatOllama: |
| 52 | + """Instantiate default LLM object for use in testing.""" |
| 53 | + |
| 54 | + llm = ChatOllama( |
| 55 | + model_name="gemma", |
| 56 | + temperature=0, |
| 57 | + # max_output_tokens=2048, |
| 58 | + ) |
| 59 | + |
| 60 | + return llm |
| 61 | + |
| 62 | + |
| 63 | +MPS_SAMPLE = [ |
| 64 | + ( |
| 65 | + "Bob Seely", |
| 66 | + "Conservative, Isle of Wight", |
| 67 | + "https://www.theyworkforyou.com/mp/25645/bob_seely/isle_of_wight", |
| 68 | + ), |
| 69 | + ( |
| 70 | + "Mark Logan", |
| 71 | + "Conservative, Bolton North East", |
| 72 | + "https://www.theyworkforyou.com/mp/25886/mark_logan/bolton_north_east", |
| 73 | + ), |
| 74 | + ( |
| 75 | + "Nigel Huddleston", |
| 76 | + "Conservative, Mid Worcestershire", |
| 77 | + "https://www.theyworkforyou.com/mp/25381/nigel_huddleston/mid_worcestershire", |
| 78 | + ), |
| 79 | + ( |
| 80 | + "Heather Wheeler", |
| 81 | + "Conservative, South Derbyshire", |
| 82 | + "https://www.theyworkforyou.com/mp/24769/heather_wheeler/south_derbyshire", |
| 83 | + ), |
| 84 | + ( |
| 85 | + "Ian Paisley Jnr", |
| 86 | + "DUP, North Antrim", |
| 87 | + "https://www.theyworkforyou.com/mp/13852/ian_paisley_jnr/north_antrim", |
| 88 | + ), |
| 89 | + ( |
| 90 | + "Matthew Offord", |
| 91 | + "Conservative, Hendon", |
| 92 | + "https://www.theyworkforyou.com/mp/24955/matthew_offord/hendon", |
| 93 | + ), |
| 94 | + ( |
| 95 | + "John Howell", |
| 96 | + "Conservative, Henley", |
| 97 | + "https://www.theyworkforyou.com/mp/14131/john_howell/henley", |
| 98 | + ), |
| 99 | + ( |
| 100 | + "Robert Goodwill", |
| 101 | + "Conservative, Scarborough and Whitby", |
| 102 | + "https://www.theyworkforyou.com/mp/11804/robert_goodwill/scarborough_and_whitby", |
| 103 | + ), |
| 104 | + ( |
| 105 | + "Naseem Shah", |
| 106 | + "Labour, Bradford West", |
| 107 | + "https://www.theyworkforyou.com/mp/25385/naseem_shah/bradford_west", |
| 108 | + ), |
| 109 | + ( |
| 110 | + "Amy Callaghan", |
| 111 | + "Scottish National Party, East Dunbartonshire", |
| 112 | + "https://www.theyworkforyou.com/mp/25863/amy_callaghan/east_dunbartonshire", |
| 113 | + ), |
| 114 | +] |
| 115 | + |
| 116 | +GOV_DEPARTMENTS = [ |
| 117 | + "Attorney General's Office", |
| 118 | + "Cabinet Office", |
| 119 | + "Department for Business and Trade", |
| 120 | + "Department for Culture, Media and Sport", |
| 121 | + "Department for Education", |
| 122 | + "Department for Energy Security and Net Zero", |
| 123 | + "Department for Environment, Food and Rural Affairs", |
| 124 | + "Department for Levelling Up, Housing and Communities", |
| 125 | + "Department for Science, Innovation and Technology", |
| 126 | + "Department for Transport", |
| 127 | + "Department for Work and Pensions", |
| 128 | + "Department of Health and Social Care", |
| 129 | + "Export Credits Guarantee Department", |
| 130 | + "Foreign, Commonwealth and Development Office", |
| 131 | + "HM Treasury", |
| 132 | + "Home Office", |
| 133 | + "Ministry of Defence", |
| 134 | + "Ministry of Justice", |
| 135 | + "Northern Ireland Office", |
| 136 | + "Office of the Advocate General for Scotland", |
| 137 | + "Office of the Leader of the House of Commons", |
| 138 | + "Office of the Leader of the House of Lords", |
| 139 | + "Office of the Secretary of State for Scotland", |
| 140 | + "Office of the Secretary of State for Wales", |
| 141 | +] |
| 142 | + |
| 143 | +SEARCH_TERMS = ( |
| 144 | + "ONS", |
| 145 | + "Office for National Statistics", |
| 146 | + "National Statistician", |
| 147 | +) |
| 148 | + |
| 149 | +TODAY = dt.date.today() |
| 150 | +ST_DATES = st.dates(TODAY - rd.relativedelta(years=4), TODAY) |
| 151 | + |
| 152 | +ST_FREE_TEXT = st.text( |
| 153 | + string.ascii_letters + string.digits + ".:;!?-", min_size=1 |
| 154 | +) |
| 155 | + |
| 156 | +MODEL_NAMES = ["llama3", "mistral", "openhermes"] |
| 157 | + |
| 158 | +GEMMA_PREAMBLES = [ |
| 159 | + "Sure! Here is the text you are looking for: \nMy right honourable friend...", |
| 160 | + "Sure - here is the quote: My right honourable friend...", |
| 161 | + "Sure!The following contains references to your search terms:My right honourable friend...", |
| 162 | +] |
0 commit comments