Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-senechal committed Dec 13, 2024
1 parent 58b6dcb commit f7ced54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 40 deletions.
6 changes: 5 additions & 1 deletion deweb-index/assembly/__tests__/remove_website.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
removeWebsite,
updateWebsite,
} from '../contracts/deweb-index';
import { _indexByOwnerBaseKey } from '../contracts/utils';

import { scOwner, version, websiteAddress, websiteOwner } from './const';

describe('removeWebsite tests', () => {
Expand All @@ -32,7 +34,9 @@ describe('removeWebsite tests', () => {
removeWebsite(args);

const key = Storage.getKeys(
stringToBytes(websiteOwner.toString().concat(websiteAddress.toString())),
_indexByOwnerBaseKey(websiteOwner.toString()).concat(
stringToBytes(websiteAddress.toString()),
),
);
expect(key).toHaveLength(0);
});
Expand Down
67 changes: 28 additions & 39 deletions deweb-index/assembly/__tests__/update_website.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Args, i32ToBytes, stringToBytes } from '@massalabs/as-types';
import { Args, stringToBytes } from '@massalabs/as-types';
import {
deleteOf,
resetStorage,
Expand All @@ -11,9 +11,12 @@ import { OWNER_KEY } from '@massalabs/sc-standards/assembly/contracts/utils/owne
import {
constructor,
DEWEB_VERSION_TAG,
INDEX_BY_OWNER_TAG,
updateWebsite,
} from '../contracts/deweb-index';
import {
_addressToOwnerBaseKey,
_indexByOwnerBaseKey,
} from '../contracts/utils';
import { scOwner, version, websiteAddress, websiteOwner } from './const';

describe('updateWebsite tests', () => {
Expand All @@ -29,9 +32,7 @@ describe('updateWebsite tests', () => {
const args = new Args().add(websiteAddress).serialize();
updateWebsite(args);

const ownerPrefixKey = INDEX_BY_OWNER_TAG.concat(
i32ToBytes(websiteOwner.toString().length),
).concat(stringToBytes(websiteOwner.toString()));
const ownerPrefixKey = _indexByOwnerBaseKey(websiteOwner.toString());

const ownerKeys = Storage.getKeys(ownerPrefixKey);
expect(ownerKeys).toHaveLength(1, 'Expected one key for owner');
Expand All @@ -40,15 +41,13 @@ describe('updateWebsite tests', () => {
'Owner keys',
);

const websiteKeys = Storage.getKeys(
stringToBytes(websiteAddress.toString()),
);
const websitePrefixKey = _addressToOwnerBaseKey(websiteAddress.toString());

const websiteKeys = Storage.getKeys(websitePrefixKey);

expect(websiteKeys).toHaveLength(1, 'Expected one key for website');
expect(websiteKeys[0]).toStrictEqual(
stringToBytes(websiteAddress.toString()).concat(
stringToBytes(websiteOwner.toString()),
),
websitePrefixKey.concat(stringToBytes(websiteOwner.toString())),
'Website keys',
);
});
Expand All @@ -59,74 +58,64 @@ describe('updateWebsite tests', () => {
const args = new Args().add(websiteAddress).serialize();
updateWebsite(args);

const websiteKeys = Storage.getKeys(
stringToBytes(websiteAddress.toString()),
);
const websitePrefixKey = _addressToOwnerBaseKey(websiteAddress.toString());

const websiteKeys = Storage.getKeys(websitePrefixKey);
expect(websiteKeys).toHaveLength(
1,
`Expected one key for website ${websiteAddress.toString()}`,
);
expect(websiteKeys[0]).toStrictEqual(
stringToBytes(websiteAddress.toString()),
'Website keys',
);
expect(websiteKeys[0]).toStrictEqual(websitePrefixKey, 'Website keys');
});

test('update website', () => {
test('should update website owner', () => {
setOf(websiteAddress, OWNER_KEY, scOwner.toString());
const args = new Args().add(websiteAddress).serialize();
updateWebsite(args);

const oldOwnerPrefixKey = INDEX_BY_OWNER_TAG.concat(
i32ToBytes(scOwner.toString().length),
).concat(stringToBytes(scOwner.toString()));

const oldOwnerPrefixKey = _indexByOwnerBaseKey(websiteOwner.toString());
const oldOwnerKeys = Storage.getKeys(
stringToBytes(oldOwnerPrefixKey.toString()),
);
expect(oldOwnerKeys).toHaveLength(0, 'Old owner keys should be empty');

const newOwnerPrefixKey = INDEX_BY_OWNER_TAG.concat(
i32ToBytes(scOwner.toString().length),
).concat(stringToBytes(scOwner.toString()));
const newOwnerPrefixKey = _indexByOwnerBaseKey(scOwner.toString());

const newOwnerKeys = Storage.getKeys(newOwnerPrefixKey);
expect(newOwnerKeys).toHaveLength(1, 'New owner keys should have one key');

const websiteKeys = Storage.getKeys(
stringToBytes(websiteAddress.toString()),
);
const websitePrefixKey = _addressToOwnerBaseKey(websiteAddress.toString());

const websiteKeys = Storage.getKeys(websitePrefixKey);
expect(websiteKeys).toHaveLength(1, 'Website keys should have one key');
});

test('should do nothing if the address is not a website', () => {
const args = new Args().add(scOwner).serialize();
updateWebsite(args);

const websiteKeys = Storage.getKeys(stringToBytes(scOwner.toString()));
const prefix = _addressToOwnerBaseKey(scOwner.toString());

const websiteKeys = Storage.getKeys(prefix);
expect(websiteKeys).toHaveLength(0);
});

test('should remove website if not a website anymore', () => {
const args = new Args().add(websiteAddress).serialize();
updateWebsite(args);

const websiteKeys = Storage.getKeys(
stringToBytes(websiteAddress.toString()),
);
const websitePrefixKey = _addressToOwnerBaseKey(websiteAddress.toString());

const websiteKeys = Storage.getKeys(websitePrefixKey);
expect(websiteKeys).toHaveLength(1, 'Website keys should have one key');

deleteOf(websiteAddress, DEWEB_VERSION_TAG);
updateWebsite(args);

const updatedWebsiteKeys = Storage.getKeys(
stringToBytes(websiteAddress.toString()),
);
const updatedWebsiteKeys = Storage.getKeys(websitePrefixKey);
expect(updatedWebsiteKeys).toHaveLength(0, 'Website keys should be empty');

const ownerPrefixKey = INDEX_BY_OWNER_TAG.concat(
i32ToBytes(websiteOwner.toString().length),
).concat(stringToBytes(websiteOwner.toString()));
const ownerPrefixKey = _indexByOwnerBaseKey(websiteOwner.toString());
const ownerKeys = Storage.getKeys(ownerPrefixKey);
expect(ownerKeys).toHaveLength(0, 'Owner keys should be empty');
});
Expand Down
Empty file.

0 comments on commit f7ced54

Please sign in to comment.