Skip to content

Commit

Permalink
add unit tests in addition to e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FredHappyface committed Mar 30, 2024
1 parent 9a3177d commit da39a98
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tests/test_layeredimage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import pytest
from PIL import Image

from layeredimage.layeredimage import LayeredImage
from layeredimage.layergroup import Group, Layer


@pytest.fixture()
def example_layer():
# Provide a simple example layer for testing
return Layer(name="Example Layer", image=Image.new("RGBA", (100, 100)))

@pytest.fixture()
def example_group(example_layer):
# Provide a simple example group containing the example layer for testing
return Group(name="Example Group",layers=[example_layer])

@pytest.fixture()
def example_image(example_group):
# Provide a simple example LayeredImage containing the example group for testing
return LayeredImage(layersAndGroups=[example_group])

def test_layered_image_init() -> None:
# Test initialization of LayeredImage
img = LayeredImage([])
assert isinstance(img, LayeredImage)

def test_add_layer_or_group(example_image, example_layer) -> None:
# Test adding a layer to LayeredImage
initial_count = len(example_image.layersAndGroups)
example_image.addLayerOrGroup(example_layer)
assert len(example_image.layersAndGroups) == initial_count + 1

def test_insert_layer_or_group(example_image, example_layer) -> None:
# Test inserting a layer into LayeredImage
example_image.insertLayerOrGroup(example_layer, 0)
assert example_image.layersAndGroups[0] == example_layer

def test_remove_layer_or_group(example_image, example_layer) -> None:
# Test removing a layer from LayeredImage
example_image.addLayerOrGroup(example_layer)
initial_count = len(example_image.layersAndGroups)
example_image.removeLayerOrGroup(0)
assert len(example_image.layersAndGroups) == initial_count - 1

def test_get_layer_or_group(example_image, example_group) -> None:
# Test getting a layer or group from LayeredImage
assert example_image.getLayerOrGroup(0) == example_group

def test_extract_layers(example_image, example_layer) -> None:
# Test extracting layers from LayeredImage
example_image.addLayerOrGroup(example_layer)
assert len(example_image.extractLayers()) == 2

def test_update_layers(example_image, example_layer) -> None:
# Test updating layers in LayeredImage
example_image.addLayerOrGroup(example_layer)
example_image.updateLayers()
assert len(example_image.layers) == 2

def test_extract_groups(example_image, example_group) -> None:
# Test extracting groups from LayeredImage
assert len(example_image.extractGroups()) == 1

def test_update_groups(example_image, example_group) -> None:
# Test updating groups in LayeredImage
example_image.addLayerOrGroup(example_group)
example_image.updateGroups()
assert len(example_image.groups) == 2

def test_get_flatten_layers(example_image) -> None:
# Test flattening layers in LayeredImage
img = example_image.getFlattenLayers()
assert isinstance(img, Image.Image)

def test_layered_image_repr(example_image) -> None:
# Test string representation of LayeredImage
assert repr(example_image) == str(example_image)

def test_layered_image_str(example_image) -> None:
# Test string representation of LayeredImage
assert str(example_image) == "<LayeredImage (100x100) with 1 children>"

def test_layered_image_json(example_image) -> None:
# Test getting LayeredImage as a dictionary
assert isinstance(example_image.json(), dict)
51 changes: 51 additions & 0 deletions tests/test_layergroup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest
from PIL import Image

from layeredimage.layergroup import Group, Layer


@pytest.fixture()
def example_image():
# Provide a simple example PIL image for testing
return Image.new("RGBA", (100, 100))

@pytest.fixture()
def example_layer(example_image):
# Provide a simple example layer for testing
return Layer(name="Example Layer", image=example_image)

@pytest.fixture()
def example_group(example_layer):
# Provide a simple example group containing the example layer for testing
return Group(name="Example Group", layers=[example_layer])

def test_layer_group_init(example_image) -> None:
# Test initialization of LayerGroup
layer_group = Layer(name="Test Layer", dimensions=(100, 100), image=example_image)
assert isinstance(layer_group, Layer)

def test_layer_init(example_image) -> None:
# Test initialization of Layer
layer = Layer(name="Test Layer", image=example_image)
assert isinstance(layer, Layer)

def test_group_init(example_layer) -> None:
# Test initialization of Group
group = Group(name="Test Group", layers=[example_layer])
assert isinstance(group, Group)

def test_layer_group_repr(example_layer) -> None:
# Test string representation of LayerGroup
assert repr(example_layer) == str(example_layer)

def test_layer_group_str(example_layer) -> None:
# Test string representation of LayerGroup
assert str(example_layer) == f'<LayeredImage "{example_layer.name}" ({example_layer.dimensions[0]}x{example_layer.dimensions[1]})>'

def test_layer_json(example_layer) -> None:
# Test getting Layer as a dictionary
assert isinstance(example_layer.json(), dict)

def test_group_json(example_group) -> None:
# Test getting Group as a dictionary
assert isinstance(example_group.json(), dict)

0 comments on commit da39a98

Please sign in to comment.