-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from numat/add_tags
Prep for adding tags by adding tests
- Loading branch information
Showing
7 changed files
with
300 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Python Django | ||
# Test a Django project on multiple versions of Python. | ||
# Add steps that analyze code, save build artifacts, deploy, and more: | ||
# https://docs.microsoft.com/azure/devops/pipelines/languages/python | ||
|
||
trigger: | ||
- master | ||
|
||
pool: | ||
vmImage: 'ubuntu-latest' | ||
strategy: | ||
matrix: | ||
Python37: | ||
PYTHON_VERSION: '3.7' | ||
maxParallel: 3 | ||
|
||
steps: | ||
- task: UsePythonVersion@0 | ||
inputs: | ||
versionSpec: '$(PYTHON_VERSION)' | ||
architecture: 'x64' | ||
|
||
- script: | | ||
python -m pip install --upgrade pip | ||
pip install '.[test]' | ||
displayName: 'Install dependencies' | ||
|
||
- script: | | ||
cd clickplc | ||
pytest --junitxml=../reports/test-coverage.xml --cov=. --cov-report=xml | ||
env: | ||
USER_TOKEN: $(USER_TOKEN) | ||
displayName: 'Run tests' | ||
|
||
- task: PublishTestResults@2 | ||
inputs: | ||
testResultsFiles: 'reports/test-coverage.xml' | ||
testRunTitle: '$(Agent.OS) - $(Build.BuildNumber)[$(Agent.JobName)] - Python $(python.version)' | ||
condition: succeededOrFailed() | ||
|
||
- task: PublishCodeCoverageResults@1 | ||
inputs: | ||
codeCoverageTool: Cobertura | ||
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml' | ||
reportDirectory: '$(System.DefaultWorkingDirectory)/**/htmlcov' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/python | ||
#!/usr/bin/python3 | ||
""" | ||
A Python driver for Koyo ClickPLC ethernet units. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from collections import defaultdict | ||
from unittest.mock import MagicMock | ||
|
||
from pymodbus.bit_read_message import ReadCoilsResponse, ReadDiscreteInputsResponse | ||
from pymodbus.bit_write_message import WriteSingleCoilResponse, WriteMultipleCoilsResponse | ||
from pymodbus.register_read_message import ReadHoldingRegistersResponse | ||
from pymodbus.register_write_message import WriteMultipleRegistersResponse | ||
|
||
from clickplc.driver import ClickPLC as realClickPLC | ||
|
||
|
||
class AsyncMock(MagicMock): | ||
"""Magic mock that works with async methods""" | ||
async def __call__(self, *args, **kwargs): | ||
return super(AsyncMock, self).__call__(*args, **kwargs) | ||
|
||
|
||
class ClickPLC(realClickPLC): | ||
"""A version of the driver with the remote communication replaced with local data storage | ||
for testing""" | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.client = AsyncMock() | ||
self._coils = defaultdict(bool) | ||
self._discrete_inputs = defaultdict(bool) | ||
self._registers = defaultdict(bytes) | ||
|
||
async def _request(self, method, *args, **kwargs): | ||
if method == 'read_coils': | ||
address, count = args | ||
return ReadCoilsResponse([self._coils[address + i] for i in range(count)]) | ||
if method == 'read_discrete_inputs': | ||
address, count = args | ||
return ReadDiscreteInputsResponse([self._discrete_inputs[address + i] | ||
for i in range(count)]) | ||
elif method == 'read_holding_registers': | ||
address, count = args | ||
return ReadHoldingRegistersResponse([int.from_bytes(self._registers[address + i], | ||
byteorder='big') | ||
for i in range(count)]) | ||
elif method == 'write_coil': | ||
address, data = args | ||
self._coils[address] = data | ||
return WriteSingleCoilResponse(address, data) | ||
elif method == 'write_coils': | ||
address, data = args | ||
for i, d in enumerate(data): | ||
self._coils[address + i] = d | ||
return WriteMultipleCoilsResponse(address, data) | ||
elif method == 'write_registers': | ||
address, data = args | ||
for i, d in enumerate(data): | ||
self._registers[address + i] = d | ||
return WriteMultipleRegistersResponse(address, data) | ||
return NotImplementedError(f'Unrecognised method: {method}') |
Oops, something went wrong.