Metadata-Version: 2.4
Name: inlaw
Version: 0.1.0
Summary: A lightweight wrapper around Great Expectations for database validation testing
Author-email: Fred Trotter <ftrotter@gmail.com>
License: Creative Commons Legal Code
        
        CC0 1.0 Universal
        
            CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
            LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
            ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
            INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
            REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
            PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
            THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
            HEREUNDER.
        
        Statement of Purpose
        
        The laws of most jurisdictions throughout the world automatically confer
        exclusive Copyright and Related Rights (defined below) upon the creator
        and subsequent owner(s) (each and all, an "owner") of an original work of
        authorship and/or a database (each, a "Work").
        
        Certain owners wish to permanently relinquish those rights to a Work for
        the purpose of contributing to a commons of creative, cultural and
        scientific works ("Commons") that the public can reliably and without fear
        of later claims of infringement build upon, modify, incorporate in other
        works, reuse and redistribute as freely as possible in any form whatsoever
        and for any purposes, including without limitation commercial purposes.
        These owners may contribute to the Commons to promote the ideal of a free
        culture and the further production of creative, cultural and scientific
        works, or to gain reputation or greater distribution for their Work in
        part through the use and efforts of others.
        
        For these and/or other purposes and motivations, and without any
        expectation of additional consideration or compensation, the person
        associating CC0 with a Work (the "Affirmer"), to the extent that he or she
        is an owner of Copyright and Related Rights in the Work, voluntarily
        elects to apply CC0 to the Work and publicly distribute the Work under its
        terms, with knowledge of his or her Copyright and Related Rights in the
        Work and the meaning and intended legal effect of CC0 on those rights.
        
        1. Copyright and Related Rights. A Work made available under CC0 may be
        protected by copyright and related or neighboring rights ("Copyright and
        Related Rights"). Copyright and Related Rights include, but are not
        limited to, the following:
        
          i. the right to reproduce, adapt, distribute, perform, display,
             communicate, and translate a Work;
         ii. moral rights retained by the original author(s) and/or performer(s);
        iii. publicity and privacy rights pertaining to a person's image or
             likeness depicted in a Work;
         iv. rights protecting against unfair competition in regards to a Work,
             subject to the limitations in paragraph 4(a), below;
          v. rights protecting the extraction, dissemination, use and reuse of data
             in a Work;
         vi. database rights (such as those arising under Directive 96/9/EC of the
             European Parliament and of the Council of 11 March 1996 on the legal
             protection of databases, and under any national implementation
             thereof, including any amended or successor version of such
             directive); and
        vii. other similar, equivalent or corresponding rights throughout the
             world based on applicable law or treaty, and any national
             implementations thereof.
        
        2. Waiver. To the greatest extent permitted by, but not in contravention
        of, applicable law, Affirmer hereby overtly, fully, permanently,
        irrevocably and unconditionally waives, abandons, and surrenders all of
        Affirmer's Copyright and Related Rights and associated claims and causes
        of action, whether now known or unknown (including existing as well as
        future claims and causes of action), in the Work (i) in all territories
        worldwide, (ii) for the maximum duration provided by applicable law or
        treaty (including future time extensions), (iii) in any current or future
        medium and for any number of copies, and (iv) for any purpose whatsoever,
        including without limitation commercial, advertising or promotional
        purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
        member of the public at large and to the detriment of Affirmer's heirs and
        successors, fully intending that such Waiver shall not be subject to
        revocation, rescission, cancellation, termination, or any other legal or
        equitable action to disrupt the quiet enjoyment of the Work by the public
        as contemplated by Affirmer's express Statement of Purpose.
        
        3. Public License Fallback. Should any part of the Waiver for any reason
        be judged legally invalid or ineffective under applicable law, then the
        Waiver shall be preserved to the maximum extent permitted taking into
        account Affirmer's express Statement of Purpose. In addition, to the
        extent the Waiver is so judged Affirmer hereby grants to each affected
        person a royalty-free, non transferable, non sublicensable, non exclusive,
        irrevocable and unconditional license to exercise Affirmer's Copyright and
        Related Rights in the Work (i) in all territories worldwide, (ii) for the
        maximum duration provided by applicable law or treaty (including future
        time extensions), (iii) in any current or future medium and for any number
        of copies, and (iv) for any purpose whatsoever, including without
        limitation commercial, advertising or promotional purposes (the
        "License"). The License shall be deemed effective as of the date CC0 was
        applied by Affirmer to the Work. Should any part of the License for any
        reason be judged legally invalid or ineffective under applicable law, such
        partial invalidity or ineffectiveness shall not invalidate the remainder
        of the License, and in such case Affirmer hereby affirms that he or she
        will not (i) exercise any of his or her remaining Copyright and Related
        Rights in the Work or (ii) assert any associated claims and causes of
        action with respect to the Work, in either case contrary to Affirmer's
        express Statement of Purpose.
        
        4. Limitations and Disclaimers.
        
         a. No trademark or patent rights held by Affirmer are waived, abandoned,
            surrendered, licensed or otherwise affected by this document.
         b. Affirmer offers the Work as-is and makes no representations or
            warranties of any kind concerning the Work, express, implied,
            statutory or otherwise, including without limitation warranties of
            title, merchantability, fitness for a particular purpose, non
            infringement, or the absence of latent or other defects, accuracy, or
            the present or absence of errors, whether or not discoverable, all to
            the greatest extent permissible under applicable law.
         c. Affirmer disclaims responsibility for clearing rights of other persons
            that may apply to the Work or any use thereof, including without
            limitation any person's Copyright and Related Rights in the Work.
            Further, Affirmer disclaims responsibility for obtaining any necessary
            consents, permissions or other rights required for any use of the
            Work.
         d. Affirmer understands and acknowledges that Creative Commons is not a
            party to this document and has no duty or obligation with respect to
            this CC0 or use of the Work.
Project-URL: Homepage, https://github.com/ftrotter/inlaw
Project-URL: Repository, https://github.com/ftrotter/inlaw
Project-URL: Issues, https://github.com/ftrotter/inlaw/issues
Keywords: testing,database,validation,great-expectations,data-quality
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Testing
Classifier: Topic :: Database
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: great-expectations>=0.18.0
Requires-Dist: sqlalchemy>=2.0.0
Requires-Dist: pandas>=2.0.0
Requires-Dist: python-dotenv>=1.0.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Provides-Extra: duckdb
Requires-Dist: duckdb-engine>=0.9.0; extra == "duckdb"
Provides-Extra: postgres
Requires-Dist: psycopg2-binary>=2.9.0; extra == "postgres"
Provides-Extra: mysql
Requires-Dist: pymysql>=1.0.0; extra == "mysql"
Dynamic: license-file

# InLaw

A lightweight wrapper around Great Expectations for database validation testing with minimal boilerplate.

InLaw allows you to write simple database validation tests as Python classes and run them from the command line or programmatically.

## Features

- 🚀 **Simple CLI tool** - Run tests with a single command
- 🔍 **Auto-discovery** - Automatically finds and loads test files from directories
- 🔌 **Environment-based configuration** - Database connections via .env files
- 📊 **Great Expectations integration** - Leverage the power of GX without the complexity
- 🗃️ **DBTable support** - Flexible database table identification across SQL dialects

## Installation

```bash
# Basic installation
pip install inlaw

# With DuckDB support
pip install inlaw[duckdb]

# With PostgreSQL support
pip install inlaw[postgres]

# With MySQL support
pip install inlaw[mysql]

# With all database drivers
pip install inlaw[duckdb,postgres,mysql]
```

For local development:

```bash
git clone https://github.com/ftrotter/inlaw.git
cd inlaw
pip install -e .
```

### Python Version Compatibility

**Important**: While this package supports Python 3.10+, Great Expectations (a core dependency) currently has runtime compatibility issues with Python 3.14 due to Pydantic v1 limitations. For best results, use Python 3.10, 3.11, 3.12, or 3.13.

If you encounter Pydantic-related errors on Python 3.14, please downgrade to Python 3.13 or earlier.

## Quick Start

### 1. Create a test file

Create a file with your InLaw test classes (e.g., `my_tests.py`):

```python
from inlaw import InLaw, DBTable

class TestUsersTableExists(InLaw):
    title = "Verify users table exists and has records"
    
    @staticmethod
    def run(engine, config=None):
        # Create a DBTable reference
        users_table = DBTable(database='mydb', schema='public', table='users')
        
        # Query the table
        sql = f"SELECT COUNT(*) as count FROM {users_table}"
        gx_df = InLaw.sql_to_gx_df(sql=sql, engine=engine)
        
        # Use Great Expectations validations
        result = gx_df.expect_column_values_to_be_between(
            column='count',
            min_value=1
        )
        
        if result.success:
            return True
        else:
            return "Users table is empty"
```

### 2. Configure database connection

Create a `.env` file in your project directory:

```bash
# Complete connection URL (option 1)
INLAW_URL=postgresql://user:password@localhost:5432/mydb

# OR individual components (option 2)
INLAW_DIALECT=postgresql
INLAW_DRIVER=psycopg2
INLAW_USER=myuser
INLAW_PASSWORD=mypassword
INLAW_HOST=localhost
INLAW_PORT=5432
INLAW_DATABASE=mydb
```

### 3. Run tests

```bash
# Run tests in current directory
inlaw

# Run tests in specific directory
inlaw /path/to/tests

# Use custom .env file
inlaw --db-config-file /path/to/custom.env

# Run tests in specific directory with custom config
inlaw /path/to/tests --db-config-file /path/to/.env
```

## Environment Variables

InLaw uses environment variables with the `INLAW_` prefix for database configuration:

| Variable | Required | Description | Example |
|----------|----------|-------------|---------|
| `INLAW_URL` | * | Complete SQLAlchemy connection URL | `postgresql://user:pass@host:5432/db` |
| `INLAW_DIALECT` | ** | Database dialect | `postgresql`, `mysql`, `sqlite`, `duckdb` |
| `INLAW_DRIVER` | No | Database driver | `psycopg2`, `pymysql` |
| `INLAW_USER` | ** | Database username | `myuser` |
| `INLAW_PASSWORD` | No | Database password | `secretpass` |
| `INLAW_HOST` | ** | Database host | `localhost`, `db.example.com` |
| `INLAW_PORT` | No | Database port | `5432`, `3306` |
| `INLAW_DATABASE` | ** | Database name/path | `mydb`, `/path/to/db.duckdb` |

\* Either `INLAW_URL` OR the individual components (\*\*) are required.

### Database-Specific Configuration

#### DuckDB

DuckDB can be used with either an in-memory database or a file-based database:

```bash
# File-based DuckDB
INLAW_DIALECT=duckdb
INLAW_DATABASE=/path/to/mydata.duckdb

# Or using URL format
INLAW_URL=duckdb:///path/to/mydata.duckdb

# In-memory DuckDB (for testing)
INLAW_URL=duckdb:///:memory:
```

#### PostgreSQL

```bash
# Using URL
INLAW_URL=postgresql://user:password@localhost:5432/mydb

# Or individual components
INLAW_DIALECT=postgresql
INLAW_DRIVER=psycopg2
INLAW_USER=myuser
INLAW_PASSWORD=mypassword
INLAW_HOST=localhost
INLAW_PORT=5432
INLAW_DATABASE=mydb
```

#### MySQL

```bash
# Using URL
INLAW_URL=mysql+pymysql://user:password@localhost:3306/mydb

# Or individual components
INLAW_DIALECT=mysql
INLAW_DRIVER=pymysql
INLAW_USER=myuser
INLAW_PASSWORD=mypassword
INLAW_HOST=localhost
INLAW_PORT=3306
INLAW_DATABASE=mydb
```

#### SQLite

```bash
# Using URL
INLAW_URL=sqlite:////absolute/path/to/database.db

# Or individual components
INLAW_DIALECT=sqlite
INLAW_DATABASE=/absolute/path/to/database.db
```

## .env File Discovery

InLaw automatically searches for `.env` files in the following order:

1. Custom file specified with `--db-config-file`
2. `.env` in the test directory
3. `.env` in the parent directory of the test directory

## Writing InLaw Tests

### Basic Test Structure

Every InLaw test is a class that:

1. Inherits from `InLaw`
2. Has a `title` class attribute
3. Implements a `run(engine, config)` static method

```python
from inlaw import InLaw

class MyTest(InLaw):
    title = "My Test Description"
    
    @staticmethod
    def run(engine, config=None):
        # Your test logic here
        # Return True for pass, or a string error message for fail
        return True
```

### Using DBTable

The `DBTable` class provides flexible table identification across different SQL dialects:

```python
from inlaw import DBTable

# PostgreSQL style (database + schema + table)
table = DBTable(database='mydb', schema='public', table='users')

# MySQL style (database + table)
table = DBTable(database='mydb', table='users')

# Databricks style (catalog + database + table)
table = DBTable(catalog='main', database='mydb', table='users')

# Use in SQL queries
sql = f"SELECT * FROM {table}"  # Becomes: "mydb.public.users"

# Create related tables
staging_table = table.make_child('staging')  # Creates "users_staging"
```

### Using Great Expectations

InLaw provides the `sql_to_gx_df()` helper to convert SQL queries into GX validators:

```python
@staticmethod
def run(engine, config=None):
    sql = "SELECT age, email FROM users"
    gx_df = InLaw.sql_to_gx_df(sql=sql, engine=engine)
    
    # Use any Great Expectations expectation
    result = gx_df.expect_column_values_to_not_be_null(column='email')
    
    if result.success:
        return True
    else:
        return f"Found {result.result['unexpected_count']} null emails"
```

### Return Values

Your `run()` method should return:

- `True` - Test passed
- `str` - Test failed with this error message

```python
@staticmethod
def run(engine, config=None):
    # ... test logic ...
    
    if everything_ok:
        return True
    else:
        return "Specific error message explaining what failed"
```

## Programmatic Usage

You can also use InLaw programmatically in your Python code:

```python
from sqlalchemy import create_engine
from inlaw import InLaw

# Create engine
engine = create_engine('postgresql://user:pass@localhost/mydb')

# Run all tests in a directory
results = InLaw.run_all(
    engine=engine,
    inlaw_dir='/path/to/tests',
    config={'threshold': 100}  # Optional config passed to tests
)

print(f"Passed: {results['passed']}")
print(f"Failed: {results['failed']}")
print(f"Errors: {results['errors']}")
```

## CLI Options

```
usage: inlaw [-h] [--db-config-file DB_CONFIG_FILE] [directory]

InLaw - Run database validation tests

positional arguments:
  directory             Directory containing InLaw test files (default: current directory)

optional arguments:
  -h, --help            show this help message and exit
  --db-config-file DB_CONFIG_FILE
                        Path to .env file with database configuration

Examples:
  inlaw                              # Run tests in current directory
  inlaw /path/to/tests              # Run tests in specific directory
  inlaw --db-config-file custom.env  # Use custom .env file
  inlaw /path/to/tests --db-config-file /path/to/.env
```

## Exit Codes

The CLI returns the following exit codes:

- `0` - All tests passed
- `1` - One or more tests failed or an error occurred

## License

See [LICENSE](LICENSE) file for details.

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## Support

For issues, questions, or contributions, please visit the [GitHub repository](https://github.com/ftrotter/inlaw).
