File size: 5,253 Bytes
b339b93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Contributing to Paper Trail API

Thank you for your interest in contributing to Paper Trail API! This document provides guidelines and instructions for contributing.

## Development Setup

### Prerequisites

- Python 3.13+
- [uv](https://docs.astral.sh/uv/) (recommended package manager)
- Git

### Getting Started

1. **Clone the repository**
   ```bash

   git clone https://github.com/Operation-Hope/paper-trail-api.git

   cd paper-trail-api

   ```

2. **Create a virtual environment and install dependencies**
   ```bash

   uv venv

   source .venv/bin/activate  # On Windows: .venv\Scripts\activate

   uv sync --all-extras

   ```

3. **Install the duckdb-loader package in development mode**
   ```bash

   uv pip install -e duckdb_loader/

   ```

4. **Set up pre-commit hooks**
   ```bash

   uv run pre-commit install

   ```

5. **Copy environment template**
   ```bash

   cp .env.example .env

   # Edit .env with your configuration

   ```

## Development Workflow

### Code Quality

We use the following tools to maintain code quality:

- **[Ruff](https://docs.astral.sh/ruff/)** - Linting and formatting
- **[ty](https://github.com/astral-sh/ty)** - Type checking
- **[pytest](https://pytest.org/)** - Testing

### Running Checks Locally

```bash

# Linting

uv run ruff check .



# Auto-fix linting issues

uv run ruff check --fix .



# Formatting

uv run ruff format .



# Type checking

uv run ty check duckdb_loader/



# Run tests

uv run pytest tests/ -v



# Run tests with coverage

uv run pytest tests/ --cov=duckdb_loader --cov-report=term-missing

```

### Pre-commit Hooks

Pre-commit hooks run automatically on `git commit`. They check:
- Ruff linting and formatting
- Trailing whitespace
- YAML validity
- Large files
- Merge conflicts
- Private keys

To run hooks manually:
```bash

uv run pre-commit run --all-files

```

## Making Changes

### Branch Naming

Use descriptive branch names:
- `feature/description` - New features
- `fix/description` - Bug fixes
- `docs/description` - Documentation updates
- `refactor/description` - Code refactoring

### Commit Messages

Write clear, concise commit messages:
- Use present tense ("Add feature" not "Added feature")
- First line should be 50 characters or less
- Include context in the body if needed

Example:
```

Add cycle filter preset for recent elections



Adds `recent_cycles(n)` function that returns a CycleFilter

for the last N election cycles, defaulting to 4 (2018-2024).

```

### Pull Requests

1. Create a feature branch from `main`
2. Make your changes
3. Ensure all checks pass locally
4. Push your branch and create a PR
5. Fill out the PR template
6. Request review

PR titles should be descriptive and follow the same guidelines as commit messages.

## Testing

### Running Tests

```bash

# Run all tests

uv run pytest tests/ -v



# Run specific test file

uv run pytest tests/test_filters.py -v



# Run specific test class or function

uv run pytest tests/test_filters.py::TestCycleFilter -v

uv run pytest tests/test_filters.py::TestCycleFilter::test_single_cycle_match -v



# Run with coverage

uv run pytest tests/ --cov=duckdb_loader --cov-report=html

```

### Writing Tests

- Place tests in the `tests/` directory
- Name test files `test_*.py`
- Name test functions `test_*`
- Use fixtures from `conftest.py` for shared data
- Add markers for slow or integration tests:
  ```python

  @pytest.mark.slow

  def test_large_dataset():

      ...



  @pytest.mark.integration

  def test_postgres_connection():

      ...

  ```

### Test Coverage

We aim for high test coverage on core functionality:
- Filters and filter presets
- Schema creation and validation
- Data loading logic (unit tests with mocks)

## Project Structure

```

paper-trail-api/

β”œβ”€β”€ duckdb_loader/           # Main Python package

β”‚   └── duckdb_loader/       # Package source

β”‚       β”œβ”€β”€ cli.py           # CLI commands

β”‚       β”œβ”€β”€ filters.py       # Data filters

β”‚       β”œβ”€β”€ loader.py        # DuckDB loading

β”‚       β”œβ”€β”€ postgres_loader.py # PostgreSQL loading

β”‚       └── schema.py        # Schema definitions

β”œβ”€β”€ scripts/                 # Utility scripts

β”œβ”€β”€ tests/                   # Test suite

β”œβ”€β”€ docs/                    # Documentation

β”œβ”€β”€ database/                # SQL schemas

β”œβ”€β”€ examples/                # Usage examples

└── data/                    # Data storage (gitignored)

```

## Code Style

### Python

- Follow PEP 8 (enforced by Ruff)
- Use type hints for function signatures
- Write docstrings for public functions and classes
- Keep functions focused and small
- Prefer composition over inheritance

### Documentation

- Update README.md for user-facing changes
- Add docstrings for new public APIs
- Update examples if behavior changes

## Getting Help

- Open an issue for bugs or feature requests
- Check existing issues before creating new ones
- Join discussions in pull requests

## License

By contributing, you agree that your contributions will be licensed under the MIT License.