Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioteula committed Jan 1, 2022
2 parents 1e68524 + 0e0ff1d commit 159074d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pyrmanent/pyrmanent_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,6 @@ def _prepare_path(self, folder):
self._path = self._folder + self._filename

def _get_filename(self):
if not self._name:
return self.__class__.__name__ + ".pickle"
return self.__class__.__name__ + "_" + self._name + ".pickle"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pyrmanent",
version="1.0.0",
version="1.0.1",
author="Sergio Abad",
author_email="sergio.abad@bytelix.com",
description="Make all your classes permanent in a flash",
Expand Down
26 changes: 26 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
import shutil
import unittest

from pyrmanent import Pyrmanent


class Example(Pyrmanent):
pass


class TestFiles(unittest.TestCase):
def test_pickle_file(self):
Example()
self.assertTrue(os.path.isfile("Example.pickle"))
os.remove("Example.pickle")

def test_custom_folder(self):
Example(folder="saves")
self.assertTrue(os.path.isfile("saves/Example.pickle"))
shutil.rmtree("saves")

def test_custom_nested_folder(self):
Example(folder="saves/data")
self.assertTrue(os.path.isfile("saves/data/Example.pickle"))
shutil.rmtree("saves")
20 changes: 20 additions & 0 deletions tests/test_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import unittest

from pyrmanent import Pyrmanent


class Example(Pyrmanent):
pass


class TestName(unittest.TestCase):
def test_custom_name(self):
Example(name="foo")
self.assertTrue(os.path.isfile("Example_foo.pickle"))
os.remove("Example_foo.pickle")

def test_no_custom_name(self):
Example()
self.assertTrue(os.path.isfile("Example.pickle"))
os.remove("Example.pickle")
50 changes: 50 additions & 0 deletions tests/test_persistency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
import unittest

from pyrmanent import Pyrmanent


class Example(Pyrmanent):
def init(self):
self.menu = "pizza"


class TestPersistency(unittest.TestCase):
def test_init_values(self):
example = Example()
self.assertTrue(hasattr(example, "menu"))
self.assertEqual(example.menu, "pizza")
os.remove("Example.pickle")

def test_not_saved_values(self):
example = Example()
example.menu = "rice"
example = Example()
self.assertTrue(hasattr(example, "menu"))
self.assertEqual(example.menu, "pizza")
os.remove("Example.pickle")

def test_saved_values(self):
example = Example()
example.menu = "rice"
example.save()
example = Example()
self.assertTrue(hasattr(example, "menu"))
self.assertEqual(example.menu, "rice")
os.remove("Example.pickle")

def test_values_for_different_names(self):
first = Example(name="first")
second = Example(name="second")
first.menu = "rice"
second.menu = "soup"
first.save()
second.save()
first = Example(name="first")
second = Example(name="second")
self.assertTrue(hasattr(first, "menu"))
self.assertTrue(hasattr(second, "menu"))
self.assertEqual(first.menu, "rice")
self.assertEqual(second.menu, "soup")
os.remove("Example_first.pickle")
os.remove("Example_second.pickle")

0 comments on commit 159074d

Please sign in to comment.