Skip to content

Commit

Permalink
added second test for file_tokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFJcurve committed Nov 14, 2024
1 parent ceac0a3 commit 8aed704
Show file tree
Hide file tree
Showing 20 changed files with 58 additions and 10,183 deletions.
5 changes: 2 additions & 3 deletions backend/code/file_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
This file helps in creating and interpreting a .hackthehill file
"""

import hashlib
import os
import json

Expand Down Expand Up @@ -45,10 +44,10 @@ def hash_file_blocks(file_path: str, block_size: int = 512):

block_hashes = {}

with open(file_path, 'rb') as file:
with open(file_path, "r", encoding="utf-8") as file:
for index in range(num_blocks):
block = file.read(block_size)
block_hash = hashlib.sha256(block).hexdigest()
block_hash = custom_hash(block)
block_hashes[index] = block_hash

header["blocks"] = block_hashes
Expand Down
57 changes: 49 additions & 8 deletions backend/test/test_file_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ def test_hash_file_blocks_with_empty_file_returns_correct_value(self):
We should only see the header.
"""

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
)
function_name = "test_hash_file_blocks_with_empty_file_returns_correct_value"
testing_file = os.path.join(UPLOADS_FOLDER, f"{function_name}.txt")
hackthehill_file = os.path.join(SOURCES_FOLDER, function_name + HASH_EXTENSION)
test_header = {
"header": {
"file_name": os.path.basename(testing_file),
Expand All @@ -52,3 +47,49 @@ def test_hash_file_blocks_with_empty_file_returns_correct_value(self):

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

def test_hash_file_blocks_with_non_empty_file_returns_correct_value(self):
"""
We should see the correct header plus the correct hashed blocks
"""

function_name = "test_hash_file_blocks_with_non_empty_file_returns_correct_value"
testing_file = os.path.join(UPLOADS_FOLDER, f"{function_name}.txt")
hackthehill_file = os.path.join(SOURCES_FOLDER, function_name + HASH_EXTENSION)
block_size = 1024
test_header = {
"header": {
"file_name": os.path.basename(testing_file),
"file_size": 0,
"number_of_blocks": 0,
"block_size": block_size,
},
"blocks": {}
}

with open(testing_file, "x", encoding="utf-8") as f:
f.write(function_name)

file_size = os.path.getsize(testing_file)
num_blocks = (file_size + block_size - 1) // block_size

test_header["header"]["file_size"] = file_size

with open(testing_file, "r", encoding="utf-8") as f:
hackthehill_file_hashed_content = hash_file_blocks(testing_file, block_size)

for index in range(num_blocks):
block = f.read(block_size)
block_hash = custom_hash(block)
test_header["blocks"][str(index)] = str(block_hash)
test_header["header"]["number_of_blocks"] += 1

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)
21 changes: 7 additions & 14 deletions backend/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ def test_get_filename_by_file_id_with_no_matching_id_returns_none(self):
If none of the hashed ids match with the file id, we should return None
"""

testing_file = os.path.join(
UPLOADS_FOLDER,
"test_get_filename_by_file_id_with_no_matching_id_returns_none.txt")
message = "test_get_filename_by_file_id_with_no_matching_id_returns_none"
function_name = "test_get_filename_by_file_id_with_no_matching_id_returns_none"
testing_file = os.path.join(UPLOADS_FOLDER, f"{function_name}.txt")

with open(testing_file, "x", encoding="utf-8") as f:
f.write(message)
f.write(function_name)
file_id = "random-file-id"

self.assertEqual(get_filename_by_file_id(file_id), None)
Expand All @@ -75,17 +73,12 @@ def test_get_filename_by_file_id_with_matching_id_returns_tuple(self):
tuple of filenames
"""

testing_file = os.path.join(
UPLOADS_FOLDER,
"test_get_filename_by_file_id_with_matching_id_returns_tuple.txt")
hackthehill_file = os.path.join(
SOURCES_FOLDER,
"test_get_filename_by_file_id_with_matching_id_returns_tuple" + HASH_EXTENSION)

message = "test_get_filename_by_file_id_with_matching_id_returns_tuple"
function_name = "test_get_filename_by_file_id_with_matching_id_returns_tuple"
testing_file = os.path.join(UPLOADS_FOLDER, f"{function_name}.txt")
hackthehill_file = os.path.join(SOURCES_FOLDER, function_name + HASH_EXTENSION)

with open(testing_file, "x", encoding="utf-8") as f:
f.write(message)
f.write(function_name)
hash_file_blocks(testing_file)

with open(hackthehill_file, "r", encoding="utf-8") as g:
Expand Down
3 changes: 0 additions & 3 deletions frontend/.eslintrc.json

This file was deleted.

36 changes: 0 additions & 36 deletions frontend/.gitignore

This file was deleted.

36 changes: 0 additions & 36 deletions frontend/README.md

This file was deleted.

20 changes: 0 additions & 20 deletions frontend/components.json

This file was deleted.

57 changes: 0 additions & 57 deletions frontend/components/ui/button.tsx

This file was deleted.

76 changes: 0 additions & 76 deletions frontend/components/ui/card.tsx

This file was deleted.

Loading

0 comments on commit 8aed704

Please sign in to comment.