Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:
python3 -c "from c2pa import C2paError; print('C2paError imported successfully')"

- name: Run tests
run: python3 ./tests/test_unit_tests.py
run: python3 -m unittest discover -s tests -p "test_*.py" -v

tests-windows:
name: Unit tests for developer setup (Windows)
Expand Down Expand Up @@ -182,7 +182,7 @@ jobs:
python -c "from c2pa import C2paError; print('C2paError imported successfully')"

- name: Run tests
run: python .\tests\test_unit_tests.py
run: python -m unittest discover -s tests -p "test_*.py" -v

build-linux-wheel:
name: Build Linux wheel
Expand Down Expand Up @@ -254,10 +254,10 @@ jobs:
source venv/bin/activate
pip install dist/c2pa_python-*.whl

- name: Run unittest tests on installed wheel
- name: Run tests with unittest (venv)
run: |
source venv/bin/activate
python ./tests/test_unit_tests.py
python -m unittest discover -s tests -p "test_*.py" -v

- name: Install pytest (in venv)
run: |
Expand All @@ -267,7 +267,7 @@ jobs:
- name: Run tests with pytest (venv)
run: |
source venv/bin/activate
venv/bin/pytest tests/test_unit_tests.py -v
venv/bin/pytest tests/ -v

build-windows-wheel:
name: Build Windows wheel
Expand Down Expand Up @@ -333,10 +333,10 @@ jobs:
if (-not $wheel) { Write-Error "No wheel file found in dist directory"; exit 1 }
pip install $wheel.FullName

- name: Run unittest tests on installed wheel
- name: Run tests with unittest (venv)
run: |
.\venv\Scripts\activate
python .\tests\test_unit_tests.py
python -m unittest discover -s tests -p "test_*.py" -v

- name: Install pytest (in venv)
run: |
Expand All @@ -346,7 +346,7 @@ jobs:
- name: Run tests with pytest (venv)
run: |
.\venv\Scripts\activate
.\venv\Scripts\pytest .\tests\test_unit_tests.py -v
.\venv\Scripts\pytest tests\ -v

build-macos-wheel:
name: Build macOS wheels
Expand Down Expand Up @@ -422,10 +422,10 @@ jobs:
source venv/bin/activate
pip install dist/c2pa_python-*.whl

- name: Run unittest tests on installed wheel
- name: Run tests with unittest (venv)
run: |
source venv/bin/activate
python ./tests/test_unit_tests.py
python -m unittest discover -s tests -p "test_*.py" -v

- name: Install pytest (in venv)
run: |
Expand All @@ -435,7 +435,7 @@ jobs:
- name: Run tests with pytest (venv)
run: |
source venv/bin/activate
venv/bin/pytest tests/test_unit_tests.py -v
venv/bin/pytest tests/ -v

sdist:
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ C2PA_VERSION := $(shell cat c2pa-native-version.txt)

# Start from clean env: Delete `.venv`, then `python3 -m venv .venv`
# Pre-requisite: Python virtual environment is active (source .venv/bin/activate)
# Run Pytest tests in virtualenv: .venv/bin/pytest tests/test_unit_tests.py -v
# Run tests in virtualenv: .venv/bin/python -m unittest discover -s tests -p "test_*.py" -v

# Removes build artifacts, distribution files, and other generated content
clean:
Expand Down Expand Up @@ -39,8 +39,8 @@ run-examples:
# Runs the examples, then the unit tests
test:
make run-examples
python3 ./tests/test_unit_tests.py
python3 ./tests/test_unit_tests_threaded.py
python3 -m unittest discover -s tests -p "test_*.py"
python3 -m unittest tests.threaded_test

# Runs benchmarks in the venv
benchmark:
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sys
import os

sys.path.insert(0, os.path.dirname(__file__))
141 changes: 141 additions & 0 deletions tests/test_builder_with_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Copyright 2023 Adobe. All rights reserved.
# This file is licensed to you under the Apache License,
# Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
# or the MIT license (http://opensource.org/licenses/MIT),
# at your option.

# Unless required by applicable law or agreed to in writing,
# this software is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
# implied. See the LICENSE-MIT and LICENSE-APACHE files for the
# specific language governing permissions and limitations under
# each license.

import os
import unittest
import json
import tempfile

from c2pa import Builder, C2paError as Error, Reader
from c2pa import Settings, Context

from test_common import DEFAULT_TEST_FILE
from test_context_base import TestContextAPIs

class TestBuilderWithContext(TestContextAPIs):

def test_contextual_builder_with_default_context(self):
context = Context()
builder = Builder(self.test_manifest, context)
self.assertIsNotNone(builder)
builder.close()
context.close()

def test_contextual_builder_with_settings_context(self):
settings = Settings.from_dict({
"builder": {
"thumbnail": {"enabled": False}
}
})
context = Context(settings)
builder = Builder(self.test_manifest, context)
signer = self._ctx_make_signer()
with tempfile.TemporaryDirectory() as temp_dir:
dest_path = os.path.join(temp_dir, "out.jpg")
with (
open(DEFAULT_TEST_FILE, "rb") as source_file,
open(dest_path, "w+b") as dest_file,
):
builder.sign(
signer, "image/jpeg", source_file, dest_file,
)
reader = Reader(dest_path)
manifest = reader.get_active_manifest()
self.assertIsNone(
manifest.get("thumbnail")
)
reader.close()
builder.close()
context.close()
settings.close()

def test_contextual_builder_from_json_with_context(self):
context = Context()
builder = Builder.from_json(self.test_manifest, context)
self.assertIsNotNone(builder)
builder.close()
context.close()

def test_contextual_builder_sign_context_signer(self):
signer = self._ctx_make_signer()
context = Context(signer=signer)
builder = Builder(
self.test_manifest, context=context,
)
with tempfile.TemporaryDirectory() as temp_dir:
dest_path = os.path.join(temp_dir, "out.jpg")
with (
open(DEFAULT_TEST_FILE, "rb") as source_file,
open(dest_path, "w+b") as dest_file,
):
manifest_bytes = builder.sign_with_context(
"image/jpeg",
source_file,
dest_file,
)
self.assertIsNotNone(manifest_bytes)
self.assertGreater(len(manifest_bytes), 0)
reader = Reader(dest_path)
data = reader.json()
self.assertIsNotNone(data)
reader.close()
builder.close()
context.close()

def test_contextual_builder_sign_signer_ovverride(self):
context_signer = self._ctx_make_signer()
context = Context(signer=context_signer)
builder = Builder(
self.test_manifest, context=context,
)
explicit_signer = self._ctx_make_signer()
with tempfile.TemporaryDirectory() as temp_dir:
dest_path = os.path.join(temp_dir, "out.jpg")
with (
open(DEFAULT_TEST_FILE, "rb") as source_file,
open(dest_path, "w+b") as dest_file,
):
manifest_bytes = builder.sign(
explicit_signer,
"image/jpeg", source_file, dest_file,
)
self.assertIsNotNone(manifest_bytes)
self.assertGreater(len(manifest_bytes), 0)
builder.close()
explicit_signer.close()
context.close()

def test_contextual_builder_sign_no_signer_raises(self):
context = Context()
builder = Builder(
self.test_manifest, context=context,
)
with tempfile.TemporaryDirectory() as temp_dir:
dest_path = os.path.join(temp_dir, "out.jpg")
with (
open(DEFAULT_TEST_FILE, "rb") as source_file,
open(dest_path, "w+b") as dest_file,
):
with self.assertRaises(Error):
builder.sign_with_context(
"image/jpeg",
source_file,
dest_file,
)
builder.close()
context.close()



if __name__ == '__main__':
unittest.main(warnings='ignore')
Loading