Skip to content

Commit

Permalink
started working on test_file_tokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFJcurve committed Nov 13, 2024
1 parent b31b935 commit ceac0a3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
3 changes: 2 additions & 1 deletion backend/code/file_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pathlib import Path

from code.utils import custom_hash
from config import SOURCES_FOLDER, HASH_EXTENSION


Expand Down Expand Up @@ -57,7 +58,7 @@ def hash_file_blocks(file_path: str, block_size: int = 512):
with open(os.path.join(SOURCES_FOLDER, hackthehill_file), 'w', encoding="utf-8") as f:
f.write(hash_block)

return hashlib.sha256(json.dumps(header).encode('utf-8')).hexdigest()
return custom_hash(json.dumps(header))


def get_block_content(file_path, block_index: int, block_size: int = 512) -> bytes:
Expand Down
48 changes: 42 additions & 6 deletions backend/test/test_file_tokenizer.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
"""
TODO
Testing the File Tokenizer functions.
"""

import json
import os.path
import unittest

from code.file_tokenizer import hash_file_blocks
from code.utils import custom_hash
from config import UPLOADS_FOLDER, SOURCES_FOLDER, HASH_EXTENSION


class TestFileTokenizer(unittest.TestCase):
"""
TODO
File Tokenizer functions are the core of project, as how they convert data and de-hash it is
very important to how our application is looking at files. The better the hash and de-hashing,
the better quality of file sharing we provide.
"""

def test_dummy(self):
def test_hash_file_blocks_with_empty_file_returns_correct_value(self):
"""
TODO
We should only see the header.
"""

self.assertEqual(1 + 1, 2)
testing_file = os.path.join(
UPLOADS_FOLDER,
"test_hash_file_blocks_with_empty_file_returns_correct_value.txt"
)
hackthehill_file = os.path.join(
SOURCES_FOLDER,
"test_hash_file_blocks_with_empty_file_returns_correct_value" + HASH_EXTENSION
)
test_header = {
"header": {
"file_name": os.path.basename(testing_file),
"file_size": 0,
"number_of_blocks": 0,
"block_size": 512,
},
"blocks": {}
}

with open(testing_file, "x", encoding="utf-8") as _:
hackthehill_file_hashed_content = hash_file_blocks(testing_file)

with open(hackthehill_file, "r", encoding="utf-8") as g:
hackthehill_file_content = json.loads(g.read())

self.assertEqual(hackthehill_file_content, test_header)
self.assertEqual(hackthehill_file_hashed_content,
custom_hash(json.dumps(hackthehill_file_content)))

os.remove(testing_file)
os.remove(hackthehill_file)

0 comments on commit ceac0a3

Please sign in to comment.