Skip to content

Commit

Permalink
made a common test file
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFJcurve committed Nov 15, 2024
1 parent b442507 commit c0f6b48
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 66 deletions.
36 changes: 19 additions & 17 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ name: Pylint

on:
push:
branches: [ "main" ]
branches:
- "**"
pull_request:
branches: [ "main" ]
branches:
- "**"

jobs:
build:
Expand All @@ -13,18 +15,18 @@ jobs:
matrix:
python-version: ["3.9"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
cd backend
pip install -r requirements.txt
pip install pylint
- name: Analysing the code with pylint
run: |
cd backend
pylint --fail-under=7.0 $(git ls-files '*.py')
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
cd backend
pip install -r requirements.txt
pip install pylint
- name: Analysing the code with pylint
run: |
cd backend
pylint --fail-under=7.0 $(git ls-files '*.py')
47 changes: 24 additions & 23 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,36 @@ name: Python application

on:
push:
branches: [ "main" ]
branches:
- "**"
pull_request:
branches: [ "main" ]
branches:
- "**"

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.9
uses: actions/setup-python@v3
with:
python-version: "3.9"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
cd backend
pip install -r requirements.txt
- name: Test
run: |
cd backend
python -m unittest discover .
- name: View Coverage
run: |
cd backend
coverage run --source=code -m unittest discover -s .
coverage report -m
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.9
uses: actions/setup-python@v3
with:
python-version: "3.9"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
cd backend
pip install -r requirements.txt
- name: Test
run: |
cd backend
python -m unittest discover .
- name: View Coverage
run: |
cd backend
coverage run --source=code -m unittest discover -s .
coverage report -m
2 changes: 1 addition & 1 deletion backend/code/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from config import SOURCES_FOLDER, UPLOADS_FOLDER


def custom_encoding(normal_input: any) -> bytes:
def custom_encoding(normal_input: any) -> str:
"""
Using utf-8 encoding. This is our custom encoding function we use in the entire project,
we should not use the inbuilt functions.
Expand Down
34 changes: 34 additions & 0 deletions backend/test/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Common Code for testing class
"""

import os

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


def write_to_testing_file_and_create_hackthehill(function_name: str, testing_file: str) -> None:
"""
Write the function name to the testing file (which is generated as a side effect) and
generate the hackthehill function (also a side effect).
"""

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


def remove_files(function_name: str):
"""
Remove the files generated while testing components
"""

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

if os.path.exists(testing_file):
os.remove(testing_file)

if os.path.exists(hackthehill_file):
os.remove(hackthehill_file)
18 changes: 6 additions & 12 deletions backend/test/test_file_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from code.file_tokenizer import hash_file_blocks, get_block_content
from code.utils import custom_encoding
from config import UPLOADS_FOLDER, SOURCES_FOLDER, HASH_EXTENSION
from test.common import write_to_testing_file_and_create_hackthehill, remove_files


class TestFileTokenizer(unittest.TestCase):
Expand Down Expand Up @@ -45,8 +46,7 @@ def test_hash_file_blocks_with_empty_file_returns_correct_value(self):
self.assertEqual(hackthehill_file_hashed_content,
custom_encoding(json.dumps(hackthehill_file_content)))

os.remove(testing_file)
os.remove(hackthehill_file)
remove_files(function_name)

def test_hash_file_blocks_with_non_empty_file_returns_correct_value(self):
"""
Expand Down Expand Up @@ -91,8 +91,7 @@ def test_hash_file_blocks_with_non_empty_file_returns_correct_value(self):
self.assertEqual(hackthehill_file_hashed_content,
custom_encoding(json.dumps(hackthehill_file_content)))

os.remove(testing_file)
os.remove(hackthehill_file)
remove_files(function_name)

def test_get_block_content_block_out_index_throws_error(self):
"""
Expand All @@ -103,16 +102,12 @@ def test_get_block_content_block_out_index_throws_error(self):
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(function_name)

hash_file_blocks(testing_file)
write_to_testing_file_and_create_hackthehill(function_name, testing_file)

self.assertRaises(ValueError, get_block_content, hackthehill_file, -1)
self.assertRaises(ValueError, get_block_content, hackthehill_file, 1)

os.remove(testing_file)
os.remove(hackthehill_file)
remove_files(function_name)

def test_get_block_content_returns_correct_value(self):
"""
Expand All @@ -135,5 +130,4 @@ def test_get_block_content_returns_correct_value(self):

self.assertEqual(get_block_content(hackthehill_file, 0), encoded_text)

os.remove(testing_file)
os.remove(hackthehill_file)
remove_files(function_name)
24 changes: 11 additions & 13 deletions backend/test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""
Testing the Utilities class
"""
import hashlib

import os.path
import unittest

from code.file_tokenizer import hash_file_blocks
from code.utils import find_file, custom_encoding, get_filename_by_file_id, custom_decoding

from config import SOURCES_FOLDER, CODE_FOLDER, UPLOADS_FOLDER, HASH_EXTENSION
from test.common import write_to_testing_file_and_create_hackthehill, remove_files


class TestUtils(unittest.TestCase):
Expand Down Expand Up @@ -74,7 +75,7 @@ def test_get_filename_by_file_id_with_no_matching_id_returns_none(self):

self.assertEqual(get_filename_by_file_id(file_id), None)

os.remove(testing_file)
remove_files(function_name)

def test_get_filename_by_file_id_with_matching_id_returns_tuple(self):
"""
Expand All @@ -86,17 +87,14 @@ def test_get_filename_by_file_id_with_matching_id_returns_tuple(self):
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(function_name)
hash_file_blocks(testing_file)
write_to_testing_file_and_create_hackthehill(function_name, testing_file)

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

self.assertEqual(get_filename_by_file_id(file_id),
(os.path.basename(testing_file),
os.path.basename(hackthehill_file)))
self.assertEqual(get_filename_by_file_id(file_id),
(os.path.basename(testing_file),
os.path.basename(hackthehill_file)))

os.remove(testing_file)
os.remove(hackthehill_file)
remove_files(function_name)

0 comments on commit c0f6b48

Please sign in to comment.