Skip to content

Commit

Permalink
call subprocess and check output test case
Browse files Browse the repository at this point in the history
  • Loading branch information
DeutscheGabanna committed Oct 25, 2023
1 parent 089eb5b commit 8923fb6
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 101 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ jobs:
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install dependencies
- name: Unittest main.py
run: |
python test_main.py ./_tests/testing_sheet.csv ./_tests/files
python _tests/test_main.py
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*.md
__pycache__/
__pycache__/
_tests/computed_results
10 changes: 5 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": [
"./_tests/testing_sheet.csv",
"~/Daylio export/"
]
"justMyCode": true
// "args": [
// "./_tests/testing_sheet.csv",
// "~/Daylio export/"
// ]
}
]
}
15 changes: 15 additions & 0 deletions _tests/expected_results/2022-10-25.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
tags: daily
---

## hungry - 11:36 PM
I felt #hungry with the following: #allegro #working-remotely #colleague-interaction
Nulla vel risus eget magna lacinia aliquam ac in arcu.

## rad - 11:40 PM
I felt #rad.
Uet nulla nunc lobortis quisque.

## vaguely ok - 5:00 PM
I felt #vaguely-ok.

28 changes: 28 additions & 0 deletions _tests/expected_results/2022-10-26.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
tags: daily
---

## captivated - 10:00 PM
I felt #captivated with the following: #at-the-office #board-game #colleague-interaction #big-social-gathering
Sed ut est interdum

## tired - 8:00 PM
I felt #tired with the following: #allegro #at-the-office #board-game #colleague-interaction #big-social-gathering
Quisque dictum odio quis augue consectetur, at convallis żodio aliquam.

## grateful - 7:30 PM
I felt #grateful with the following: #allegro #at-the-office #acknowledged-efforts #colleague-interaction
Nulla aćcumsan sem sit amet lectus pretium, ac interdum tellus porta.

## blissful - 1:00 PM
I felt #blissful with the following: #allegro #at-the-office
Ut et elit id lectus hendrerit ełementum quis auctor ipsum.

## in awe - 9:00 AM
I felt #in-awe with the following: #allegro #at-the-office #outdoors #notable-event
Nunc lobortis enim eu nisi ultrices, sit amet sagittis lacus venenatis.

## lifeless - 7:50 AM
I felt #lifeless with the following: #podcast #politics #world-event
Etiam commódo enim ut orci varius viverra.

12 changes: 12 additions & 0 deletions _tests/expected_results/2022-10-27.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
tags: daily
---

## vaguely good - 1:49 PM
I felt #vaguely-good with the following: #chess
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

## fatigued - 12:00 AM
I felt #fatigued with the following: #allegro #working-remotely
Phaśellus pharetra justo ac dui lacinia ullamcorper.

8 changes: 8 additions & 0 deletions _tests/expected_results/2022-10-30.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
tags: daily
---

## vaguely ok - 10:04 AM
I felt #vaguely-ok with the following: #2ćities-skylines #dólóó-fas_ą
Lorem ipsum sit dolomet amęt.

120 changes: 120 additions & 0 deletions _tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""Test cases for main.py"""
import unittest
import shutil # to delete temporary files
import subprocess # to run main.py
import os # to make temporary directories and join them

# import csv
# # Custom
# import parse_csv
# import utils

# class TestScript(unittest.TestCase):
# def test_expanded_path(self):
# self.assertEqual(
# utils.expand_path(r"./_tests/testing_sheet.csv"),
# r"/home/deutschegabanna/Obsidian-Daylio-Parser/_tests/testing_sheet.csv"
# )

# def test_this_row(self, row_to_parse, time, mood, activities, title, note):
# """Checks if Entry object correctly parses CSV row and populates its parameters.
# time, mood, activities, title, and note in function arguments should be the expected values."""
# tmp_entry = parse_csv.Entry(csv.reader([row_to_parse], delimiter=',', quotechar='"'))
# self.assertEqual(tmp_entry.time, time, "Entry created with wrong time.")
# self.assertEqual(tmp_entry.mood, mood, "Entry created with wrong mood.")
# self.assertListEqual(tmp_entry.activities, activities, "Entry created with wrong activities array.")
# self.assertEqual(tmp_entry.title, title, "Entry created with wrong title.")
# self.assertEqual(tmp_entry.note, note, "Entry created with wrong note.")

# def test_parsing(self):
# """Check several rows to trigger test_this_row for each of them."""
# # Sample entry
# self.test_this_row(
# "2022-10-30,October 30,Sunday,10:04 AM,vaguely ok,2ćities skylines | dó#lóó fa$$s_ą%,\"Dolomet\",\"Lorem ipsum sit dolomet amęt.\"",
# "10:04 AM",
# "vaguely ok",
# ["2ćities-skylines", "dólóó-fas_ą"],
# "Dolomet",
# "Lorem ipsum sit dolomet amęt."
# )
# # Sample entry
# self.test_this_row(
# "2022-10-27,October 27,Thursday,1:49 PM,vaguely good,chess,\"Cras pretium\",\"Lorem ipsum dolor sit amet, consectetur adipiscing elit.\"",
# "1:49 PM",
# "vaguely good",
# ["chess"],
# "Cras pretium",
# "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
# )
# # Sample entry
# self.test_this_row(
# "2022-10-29,October 29,Saturday,3:30 PM,happy,drawing,\"Art session\",\"Had a great time drawing today.\"",
# "3:30 PM",
# "happy",
# ["drawing"],
# "Art session",
# "Had a great time drawing today."
# )
# # Sample entry
# self.test_this_row(
# "2022-10-28,October 28,Wednesday,9:15 AM,sad,reading,\"A good book\",\"Enjoyed reading for a while.\"",
# "9:15 AM",
# "sad",
# ["reading"],
# "A good book",
# "Enjoyed reading for a while."
# )


# def test_parsing_incomplete_data(self):
# """Check if Entry object correctly consumes an array with incomplete data"""
# sample_row = [
# "2022-10-30",
# "October 30",
# "Sunday",
# "10:04 AM",
# "vaguely ok"
# ]
# try:
# sample_entry = parse_csv.Entry(sample_row)
# except IndexError:
# pass
# else:
# pass

FILES = [
"2022-10-25.md",
"2022-10-26.md",
"2022-10-27.md",
"2022-10-30.md"
]
TEST_DIR = os.path.join(os.getcwd(), "_tests")
COMPUTED = os.path.join(TEST_DIR, "computed_results")
EXPECTED = os.path.join(TEST_DIR, "expected_results")

class TestScript(unittest.TestCase):
"""Tests ../main.py"""
def setUp(self):
"""Create temporary directory for test output files"""
if not os.path.exists(COMPUTED):
os.mkdir(COMPUTED)

def test_main(self):
"""Gives ../main.py a CSV file and checks whether it is identical to the expected_results"""
if not subprocess.run(["python", "main.py", os.path.join(TEST_DIR, "testing_sheet.csv"), COMPUTED], check=True):
self.fail()

# Open each computed file and check if it's equal to the expected one
for file in FILES:
with open(os.path.join(EXPECTED, file), encoding="UTF-8") as expected_file, open(os.path.join(COMPUTED, file), encoding="UTF-8") as computed_file:
self.assertListEqual(list(expected_file), list(computed_file))

def tearDown(self):
if os.path.isdir(COMPUTED):
shutil.rmtree(COMPUTED)
else:
raise FileNotFoundError(f"{COMPUTED} is missing at teardown.")

# is this run as a main program, not component?
if __name__ == '__main__':
unittest.main(argv=["first-arg-is-ignored"])
3 changes: 2 additions & 1 deletion parse_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class Entry:
"""Journal entry made at a given moment in time, and describing a particular emotional state"""
def __init__(self, parsed_line, prop_inside_delimiter = delimiter):
# expected CSV row structure: full_date,date,weekday,time,mood,activities,note_title,note
# TODO: incomplete CSV support
if len(parsed_line)<7:
raise IndexError(f"{parsed_line} contains too few elements")
self.time = parsed_line[3]
self.mood = parsed_line[4]
self.activities = self.slice_quotes(parsed_line[5]).split(prop_inside_delimiter)
Expand Down
91 changes: 0 additions & 91 deletions test_main.py

This file was deleted.

0 comments on commit 8923fb6

Please sign in to comment.