Ejemplo n.º 1
0
    def test_store_entity_with_virtual_fields_default_store_location (self):
        s = Related()
        s.bind(self.path)

        class Person (Entity):
            pk = Field(Integer, primary_key=True)
            firstname = Field(String)
            surname = Field(String, virtual=True)

        class Account (Entity):
            homedir = Field(String, virtual=True)
            person = Field(OneToOne, entity=Person, primary_key=True)

        p1 = Person(pk=1, firstname='Homer', surname='Simpson')
        p2 = Person(pk=2, firstname='Bart', surname='Simpson')

        a1 = Account(homedir='/home/hsimpson', person=p1)
        a2 = Account(homedir='/home/bsimpson', person=p2)

        s.store_location(p1)
        s.store_location(p2)

        self.assertTrue(os.path.isfile(os.path.join(self.path, 'Person', '1.conf')))
        self.assertTrue(os.path.isfile(os.path.join(self.path, 'Person', '2.conf')))

        self.assertEqual(open(os.path.join(self.path, 'Person', '1.conf'), 'rU').read(),
                         '\n[Person]\n\npk: 1\nfirstname: Homer\n'
                         '\n[Account]\n\nperson: 1\n')
        self.assertEqual(open(os.path.join(self.path, 'Person', '2.conf'), 'rU').read(),
                         '\n[Person]\n\npk: 2\nfirstname: Bart\n'
                         '\n[Account]\n\nperson: 2\n')
Ejemplo n.º 2
0
    def test_store_load_entity_with_virtual_pk_default_store_location (self):
        s = Related()
        s.bind(self.path)

        class Person (Entity):
            pk = Field(Indexer, primary_key=True, autoincrement=True, virtual=True)
            firstname = Field(String)
            surname = Field(String)

        class Account (Entity):
            homedir = Field(String)
            person = Field(OneToOne, entity=Person, primary_key=True, virtual=True)

        p1 = Person(firstname='Homer', surname='Simpson')
        p2 = Person(firstname='Bart', surname='Simpson')

        a1 = Account(homedir='/home/hsimpson', person=p1)
        a2 = Account(homedir='/home/bsimpson', person=p2)

        s.store_location(p1)
        s.store_location(p2)

        self.assertTrue(os.path.isfile(os.path.join(self.path, 'Person', '0.conf')))
        self.assertTrue(os.path.isfile(os.path.join(self.path, 'Person', '1.conf')))

        self.assertEqual(open(os.path.join(self.path, 'Person', '0.conf'), 'rU').read(),
                         '\n[Person]\n\nfirstname: Homer\nsurname: Simpson\n'
                         '\n[Account]\n\nhomedir: /home/hsimpson\n')
        self.assertEqual(open(os.path.join(self.path, 'Person', '1.conf'), 'rU').read(),
                         '\n[Person]\n\nfirstname: Bart\nsurname: Simpson\n'
                         '\n[Account]\n\nhomedir: /home/bsimpson\n')

        np1 = s.load_location(Person, 0)
        np2 = s.load_location(Person, 1)

        self.assertTrue(isinstance(np1, list))
        self.assertEqual(np1[0].meta.name, 'Person')
        self.assertEqual(np1[0].pk, 0)
        self.assertEqual(np1[0].firstname, 'Homer')
        self.assertEqual(np1[0].surname, 'Simpson')
        self.assertEqual(np1[1].homedir, '/home/hsimpson')
        self.assertEqual(np1[1].person, np1[0])

        self.assertTrue(isinstance(np2, list))
        self.assertEqual(np2[0].meta.name, 'Person')
        self.assertEqual(np2[0].pk, 1)
        self.assertEqual(np2[0].firstname, 'Bart')
        self.assertEqual(np2[0].surname, 'Simpson')
        self.assertEqual(np2[1].homedir, '/home/bsimpson')
        self.assertEqual(np2[1].person, np2[0])
Ejemplo n.º 3
0
    def test_list_entity_default_store_location (self):
        s = Related()
        s.bind(self.path)

        class Person (Entity):
            pk = Field(Integer, primary_key=True)
            firstname = Field(String)
            surname = Field(String)

        class Account (Entity):
            homedir = Field(String)
            person = Field(OneToOne, entity=Person, primary_key=True)

        p1 = Person(pk=1, firstname='Homer', surname='Simpson')
        p2 = Person(pk=2, firstname='Bart', surname='Simpson')

        a1 = Account(homedir='/home/hsimpson', person=p1)

        s.store_location(p1)
        s.store_location(p2)

        self.assertTrue(os.path.isfile(os.path.join(self.path, 'Person', '1.conf')))
        self.assertTrue(os.path.isfile(os.path.join(self.path, 'Person', '2.conf')))

        self.assertEqual(s.list_location(p1),
                         os.path.join(self.path, 'Person', '1.conf'))
        self.assertEqual(s.list_location(Person, 1),
                         os.path.join(self.path, 'Person', '1.conf'))
        self.assertEqual(s.list_location(p2),
                         os.path.join(self.path, 'Person', '2.conf'))
        self.assertEqual(s.list_location(Person, 2),
                         os.path.join(self.path, 'Person', '2.conf'))
        self.assertEqual(s.list_location(Person, 3), None)

        self.assertEqual(s.list_location(a1),
                         os.path.join(self.path, 'Person', '1.conf'))
        self.assertEqual(s.list_location(Account, 1),
                         os.path.join(self.path, 'Person', '1.conf'))
        self.assertEqual(s.list_location(Account, 2), None)

        self.assertEqual(list(s.list_location(Person)),
                         [os.path.join(self.path, 'Person', '1.conf'),
                          os.path.join(self.path, 'Person', '2.conf')])

        self.assertEqual(list(s.list_location(Account)),
                         [os.path.join(self.path, 'Person', '1.conf')])
Ejemplo n.º 4
0
    def test_load_entity_default_store_location (self):
        s = Related()
        s.bind(self.path)

        class Person (Entity):
            pk = Field(Integer, primary_key=True)
            firstname = Field(String)
            surname = Field(String)

        class Account (Entity):
            homedir = Field(String)
            person = Field(OneToOne, entity=Person, primary_key=True)

        p1 = Person(pk=1, firstname='Homer', surname='Simpson')
        p2 = Person(pk=2, firstname='Bart', surname='Simpson')

        a1 = Account(homedir='/home/hsimpson', person=p1)
        a2 = Account(homedir='/home/bsimpson', person=p2)

        s.store_location(p1)
        s.store_location(p2)

        self.assertTrue(os.path.isfile(os.path.join(self.path, 'Person', '1.conf')))
        self.assertTrue(os.path.isfile(os.path.join(self.path, 'Person', '2.conf')))

        np1 = s.load_location(Person, 1)
        np2 = s.load_location(Person, 2)

        self.assertTrue(isinstance(np1, list))
        self.assertEqual(np1[0].meta.name, 'Person')
        self.assertEqual(np1[0].pk, 1)
        self.assertEqual(np1[0].firstname, 'Homer')
        self.assertEqual(np1[0].surname, 'Simpson')
        self.assertEqual(np1[1].homedir, '/home/hsimpson')
        self.assertEqual(np1[1].person, np1[0])

        self.assertTrue(isinstance(np2, list))
        self.assertEqual(np2[0].meta.name, 'Person')
        self.assertEqual(np2[0].pk, 2)
        self.assertEqual(np2[0].firstname, 'Bart')
        self.assertEqual(np2[0].surname, 'Simpson')
        self.assertEqual(np2[1].homedir, '/home/bsimpson')
        self.assertEqual(np2[1].person, np2[0])