Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactivate some of the unit tests #146

Closed
wants to merge 9 commits into from
26 changes: 11 additions & 15 deletions src/gourmand/backends/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import shutil
import time
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple, Union

import sqlalchemy
import sqlalchemy.orm
from gi.repository import Gtk
from sqlalchemy import (Boolean, Column, Float, ForeignKey, Integer,
LargeBinary, Numeric, String, Table, Text,
event, func, select)
Expand All @@ -18,7 +17,6 @@
from gourmand import Undo, convert, image_utils
from gourmand.defaults import lang as defaults
from gourmand.gdebug import TimeAction, debug
from gourmand.gtk_extras.dialog_extras import show_message
from gourmand.i18n import _
from gourmand.keymanager import KeyManager
from gourmand.plugin import DatabasePlugin
Expand Down Expand Up @@ -2122,22 +2120,20 @@ def get_database (*args, **kwargs):
return RecData.instance_for(*args, **kwargs)


def backup_database(filename: Path) -> Path:
backup_name = filename.with_name(filename.name + '.backup-' + time.strftime('%d-%m-%y'))
def backup_database(filename: Union[None, Path, str]) -> Union[None, Path]:
if filename is None:
return None

if type(filename) == str:
filename = Path(filename)

backup_name = filename.with_name(
filename.name + '.backup-' + time.strftime('%d-%m-%y')
)

while backup_name.is_file():
backup_name = backup_name.with_name(backup_name.name + 'I')

show_message(
Copy link
Contributor Author

@jayaddison jayaddison Mar 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todo:

  • As noted, this informational message needs to be restored elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@jayaddison jayaddison Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about the approach taken in feb0d5a @cydanil @MrShark?

It uses a SQLite 'in-memory' URI for the database. That means that self.filename for the database is empty, and the backup is skipped.

It feels a little fragile - it's relying on a series of interconnected things fitting together. But it works (tests pass, and the UI doesn't display the 'backup' message -- but should still do during interactive usage when a database upgrade occurs).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'd probably like to attempt to find a better solution, but until then.. review & feedback welcome)

title=_("Database Backup"),
label=_("Database Backup"),
sublabel=_("Depending on the size of your database, this may take some time."),
expander=(_("Details"),
_("A backup will be made as %s in case something goes wrong."
" If this upgrade fails, you can manually rename the "
"backup file to recipes.db to recover it.") % backup_name),
message_type=Gtk.MessageType.INFO)

shutil.copy(filename, backup_name)

assert backup_name.is_file()
Expand Down
4 changes: 0 additions & 4 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ def setUp (self):
def testEqual (self):
self.assertEqual(self.c.convert_simple('c','c'),1)

@pytest.mark.skip("Broken as of 20220813")
def testDensity (self):
self.assertEqual(self.c.convert_w_density('ml','g',item='water'),1)
self.assertEqual(self.c.convert_w_density('ml','g',density=0.5),0.5)

@pytest.mark.skip("Broken as of 20220813")
def testReadability (self):
self.assertTrue(self.c.readability_score(1,'cup') > self.c.readability_score(0.8,'cups') )
self.assertTrue(self.c.readability_score(1/3.0,'tsp.') > self.c.readability_score(0.123,'tsp.'))
Expand All @@ -37,7 +35,6 @@ def testFractionGenerator (self):
('1/%s'%d)
)

@pytest.mark.skip("Broken as of 20220813")
def testFractToFloat (self):
for s,n in [
('1',1),
Expand All @@ -51,7 +48,6 @@ def testFractToFloat (self):
]:
self.assertEqual(convert.frac_to_float(s),n)

@pytest.mark.skip("Broken as of 20220813")
def test_ingmatcher (self):
for s,a,u,i in [
('1 cup sugar', '1','cup','sugar'),
Expand Down
Loading