<commit_before># -*- encoding: utf-8 -*-
import unittest
from . import peerdatastorage
from yamlns import namespace as ns
import os

"""
# TODO's

"""

somacmeyaml=u"""\
intercoopVersion: 1.0
peerVersion: 1
peerid: somacme
name: <NAME>, SCCL
"""

sombogusyaml=u"""\
intercoopVersion: 1.0
peerVersion: 1
peerid: sombogus
name: <NAME>, SCCL
"""

class PeerDataStorage_Test(unittest.TestCase):
    
    def setUp(self):
        import os
        self.peerdatadir = "peerdata"
        self.cleanUp()

        os.makedirs(self.peerdatadir)
        self.write('somacme.yaml', somacmeyaml)
        self.write('sombogus.yaml', sombogusyaml)
        

    def tearDown(self):
        self.cleanUp()

    def cleanUp(self):
        import shutil
        try:
            shutil.rmtree(self.peerdatadir)
        except: pass

    def write(self, filename, content):
        with open(os.path.join(self.peerdatadir, filename),'w') as f:
            f.write(content)


    def test_get(self):
        self.write('somacme.yaml', somacmeyaml)

        s = peerdatastorage.PeerDataStorage(self.peerdatadir)
        peerData = s.get("somacme")
        self.assertEqual(peerData,ns.loads(somacmeyaml))

    def test_get_badpeer(self):
        s = peerdatastorage.PeerDataStorage(self.peerdatadir)
        with self.assertRaises(Exception) as ctx:
            peerData = s.get("badpeer")

        self.assertEqual(str(ctx.exception),
            "Not such peer 'badpeer'")
 
    def test_get_invalidpeer(self):
        s = peerdatastorage.PeerDataStorage(self.peerdatadir)
        with self.assertRaises(Exception) as ctx:
            peerData = s.get("../../etc/passwd")

        self.assertEqual(str(ctx.exception),
            "Invalid peer '../../etc/passwd'")
 
    def test_iter(self):
        s = peerdatastorage.PeerDataStorage(self.peerdatadir)
        try:
            self.assertCountEqual(list(s),[
                    ns.loads(somacmeyaml),
                    ns.loads(sombogusyaml),
                ]
            )
        except AttributeError:
            self.assertItemsEqual(list(s),[
                    ns.loads(somacmeyaml),
                    ns.loads(sombogusyaml),
                ]
            )


# vim: ts=4 sw=4 et<commit_msg>peerdatastorage_test: simplified iterator test<commit_after># -*- encoding: utf-8 -*-
import unittest
from . import peerdatastorage
from yamlns import namespace as ns
import os

"""
# TODO's

"""

somacmeyaml=u"""\
intercoopVersion: 1.0
peerVersion: 1
peerid: somacme
name: <NAME>, SCCL
"""

sombogusyaml=u"""\
intercoopVersion: 1.0
peerVersion: 1
peerid: sombogus
name: <NAME>, SCCL
"""

class PeerDataStorage_Test(unittest.TestCase):
    
    def setUp(self):
        import os
        self.peerdatadir = "peerdata"
        self.cleanUp()

        os.makedirs(self.peerdatadir)
        self.write('somacme.yaml', somacmeyaml)
        self.write('sombogus.yaml', sombogusyaml)
        

    def tearDown(self):
        self.cleanUp()

    def cleanUp(self):
        import shutil
        try:
            shutil.rmtree(self.peerdatadir)
        except: pass

    def write(self, filename, content):
        with open(os.path.join(self.peerdatadir, filename),'w') as f:
            f.write(content)


    def test_get(self):
        self.write('somacme.yaml', somacmeyaml)

        s = peerdatastorage.PeerDataStorage(self.peerdatadir)
        peerData = s.get("somacme")
        self.assertEqual(peerData,ns.loads(somacmeyaml))

    def test_get_badpeer(self):
        s = peerdatastorage.PeerDataStorage(self.peerdatadir)
        with self.assertRaises(Exception) as ctx:
            peerData = s.get("badpeer")

        self.assertEqual(str(ctx.exception),
            "Not such peer 'badpeer'")
 
    def test_get_invalidpeer(self):
        s = peerdatastorage.PeerDataStorage(self.peerdatadir)
        with self.assertRaises(Exception) as ctx:
            peerData = s.get("../../etc/passwd")

        self.assertEqual(str(ctx.exception),
            "Invalid peer '../../etc/passwd'")
 
    def test_iter(self):
        s = peerdatastorage.PeerDataStorage(self.peerdatadir)
        self.assertEqual(list(s),[
                ns.loads(somacmeyaml),
                ns.loads(sombogusyaml),
            ]
        )


# vim: ts=4 sw=4 et
{'fraction_non_alphanumeric': 0.1204115033902268, 'fraction_numerical': 0.004208557400046762, 'mean_word_length': 2.324009324009324, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2544643', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1385, 'n_words': 250}