Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josemanuelcarretero committed Sep 15, 2024
1 parent 8716bb0 commit 22f2fc5
Show file tree
Hide file tree
Showing 24 changed files with 208 additions and 208 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.6'
python-version: '3.8'

- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def read_requirements(file):

setup(
name="simple_dependency_injector",
version="0.1.0",
version="0.2.0",
description="A simple dependency injection framework for Python",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
Expand Down
25 changes: 25 additions & 0 deletions tests/config/another_services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"services": {
"another_service": {
"class": "tests/services/my_module.py#AnotherService",
"arguments": [
"@core_service",
"!tagged service",
"!context"
]
},
"core_service": {
"class": "tests/services/my_module.py#CoreService",
"tags": [
"service"
]
},
"base_service": {
"class": "tests/services/my_module.py#BaseService",
"tags": [
"service"
]
}
},
"imports": []
}
14 changes: 14 additions & 0 deletions tests/config/another_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services = {
"another_service": {
"class": "tests/services/my_module.py#AnotherService",
"arguments": [
"@core_service",
"!tagged service",
"!context",
],
},
"core_service": {"class": "tests/services/my_module.py#CoreService", "tags": ["service"]},
"base_service": {"class": "tests/services/my_module.py#BaseService", "tags": ["service"]},
}

imports = []
13 changes: 12 additions & 1 deletion tests/config/another_services.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
services:
another_service:
class: 'tests/services/my_module.py#AnotherService'
scope: singleton
arguments:
- '@core_service'
- '!tagged service'
- '!context'
core_service:
class: 'tests/services/my_module.py#CoreService'
tags:
- 'service'
base_service:
class: 'tests/services/my_module.py#BaseService'
tags:
- 'service'
8 changes: 0 additions & 8 deletions tests/config/another_services_python.py

This file was deleted.

7 changes: 0 additions & 7 deletions tests/config/invalid_class_python.py

This file was deleted.

10 changes: 0 additions & 10 deletions tests/config/invalid_factory_method_python.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/config/invalid_scope_python.py

This file was deleted.

5 changes: 0 additions & 5 deletions tests/config/missing_class_parameter_python.py

This file was deleted.

3 changes: 0 additions & 3 deletions tests/config/missing_instance_parameter_python.py

This file was deleted.

26 changes: 26 additions & 0 deletions tests/config/services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"services": {
"my_singleton_service": {
"class": "tests/services/my_module.py#MyService",
"scope": "singleton"
},
"my_service": {
"class": "tests/services/my_module.py#MyService"
},
"my_factory_service": {
"factory": {
"class": "tests/services/my_module.py#MyFactory",
"method": "create_service"
},
"scope": "request"
},
"my_instance_service": {
"instance": "tests/services/my_module.py#MyInstance"
}
},
"imports": [
{
"resource": "another_services.py"
}
]
}
7 changes: 5 additions & 2 deletions tests/config/services_python.py → tests/config/services.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
services = {
"my_service": {
"my_singleton_service": {
"class": "tests/services/my_module.py#MyService",
"scope": "singleton",
},
"my_service": {
"class": "tests/services/my_module.py#MyService",
},
"my_factory_service": {
"factory": {
"class": "tests/services/my_module.py#MyFactory",
Expand All @@ -15,4 +18,4 @@
},
}

imports = [{"resource": "another_services_python.py"}]
imports = [{"resource": "another_services.py"}]
5 changes: 4 additions & 1 deletion tests/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
services:
my_service:
my_singleton_service:
class: 'tests/services/my_module.py#MyService'
scope: singleton

my_service:
class: 'tests/services/my_module.py#MyService'

my_factory_service:
factory:
class: 'tests/services/my_module.py#MyFactory'
Expand Down
18 changes: 17 additions & 1 deletion tests/services/my_module.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Any, List


class MyService:
def __init__(self):
self.name = "Service"
Expand All @@ -17,6 +20,19 @@ def __init__(self):
MyInstance = MyInstanceGenerator()


class AnotherService:
class CoreService:
def __init__(self):
self.name = "Core Service"


class BaseService:
def __init__(self):
self.name = "Base Service"


class AnotherService:
def __init__(self, core: CoreService, services: List[Any], context: Any):
self.name = "Another Service"
self.core = core
self.services = services
self.context = context
25 changes: 0 additions & 25 deletions tests/test_class_services.py

This file was deleted.

15 changes: 10 additions & 5 deletions tests/test_contexts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
import os.path
import unittest

from simple_dependency_injector import DependencyInjector

Expand All @@ -9,12 +9,17 @@ def setUp(self):
self.injector = DependencyInjector(base_path=os.path.dirname(os.path.dirname(__file__)))

def test_create_context(self):
self.injector.load("tests/config/services_python.py")
self.injector.load("tests/config/services.yaml")
self.injector.compile()
context = self.injector.create_context()
context1 = self.injector.create_context()
context2 = self.injector.create_context()

service1 = context1.get("my_service")
service2 = context2.get("my_service")

service = context.get("my_service")
self.assertEqual(service.name, "Service")
self.assertEqual(service1.name, "Service")
self.assertEqual(service2.name, "Service")
self.assertNotEqual(service1, service2)


if __name__ == "__main__":
Expand Down
51 changes: 2 additions & 49 deletions tests/test_failures.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import unittest
import os.path
import unittest

from simple_dependency_injector import DependencyInjector


class TestFailureCases(unittest.TestCase):
class TestFailures(unittest.TestCase):
def setUp(self):
self.injector = DependencyInjector(base_path=os.path.dirname(os.path.dirname(__file__)))

Expand Down Expand Up @@ -58,53 +58,6 @@ def test_missing_instance_parameter_yaml(self):
str(context.exception),
)

def test_invalid_class_python(self):
with self.assertRaises(AttributeError) as context:
self.injector.load("tests/config/invalid_class_python.py")
self.injector.compile()
self.injector.get("invalid_class")
self.assertEqual(
"module 'module.name' has no attribute 'NonExistentClass'",
str(context.exception),
)

def test_invalid_factory_method_python(self):
with self.assertRaises(AttributeError) as context:
self.injector.load("tests/config/invalid_factory_method_python.py")
self.injector.compile()
self.injector.get("invalid_factory_method")
self.assertIn("non_existent_method", str(context.exception))

def test_missing_class_parameter_python(self):
with self.assertRaises(ValueError) as context:
self.injector.load("tests/config/missing_class_parameter_python.py")
self.injector.compile()
self.injector.get("missing_class_parameter")
self.assertEqual(
"Service 'missing_class_parameter' has an invalid class name: 123",
str(context.exception),
)

def test_invalid_scope_python(self):
with self.assertRaises(ValueError) as context:
self.injector.load("tests/config/invalid_scope_python.py")
self.injector.compile()
self.injector.get("invalid_scope")
self.assertEqual(
"Service 'invalid_scope' has an invalid scope: invalid_scope",
str(context.exception),
)

def test_missing_instance_parameter_python(self):
with self.assertRaises(ValueError) as context:
self.injector.load("tests/config/missing_instance_parameter_python.py")
self.injector.compile()
self.injector.get("missing_instance_parameter")
self.assertEqual(
"Service 'missing_instance_parameter' has an invalid instance: 123",
str(context.exception),
)


if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions tests/test_imported_services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
import os.path
import unittest

from simple_dependency_injector import DependencyInjector

Expand All @@ -15,7 +15,7 @@ def test_load_yaml_imported_service(self):
self.assertEqual(another_service.name, "Another Service")

def test_load_python_imported_service(self):
self.injector.load("tests/config/services_python.py")
self.injector.load("tests/config/services.py")
self.injector.compile()
another_service = self.injector.get("another_service")
self.assertEqual(another_service.name, "Another Service")
Expand Down
25 changes: 0 additions & 25 deletions tests/test_instance_services.py

This file was deleted.

Loading

0 comments on commit 22f2fc5

Please sign in to comment.