<commit_before># -*- coding: utf-8 -*-

import unittest
import datetime
from yamlns import namespace as ns
from generationkwh.isodates import isodate
dbconfig = None
try:
    import dbconfig
    import erppeek
except ImportError:
    pass

@unittest.skipIf(not dbconfig, "depends on ERP")
class Assignment_Test(unittest.TestCase):

    def setUp(self):
        self.erp = erppeek.Client(**dbconfig.erppeek)
        self.Assignment = self.erp.GenerationkwhAssignment
        self.Assignment.dropAll()

        (
            self.member,
            self.member2,
            self.member3,
        ) = self.erp.SomenergiaSoci.search([],limit=3)
        (
            self.contract,
            self.contract2,
            self.contract3,
        ) = self.erp.GiscedataPolissa.search([], limit=3)
        self.today = str(datetime.date.today())

    def setupProvider(self,assignments=[]):
        for contract, member, priority in assignments:
            self.Assignment.create(dict(
                contract_id = contract,
                member_id = member,
                priority = priority,
                ))

    def assertAssignmentsEqual(self, expectation):
        result = self.Assignment.browse([])
        self.assertEqual( [
                (
                    r.contract_id.id,
                    r.member_id.id,
                    r.priority,
                    r.end_date,
                )
                for r in result
            ],expectation)

    def tearDown(self):
        self.Assignment.dropAll()

    def test_no_assignments(self):
        self.setupProvider()
        self.assertAssignmentsEqual([])

    def test_default_values(self):
        self.Assignment.create(dict(
            member_id = self.member,
            contract_id = self.contract,
            priority = 0,
            ))
        self.assertAssignmentsEqual([
            (self.contract, self.member, 0, False),
            ])

    def test_create_priorityRequired(self):

        with self.assertRaises(Exception) as ctx:
            self.Assignment.create(dict(
                member_id = self.member,
                contract_id = self.contract,
                ))
        self.assertRegexpMatches(
            #'null value in column "priority" violates not-null constraint',
            str(ctx.exception),
            'Integrity Error.*priority.*not.*null')

    def test_create_contractRequired(self):

        with self.assertRaises(Exception) as ctx:
            self.Assignment.create(dict(
                member_id = self.member,
                priority = 0,
                ))
        self.assertRegexpMatches(
            #'null value in column "priority" violates not-null constraint',
            str(ctx.exception),
            'Integrity Error.*contract_id.*not.*null')

    def test_create_memberRequired(self):

        with self.assertRaises(Exception) as ctx:
            self.Assignment.create(dict(
                contract_id = self.contract,
                priority = 0,
                ))
        self.assertRegexpMatches(
            #'null value in column "priority" violates not-null constraint',
            str(ctx.exception),
            'Integrity Error.*member_id.*not.*null')<commit_msg>Newer postgres version change the error message<commit_after># -*- coding: utf-8 -*-

import unittest
import datetime
from yamlns import namespace as ns
from generationkwh.isodates import isodate
dbconfig = None
try:
    import dbconfig
    import erppeek
except ImportError:
    pass

@unittest.skipIf(not dbconfig, "depends on ERP")
class Assignment_Test(unittest.TestCase):

    def setUp(self):
        self.erp = erppeek.Client(**dbconfig.erppeek)
        self.Assignment = self.erp.GenerationkwhAssignment
        self.Assignment.dropAll()

        (
            self.member,
            self.member2,
            self.member3,
        ) = self.erp.SomenergiaSoci.search([],limit=3)
        (
            self.contract,
            self.contract2,
            self.contract3,
        ) = self.erp.GiscedataPolissa.search([], limit=3)
        self.today = str(datetime.date.today())

    def setupProvider(self,assignments=[]):
        for contract, member, priority in assignments:
            self.Assignment.create(dict(
                contract_id = contract,
                member_id = member,
                priority = priority,
                ))

    def assertAssignmentsEqual(self, expectation):
        result = self.Assignment.browse([])
        self.assertEqual( [
                (
                    r.contract_id.id,
                    r.member_id.id,
                    r.priority,
                    r.end_date,
                )
                for r in result
            ],expectation)

    def tearDown(self):
        self.Assignment.dropAll()

    def test_no_assignments(self):
        self.setupProvider()
        self.assertAssignmentsEqual([])

    def test_default_values(self):
        self.Assignment.create(dict(
            member_id = self.member,
            contract_id = self.contract,
            priority = 0,
            ))
        self.assertAssignmentsEqual([
            (self.contract, self.member, 0, False),
            ])

    def test_create_priorityRequired(self):

        with self.assertRaises(Exception) as ctx:
            self.Assignment.create(dict(
                member_id = self.member,
                contract_id = self.contract,
                ))
        self.assertRegexpMatches(
            #'null value in column "priority" violates not-null constraint',
            str(ctx.exception),
            'Integrity.*Error.*priority.*not.*null')

    def test_create_contractRequired(self):

        with self.assertRaises(Exception) as ctx:
            self.Assignment.create(dict(
                member_id = self.member,
                priority = 0,
                ))
        self.assertRegexpMatches(
            #'null value in column "priority" violates not-null constraint',
            str(ctx.exception),
            'Integrity.*Error.*contract_id.*not.*null')

    def test_create_memberRequired(self):

        with self.assertRaises(Exception) as ctx:
            self.Assignment.create(dict(
                contract_id = self.contract,
                priority = 0,
                ))
        self.assertRegexpMatches(
            #'null value in column "priority" violates not-null constraint',
            str(ctx.exception),
            'Integrity.*Error.*member_id.*not.*null')
{'fraction_non_alphanumeric': 0.0963225111555624, 'fraction_numerical': 0.0033851361747961224, 'mean_word_length': 1.755404832556168, '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': '2103143', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1709, 'n_words': 346}