Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept date strings in test data for assurance tests #1845

Open
milanwiedemann opened this issue Dec 15, 2023 · 0 comments
Open

Accept date strings in test data for assurance tests #1845

milanwiedemann opened this issue Dec 15, 2023 · 0 comments

Comments

@milanwiedemann
Copy link
Member

milanwiedemann commented Dec 15, 2023

Currently dates have to be specified as datetime.date objects but we would like to also accept date strings so that dates in assurance tests can be specified like this:

    1: {
        "patients": [{"date_of_birth": "1950-01-01"}],
        "expected_in_population": True,
        "expected_columns": {
            "age": 73,
        },
    },

This currently fails with an AttributeError when using the test patient above to calculate age using .age_on in the dataset definition:

Traceback (most recent call last):
  File "/opt/venv/bin/ehrql", line 8, in <module>
    sys.exit(entrypoint())
             ^^^^^^^^^^^^
  File "/app/ehrql/__main__.py", line 67, in entrypoint
    return main(sys.argv[1:], environ=os.environ)  # pragma: no cover
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ehrql/__main__.py", line 92, in main
    function(**kwargs)
  File "/app/ehrql/main.py", line 326, in assure
    results = assurance.validate(variable_definitions, test_data)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ehrql/assurance.py", line 63, in validate
    query_results = {
                    ^
  File "/app/ehrql/assurance.py", line 63, in <dictcomp>
    query_results = {
                    ^
  File "/app/ehrql/query_engines/in_memory.py", line 32, in get_results
    table = self.get_results_as_table(variable_definitions)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ehrql/query_engines/in_memory.py", line 56, in get_results_as_table
    col = self.visit(node)
          ^^^^^^^^^^^^^^^^
  File "/app/ehrql/query_engines/in_memory.py", line 86, in visit
    value = visitor(node)
            ^^^^^^^^^^^^^
  File "/app/ehrql/query_engines/in_memory.py", line 313, in visit_DateDifferenceInYears
    return self.visit_binary_op_with_null(node, date_utils.date_difference_in_years)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ehrql/query_engines/in_memory.py", line 203, in visit_binary_op_with_null
    return self.visit_binary_op(node, handle_null(op))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ehrql/query_engines/in_memory.py", line 192, in visit_binary_op
    return apply_function(op, lhs, rhs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ehrql/query_engines/in_memory_database.py", line 457, in apply_function
    {p: fn(*[col[p] for col in columns]) for p in patients},
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ehrql/query_engines/in_memory_database.py", line 457, in <dictcomp>
    {p: fn(*[col[p] for col in columns]) for p in patients},
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ehrql/query_engines/in_memory_database.py", line 503, in fn_with_null
    return fn(*values)
           ^^^^^^^^^^^
  File "/app/ehrql/utils/date_utils.py", line 30, in date_difference_in_years
    year_diff = end.year - start.year
                           ^^^^^^^^^^
AttributeError: 'str' object has no attribute 'year'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant