Skip to content

Commit

Permalink
LIBFCREPO-1120. Added pytest test suite.
Browse files Browse the repository at this point in the history
Tests date field for EDTF -> timestamp parsing.

https://issues.umd.edu/browse/LIBFCREPO-1120
  • Loading branch information
peichman-umd committed Apr 27, 2023
1 parent 5d1e9e9 commit 5ed71e3
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 9 deletions.
8 changes: 7 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
.dockerignore
../Dockerfile
.gitignore
.pytest_cache
.python-version
.venv
Dockerfile
requirements.txt
tests
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.pytest_cache
.venv
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.11
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ UMD Libraries Solr Index for Fedora (fedora4 core)
## Docker Image

Uses the [Solr Docker base image](https://hub.docker.com/_/solr/), Solr
version 6.
version 7.7.3.

[Dockerfile](Dockerfile)

### Volumes

|Mount point|Purpose|
|-----------|-------|
|/var/opt/solr|Persistent core data|
| Mount point | Purpose |
|---------------|----------------------|
| /var/opt/solr | Persistent core data |

### Ports

|Port number|Purpose|
|-----------|-------|
|8983 |Solr web admin interface|
| Port number | Purpose |
|-------------|--------------------------|
| 8983 | Solr web admin interface |

### Build

Expand All @@ -33,7 +33,7 @@ Build the image:
docker build -t docker.lib.umd.edu/fcrepo-solr-fedora4 .
```

TODO: specify required environment to run this image
### Run

```bash
docker run -it --rm --name fcrepo-solr-fedora4 \
Expand All @@ -46,6 +46,33 @@ The Solr web admin console will be at <http://localhost:8983/solr/#/>
The Java heap size can be controlled by setting the environment variable
`SOLR_FEDORA4_HEAP_SIZE`. The default if it is not set is `1024m`.

## Testing

This repository has a Python [pytest] test suite for testing.

### Installing

```bash
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

### Running

The tests require a running instance of this Solr image. By default,
it assumes that the endpoint URL is <http://localhost:8983/solr/fedora4>.
This can be changed by setting the environment variable `SOLR_ENDPOINT`.

```bash
# run tests with the default endpoint
pytest

# run with a custom endpoint (e.g., a different port)
export SOLR_ENDPOINT=http://localhost:18983/solr/fedora4
pytest
```

## History

This code comes from the
Expand All @@ -59,3 +86,4 @@ See the [LICENSE](LICENSE) file for license rights and limitations (Apache 2.0).

[Solr]: https://solr.apache.org/
[umd-fcrepo-docker]: https://github.com/umd-lib/umd-fcrepo-docker
[pytest]: https://pypi.org/project/pytest/
10 changes: 10 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
certifi==2022.12.7
charset-normalizer==3.1.0
idna==3.4
iniconfig==2.0.0
packaging==23.1
pluggy==1.0.0
pysolr==3.9.0
pytest==7.3.1
requests==2.29.0
urllib3==1.26.15
189 changes: 189 additions & 0 deletions tests/test_date_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import os
from uuid import uuid4

import pytest
from pysolr import Solr


def _index_doc(solr, doc):
uuid = str(uuid4())
solr.add([{'id': uuid, **doc}])
solr.commit()
results = solr.search(f'id:{uuid}')
return uuid, results


def _cleanup(solr, uuid):
# clean up after ourselves
solr.delete(id=uuid)
solr.commit()


@pytest.fixture()
def solr_endpoint():
return os.environ.get('SOLR_ENDPOINT', 'http://localhost:8983/solr/fedora4')


@pytest.fixture()
def solr(solr_endpoint):
return Solr(solr_endpoint)


@pytest.mark.parametrize(
('input_date', 'output_date'),
[
('2012', '2012-01-01T00:00:00Z'),
('~2012', '2012-01-01T00:00:00Z'),
('2012~', '2012-01-01T00:00:00Z'),
('?2012', '2012-01-01T00:00:00Z'),
('2012?', '2012-01-01T00:00:00Z'),
('%2012', '2012-01-01T00:00:00Z'),
('2012%', '2012-01-01T00:00:00Z'),
('211X', '2110-01-01T00:00:00Z'),
('21XX', '2100-01-01T00:00:00Z'),
('2XXX', '2000-01-01T00:00:00Z'),
]
)
def test_date_field_year_only(solr, input_date, output_date):
uuid, results = _index_doc(solr, {'date': input_date})
assert results.docs[0]['date'] == output_date
_cleanup(solr, uuid)


@pytest.mark.parametrize(
('input_date', 'output_date'),
[
('2012-04', '2012-04-01T00:00:00Z'),
('~2012-04', '2012-04-01T00:00:00Z'),
('2012~-04', '2012-04-01T00:00:00Z'),
('2012-~04', '2012-04-01T00:00:00Z'),
('2012-04~', '2012-04-01T00:00:00Z'),
('?2012-04', '2012-04-01T00:00:00Z'),
('2012?-04', '2012-04-01T00:00:00Z'),
('2012-?04', '2012-04-01T00:00:00Z'),
('2012-04?', '2012-04-01T00:00:00Z'),
('%2012-04', '2012-04-01T00:00:00Z'),
('2012%-04', '2012-04-01T00:00:00Z'),
('2012-%04', '2012-04-01T00:00:00Z'),
('2012-04%', '2012-04-01T00:00:00Z'),
('2112-0X', '2112-01-01T00:00:00Z'),
('2112-1X', '2112-10-01T00:00:00Z'),
('211X-0X', '2110-01-01T00:00:00Z'),
('211X-1X', '2110-10-01T00:00:00Z'),
('21XX-0X', '2100-01-01T00:00:00Z'),
('21XX-1X', '2100-10-01T00:00:00Z'),
('2XXX-0X', '2000-01-01T00:00:00Z'),
('2XXX-1X', '2000-10-01T00:00:00Z'),
]
)
def test_date_field_year_month(solr, input_date, output_date):
uuid, results = _index_doc(solr, {'date': input_date})
assert results.docs[0]['date'] == output_date
_cleanup(solr, uuid)


@pytest.mark.parametrize(
('input_date', 'output_date'),
[
('2012-04-26', '2012-04-26T00:00:00Z'),
('~2012-04-26', '2012-04-26T00:00:00Z'),
('2012~-04-26', '2012-04-26T00:00:00Z'),
('2012-~04-26', '2012-04-26T00:00:00Z'),
('2012-04~-26', '2012-04-26T00:00:00Z'),
('2012-04-~26', '2012-04-26T00:00:00Z'),
('2012-04-26~', '2012-04-26T00:00:00Z'),
('?2012-04-26', '2012-04-26T00:00:00Z'),
('2012?-04-26', '2012-04-26T00:00:00Z'),
('2012-?04-26', '2012-04-26T00:00:00Z'),
('2012-04?-26', '2012-04-26T00:00:00Z'),
('2012-04-?26', '2012-04-26T00:00:00Z'),
('2012-04-26?', '2012-04-26T00:00:00Z'),
('%2012-04-26', '2012-04-26T00:00:00Z'),
('2012%-04-26', '2012-04-26T00:00:00Z'),
('2012-%04-26', '2012-04-26T00:00:00Z'),
('2012-04%-26', '2012-04-26T00:00:00Z'),
('2012-04-%26', '2012-04-26T00:00:00Z'),
('2012-04-26%', '2012-04-26T00:00:00Z'),
# 2112-*-*
('2112-04-0X', '2112-04-01T00:00:00Z'),
('2112-04-1X', '2112-04-10T00:00:00Z'),
('2112-04-2X', '2112-04-20T00:00:00Z'),
('2112-04-3X', '2112-04-30T00:00:00Z'),
('2112-0X-13', '2112-01-13T00:00:00Z'),
('2112-0X-0X', '2112-01-01T00:00:00Z'),
('2112-0X-1X', '2112-01-10T00:00:00Z'),
('2112-0X-2X', '2112-01-20T00:00:00Z'),
('2112-0X-3X', '2112-01-30T00:00:00Z'),
('2112-1X-13', '2112-10-13T00:00:00Z'),
('2112-1X-0X', '2112-10-01T00:00:00Z'),
('2112-1X-1X', '2112-10-10T00:00:00Z'),
('2112-1X-2X', '2112-10-20T00:00:00Z'),
('2112-1X-3X', '2112-10-30T00:00:00Z'),
# 211X-*-*
('211X-0X-13', '2110-01-13T00:00:00Z'),
('211X-0X-0X', '2110-01-01T00:00:00Z'),
('211X-0X-1X', '2110-01-10T00:00:00Z'),
('211X-0X-2X', '2110-01-20T00:00:00Z'),
('211X-0X-3X', '2110-01-30T00:00:00Z'),
('211X-1X-13', '2110-10-13T00:00:00Z'),
('211X-1X-0X', '2110-10-01T00:00:00Z'),
('211X-1X-1X', '2110-10-10T00:00:00Z'),
('211X-1X-2X', '2110-10-20T00:00:00Z'),
('211X-1X-3X', '2110-10-30T00:00:00Z'),
# 21XX-*-*
('21XX-0X-13', '2100-01-13T00:00:00Z'),
('21XX-0X-0X', '2100-01-01T00:00:00Z'),
('21XX-0X-1X', '2100-01-10T00:00:00Z'),
('21XX-0X-2X', '2100-01-20T00:00:00Z'),
('21XX-0X-3X', '2100-01-30T00:00:00Z'),
('21XX-1X-13', '2100-10-13T00:00:00Z'),
('21XX-1X-0X', '2100-10-01T00:00:00Z'),
('21XX-1X-1X', '2100-10-10T00:00:00Z'),
('21XX-1X-2X', '2100-10-20T00:00:00Z'),
('21XX-1X-3X', '2100-10-30T00:00:00Z'),
# 2XXX-*-*
('2XXX-0X-13', '2000-01-13T00:00:00Z'),
('2XXX-0X-0X', '2000-01-01T00:00:00Z'),
('2XXX-0X-1X', '2000-01-10T00:00:00Z'),
('2XXX-0X-2X', '2000-01-20T00:00:00Z'),
('2XXX-0X-3X', '2000-01-30T00:00:00Z'),
('2XXX-1X-13', '2000-10-13T00:00:00Z'),
('2XXX-1X-0X', '2000-10-01T00:00:00Z'),
('2XXX-1X-1X', '2000-10-10T00:00:00Z'),
('2XXX-1X-2X', '2000-10-20T00:00:00Z'),
('2XXX-1X-3X', '2000-10-30T00:00:00Z'),
]
)
def test_date_field_year_month_day(solr, input_date, output_date):
uuid, results = _index_doc(solr, {'date': input_date})
assert results.docs[0]['date'] == output_date
_cleanup(solr, uuid)


@pytest.mark.parametrize(
('input_date', 'output_date'),
[
('2012/', '2012-01-01T00:00:00Z'),
('/2012', '2012-01-01T00:00:00Z'),
('2012/..', '2012-01-01T00:00:00Z'),
('../2012', '2012-01-01T00:00:00Z'),
]
)
def test_date_field_interval(solr, input_date, output_date):
uuid, results = _index_doc(solr, {'date': input_date})
assert results.docs[0]['date'] == output_date
_cleanup(solr, uuid)


@pytest.mark.parametrize(
('input_date', 'output_date'),
[
('[2012-04-26,2012-05]', '2012-04-26T00:00:00Z'),
('[2012..]', '2012-01-01T00:00:00Z'),
('[..2012]', '2012-01-01T00:00:00Z'),
]
)
def test_date_field_set(solr, input_date, output_date):
uuid, results = _index_doc(solr, {'date': input_date})
assert results.docs[0]['date'] == output_date
_cleanup(solr, uuid)

0 comments on commit 5ed71e3

Please sign in to comment.