Skip to content

Commit

Permalink
Edited tests and github-actions file
Browse files Browse the repository at this point in the history
  • Loading branch information
EitanHemed committed May 21, 2024
1 parent 2115dd7 commit 67a6342
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 16 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/pkg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: pkg

on:
push:
branches: [ dev-cli ]
pull_request:
branches: [ dev-cli ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: 3.10
- name: Install library
run: |
python -m pip install --upgrade pip
pip install .
- name: Test with unittest
run: python -m unittest discover
7 changes: 4 additions & 3 deletions src/pavlovia_survey_utils/api/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def process_image(image_str: str, pth: str | pathlib.Path) -> None:
:param pth: The path to save the image to.
:return: None
"""
image_format, image_data = read_base64_img(image_str)
image_format, image_data = read_base64_image_str(image_str)
save_base64_image(pth, image_format, image_data)


Expand All @@ -30,7 +30,7 @@ def find_image_columns(df: pd.DataFrame) -> typing.List:
df[str_cols].apply(lambda s: s.str.startswith('data:image')).any(axis='index').values].tolist()


def read_base64_img(image_string: str) -> typing.Tuple[str, bytes]:
def read_base64_image_str(image_string: str) -> typing.Tuple[str, bytes]:
"""
Read a base64 image string and return the image format and image data.
:param image_string:
Expand Down Expand Up @@ -71,7 +71,8 @@ def save_image_columns(df: pd.DataFrame, survey_name: str, root: str | pathlib.P

for n, g in grouped:
for i in image_columns:
pth = os.path.join(os.path.abspath(root), 'pavlovia-survey-utils', survey_name, 'images', i)
pth = os.path.join(os.path.abspath(root),
'pavlovia-survey-utils', survey_name, 'images', i)
os.makedirs(pth, exist_ok=True)
process_image(g[i].values[0],
os.path.join(pth, n))
1 change: 1 addition & 0 deletions src/tests/test_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

33 changes: 20 additions & 13 deletions src/tests/test_api/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@
import unittest
import unittest.mock as mock

import requests

from pavlovia_survey_utils.api import auth
from pavlovia_survey_utils.api.auth import USERS_CACHE_FNAME, TOKEN_KEY_NAME, REGISTRATION_DATE_KEY_NAME
from pavlovia_survey_utils.api.auth import TOKEN_KEY_NAME, REGISTRATION_DATE_KEY_NAME

mock_user_1 = 'mock_user_1'
mock_password_1 = 'mock_password_1'
mock_token_1 = 'mock_token_1'
registration_date_1 = auth._get_timestamp()

mock_user_1_updated = 'mock_user_1'
mock_password_1_updated = 'mock_password_1'
mock_token_1_updated = 'mock_token_1 UPDATED'
registration_date_1_updated = 'UPDATED TIMESTAMP'

mock_user_2 = 'mock_user_2'
mock_password_2 = 'mock_password_2'
mock_token_2 = 'mock_token_2'
registration_date_2 = auth._get_timestamp()
registration_date_2 = registration_date_1

two_users = {mock_user_1: {TOKEN_KEY_NAME: mock_token_1, REGISTRATION_DATE_KEY_NAME: registration_date_1},
mock_user_2: {TOKEN_KEY_NAME: mock_token_2, REGISTRATION_DATE_KEY_NAME: registration_date_2}}
only_user_1 = {mock_user_1: {TOKEN_KEY_NAME: mock_token_1, REGISTRATION_DATE_KEY_NAME: registration_date_1}}
only_user_2 = {mock_user_2: {TOKEN_KEY_NAME: mock_token_2, REGISTRATION_DATE_KEY_NAME: registration_date_2}}

two_users = {**only_user_1, **only_user_2}

only_user_1_updated = {mock_user_1: {TOKEN_KEY_NAME: mock_token_1_updated,
REGISTRATION_DATE_KEY_NAME: registration_date_1_updated}}


class TestAuth(unittest.TestCase):
Expand Down Expand Up @@ -62,19 +67,21 @@ def test_add_user_to_cache(self):
def test_add_user_to_cache_force_update(self):

with mock.patch('builtins.open', mock.mock_open()) as mock_open:
mock_open.return_value.read.return_value = json.dumps({})

with mock.patch('requests.post') as mock_post:

mock_post.return_value.json.return_value = json.dumps({'access_token': mock_token_1})

mock_post.return_value.json.return_value = {'access_token': mock_token_1}
# Add the user to the cache
auth.add_user_to_cache(mock_user_1, mock_password_1)
mock_open().write.assert_called_once_with(json.dumps(only_user_1))

mock_open().write.reset_mock()
mock_post().reset_mock()
mock_open().read.return_value = json.dumps(only_user_1)

mock_post.return_value.json.return_value = {'access_token': only_user_1_updated[mock_user_1][TOKEN_KEY_NAME]}
mock_open().write.return_value = json.dumps(only_user_1_updated)
auth.add_user_to_cache(mock_user_1, mock_password_1, force_update=True)
mock_open().write.assert_called_once_with(json.dumps(only_user_1))
mock_open().read.return_value = json.dumps(only_user_1_updated)

assert auth.load_token_for_user(mock_user_1) == only_user_1_updated[mock_user_1][TOKEN_KEY_NAME]

def test_remove_user_from_cache(self):
with mock.patch('builtins.open', mock.mock_open()) as mock_open:
Expand Down
33 changes: 33 additions & 0 deletions src/tests/test_api/test_file_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest

import pandas as pd

from pavlovia_survey_utils.api import file_utils


class TestFileUtils(unittest.TestCase):

def test_find_image_columns(self):
df = pd.DataFrame({
'image1': ['data:image/png;base64,abc', 'data:image/png;base64,def'],
'image2': ['data:image/png;base64,ghi', 'data:image/png;base64,jkl'],
'text': ['abc', 'def']
})

self.assertEqual(file_utils.find_image_columns(df), ['image1', 'image2'])

self.assertEqual(file_utils.find_image_columns(
df.drop(columns=['image1'])), ['image2'])

self.assertEqual(file_utils.find_image_columns(
df[['text']]), []) # No image columns

def test_read_base64_img_str(self):
pass

def test_save_image_columns(self):
pass


if __name__ == "__main__":
unittest.main()
Empty file added src/tests/test_cli/__init__.py
Empty file.

0 comments on commit 67a6342

Please sign in to comment.