[test] create unit-test framework for RPC wallets (#2396)
---------
Co-authored-by: dni ⚡ <office@dnilabs.com>
This commit is contained in:
parent
b145bff566
commit
69ce0e565b
13 changed files with 2128 additions and 270 deletions
BIN
tests/wallets/fixtures/certificates/breez.crt
Normal file
BIN
tests/wallets/fixtures/certificates/breez.crt
Normal file
Binary file not shown.
32
tests/wallets/fixtures/certificates/cert.pem
Normal file
32
tests/wallets/fixtures/certificates/cert.pem
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
-----BEGIN CERTIFICATE-----
|
||||
MIIFbzCCA1egAwIBAgIUfkee1G4E8QAadd517sY/9+6xr0AwDQYJKoZIhvcNAQEL
|
||||
BQAwRjELMAkGA1UEBhMCU1YxFDASBgNVBAgMC0VsIFNhbHZhZG9yMSEwHwYDVQQK
|
||||
DBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwIBcNMjQwNDAzMTMyMTM5WhgPMjA1
|
||||
MTA4MjAxMzIxMzlaMEYxCzAJBgNVBAYTAlNWMRQwEgYDVQQIDAtFbCBTYWx2YWRv
|
||||
cjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIICIjANBgkqhkiG
|
||||
9w0BAQEFAAOCAg8AMIICCgKCAgEAnW4MKs2Y3qZnn2+J/Bp21aUuJ7oE8ll82Q2C
|
||||
uh8VAlsNnGDpTyOSRLHLmxV+cu82umvVPBpOVwAl17/VuxcLjFVSk7YOMj3MWoF5
|
||||
hm+oBtetouSDt3H0+BoDuXN3eVsLI4b+e1F6ag7JIwsDQvRUbGTFiyHVvXolTZPb
|
||||
wtFzlwQSB5i6KHKRQ+W6Q+cz4khIRO79IhaEiu5TWDrmx+6WkZxWYYO/g/I/S1gX
|
||||
l1JP6gXQFabwUFn+CBAxPsi7f+igi6gIepXBQOIG1dkZ5ojJPabtvblO7mWJTsec
|
||||
2D4Vb3L7OfboIYC85gY1cudWBX3oAASIVh9m9YoCZW2WOMNr6apnJSXx36ueJXAS
|
||||
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dANFGr/yWI6WBOibjoEFZMEZqzwlcEErgxLRinUvb
|
||||
9COmr6ig+zC1570V2ktmn1P/qodOD4tOL0ICSkKoTQLFPfevM2y0DdN48T2kxzZ5
|
||||
TruiKHuAnOhvwKwUpF+TRFMUWft3VG9GJXm/4A9FWm/ALLrqw2oSXGrl5z8pq29z
|
||||
SN2A
|
||||
-----END CERTIFICATE-----
|
||||
1517
tests/wallets/fixtures/json/fixtures_rest.json
Normal file
1517
tests/wallets/fixtures/json/fixtures_rest.json
Normal file
File diff suppressed because it is too large
Load diff
1527
tests/wallets/fixtures/json/fixtures_rpc.json
Normal file
1527
tests/wallets/fixtures/json/fixtures_rpc.json
Normal file
File diff suppressed because it is too large
Load diff
134
tests/wallets/fixtures/models.py
Normal file
134
tests/wallets/fixtures/models.py
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
from typing import Dict, List, Optional, Union
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class FundingSourceConfig(BaseModel):
|
||||
name: str
|
||||
skip: Optional[bool]
|
||||
wallet_class: str
|
||||
client_field: Optional[str]
|
||||
settings: dict
|
||||
|
||||
|
||||
class FunctionMock(BaseModel):
|
||||
uri: Optional[str]
|
||||
query_params: Optional[dict]
|
||||
headers: Optional[dict]
|
||||
method: Optional[str]
|
||||
|
||||
|
||||
class TestMock(BaseModel):
|
||||
skip: Optional[bool]
|
||||
description: Optional[str]
|
||||
request_type: Optional[str]
|
||||
request_body: Optional[dict]
|
||||
response_type: str
|
||||
response: Union[str, dict]
|
||||
|
||||
|
||||
class Mock(FunctionMock, TestMock):
|
||||
|
||||
@staticmethod
|
||||
def combine_mocks(fs_mock, test_mock):
|
||||
_mock = fs_mock | test_mock
|
||||
if "response" in _mock and "response" in fs_mock:
|
||||
_mock["response"] |= fs_mock["response"]
|
||||
return Mock(**_mock)
|
||||
|
||||
|
||||
class FunctionMocks(BaseModel):
|
||||
mocks: Dict[str, FunctionMock]
|
||||
|
||||
|
||||
class FunctionTest(BaseModel):
|
||||
description: str
|
||||
call_params: dict
|
||||
expect: dict
|
||||
mocks: Dict[str, List[Dict[str, TestMock]]]
|
||||
|
||||
|
||||
class FunctionData(BaseModel):
|
||||
"""Data required for testing this function"""
|
||||
|
||||
"Function level mocks that apply for all tests of this function"
|
||||
mocks: List[FunctionMock] = []
|
||||
|
||||
"All the tests for this function"
|
||||
tests: List[FunctionTest] = []
|
||||
|
||||
|
||||
class WalletTest(BaseModel):
|
||||
skip: Optional[bool]
|
||||
function: str
|
||||
description: str
|
||||
funding_source: FundingSourceConfig
|
||||
call_params: Optional[dict] = {}
|
||||
expect: Optional[dict]
|
||||
expect_error: Optional[dict]
|
||||
mocks: List[Mock] = []
|
||||
|
||||
@staticmethod
|
||||
def tests_for_funding_source(
|
||||
fs: FundingSourceConfig,
|
||||
fn_name: str,
|
||||
fn,
|
||||
test,
|
||||
) -> List["WalletTest"]:
|
||||
t = WalletTest(
|
||||
**{
|
||||
"funding_source": fs,
|
||||
"function": fn_name,
|
||||
**test,
|
||||
"mocks": [],
|
||||
"skip": fs.skip,
|
||||
}
|
||||
)
|
||||
if "mocks" in test:
|
||||
if fs.name not in test["mocks"]:
|
||||
t.skip = True
|
||||
return [t]
|
||||
|
||||
return t._tests_from_fs_mocks(fn, test, fs.name)
|
||||
|
||||
return [t]
|
||||
|
||||
def _tests_from_fs_mocks(self, fn, test, fs_name: str) -> List["WalletTest"]:
|
||||
tests: List[WalletTest] = []
|
||||
|
||||
fs_mocks = fn["mocks"][fs_name]
|
||||
test_mocks = test["mocks"][fs_name]
|
||||
|
||||
for mock_name in fs_mocks:
|
||||
tests += self._tests_from_mocks(fs_mocks[mock_name], test_mocks[mock_name])
|
||||
return tests
|
||||
|
||||
def _tests_from_mocks(self, fs_mock, test_mocks) -> List["WalletTest"]:
|
||||
tests: List[WalletTest] = []
|
||||
for test_mock in test_mocks:
|
||||
# different mocks that result in the same
|
||||
# return value for the tested function
|
||||
unique_test = self._test_from_mocks(fs_mock, test_mock)
|
||||
|
||||
tests.append(unique_test)
|
||||
return tests
|
||||
|
||||
def _test_from_mocks(self, fs_mock, test_mock) -> "WalletTest":
|
||||
mock = Mock.combine_mocks(fs_mock, test_mock)
|
||||
|
||||
return WalletTest(
|
||||
**(
|
||||
self.dict()
|
||||
| {
|
||||
"description": f"""{self.description}:{mock.description or ""}""",
|
||||
"mocks": self.mocks + [mock],
|
||||
"skip": self.skip or mock.skip,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class DataObject:
|
||||
def __init__(self, **kwargs):
|
||||
for k in kwargs:
|
||||
setattr(self, k, kwargs[k])
|
||||
Loading…
Add table
Add a link
Reference in a new issue