Skip to content

Commit

Permalink
Merge pull request #1 from DeepLcom/linting-setup
Browse files Browse the repository at this point in the history
Linting setup
  • Loading branch information
Somtom committed Oct 15, 2023
2 parents 4b825d9 + 6fe0497 commit 3fb7944
Show file tree
Hide file tree
Showing 22 changed files with 639 additions and 64 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python package

on:
push:
branches: [ "main" ]
pull_request:

jobs:
build:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.5.1
- name: Install dependencies
run: |
poetry install
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
poetry run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=119 --statistics --ignore E203,E266,E501,W503
- name: Test with pytest
run: |
poetry run pytest tests/
37 changes: 37 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
default_install_hook_types:
- pre-commit
- pre-push
repos:
- repo: https://github.com/dhruvmanila/remove-print-statements
rev: 'v0.4.0' # Replace with latest tag on GitHub
hooks:
- id: remove-print-statements
exclude: "^(.gitlab|.scripts|sisyphus/test_tasks|packages/achtung)"
args: ['--verbose'] # Show all the print statements to be removed
- repo: https://github.com/PyCQA/isort
rev: 5.11.5
hooks:
- id: isort
args: ["--line-width", "119", "--profile", "black"]
additional_dependencies:
- "toml>=0.10"
stages: [commit]
- repo: https://github.com/python/black
rev: 22.3.0
hooks:
- id: black
args: ["--line-length", "119"]
stages: [commit]
- repo: https://github.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
args: ["--ignore", "E203,E266,E501,W503", "--max-line-length", "119", "--max-complexity", "18", "--select", "B,C,E,F,W,T4,B9"]
stages: [commit]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-ast
stages: [ commit ]
- id: check-merge-conflict
stages: [ commit ]
107 changes: 104 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# SQL Mock: Python Library for Mocking SQL Queries with Dictionary Inputs

## Usage

The primary purpose of this library is to simplify the testing of SQL data models and queries by allowing users to mock input data and create tests for various scenarios. It library provides a consistent and convenient way to test the execution of your query without the need to process a massive amount of data.
The primary purpose of this library is to simplify the testing of SQL data models and queries by allowing users to mock input data and create tests for various scenarios. It provides a consistent and convenient way to test the execution of your query without the need to process a massive amount of data.

The library currently supports the following databases. Database specific documentations are provided in the links:
* [BigQuery](src/sql_mock/bigquery/README.md)
* [Clickhouse](src/sql_mock/clickhouse/README.md)

## Usage

### How it works

Before diving into specific database scenarios, let's start with a simplified example of how SQL Mock works behind the scenes.
Expand Down Expand Up @@ -95,3 +95,104 @@ We also recommend using [pytest-icdiff](https://github.com/hjwp/pytest-icdiff) f

### Examples
You can find some examples in the [examples folder](examples/).


## Contributing

We welcome contributions to improve and enhance this open-source project. Whether you want to report issues, suggest new features, or directly contribute to the codebase, your input is valuable. To ensure a smooth and collaborative experience for both contributors and maintainers, please follow these guidelines:

### Reporting Issues

If you encounter a bug, have a feature request, or face any issues with the project, we encourage you to report them using the project's issue tracker. When creating an issue, please include the following information:

- A clear and descriptive title.
- A detailed description of the problem or suggestion.
- Steps to reproduce the issue (if applicable).
- Any error messages or screenshots that help clarify the problem.

### Feature Requests

If you have ideas for new features or improvements, please use the project's issue tracker to submit a feature request. We appreciate well-documented feature requests that explain the motivation and potential use cases.

### Contributing Code

If you're interested in contributing code, follow these steps:

1. **Fork the Repository**: Fork the project's repository to your GitHub account.

2. **Create a Branch**: Create a new branch for your contribution, preferably with a name that describes the feature or fix you're working on.

3. **Code and Test**: Write your code, and make sure to test it thoroughly to ensure it functions as expected.

4. **Documentation**: If your contribution involves code changes, update the relevant documentation to reflect those changes.

5. **Submit a Pull Request**: Submit a pull request to the project's repository. Be sure to provide a clear and concise description of your changes. Include a reference to any related issues.

6. **Code Review**: Your pull request will undergo code review by maintainers and contributors. Be prepared to address any feedback and make necessary changes.

7. **Merge**: Once your contribution is approved and passes all checks, it will be merged into the project.

### Coding Standards

When contributing code, adhere to the following coding standards:

- Follow the project's coding style, including code formatting and naming conventions.
- Ensure your code is well-documented and includes comments where necessary.
- Write clear commit messages that describe the purpose of each commit.

## Local Setup

To set up your local development environment for this project, follow these steps:

### 1. Clone the Repository

```bash
git clone https://github.com/your-project/repository.git
cd repository
```

### 2. Install Dependencies

We use [Poetry](https://python-poetry.org/) for dependency management. If you don't have Poetry installed, you can get it from [here](https://python-poetry.org/docs/#installation).

Once you have Poetry, you can install the project's dependencies:

```bash
poetry install
```

### 3. Pre-Commit Hooks

This project uses pre-commit hooks to ensure code quality. To install the hooks, run:

```bash
poetry run pre-commit install
```

This will set up the necessary hooks to check code formatting, linting, and other code quality checks before each commit.

### 4. Running Tests

We use [pytest](https://docs.pytest.org/en/latest/) for running tests. You can run all the tests with:

```bash
poetry run pytest tests/
```

### 5. Environment Variables

If you're working with database-specific sections (e.g., BigQuery or ClickHouse), make sure to set the required environment variables for your chosen database. Refer to the respective "Usage" sections for details on these variables.

### 6. Development Workflow

Before you start contributing, create a new branch for your work:

```bash
git checkout -b your-feature-branch
```

Make your code changes, commit them, and create a pull request to the project's repository following the [Contributing Guidelines](#Contributing).

### 7. Code Formatting and Linting

As part of the pre-commit hooks, code formatting and linting will be automatically checked before each commit. Be sure to address any issues reported by the hooks.
3 changes: 2 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import pytest
pytest.register_assert_rewrite('sql_mock')

pytest.register_assert_rewrite("sql_mock")
9 changes: 8 additions & 1 deletion examples/bigquery/test_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime

from sql_mock.bigquery import column_mocks as col
from sql_mock.bigquery.table_mocks import BigQueryMockTable

Expand All @@ -11,20 +12,24 @@
GROUP BY user_id
"""


class UserTable(BigQueryMockTable):
user_id = col.Int(default=1)
user_name = col.String(default="Mr. T")


class SubscriptionTable(BigQueryMockTable):
subscription_id = col.Int(default=1)
period_start_date = col.Date(default=datetime.date(2023, 9, 5))
period_end_date = col.Date(default=datetime.date(2023, 9, 5))
user_id = col.Int(default=1)


class SubscriptionCountTable(BigQueryMockTable):
subscription_count = col.Int(default=1)
user_id = col.Int(default=1)


def test_something():
users = UserTable(data=[{"user_id": 1}, {"user_id": 2}])
subscriptions = SubscriptionTable(
Expand All @@ -37,6 +42,8 @@ def test_something():

expected = [{"user_id": 1, "subscription_count": 2}, {"user_id": 2, "subscription_count": 1}]

res = SubscriptionCountTable.from_inputs(query=query, input_data={"data.users": users, "data.subscriptions": subscriptions})
res = SubscriptionCountTable.from_inputs(
query=query, input_data={"data.users": users, "data.subscriptions": subscriptions}
)

res.assert_equal(expected)
11 changes: 8 additions & 3 deletions examples/clickhouse/test_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime

from sql_mock.clickhouse import column_mocks as col
from sql_mock.clickhouse.table_mocks import ClickHouseTableMock

Expand All @@ -23,10 +24,12 @@ class SubscriptionTable(ClickHouseTableMock):
period_end_date = col.Date(default=datetime.date(2023, 9, 5))
user_id = col.Int(default=1)


class SubscriptionCountTable(ClickHouseTableMock):
subscription_count = col.Int(default=1)
user_id = col.Int(default=1)


def test_something():
users = UserTable(data=[{"user_id": 1}, {"user_id": 2}])
subscriptions = SubscriptionTable(
Expand All @@ -38,7 +41,9 @@ def test_something():
)

expected = [{"user_id": 2, "subscription_count": 1}, {"user_id": 1, "subscription_count": 2}]

res = SubscriptionCountTable.from_inputs(query=query, input_data={"data.users": users, "data.subscriptions": subscriptions})


res = SubscriptionCountTable.from_inputs(
query=query, input_data={"data.users": users, "data.subscriptions": subscriptions}
)

res.assert_equal(expected)
Loading

0 comments on commit 3fb7944

Please sign in to comment.