-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): add unit tests to contract functions
- Loading branch information
1 parent
441cf11
commit 8637271
Showing
21 changed files
with
1,665 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
/** | ||
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
declare let SmartWeave: any; | ||
declare let ContractError: any; |
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,58 @@ | ||
/** | ||
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { ANTState, AntContractReadResult } from 'src/types'; | ||
|
||
import { | ||
INVALID_INPUT_MESSAGE, | ||
baselineAntState, | ||
} from '../../../tests/utils/constants'; | ||
import { balance } from './balance'; | ||
|
||
describe('balance', () => { | ||
let state: ANTState = { ...baselineAntState }; | ||
|
||
beforeEach(() => { | ||
state = { ...baselineAntState }; | ||
}); | ||
|
||
it.each(['test', '?', false, Infinity, 0, -1, parseInt(''.padEnd(43, '1'))])( | ||
'should throw error on invalid address type', | ||
async (user: any) => { | ||
const _state = { ...state, balances: { [user]: 1 } }; | ||
const result = await balance(_state, { | ||
caller: 'test', | ||
input: { | ||
function: 'balance', | ||
target: user, | ||
}, | ||
}).catch((e) => e); | ||
expect(result.message).toEqual(INVALID_INPUT_MESSAGE); | ||
}, | ||
); | ||
|
||
it('should return 0 for non-existent user', async () => { | ||
const { result } = (await balance(state, { | ||
caller: 'test', | ||
input: { | ||
function: 'balance', | ||
target: ''.padEnd(43, '1'), | ||
}, | ||
})) as AntContractReadResult; | ||
const { balance: _balance } = result as any; | ||
expect(_balance).toEqual(0); | ||
}); | ||
}); |
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
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,101 @@ | ||
/** | ||
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { AntContractWriteResult } from 'src/types'; | ||
|
||
import { | ||
INVALID_INPUT_MESSAGE, | ||
NON_CONTRACT_OWNER_MESSAGE, | ||
baselineAntState, | ||
} from '../../../tests/utils/constants'; | ||
import { removeController } from './removeController'; | ||
|
||
describe('removeController', () => { | ||
let state = { ...baselineAntState }; | ||
|
||
beforeEach(() => { | ||
state = { ...baselineAntState }; | ||
}); | ||
|
||
it.each([ | ||
undefined, | ||
false, | ||
1, | ||
0, | ||
Infinity, | ||
-1, | ||
'?'.padEnd(42, '1'), | ||
''.padEnd(44, '1'), | ||
])('should throw on bad target', async (target: any) => { | ||
const _state = { ...state, controllers: [target] }; | ||
const result = await removeController(_state, { | ||
caller: 'test', | ||
input: { | ||
function: 'removeController', | ||
target, | ||
}, | ||
}).catch((e) => e); | ||
expect(result.message).toEqual(INVALID_INPUT_MESSAGE); | ||
}); | ||
|
||
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])( | ||
'should not remove controller as non-owner', | ||
async (target: string) => { | ||
const _state = { ...state, controllers: [target] }; | ||
const result = await removeController(_state, { | ||
caller: target, | ||
input: { | ||
function: 'removeController', | ||
target, | ||
}, | ||
}).catch((e) => e); | ||
expect(result.message).toEqual(NON_CONTRACT_OWNER_MESSAGE); | ||
}, | ||
); | ||
|
||
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])( | ||
'should throw if target not a controller', | ||
async (target: string) => { | ||
const _state = { ...state, controllers: [] }; | ||
const result = await removeController(_state, { | ||
caller: 'test', | ||
input: { | ||
function: 'removeController', | ||
target, | ||
}, | ||
}).catch((e) => e); | ||
|
||
expect(result.message).toContain('is not a controller'); | ||
expect(result.message).toContain(target); | ||
}, | ||
); | ||
|
||
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])( | ||
'should remove controller', | ||
async (target: string) => { | ||
const _state = { ...state, controllers: [target] }; | ||
const result = (await removeController(_state, { | ||
caller: 'test', | ||
input: { | ||
function: 'removeController', | ||
target, | ||
}, | ||
})) as AntContractWriteResult; | ||
|
||
expect(result.state).not.toContain(target); | ||
}, | ||
); | ||
}); |
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
Oops, something went wrong.