Skip to content

Commit

Permalink
WIP: pytest and GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry871002 committed Jul 17, 2024
1 parent ee8f2eb commit ac00cbf
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Upload Python Package

on:
release:
types: [published]

permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
27 changes: 27 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test with pytest

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

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest pytest-cov
pip install .
- name: Test with pytest
run: pytest --cov=src
36 changes: 36 additions & 0 deletions tests/test_fingerprint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest

from pybitshred.fingerprint import Fingerprint, jaccard_distance

def test_jaccard_distance():
# Test case 1: Empty fingerprints
fp_a = Fingerprint(bytearray(), 0)
fp_b = Fingerprint(bytearray(), 0)
with pytest.raises(ZeroDivisionError):
jaccard_distance(fp_a, fp_b)

# Test case 2: Fingerprint with all bits set to 0
fp_a = Fingerprint(bytearray(b"\x00\x00\x00\x00"), 0)
fp_b = Fingerprint(bytearray(b"\x00\x00\x00\x00"), 0)
with pytest.raises(ZeroDivisionError):
jaccard_distance(fp_a, fp_b)

# Test case 3: Identical fingerprints with all bits set to 0
fp_a = Fingerprint(bytearray(b"\xFF\xFF\xFF\xFF"), 32)
fp_b = Fingerprint(bytearray(b"\xFF\xFF\xFF\xFF"), 32)
assert jaccard_distance(fp_a, fp_b) == pytest.approx(1.0)

# Test case 4: Identical fingerprint with some bits set to 1
fp_a = Fingerprint(bytearray(b"\x0F\x00\xFF\x55"), 16)
fp_b = Fingerprint(bytearray(b"\x0F\x00\xFF\x55"), 16)
assert jaccard_distance(fp_a, fp_b) == pytest.approx(1.0)

# Test case 5: Fingerprint with different bit vectors
fp_a = Fingerprint(bytearray(b"\xAA\x55\xAA\x55"), 16)
fp_b = Fingerprint(bytearray(b"\x55\xAA\x55\xAA"), 16)
assert jaccard_distance(fp_a, fp_b) == pytest.approx(0.0)

# Test case 6: Fingerprint with different bit vectors
fp_a = Fingerprint(bytearray(b"\xAA\xBB\xCC\xDD"), 20)
fp_b = Fingerprint(bytearray(b"\xCC\xDD\xEE\xFF"), 30)
assert jaccard_distance(fp_a, fp_b) == pytest.approx(0.470588)
65 changes: 65 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from pybitshred.utils import bit_count, bit_vector_set, djb2_hash


def test_djb2_hash():
# Test case 1: Empty data
assert djb2_hash(b"") == 5381

# Test case 2: Single byte data
assert djb2_hash(b"A") == 177638

# Test case 3: Multiple byte data
assert djb2_hash(b"Hello, World!") == 2531426958

# Test case 4: Same data should produce same hash
assert djb2_hash(b"Hello, World!") == djb2_hash(b"Hello, World!")

# Test case 5: Different data should produce different hash
assert djb2_hash(b"Hello, World!") != djb2_hash(b"Hello, GitHub Copilot!")


def test_bit_vector_set():
# Test case 1: Setting a bit at the beginning of the vector
vector = bytearray(b"\x00\x00\x00\x00")
bit_vector_set(vector, 0)
assert vector == bytearray(b"\x01\x00\x00\x00")

# Test case 2: Setting a bit in the middle of the vector
vector = bytearray(b"\x00\x00\x00\x00")
bit_vector_set(vector, 12)
assert vector == bytearray(b"\x00\x10\x00\x00")

# Test case 3: Setting a bit at the end of the vector
vector = bytearray(b"\x00\x00\x00\x00")
bit_vector_set(vector, 31)
assert vector == bytearray(b"\x00\x00\x00\x80")

# Test case 4: Setting multiple bits
vector = bytearray(b"\x00\x00\x00\x00")
bit_vector_set(vector, 0)
bit_vector_set(vector, 12)
bit_vector_set(vector, 31)
assert vector == bytearray(b"\x01\x10\x00\x80")

# Test case 5: Setting a bit that is already set
vector = bytearray(b"\x00\x00\x00\x00")
bit_vector_set(vector, 3)
bit_vector_set(vector, 3)
assert vector == bytearray(b"\x08\x00\x00\x00")


def test_bit_count():
# Test case 1: Empty fingerprint
assert bit_count(bytearray()) == 0

# Test case 2: Fingerprint with all bits set to 0
assert bit_count(bytearray(b"\x00\x00\x00\x00")) == 0

# Test case 3: Fingerprint with all bits set to 1
assert bit_count(bytearray(b"\xFF\xFF\xFF\xFF")) == 32

# Test case 4: Fingerprint with some bits set to 1
assert bit_count(bytearray(b"\x0F\x00\xFF\x55")) == 16

# Test case 5: Fingerprint with alternating bits set to 1 and 0
assert bit_count(bytearray(b"\xAA\x55\xAA\x55")) == 16

0 comments on commit ac00cbf

Please sign in to comment.