Skip to content

Commit

Permalink
Merge branch '27-implement-exercise-solution-submission-submit-subcom…
Browse files Browse the repository at this point in the history
…mand' of github.com:madeddie/moocfi_cses into 27-implement-exercise-solution-submission-submit-subcommand
  • Loading branch information
madeddie committed May 20, 2024
2 parents 3e38a5a + e555cc0 commit ee1428b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# mooc.fi CSES exercise task CLI
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/madeddie/moocfi_cses/test.yml)](https://github.com/madeddie/moocfi_cses/actions)
# Tyora: mooc.fi CSES exercise task CLI
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/madeddie/tyora/test.yml)](https://github.com/madeddie/tyora/actions)

This script interacts with the mooc.fi instance of the CSES (https://cses.fi) website to perform various actions such as logging in, retrieving exercise lists, and submitting solutions. It provides a convenient way to view and submit tasks.
This script interacts with the mooc.fi instance of the CSES (https://cses.fi) website to perform various actions such as logging in, retrieving exercise lists, and submitting solutions.
It provides a convenient way to view and submit tasks.

## Features

Expand All @@ -14,13 +15,13 @@ This script interacts with the mooc.fi instance of the CSES (https://cses.fi) we
1. Clone the repository to your local machine:

```bash
git clone https://github.com/madeddie/moocfi_cses.git
git clone https://github.com/madeddie/tyora.git
```

2. Navigate to the project directory:

```bash
cd moocfi_cses
cd tyora
```

3. Install the required dependencies:
Expand All @@ -34,27 +35,32 @@ This script interacts with the mooc.fi instance of the CSES (https://cses.fi) we
1. Configure the script by running:

```bash
python moocfi_cses.py configure
python tyora.py configure
```

Follow the prompts to enter your mooc.fi username and password. This information will be stored for future use.

2. List available exercises:

```bash
python moocfi_cses.py list
python tyora.py list
```

This will retrieve and display a list of exercises available on the CSES platform.

3. Submit a solution:

```bash
python moocfi_cses.py submit <exercise_id> <path_to_solution_file>
python tyora.py submit <exercise_id> <path_to_solution_file>
```

Replace `<exercise_id>` with the ID of the exercise you want to submit a solution for, and `<path_to_solution_file>` with the path to your solution file.

## Origin of name

The name "tyora" is derived from Finnish words: "työ" meaning "work" and "pyörä" meaning "wheel".
Anyway, `pyora` was already taken, so I went with `tyora`... ;)

## Contributing

Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue or submit a pull request.
Expand Down
35 changes: 14 additions & 21 deletions tests/test_moocfi_cses.py → tests/test_tyora.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import moocfi_cses
import tyora
import pytest
import requests_mock


def test_parse_args_missing_args() -> None:
with pytest.raises(SystemExit):
moocfi_cses.parse_args()
tyora.parse_args()


def test_parse_args_command() -> None:
args = moocfi_cses.parse_args(["list"])
args = tyora.parse_args(["list"])
assert args.cmd == "list"


Expand All @@ -25,16 +25,13 @@ class TestFindLink:
valid_return = {"href": "somelink", "text": "sometext"}

def test_find_link_success(self) -> None:
assert (
moocfi_cses.find_link(self.valid_html, self.valid_xpath)
== self.valid_return
)
assert tyora.find_link(self.valid_html, self.valid_xpath) == self.valid_return

def test_find_link_bad_xpath(self) -> None:
assert moocfi_cses.find_link(self.valid_html, self.invalid_xpath) == {}
assert tyora.find_link(self.valid_html, self.invalid_xpath) == {}

def test_find_link_bad_html(self) -> None:
assert moocfi_cses.find_link(self.invalid_html, self.valid_xpath) == {}
assert tyora.find_link(self.invalid_html, self.valid_xpath) == {}


class TestParseForm:
Expand All @@ -51,15 +48,15 @@ class TestParseForm:

# TODO: add tests for unreachable and failing endpoints, 4xx, 5xx, etc
@pytest.fixture
def mock_session() -> moocfi_cses.Session:
return moocfi_cses.Session(
def mock_session() -> tyora.Session:
return tyora.Session(
username="[email protected]",
password="test_password",
base_url="https://example.com",
)


def test_login_successful(mock_session: moocfi_cses.Session) -> None:
def test_login_successful(mock_session: tyora.Session) -> None:
# Mocking the HTTP response for successful login
with requests_mock.Mocker() as m:
m.get(
Expand All @@ -70,7 +67,7 @@ def test_login_successful(mock_session: moocfi_cses.Session) -> None:
assert mock_session.is_logged_in


def test_login_failed(mock_session: moocfi_cses.Session) -> None:
def test_login_failed(mock_session: tyora.Session) -> None:
# Mocking the HTTP response for failed login
with requests_mock.Mocker() as m:
m.get(
Expand All @@ -84,17 +81,13 @@ def test_login_failed(mock_session: moocfi_cses.Session) -> None:


# TODO: functions that use user input or read or write files
def test_create_config() -> None:
...
def test_create_config() -> None: ...


def test_write_config() -> None:
...
def test_write_config() -> None: ...


def test_read_config() -> None:
...
def test_read_config() -> None: ...


def test_get_cookiejar() -> None:
...
def test_get_cookiejar() -> None: ...
16 changes: 9 additions & 7 deletions moocfi_cses.py → tyora.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import requests


logger = logging.getLogger(name="moocfi_cses")
logger = logging.getLogger(name="tyora")


@dataclass
Expand Down Expand Up @@ -116,7 +116,7 @@ def parse_args(args: Optional[list[str]] = None) -> argparse.Namespace:
parser.add_argument(
"--config",
help="Location of config file (default: %(default)s)",
default="~/.config/moocfi_cses/config.json",
default="~/.config/tyora/config.json",
)
parser.add_argument(
"--no-state",
Expand Down Expand Up @@ -169,7 +169,7 @@ def create_config() -> dict[str, str]:
def write_config(configfile: str, config: dict[str, str]) -> None:
file_path = Path(configfile).expanduser()
if file_path.exists():
# TODO: https://github.com/madeddie/moocfi_cses/issues/28
# TODO: https://github.com/madeddie/tyora/issues/28
...
file_path.parent.mkdir(parents=True, exist_ok=True) # Ensure directory exists
print("Writing config to file")
Expand Down Expand Up @@ -287,9 +287,11 @@ def parse_task_list(html: AnyStr) -> list[Task]:
task = Task(
id=item_id,
name=item_name,
state=TaskState.COMPLETE
if "full" in item_class
else TaskState.INCOMPLETE,
state=(
TaskState.COMPLETE
if "full" in item_class
else TaskState.INCOMPLETE
),
)
task_list.append(task)

Expand Down Expand Up @@ -390,7 +392,7 @@ def main() -> None:
cookiefile = None
cookies = dict()
if not args.no_state:
state_dir = Path("~/.local/state/moocfi_cses").expanduser()
state_dir = Path("~/.local/state/tyora").expanduser()
if not state_dir.exists():
state_dir.mkdir(parents=True, exist_ok=True)
cookiefile = state_dir / "cookies.txt"
Expand Down

0 comments on commit ee1428b

Please sign in to comment.