174 lines
6.8 KiB
Python
174 lines
6.8 KiB
Python
# [[file:../roam.org::*=roam.models.File= Testing][=roam.models.File= Testing:1]]
|
|
from django.test import TestCase
|
|
from django.db.utils import IntegrityError
|
|
|
|
from roam.models import File
|
|
from django.conf import settings
|
|
from arroyo import parse_file
|
|
import subprocess
|
|
# =roam.models.File= Testing:1 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.File= Testing][=roam.models.File= Testing:2]]
|
|
from roam.models import File
|
|
class RoamFileTest(TestCase):
|
|
def setUp(self):
|
|
# super().setUp()
|
|
self.native = parse_file(str(settings.BASE_DIR / "./README.org"))
|
|
self.expected_path = str(settings.BASE_DIR / "./README.org")
|
|
self.expected_hash = (
|
|
subprocess.check_output(
|
|
f"sha256sum {self.expected_path} | awk '{{print $1}}'", shell=True
|
|
)
|
|
.decode("UTF-8")
|
|
.rstrip()
|
|
)
|
|
# =roam.models.File= Testing:2 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.File= Testing][=roam.models.File= Testing:3]]
|
|
def test_cfa(self):
|
|
File.create_from_arroyo(self.native)
|
|
obj = File.objects.first()
|
|
|
|
# ensure object is instantiated properly
|
|
self.assertEqual(obj.path, self.expected_path)
|
|
self.assertEqual(obj.digest, self.expected_hash)
|
|
# =roam.models.File= Testing:3 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.File= Testing][=roam.models.File= Testing:4]]
|
|
def test_hash_updated(self):
|
|
t_file = File(
|
|
path=self.expected_path,
|
|
digest="12345"
|
|
)
|
|
|
|
self.assertNotEqual(t_file.digest, self.expected_hash)
|
|
self.assertEqual(t_file.hash_updated(), True)
|
|
t_file.digest = self.expected_hash
|
|
self.assertEqual(t_file.hash_updated(), False)
|
|
# =roam.models.File= Testing:4 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.Keyword= Testing][=roam.models.Keyword= Testing:1]]
|
|
from roam.models import Keyword
|
|
|
|
class RoamKeywordTest(TestCase):
|
|
def setUp(self):
|
|
# super().setUp()
|
|
self.native = parse_file(str(settings.BASE_DIR / "./README.org"))
|
|
self.expected_path = str(settings.BASE_DIR / "./README.org")
|
|
# =roam.models.Keyword= Testing:1 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.Keyword= Testing][=roam.models.Keyword= Testing:2]]
|
|
def test_cfa(self):
|
|
file = File.create_from_arroyo(self.native)
|
|
kws = Keyword.create_from_arroyo(self.native)
|
|
|
|
self.assertEqual(len(kws), len(Keyword.objects.all()))
|
|
self.assertEqual(kws[0].keyword, "ARCOLOGY_KEY")
|
|
self.assertEqual(kws[0].value, "arcology/django")
|
|
self.assertEqual(kws[0].path, file)
|
|
self.assertEqual(kws[0].path.path, self.expected_path)
|
|
# =roam.models.Keyword= Testing:2 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.Keyword= Testing][=roam.models.Keyword= Testing:3]]
|
|
def test_cfa_allowed_keywords(self):
|
|
_file = File.create_from_arroyo(self.native)
|
|
kws = Keyword.create_from_arroyo(self.native)
|
|
|
|
kws_map = map(lambda kw: kw.keyword, kws)
|
|
self.assertNotIn("FILETAGS", kws_map)
|
|
# =roam.models.Keyword= Testing:3 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.Keyword= Testing][=roam.models.Keyword= Testing:4]]
|
|
def test_uniqueness(self):
|
|
file = File.create_from_arroyo(self.native)
|
|
|
|
kw1 = Keyword(
|
|
path=file,
|
|
keyword="WHICH_ONE",
|
|
value="THE_FIRST",
|
|
)
|
|
|
|
kw2 = Keyword(
|
|
path=file,
|
|
keyword="WHICH_ONE",
|
|
value="THE_SECOND",
|
|
)
|
|
|
|
kw3 = Keyword(
|
|
path=file,
|
|
keyword="WHICH_ONE",
|
|
value="THE_FIRST",
|
|
)
|
|
|
|
kw1.save() # these will work
|
|
kw2.save() # these will work
|
|
with self.assertRaises(IntegrityError):
|
|
kw3.save() # this will raise because of the uniqeuness check
|
|
# =roam.models.Keyword= Testing:4 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.Heading= Testing][=roam.models.Heading= Testing:1]]
|
|
from roam.models import Heading
|
|
|
|
class RoamHeadingTest(TestCase):
|
|
def setUp(self):
|
|
self.native = parse_file(str(settings.BASE_DIR / "./README.org"))
|
|
self.expected_path = str(settings.BASE_DIR / "./README.org")
|
|
self.file = File.create_from_arroyo(self.native)
|
|
# =roam.models.Heading= Testing:1 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.Heading= Testing][=roam.models.Heading= Testing:2]]
|
|
def test_create_create_from_arroyo(self):
|
|
headings = Heading.create_from_arroyo(self.native)
|
|
|
|
# only headings with IDs will be created; this may need to be changed later on
|
|
README_ID_HEADING_CNT = 2
|
|
self.assertEqual(len(headings), README_ID_HEADING_CNT)
|
|
# =roam.models.Heading= Testing:2 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.Heading= Testing][=roam.models.Heading= Testing:3]]
|
|
def test_heading_relationships(self):
|
|
headings = Heading.create_from_arroyo(self.native)
|
|
for heading in headings:
|
|
self.assertEquals(heading.path, self.file)
|
|
# =roam.models.Heading= Testing:3 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.Heading= Testing][=roam.models.Heading= Testing:4]]
|
|
def test_object_internals(self):
|
|
headings = Heading.create_from_arroyo(self.native)
|
|
self.assertEquals(headings[0].level, 0)
|
|
self.assertEquals(headings[0].node_id, "arcology/django/readme")
|
|
self.assertEquals(headings[0].title, "The Arcology Project: Django Edition")
|
|
# =roam.models.Heading= Testing:4 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.Heading= Testing][=roam.models.Heading= Testing:5]]
|
|
def test_to_url(self):
|
|
pass
|
|
# raise Exception("Not implemented!")
|
|
# =roam.models.Heading= Testing:5 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.HeadingProperty= Testing][=roam.models.HeadingProperty= Testing:1]]
|
|
from roam.models import HeadingProperty
|
|
|
|
class RoamHeadingPropertyTest(TestCase):
|
|
def setUp(self):
|
|
# super().setUp()
|
|
self.native = parse_file(str(settings.BASE_DIR / "./README.org"))
|
|
self.expected_path = str(settings.BASE_DIR / "./README.org")
|
|
# =roam.models.HeadingProperty= Testing:1 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.HeadingProperty= Testing][=roam.models.HeadingProperty= Testing:2]]
|
|
def test_cfa(self):
|
|
_file = File.create_from_arroyo(self.native)
|
|
headings = Heading.create_from_arroyo(self.native)
|
|
props = HeadingProperty.create_from_arroyo(self.native)
|
|
# =roam.models.HeadingProperty= Testing:2 ends here
|
|
|
|
# [[file:../roam.org::*=roam.models.HeadingProperty= Testing][=roam.models.HeadingProperty= Testing:5]]
|
|
l1_heading = next(filter(lambda h: h.title == "Rough Timeline and Task List", headings))
|
|
l1h_properties = l1_heading.headingproperty_set.all()
|
|
self.assertEquals(len(l1h_properties), 1)
|
|
|
|
self.assertEquals(l1h_properties[0].keyword, "ID")
|
|
self.assertEquals(l1h_properties[0].value, "20240205T101753.548048")
|
|
# =roam.models.HeadingProperty= Testing:5 ends here
|