<commit_before>#coding: utf8
from sweet.tests import TestCase
from sweet.orm.model import Model
from sweet.orm.relations import *
import inspect


class TestRelationBasic(TestCase):

    def test_belongs_to_without_argument(self):

        class Member(Model):
            __tablename__ = 'users'

        class Phone(Model):
            __tablename__ = 'mobiles'
            belongs_to('member', Member)

        r = Phone.__relations__.get('member')
        self.assertEqual(BelongsTo, type(r))
        self.assertEqual(Phone, r.owner)
        self.assertEqual(Member, r.target)
        self.assertEqual('member_id', r.owner_fk)
        self.assertEqual('member', r.name)
        
    def test_belongs_to_with_argument(self):

        class Member(Model):
            __pk__ = 'member_id'
            __tablename__ = 'users'

        class Phone(Model):
            __tablename__ = 'mobiles'
            belongs_to('member', Member, fk='owner_id')

        r = Phone.__relations__.get('member')
        self.assertEqual(BelongsTo, type(r))
        self.assertEqual(Phone, r.owner)
        self.assertEqual(Member, r.target)
        self.assertEqual('owner_id', r.owner_fk)
        self.assertEqual('member', r.name)

    def test_has_many_without_argument(self):

        class Phone(Model):
            __tablename__ = 'mobiles'
        
        class Member(Model):
            __tablename__ = 'users'
            has_many('phones', Phone)

        r = Member.__relations__.get('phones')
        self.assertEqual(HasMany, type(r))

        self.assertEqual(Member, r.owner)
        self.assertEqual(Phone, r.target)
        self.assertEqual('member_id', r.target_fk)
        self.assertEqual('phones', r.name)

    def test_has_many_with_argument(self):

        class Phone(Model):
            __tablename__ = 'mobiles'
        
        class Member(Model):
            __tablename__ = 'users'
            has_many('phones', Phone, fk='user_id')

        r = Member.__relations__.get('phones')
        self.assertEqual(HasMany, type(r))

        self.assertEqual(Member, r.owner)
        self.assertEqual(Phone, r.target)
        self.assertEqual('user_id', r.target_fk)
        self.assertEqual('phones', r.name)

    def test_has_one_without_argument(self):

        class Phone(Model):
            __tablename__ = 'mobiles'
        
        class Member(Model):
            __tablename__ = 'users'
            has_one('phone', Phone)

        r = Member.__relations__.get('phone')
        self.assertEqual(HasOne, type(r))

        self.assertEqual(Member, r.owner)
        self.assertEqual(Phone, r.target)
        self.assertEqual('member_id', r.target_fk)
        self.assertEqual('phone', r.name)

    def test_has_one_with_argument(self):

        class Phone(Model):
            __tablename__ = 'mobiles'
        
        class Member(Model):
            __tablename__ = 'users'
            has_one('phone', Phone, fk='user_id')

        r = Member.__relations__.get('phone')
        self.assertEqual(HasOne, type(r))

        self.assertEqual(Member, r.owner)
        self.assertEqual(Phone, r.target)
        self.assertEqual('user_id', r.target_fk)
        self.assertEqual('phone', r.name)

    def test_has_and_belongs_to_many_without_argument(self):

        class Student(Model):
            pass

        class Teacher(Model):
            has_and_belongs_to_many('students', Student)

        HasAndBelongsToMany(name='teachers', target=Teacher).set_owner(Student)

        r = Teacher.__relations__.get('students')
        self.assertEqual(HasAndBelongsToMany, type(r))
        self.assertEqual(Teacher, r.owner)
        self.assertEqual(Student, r.target)
        self.assertEqual('teacher_id', r.through_fk_on_owner)
        self.assertEqual('students', r.name)
        self.assertEqual('students_teachers', r.through_table)

        r = Student.__relations__.get('teachers')
        self.assertEqual(HasAndBelongsToMany, type(r))
        self.assertEqual(Student, r.owner)
        self.assertEqual(Teacher, r.target)
        self.assertEqual('student_id', r.through_fk_on_owner)
        self.assertEqual('teachers', r.name)
        self.assertEqual('students_teachers', r.through_table)

    def test_has_and_belongs_to_many_with_argument(self):

        class StudentAndTeacher(Model):
            __tablename__ = 'student_teacher_relation'

        class Student(Model):
            pass

        class Teacher(Model):
            has_and_belongs_to_many('students', Student, through=StudentAndTeacher)

        HasAndBelongsToMany(name='teachers', target=Teacher, through=StudentAndTeacher).set_owner(Student)

        r = Teacher.__relations__.get('students')
        self.assertEqual(HasAndBelongsToMany, type(r))
        self.assertEqual(Teacher, r.owner)
        self.assertEqual(Student, r.target)
        self.assertEqual('teacher_id', r.through_fk_on_owner)
        self.assertEqual('students', r.name)
        self.assertEqual('student_teacher_relation', r.through_table)

        r = Student.__relations__.get('teachers')
        self.assertEqual(HasAndBelongsToMany, type(r))
        self.assertEqual(Student, r.owner)
        self.assertEqual(Teacher, r.target)
        self.assertEqual('student_id', r.through_fk_on_owner)
        self.assertEqual('teachers', r.name)
        self.assertEqual('student_teacher_relation', r.through_table)


if __name__ == '__main__':
    import unittest
    unittest.main()
<commit_msg>add test logic for has_and_belongs_to_many<commit_after>#coding: utf8
from sweet.tests import TestCase
from sweet.orm.model import Model
from sweet.orm.relations import *
import inspect


class TestRelationBasic(TestCase):

    def test_belongs_to_without_argument(self):

        class Member(Model):
            __tablename__ = 'users'

        class Phone(Model):
            __tablename__ = 'mobiles'
            belongs_to('member', Member)

        r = Phone.__relations__.get('member')
        self.assertEqual(BelongsTo, type(r))
        self.assertEqual(Phone, r.owner)
        self.assertEqual(Member, r.target)
        self.assertEqual('member_id', r.owner_fk)
        self.assertEqual('member', r.name)
        
    def test_belongs_to_with_argument(self):

        class Member(Model):
            __pk__ = 'member_id'
            __tablename__ = 'users'

        class Phone(Model):
            __tablename__ = 'mobiles'
            belongs_to('member', Member, fk='owner_id')

        r = Phone.__relations__.get('member')
        self.assertEqual(BelongsTo, type(r))
        self.assertEqual(Phone, r.owner)
        self.assertEqual(Member, r.target)
        self.assertEqual('owner_id', r.owner_fk)
        self.assertEqual('member', r.name)

    def test_has_many_without_argument(self):

        class Phone(Model):
            __tablename__ = 'mobiles'
        
        class Member(Model):
            __tablename__ = 'users'
            has_many('phones', Phone)

        r = Member.__relations__.get('phones')
        self.assertEqual(HasMany, type(r))

        self.assertEqual(Member, r.owner)
        self.assertEqual(Phone, r.target)
        self.assertEqual('member_id', r.target_fk)
        self.assertEqual('phones', r.name)

    def test_has_many_with_argument(self):

        class Phone(Model):
            __tablename__ = 'mobiles'
        
        class Member(Model):
            __tablename__ = 'users'
            has_many('phones', Phone, fk='user_id')

        r = Member.__relations__.get('phones')
        self.assertEqual(HasMany, type(r))

        self.assertEqual(Member, r.owner)
        self.assertEqual(Phone, r.target)
        self.assertEqual('user_id', r.target_fk)
        self.assertEqual('phones', r.name)

    def test_has_one_without_argument(self):

        class Phone(Model):
            __tablename__ = 'mobiles'
        
        class Member(Model):
            __tablename__ = 'users'
            has_one('phone', Phone)

        r = Member.__relations__.get('phone')
        self.assertEqual(HasOne, type(r))

        self.assertEqual(Member, r.owner)
        self.assertEqual(Phone, r.target)
        self.assertEqual('member_id', r.target_fk)
        self.assertEqual('phone', r.name)

    def test_has_one_with_argument(self):

        class Phone(Model):
            __tablename__ = 'mobiles'
        
        class Member(Model):
            __tablename__ = 'users'
            has_one('phone', Phone, fk='user_id')

        r = Member.__relations__.get('phone')
        self.assertEqual(HasOne, type(r))

        self.assertEqual(Member, r.owner)
        self.assertEqual(Phone, r.target)
        self.assertEqual('user_id', r.target_fk)
        self.assertEqual('phone', r.name)

    def test_has_and_belongs_to_many_without_argument(self):

        class Student(Model):
            pass

        class Teacher(Model):
            has_and_belongs_to_many('students', Student)

        HasAndBelongsToMany(name='teachers', target=Teacher).set_owner(Student)

        r = Teacher.__relations__.get('students')
        self.assertEqual(HasAndBelongsToMany, type(r))
        self.assertEqual(Teacher, r.owner)
        self.assertEqual(Student, r.target)
        self.assertEqual('teacher_id', r.through_fk_on_owner)
        self.assertEqual('students', r.name)
        self.assertEqual('students_teachers', r.through_table)
        self.assertEqual('student_id', r.through_fk_on_target)

        r = Student.__relations__.get('teachers')
        self.assertEqual(HasAndBelongsToMany, type(r))
        self.assertEqual(Student, r.owner)
        self.assertEqual(Teacher, r.target)
        self.assertEqual('student_id', r.through_fk_on_owner)
        self.assertEqual('teachers', r.name)
        self.assertEqual('students_teachers', r.through_table)
        self.assertEqual('teacher_id', r.through_fk_on_target)

    def test_has_and_belongs_to_many_with_argument(self):

        class StudentAndTeacher(Model):
            __tablename__ = 'student_teacher_relation'

        class Student(Model):
            pass

        class Teacher(Model):
            has_and_belongs_to_many('students', Student, through=StudentAndTeacher)

        HasAndBelongsToMany(name='teachers', target=Teacher, through=StudentAndTeacher).set_owner(Student)

        r = Teacher.__relations__.get('students')
        self.assertEqual(HasAndBelongsToMany, type(r))
        self.assertEqual(Teacher, r.owner)
        self.assertEqual(Student, r.target)
        self.assertEqual('teacher_id', r.through_fk_on_owner)
        self.assertEqual('students', r.name)
        self.assertEqual('student_teacher_relation', r.through_table)
        self.assertEqual('student_id', r.through_fk_on_target)

        r = Student.__relations__.get('teachers')
        self.assertEqual(HasAndBelongsToMany, type(r))
        self.assertEqual(Student, r.owner)
        self.assertEqual(Teacher, r.target)
        self.assertEqual('student_id', r.through_fk_on_owner)
        self.assertEqual('teachers', r.name)
        self.assertEqual('student_teacher_relation', r.through_table)
        self.assertEqual('teacher_id', r.through_fk_on_target)


if __name__ == '__main__':
    import unittest
    unittest.main()

{'fraction_non_alphanumeric': 0.11195428979555397, 'fraction_numerical': 0.00017855548611731096, 'mean_word_length': 2.7055904730400266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7568838', 'n_tokens_mistral': 3424, 'n_tokens_neox': 3301, 'n_words': 524}