<commit_before># -*- encoding: UTF-8 -*-

from core.ws_utils import CachedWS as ws
from cvn.models import (Articulo, Libro, Capitulo, Congreso, Proyecto,
                        Convenio, TesisDoctoral)
from core.models import UserProfile
from cvn.utils import isdigit
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings as st
from informe_pdf import InformePDF
from informe_csv import Informe_csv
from optparse import make_option
from django.core.exceptions import ObjectDoesNotExist

class Command(BaseCommand):
    help = u'Genera un PDF/CSV con los datos de un Departamento/Area'
    option_list = BaseCommand.option_list + (
        make_option(
            "-y",
            "--year",
            dest="year",
            help="Specify the year in format YYYY",
        ),
        make_option(
            "-i",
            "--id",
            dest="id",
            help="Specify the ID of the Department/Area",
        ),
        make_option(
            "-t",
            "--type",
            dest="type",
            default='d',
            help="Specify the type of filtering: d (department) or a (area)",
        ),
        make_option(
            "-f",
            "--format",
            dest="format",
            default='pdf',
            help="Specify the output format",
        ),
    )

    def handle(self, *args, **options):
        self.check_args(options)
        year = int(options['year'])
        type_id = [int(options['id'])] if type(options['id']) is str else None
        model = None
        if options['type'] == 'a':
            # model = GrupoinvestAreaconocimiento
            self.model_type = 'area'
        else:
            self.model_type = 'department'
            self.codigo = 'cod_departamento'
        if options['format'] == 'pdf':
            self.generator = InformePDF
        else:
            self.generator = Informe_csv
        self.create_reports(year, type_id, model)

    def check_args(self, options):
        if not isdigit(options['year']):
            raise CommandError(
                "Option `--year=YYYY` must exist and be a number")
        if (not isdigit(options['id'])) and options['id'] is not None:
            raise CommandError("Option `--id=X` must be a number")
        if not options['type'] == 'a' and not options['type'] == 'd':
            raise CommandError("Option `--type=X` must be a (area) "
                               "or d (department)")
        if not options['format'] == 'pdf' and not options['format'] == 'csv':
            raise CommandError("Option `--format=X` must be pdf or csv")

    def create_reports(self, year, unit_id, model):
        if unit_id is None:
            if self.model_type == 'department':
                units = ws.get(st.WS_DEPARTMENTS_YEAR % year)
                if units is None:
                    raise IOError(
                        'WS "%s" does not work' % st.WS_DEPARTMENTS_YEAR % year)
            else:
                units = model.objects.all()
        else:
            # TODO: COGER EL DEPARTAMENTO CON ESE ID
            units = unit_id
        for unit in units:
            self.create_report(year, unit)

    def create_report(self, year, unit):
        (investigadores, articulos,
         libros, capitulos_libro, congresos, proyectos,
         convenios, tesis) = self.get_data(year, unit)
        print 'Generando Informe para [%s] %s ... ' % (
            unit['unidad']['codigo'], unit['unidad']['nombre'])
        if investigadores:
            informe = self.generator(year, unit['unidad'], investigadores,
                                     articulos, libros, capitulos_libro,
                                     congresos, proyectos, convenios, tesis,
                                     self.model_type)
            informe.go()
            print 'OK\n'
        else:
            print 'ERROR: No hay Investigadores\n'

    def get_data(self, year, unit):
        investigadores, usuarios = self.get_investigadores(unit)
        articulos = Articulo.objects.byUsuariosYear(usuarios, year)
        libros = Libro.objects.byUsuariosYear(usuarios, year)
        capitulos_libro = Capitulo.objects.byUsuariosYear(usuarios, year)
        congresos = Congreso.objects.byUsuariosYear(usuarios, year)
        proyectos = Proyecto.objects.byUsuariosYear(usuarios, year)
        convenios = Convenio.objects.byUsuariosYear(usuarios, year)
        tesis = TesisDoctoral.objects.byUsuariosYear(usuarios, year)
        return (investigadores, articulos,
                libros, capitulos_libro, congresos, proyectos,
                convenios, tesis)

    def get_investigadores(self, unit):
        investigadores = list()
        usuarios = list()
        for inv in unit['miembros']:
            inv = self.check_inves(inv)
            investigadores.append(inv)
            try:
                user = UserProfile.objects.get(rrhh_code=inv['cod_persona'])
                usuarios.append(user)
            except ObjectDoesNotExist:
                pass
        return investigadores, usuarios

    def check_inves(self, inv):
        if 'cod_persona__nombre' not in inv:
            inv['cod_persona__nombre'] = ''
        if 'cod_persona__apellido1' not in inv:
            inv['cod_persona__apellido1'] = ''
        if 'cod_persona__apellido2' not in inv:
            inv['cod_persona__apellido2'] = ''
        if 'cod_cce__descripcion' not in inv:
            inv['cod_cce_descripcion'] = ''
        return inv
<commit_msg>Refactoring Department Report<commit_after># -*- encoding: UTF-8 -*-

from core.ws_utils import CachedWS as ws
from cvn.models import (Articulo, Libro, Capitulo, Congreso, Proyecto,
                        Convenio, TesisDoctoral)
from core.models import UserProfile
from cvn.utils import isdigit
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings as st
from informe_pdf import InformePDF
from informe_csv import Informe_csv
from optparse import make_option
from django.core.exceptions import ObjectDoesNotExist

class Command(BaseCommand):
    help = u'Genera un PDF/CSV con los datos de un Departamento/Area'
    option_list = BaseCommand.option_list + (
        make_option(
            "-y",
            "--year",
            dest="year",
            help="Specify the year in format YYYY",
        ),
        make_option(
            "-i",
            "--id",
            dest="id",
            help="Specify the ID of the Department/Area",
        ),
        make_option(
            "-t",
            "--type",
            dest="type",
            default='d',
            help="Specify the type of filtering: d (department) or a (area)",
        ),
        make_option(
            "-f",
            "--format",
            dest="format",
            default='pdf',
            help="Specify the output format",
        ),
    )

    def handle(self, *args, **options):
        self.check_args(options)
        year = int(options['year'])
        unit_id = [int(options['id'])] if type(options['id']) is str else None
        model = None
        if options['type'] == 'a':
            # model = GrupoinvestAreaconocimiento
            self.model_type = 'area'
        else:
            self.model_type = 'department'
            self.codigo = 'cod_departamento'
        if options['format'] == 'pdf':
            self.generator = InformePDF
        else:
            self.generator = Informe_csv
        self.create_reports(year, unit_id, model)

    def check_args(self, options):
        if not isdigit(options['year']):
            raise CommandError(
                "Option `--year=YYYY` must exist and be a number")
        if (not isdigit(options['id'])) and options['id'] is not None:
            raise CommandError("Option `--id=X` must be a number")
        if not options['type'] == 'a' and not options['type'] == 'd':
            raise CommandError("Option `--type=X` must be a (area) "
                               "or d (department)")
        if not options['format'] == 'pdf' and not options['format'] == 'csv':
            raise CommandError("Option `--format=X` must be pdf or csv")

    def create_reports(self, year, unit_id, model):
        if unit_id is None:
            if self.model_type == 'department':
                units = ws.get(st.WS_DEPARTMENTS_YEAR % year)
                if units is None:
                    raise IOError(
                        'WS "%s" does not work' % st.WS_DEPARTMENTS_YEAR % year)
            else:
                units = model.objects.all()
            for unit in units:
                self.create_report(year, unit)
        else:
            for code in unit_id:
                unit = ws.get(st.WS_DEPARTMENT_YEAR % (code, year))
                self.create_report(year, unit[0])

    def create_report(self, year, unit):
        (investigadores, articulos,
         libros, capitulos_libro, congresos, proyectos,
         convenios, tesis) = self.get_data(year, unit)
        print 'Generando Informe para [%s] %s ... ' % (
            unit['unidad']['codigo'], unit['unidad']['nombre'])
        if investigadores:
            informe = self.generator(year, unit['unidad'], investigadores,
                                     articulos, libros, capitulos_libro,
                                     congresos, proyectos, convenios, tesis,
                                     self.model_type)
            informe.go()
            print 'OK\n'
        else:
            print 'ERROR: No hay Investigadores\n'

    def get_data(self, year, unit):
        investigadores, usuarios = self.get_investigadores(unit)
        articulos = Articulo.objects.byUsuariosYear(usuarios, year)
        libros = Libro.objects.byUsuariosYear(usuarios, year)
        capitulos_libro = Capitulo.objects.byUsuariosYear(usuarios, year)
        congresos = Congreso.objects.byUsuariosYear(usuarios, year)
        proyectos = Proyecto.objects.byUsuariosYear(usuarios, year)
        convenios = Convenio.objects.byUsuariosYear(usuarios, year)
        tesis = TesisDoctoral.objects.byUsuariosYear(usuarios, year)
        return (investigadores, articulos,
                libros, capitulos_libro, congresos, proyectos,
                convenios, tesis)

    def get_investigadores(self, unit):
        investigadores = list()
        usuarios = list()
        for inv in unit['miembros']:
            inv = self.check_inves(inv)
            investigadores.append(inv)
            try:
                user = UserProfile.objects.get(rrhh_code=inv['cod_persona'])
                usuarios.append(user)
            except ObjectDoesNotExist:
                pass
        return investigadores, usuarios

    def check_inves(self, inv):
        if 'cod_persona__nombre' not in inv:
            inv['cod_persona__nombre'] = ''
        if 'cod_persona__apellido1' not in inv:
            inv['cod_persona__apellido1'] = ''
        if 'cod_persona__apellido2' not in inv:
            inv['cod_persona__apellido2'] = ''
        if 'cod_cce__descripcion' not in inv:
            inv['cod_cce_descripcion'] = ''
        return inv
<|endoftext|>


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

import unittest
from datetime import datetime

from cwharaj import settings
from cwharaj.database_factory import DatabaseFactory, CollectionTypes
from cwharaj.items import CacheItem, HistoryItem, Ad, WebsiteTypes, City, Member, Comment, Section
from cwharaj.parser.utils.section_item import SectionItem
from cwharaj.utils.crawl_utils import CrawlUtils

class MysqlDBTest(unittest.TestCase):
    def setUp(self):
        database_factory = DatabaseFactory(settings.SQL_HOST, settings.SQL_PORT,
                                           settings.SQL_USER, settings.SQL_PASSWD,
                                           settings.SQL_DB, settings.SQL_COLLECTION_NAME)

        self._cache_db = database_factory.get_database(CollectionTypes.cache)
        self._history_db = database_factory.get_database(CollectionTypes.history)
        self._item_db = database_factory.get_database(CollectionTypes.item)

        self._cache_url = "https://sa.opensooq.com/ar/search/30002057/استراحة-سديم-للايجار-اليومي-والشهري-والسنوي-حي-الأمانة-شمال-الرياض"
        self._cache_from = WebsiteTypes.opensooq.value

        self._ads_id = 70
        self._ads_item = Ad.get_default(
            section_item=SectionItem.get_default(),
            _ads_title="اسكاليد موديل 2016 بسعر جي",
            _city_id='44',
            _ads_contact="123454321",
            _ads_body="test mysql db",
            _image_link="https://img1cdn.haraj.com.sa/userfiles30/2015-07-18/55aa1ba3366cd.jpeg,https://img1cdn.haraj.com.sa/userfiles30/2015-07-18/55aa1bab49a3b.jpeg,https://img1cdn.haraj.com.sa/userfiles30/2015-07-18/55aa1bb0b6ca6.jpeg",
            _His_announcement_id=60,
            _type_ads_or=1, _close_ads=0
        )

    def test_insert_cache_row(self):
        _guid = "1234321"
        _id = CrawlUtils.url_parse_id_from_page_url(self._cache_url, 3)

        self._cache_db.open_spider()
        item = CacheItem(
            url=self._cache_url,
            guid=_guid,
            created_at=datetime.utcnow().replace(microsecond=0).isoformat(' '),
            ID=_id,
            url_from=self._cache_from
        )
        self._cache_db.insert_for_cache(item)

    def test_oldest_cache(self):
        self._cache_db.open_spider()

        # row = self._cache_db.find_oldest_for_cache()

        self._cache_db.delete_row(self._cache_url, self._cache_from)

    def test_insert_history_row(self):
        _url = "https://sa.opensooq.com/ar/search/30002057/استراحة-سديم-للايجار-اليومي-والشهري-والسنوي-حي-الأمانة-شمال-الرياض"
        _guid = "1234321"
        # _id = CrawlUtils.url_parse_id_from_page_url(_url, 3)
        _id = "123"

        self._history_db.open_spider()
        item = HistoryItem(
            url=_url,
            guid=_guid,
            created_at=datetime.utcnow().replace(microsecond=0).isoformat(' '),
            ID=_id
        )
        self._history_db.update_for_history(_id, item)

    def test_get_year_id(self):
        _years_row = {
            "id": 58,
            "text": '2014'
        }
        _years_id = self._item_db.get_year_id(_years_row['text'])
        expect = _years_row['id']
        self.assertEqual(expect, _years_id)

    def test_insert_exist_cities(self):
        _cities_row = {
            "id": 12,
            "text": "الجوف"
        }
        _city_id = self._item_db.save_city(City.get_default(_cities_row['text']))
        expect = _cities_row['id']
        self.assertEqual(expect, _city_id)

    def test_insert_new_cities(self):
        _cities_row = {
            "id": 26,
            "text": "دبي",
            "sql": "INSERT INTO cities(text) VALUES ('دبي')"
        }
        _city_id = self._item_db.save_city(City.get_default(_cities_row['text']))
        expect = _cities_row['id']
        self.assertEqual(expect, _city_id)

    def test_insert_exist_members(self):
        _memberName = "ابراهيم لطفي"
        expect = 51
        _member_id = self._item_db.save_member(Member.get_default(_memberName))
        self.assertEqual(expect, _member_id)

    def test_insert_new_members(self):
        _memberName = "djzhang"
        expect = 60
        member = Member.get_default(_memberName)

        sql = " INSERT INTO " + "members" + " (username, password, groupnumber, email, timeregister, member_code, documentingmobile, Documentingemail, phone, sendtime, active, now, Lastactivity, subscribe_1, subscribe_2, subscribe_3, The_pay_commission) VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(
            member['username'], member['password'], member['groupnumber'], member['email'], member['timeregister'],
            member['member_code'], member['documentingmobile'], member['Documentingemail'], member['phone'],
            member['sendtime'], member['active'], member['now'], member['Lastactivity'], member['subscribe_1'],
            member['subscribe_2'], member['subscribe_3'], member['The_pay_commission']
        )

        _member_id = self._item_db.save_member(member)
        self.assertEqual(expect, _member_id)

    def test_insert_exist_ads(self):
        expect = 21
        item = Ad()
        item['ads_title'] = 'جمس سوبربان  دبل  موديل 2012 اللون فضي للبيع'
        _ads_id = self._item_db.save_ad(item)
        self.assertEqual(expect, _ads_id)

    def test_insert_new_ads(self):
        expect = self._ads_id
        item = self._ads_item

        sql = " INSERT INTO ads (ads_title, ads_city, ads_tags_R, ads_tags_F, ads_tags_FF, ads_contact, ads_body, image_link, type_ads_other_final, un_model, status, fixing, Time_added, His_announcement, type_ads_or, close_ads, Last_updated_Ad, closecomment, fixed_home, fixed_tub, fixed_sec, fixed_sec2, fixed_sec3, timer_mazad) VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(
            item['ads_title'], item['ads_city'], item['ads_tags_R'], item['ads_tags_F'], item['ads_tags_FF'],
            item['ads_contact'], item['ads_body'], item['image_link'], item['type_ads_other_final'],
            item['un_model'], item['status'], item['fixing'], item['Time_added'], item['His_announcement'],
            item['type_ads_or'], item['close_ads'], item['Last_updated_Ad'], item['closecomment'],
            item['fixed_home'], item['fixed_tub'], item['fixed_sec'], item['fixed_sec2'], item['fixed_sec3'],
            item['timer_mazad']
        )

        _ads_id = self._item_db.save_ad(item)
        self.assertEqual(expect, _ads_id)

    def test_insert_exist_comments(self):
        id_ads = 1
        id_His_response = 1
        comment = Comment.get_default(id_ads, id_His_response, "up")
        expect = 8
        _comment_id = self._item_db.save_comment(comment)
        self.assertEqual(expect, _comment_id)

    def test_insert_new_comments(self):
        id_ads = 70
        id_His_response = 60
        comment = Comment.get_default(id_ads, id_His_response, "my first comment!")

        sql = " INSERT INTO comments (id_ads, id_His_response, text, Time_added_co) VALUES ('{}','{}','{}','{}')".format(
            comment['id_ads'], comment['id_His_response'], comment['text'], comment['Time_added_co']
        )
        expect = 140
        _comment_id = self._item_db.save_comment(comment)
        self.assertEqual(expect, _comment_id)

    def test_insert_exist_section(self):
        section = Section.get_default('خدمات اخرى')
        expect = 500
        section_item = self._item_db.save_section(section)
        self.assertEqual(expect, section_item['id'])

    def test_insert_new_section(self):
        section = Section.get_default('ساعات', 502)
        sql = " INSERT INTO section (name, type, Documentto, Contents, linkmodel) VALUES ('{}','{}','{}','{}','{}')".format(
            section['name'], section['type'], section['Documentto'], section['Contents'], section['linkmodel']
        )
        expect = 512
        section_item = self._item_db.save_section(section)
        self.assertEqual(expect, section_item['id'])

    def test_update_ads(self):
        expect = self._ads_id
        item = self._ads_item

        sql = " INSERT INTO ads (ads_title, ads_city, ads_tags_R, ads_tags_F, ads_tags_FF, ads_contact, ads_body, image_link, type_ads_other_final, un_model, status, fixing, Time_added, His_announcement, type_ads_or, close_ads, Last_updated_Ad, closecomment, fixed_home, fixed_tub, fixed_sec, fixed_sec2, fixed_sec3, timer_mazad) VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(
            item['ads_title'], item['ads_city'], item['ads_tags_R'], item['ads_tags_F'],
            item['ads_tags_FF'],
            item['ads_contact'], item['ads_body'], item['image_link'], item['type_ads_other_final'],
            item['un_model'], item['status'], item['fixing'], item['Time_added'],
            item['His_announcement'],
            item['type_ads_or'], item['close_ads'], item['Last_updated_Ad'], item['closecomment'],
            item['fixed_home'], item['fixed_tub'], item['fixed_sec'], item['fixed_sec2'],
            item['fixed_sec3'],
            item['timer_mazad']
        )

        _ads_id = self._item_db.save_ad(item)
        self.assertEqual(expect, _ads_id)
<commit_msg>charset="utf8mb4",<commit_after># -*- coding: utf-8 -*-

import unittest
from datetime import datetime

from cwharaj import settings
from cwharaj.database_factory import DatabaseFactory, CollectionTypes
from cwharaj.items import CacheItem, HistoryItem, Ad, WebsiteTypes, City, Member, Comment, Section
from cwharaj.parser.utils.section_item import SectionItem
from cwharaj.utils.crawl_utils import CrawlUtils

class MysqlDBTest(unittest.TestCase):
    def setUp(self):
        database_factory = DatabaseFactory(settings.SQL_HOST, settings.SQL_PORT,
                                           settings.SQL_USER, settings.SQL_PASSWD,
                                           settings.SQL_DB, settings.SQL_COLLECTION_NAME)

        self._cache_db = database_factory.get_database(CollectionTypes.cache)
        self._history_db = database_factory.get_database(CollectionTypes.history)
        self._item_db = database_factory.get_database(CollectionTypes.item)

        self._cache_url = "https://sa.opensooq.com/ar/search/30002057/استراحة-سديم-للايجار-اليومي-والشهري-والسنوي-حي-الأمانة-شمال-الرياض"
        self._cache_from = WebsiteTypes.opensooq.value

        self._ads_id = 70
        self._ads_item = Ad.get_default(
            section_item=SectionItem.get_default(),
            _ads_title="اسكاليد موديل 2016 بسعر جي",
            _city_id='44',
            _ads_contact="123454321",
            _ads_body="test mysql db",
            _image_link="https://img1cdn.haraj.com.sa/userfiles30/2015-07-18/55aa1ba3366cd.jpeg,https://img1cdn.haraj.com.sa/userfiles30/2015-07-18/55aa1bab49a3b.jpeg,https://img1cdn.haraj.com.sa/userfiles30/2015-07-18/55aa1bb0b6ca6.jpeg",
            _His_announcement_id=60,
            _type_ads_or=1, _close_ads=0
        )

    def test_insert_cache_row(self):
        _guid = "1234321"
        _id = CrawlUtils.url_parse_id_from_page_url(self._cache_url, 3)

        self._cache_db.open_spider()
        item = CacheItem(
            url=self._cache_url,
            guid=_guid,
            created_at=datetime.utcnow().replace(microsecond=0).isoformat(' '),
            ID=_id,
            url_from=self._cache_from
        )
        self._cache_db.insert_for_cache(item)

    def test_oldest_cache(self):
        self._cache_db.open_spider()

        # row = self._cache_db.find_oldest_for_cache()

        self._cache_db.delete_row(self._cache_url, self._cache_from)

    def test_insert_history_row(self):
        _url = "https://sa.opensooq.com/ar/search/30002057/استراحة-سديم-للايجار-اليومي-والشهري-والسنوي-حي-الأمانة-شمال-الرياض"
        _guid = "1234321"
        # _id = CrawlUtils.url_parse_id_from_page_url(_url, 3)
        _id = "123"

        self._history_db.open_spider()
        item = HistoryItem(
            url=_url,
            guid=_guid,
            created_at=datetime.utcnow().replace(microsecond=0).isoformat(' '),
            ID=_id
        )
        self._history_db.update_for_history(_id, item)

    def test_get_year_id(self):
        _years_row = {
            "id": 58,
            "text": '2014'
        }
        _years_id = self._item_db.get_year_id(_years_row['text'])
        expect = _years_row['id']
        self.assertEqual(expect, _years_id)

    def test_insert_exist_cities(self):
        _cities_row = {
            "id": 12,
            "text": "الجوف"
        }
        _city_id = self._item_db.save_city(City.get_default(_cities_row['text']))
        expect = _cities_row['id']
        self.assertEqual(expect, _city_id)

    def test_insert_new_cities(self):
        _cities_row = {
            "id": 26,
            "text": "دبي",
            "sql": "INSERT INTO cities(text) VALUES ('دبي')"
        }
        _city_id = self._item_db.save_city(City.get_default(_cities_row['text']))
        expect = _cities_row['id']
        self.assertEqual(expect, _city_id)

    def test_insert_exist_members(self):
        _memberName = "ابراهيم لطفي"
        expect = 51
        _member_id = self._item_db.save_member(Member.get_default(_memberName))
        self.assertEqual(expect, _member_id)

    def test_insert_new_members(self):
        _memberName = "djzhang"
        expect = 60
        member = Member.get_default(_memberName)

        sql = " INSERT INTO " + "members" + " (username, password, groupnumber, email, timeregister, member_code, documentingmobile, Documentingemail, phone, sendtime, active, now, Lastactivity, subscribe_1, subscribe_2, subscribe_3, The_pay_commission) VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(
            member['username'], member['password'], member['groupnumber'], member['email'], member['timeregister'],
            member['member_code'], member['documentingmobile'], member['Documentingemail'], member['phone'],
            member['sendtime'], member['active'], member['now'], member['Lastactivity'], member['subscribe_1'],
            member['subscribe_2'], member['subscribe_3'], member['The_pay_commission']
        )

        _member_id = self._item_db.save_member(member)
        self.assertEqual(expect, _member_id)

    def test_insert_exist_ads(self):
        expect = 21
        item = Ad()
        item['ads_title'] = 'جمس سوبربان  دبل  موديل 2012 اللون فضي للبيع'
        _ads_id = self._item_db.save_ad(item)
        self.assertEqual(expect, _ads_id)

    def test_insert_new_ads(self):
        expect = self._ads_id
        item = self._ads_item

        sql = " INSERT INTO ads (ads_title, ads_city, ads_tags_R, ads_tags_F, ads_tags_FF, ads_contact, ads_body, image_link, type_ads_other_final, un_model, status, fixing, Time_added, His_announcement, type_ads_or, close_ads, Last_updated_Ad, closecomment, fixed_home, fixed_tub, fixed_sec, fixed_sec2, fixed_sec3, timer_mazad) VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(
            item['ads_title'], item['ads_city'], item['ads_tags_R'], item['ads_tags_F'], item['ads_tags_FF'],
            item['ads_contact'], item['ads_body'], item['image_link'], item['type_ads_other_final'],
            item['un_model'], item['status'], item['fixing'], item['Time_added'], item['His_announcement'],
            item['type_ads_or'], item['close_ads'], item['Last_updated_Ad'], item['closecomment'],
            item['fixed_home'], item['fixed_tub'], item['fixed_sec'], item['fixed_sec2'], item['fixed_sec3'],
            item['timer_mazad']
        )

        _ads_id = self._item_db.save_ad(item)
        self.assertEqual(expect, _ads_id)

    def test_insert_exist_comments(self):
        id_ads = 1
        id_His_response = 1
        comment = Comment.get_default(id_ads, id_His_response, "up")
        expect = 8
        _comment_id = self._item_db.save_comment(comment)
        self.assertEqual(expect, _comment_id)

    def test_insert_new_comments(self):
        id_ads = 70
        id_His_response = 60
        comment = Comment.get_default(id_ads, id_His_response, "my first comment!")

        sql = " INSERT INTO comments (id_ads, id_His_response, text, Time_added_co) VALUES ('{}','{}','{}','{}')".format(
            comment['id_ads'], comment['id_His_response'], comment['text'], comment['Time_added_co']
        )
        expect = 140
        _comment_id = self._item_db.save_comment(comment)
        self.assertEqual(expect, _comment_id)

    def test_insert_exist_section(self):
        section = Section.get_default('خدمات اخرى')
        expect = 500
        section_item = self._item_db.save_section(section)
        self.assertEqual(expect, section_item['id'])

    def test_insert_new_section(self):
        section = Section.get_default('ساعات', 502)
        sql = " INSERT INTO section (name, type, Documentto, Contents, linkmodel) VALUES ('{}','{}','{}','{}','{}')".format(
            section['name'], section['type'], section['Documentto'], section['Contents'], section['linkmodel']
        )
        expect = 512
        section_item = self._item_db.save_section(section)
        self.assertEqual(expect, section_item['id'])

    def test_update_ads(self):
        pass
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# Tests for the dakota.plugin.hydrotrend module.
#
# Call with:
#   $ nosetests -sv
#
# Mark Piper (mark.piper@colorado.edu)

import os
import shutil
import filecmp
import tempfile
import numpy as np
from numpy.testing import assert_almost_equal
from nose.tools import raises, with_setup, assert_is, assert_true
from dakota.plugins.hydrotrend import HydroTrend, is_installed
from dakota.utils import get_configuration
from . import start_dir, data_dir

# Global variables -----------------------------------------------------

config_file = os.path.join(data_dir, 'config.yaml')
params_file = os.path.join(data_dir, 'params.in')
known_results_file = os.path.join(data_dir, 'results.out')
results_file = 'results.out'

# Fixtures -------------------------------------------------------------

def setup_module():
    """Called before any tests are performed."""
    print('\n*** ' + __name__)
    global config
    config = get_configuration(config_file)

def setup():
    """Called at start of any test using it @with_setup()"""
    global h
    h = HydroTrend()

def teardown():
    """Called at end of any test using it @with_setup()"""
    if os.path.exists(results_file):
        os.remove(results_file)
    if os.path.exists(h.input_dir):
        shutil.rmtree(h.input_dir)
    if os.path.exists(h.output_dir):
        shutil.rmtree(h.output_dir)

def teardown_module():
    """Called after all tests have completed."""
    pass

# Tests ----------------------------------------------------------------

@with_setup(setup, teardown)
def test_setup_files():
    """Tests setup_files() against the sample configuration file."""
    r = h.setup_files(config)
    assert_is(h.input_template, config['template_file'])
    assert_is(h.hypsometry_file, config['input_files'][0])
    assert_is(h.output_files, config['response_files'])
    assert_is(h.output_statistics, config['response_statistics'])

@with_setup(setup, teardown)
def test_setup_directories():
    """Tests setup_directories() against the sample configuration file."""
    r = h.setup_directories(config)
    assert_true(os.path.exists(h.input_dir))
    assert_true(os.path.exists(h.output_dir))

@with_setup(setup, teardown)
def test_call_without_setup():
    """Tests that call() fails without input/output directories."""
    if is_installed():
        r = h.call()
        assert_is(r, None)

@with_setup(setup, teardown)
def test_load():
    """Tests load() with a text file."""
    with tempfile.NamedTemporaryFile('w', delete=False) as fp:
        fp.write('h1\nh2\n0\n1\n2\n')
        output_file = fp.name
    r = h.load(output_file)
    assert_almost_equal(r, np.arange(3, dtype=float))
    os.remove(output_file)

@raises(TypeError)
@with_setup(setup, teardown)
def test_load_zero_arguments():
    """Tests load() when no argument is passed."""
    r = h.load()

@with_setup(setup, teardown)
def test_load_does_not_exist():
    """Tests load() when a nonexistent output file is defined."""
    r = h.load('vfnqeubnuen.f')
    assert_is(r, None)

@with_setup(setup, teardown)
def test_write():
    """Test the write method output versus a known results file."""
    h.output_values = [1.0, 2.0]
    h.write(params_file, results_file)
    assert_true(filecmp.cmp(known_results_file, results_file))
<commit_msg>Use current directory for testing HydroTrend.setup_directories method<commit_after>#!/usr/bin/env python
#
# Tests for the dakota.plugin.hydrotrend module.
#
# Call with:
#   $ nosetests -sv
#
# Mark Piper (mark.piper@colorado.edu)

import os
import shutil
import filecmp
import tempfile
import numpy as np
from numpy.testing import assert_almost_equal
from nose.tools import raises, with_setup, assert_is, assert_true
from dakota.plugins.hydrotrend import HydroTrend, is_installed
from dakota.utils import get_configuration
from . import start_dir, data_dir

# Global variables -----------------------------------------------------

run_dir = os.getcwd()
config_file = os.path.join(data_dir, 'config.yaml')
params_file = os.path.join(data_dir, 'params.in')
known_results_file = os.path.join(data_dir, 'results.out')
results_file = 'results.out'

# Fixtures -------------------------------------------------------------

def setup_module():
    """Called before any tests are performed."""
    print('\n*** ' + __name__)

def setup():
    """Called at start of any test using it @with_setup()"""
    global h, config
    h = HydroTrend()
    config = get_configuration(config_file)

def teardown():
    """Called at end of any test using it @with_setup()"""
    if os.path.exists(results_file):
        os.remove(results_file)
    if os.path.exists(h.input_dir):
        shutil.rmtree(h.input_dir)
    if os.path.exists(h.output_dir):
        shutil.rmtree(h.output_dir)

def teardown_module():
    """Called after all tests have completed."""
    pass

# Tests ----------------------------------------------------------------

@with_setup(setup, teardown)
def test_setup_files():
    """Tests setup_files() against the sample configuration file."""
    r = h.setup_files(config)
    assert_is(h.input_template, config['template_file'])
    assert_is(h.hypsometry_file, config['input_files'][0])
    assert_is(h.output_files, config['response_files'])
    assert_is(h.output_statistics, config['response_statistics'])

@with_setup(setup, teardown)
def test_setup_directories():
    """Tests setup_directories() against the sample configuration file."""
    config['run_directory'] = run_dir
    r = h.setup_directories(config)
    assert_true(os.path.exists(h.input_dir))
    assert_true(os.path.exists(h.output_dir))

@with_setup(setup, teardown)
def test_call_without_setup():
    """Tests that call() fails without input/output directories."""
    if is_installed():
        r = h.call()
        assert_is(r, None)

@with_setup(setup, teardown)
def test_load():
    """Tests load() with a text file."""
    with tempfile.NamedTemporaryFile('w', delete=False) as fp:
        fp.write('h1\nh2\n0\n1\n2\n')
        output_file = fp.name
    r = h.load(output_file)
    assert_almost_equal(r, np.arange(3, dtype=float))
    os.remove(output_file)

@raises(TypeError)
@with_setup(setup, teardown)
def test_load_zero_arguments():
    """Tests load() when no argument is passed."""
    r = h.load()

@with_setup(setup, teardown)
def test_load_does_not_exist():
    """Tests load() when a nonexistent output file is defined."""
    r = h.load('vfnqeubnuen.f')
    assert_is(r, None)

@with_setup(setup, teardown)
def test_write():
    """Test the write method output versus a known results file."""
    h.output_values = [1.0, 2.0]
    h.write(params_file, results_file)
    assert_true(filecmp.cmp(known_results_file, results_file))
<|endoftext|>


<commit_before>from pathlib import Path

from conjureup import events, juju
from conjureup.app_config import app
from conjureup.models.step import StepModel
from conjureup.telemetry import track_event

class BaseBootstrapController:
    msg_cb = NotImplementedError()

    def is_existing_controller(self):
        controllers = juju.get_controllers()['controllers']
        return app.provider.controller in controllers

    async def run(self):
        await app.provider.configure_tools()

        if app.is_jaas or self.is_existing_controller():
            await self.do_add_model()
        else:
            await self.do_bootstrap()

    async def do_add_model(self):
        self.emit('Creating Juju model.')
        cloud_with_region = app.provider.cloud
        if app.provider.region:
            cloud_with_region = '/'.join([app.provider.cloud,
                                          app.provider.region])
        await juju.add_model(app.provider.model,
                             app.provider.controller,
                             cloud_with_region,
                             app.provider.credential)
        self.emit('Juju model created.')
        events.Bootstrapped.set()

    async def do_bootstrap(self):
        self.emit('Bootstrapping Juju controller.')
        track_event("Juju Bootstrap", "Started", "")
        cloud_with_region = app.provider.cloud
        if app.provider.region:
            cloud_with_region = '/'.join([app.provider.cloud,
                                          app.provider.region])
        success = await juju.bootstrap(app.provider.controller,
                                       cloud_with_region,
                                       app.provider.model,
                                       credential=app.provider.credential)
        if not success:
            log_file = '{}-bootstrap.err'.format(app.provider.controller)
            log_file = Path(app.config['spell-dir']) / log_file
            err_log = log_file.read_text('utf8').splitlines()
            app.log.error("Error bootstrapping controller: "
                          "{}".format(err_log))
            app.sentry.context.merge({'extra': {'err_log': err_log[-400:]}})
            raise Exception('Unable to bootstrap (cloud type: {})'.format(
                app.provider.cloud_type))

        self.emit('Bootstrap complete.')
        track_event("Juju Bootstrap", "Done", "")

        await juju.login()  # login to the newly created (default) model
        events.Bootstrapped.set()

    def emit(self, msg):
        app.log.info(msg)
        self.msg_cb(msg)
<commit_msg>fix lint<commit_after>from pathlib import Path

from conjureup import events, juju
from conjureup.app_config import app
from conjureup.telemetry import track_event

class BaseBootstrapController:
    msg_cb = NotImplementedError()

    def is_existing_controller(self):
        controllers = juju.get_controllers()['controllers']
        return app.provider.controller in controllers

    async def run(self):
        await app.provider.configure_tools()

        if app.is_jaas or self.is_existing_controller():
            await self.do_add_model()
        else:
            await self.do_bootstrap()

    async def do_add_model(self):
        self.emit('Creating Juju model.')
        cloud_with_region = app.provider.cloud
        if app.provider.region:
            cloud_with_region = '/'.join([app.provider.cloud,
                                          app.provider.region])
        await juju.add_model(app.provider.model,
                             app.provider.controller,
                             cloud_with_region,
                             app.provider.credential)
        self.emit('Juju model created.')
        events.Bootstrapped.set()

    async def do_bootstrap(self):
        self.emit('Bootstrapping Juju controller.')
        track_event("Juju Bootstrap", "Started", "")
        cloud_with_region = app.provider.cloud
        if app.provider.region:
            cloud_with_region = '/'.join([app.provider.cloud,
                                          app.provider.region])
        success = await juju.bootstrap(app.provider.controller,
                                       cloud_with_region,
                                       app.provider.model,
                                       credential=app.provider.credential)
        if not success:
            log_file = '{}-bootstrap.err'.format(app.provider.controller)
            log_file = Path(app.config['spell-dir']) / log_file
            err_log = log_file.read_text('utf8').splitlines()
            app.log.error("Error bootstrapping controller: "
                          "{}".format(err_log))
            app.sentry.context.merge({'extra': {'err_log': err_log[-400:]}})
            raise Exception('Unable to bootstrap (cloud type: {})'.format(
                app.provider.cloud_type))

        self.emit('Bootstrap complete.')
        track_event("Juju Bootstrap", "Done", "")

        await juju.login()  # login to the newly created (default) model
        events.Bootstrapped.set()

    def emit(self, msg):
        app.log.info(msg)
        self.msg_cb(msg)
<|endoftext|>


<commit_before>#
# Created as part of the StratusLab project (http://stratuslab.eu),
# co-funded by the European Commission under the Grant Agreement
# INFSO-RI-261552."
#
# Copyright (c) 2010, SixSq Sarl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import datetime
import inspect
import os.path
import time
import unittest
import urllib2

from stratuslab.Monitor import Monitor
from stratuslab.Registrar import Registrar
from stratuslab.Runner import Runner
from stratuslab.Uploader import Uploader
from stratuslab.Exceptions import NetworkException
from stratuslab.Exceptions import ConfigurationException
from stratuslab.Exceptions import ExecutionException
from stratuslab.ConfigHolder import ConfigHolder
from stratuslab.Util import execute
from stratuslab.Util import generateSshKeyPair
from stratuslab.Util import ping
from stratuslab.Util import printError
from stratuslab.Util import sshCmd
import Util

class Testor(unittest.TestCase):

    configHolder = None
    testNames = []

    def __init__(self, methodName='dummy'):
        super(Testor, self).__init__(methodName)

        self.vmIds = []
        self.sshKey = '/tmp/id_rsa_smoke_test'
        self.sshKeyPub = self.sshKey + '.pub'
        self.testsToRun = []

        Testor.configHolder.assign(self)
        self._setEnvVars()

    def _setEnvVars(self):
        self._setSingleEnvVar('appRepoUsername', 'STRATUSLAB_REPO_USERNAME')
        self._setSingleEnvVar('appRepoPassword', 'STRATUSLAB_REPO_PASSWORD')
        self._setSingleEnvVar('appRepoUrl', 'STRATUSLAB_REPO_ADDRESS')
        self._setSingleEnvVar('username', 'STRATUSLAB_USERNAME')
        self._setSingleEnvVar('password', 'STRATUSLAB_PASSWORD')
        self._fillEndpointOption()

    def _setSingleEnvVar(self, field, env):
        if env in os.environ:
            setattr(self, field, os.environ[env])

    def _setOption(self, key, value):
        Testor.configHolder.options[key] = value

    def _fillEndpointOption(self):
        if Util.envEndpoint in os.environ:
            return
        if not self.frontendIp:
            raise ConfigurationException('Missing environment variable %s or configuration parameter frontend_ip' % Util.envEndpoint)
        os.environ[Util.envEndpoint] = self.frontendIp

    def dummy(self):
        pass

    def runTests(self):
        suite = unittest.TestSuite()
        tests = []
        if self.testNames:
            tests = self.testNames
        else:
            tests = self._extractTestMethodNames()

        for test in tests:
            suite.addTest(Testor(test))

        testResult = unittest.TextTestRunner(verbosity=2).run(suite)
        return testResult.wasSuccessful()

    def runMethod(self, method):
        return method()

    def runInstancePublicNetworkTest(self):
        '''Start new instance, ping it via public network and ssh into it, then stop it'''
        self._runInstanceTest()

    def runInstanceLocalNetworkTest(self):
        '''Start new instance, ping it via local network and ssh into it, then stop it'''
        self._runInstanceTest(True)

    def _runInstanceTest(self, withLocalNetwork=False):
        runner = self._startVm(withLocalNetwork)
        self._repeatCall(self._ping, runner)
        self._repeatCall(self._loginViaSsh, runner)
        self._stopVm(runner)

    def _prepareLog(self, logFile):
        log = open(logFile,'aw')
        log.write('\n'*3 + '=' * 60 + '\n')
        log.write(str(datetime.datetime.now()) + '\n')
        log.write('=' * 60 + '\n'*3)
        return log

    def _startVm(self, withLocalNetwork=False):
        generateSshKeyPair(self.sshKey)

        options = Runner.defaultRunOptions()
        options['username'] = self.oneUsername
        options['password'] = self.proxyOneadminPassword
        options['userKey'] = self.sshKeyPub
        options['verboseLevel'] = self.verboseLevel

        if withLocalNetwork:
            options['isLocalIp'] = True

        configHolder = ConfigHolder(options)
        image = 'http://appliances.stratuslab.org/images/base/ttylinux-9.5-i486-base/1.0/ttylinux-9.5-i486-base-1.0.img.gz'

        runner = Runner(image, configHolder)
        self.vmIds = runner.runInstance()

        for id in self.vmIds:
            vmStarted = runner.waitUntilVmRunningOrTimeout(id)
            if not vmStarted:
                printError('Failed to start VM id: %s' % id)

        return runner

    def _repeatCall(self, method, args=[]):
        numberOfRepetition = 60
        for _ in range(numberOfRepetition):
            failed = False
            try:
                if args:
                    method(args)
                else:
                    method()
            except ExecutionException:
                failed = True
                time.sleep(10)
            else:
                break

        if failed:
            printError('Failed executing method %s %s times, giving-up' % (method, numberOfRepetition), exit=False)
            raise

    def _ping(self, runner):

        for vmId in self.vmIds:
            _, ip = runner.getNetworkDetail(vmId)
            res = ping(ip)
            if not res:
                raise ExecutionException('Failed to ping %s' % ip)

    def _loginViaSsh(self, runner):

        loginCommand = 'ls /tmp'

        for vmId in self.vmIds:
            _, ip = runner.getNetworkDetail(vmId)
            res = sshCmd(loginCommand, ip, self.sshKey)
            if res:
                raise ExecutionException('Failed to SSH into machine for %s with return code %s' % (ip, res))

    def _stopVm(self, runner):
        runner.killInstances(self.vmIds)

    def applianceRepositoryTest(self):
        '''Authenticate, then upload a dummy image to the appliance repository, and remove after'''

        self._checkAttributePresent(['appRepoUsername', 'appRepoPassword'])
        self._testRepoConnection()
        self._uploadAndDeleteDummyImage()

    def _checkAttributePresent(self, attrs):
        for attr in attrs:
            if attr not in self.__dict__:
                raise Exception('Missing attribute %s. Missing an option argument?' % attr)

    def _testRepoConnection(self):
        passwordMgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
        passwordMgr.add_password(None,
                                 self.appRepoUrl,
                                 self.appRepoUsername,
                                 self.appRepoPassword)

        handler = urllib2.HTTPBasicAuthHandler(passwordMgr)
        opener = urllib2.build_opener(handler)

        try:
            opener.open(self.appRepoUrl)
        except RuntimeError:
            raise NetworkException('Authentication to appliance repository failed')

    def _uploadAndDeleteDummyImage(self):
        dummyFile = '/tmp/stratus-dummy.img'
        self._generateDummyImage(dummyFile)

        manifest = ''
        options = self.configHolder.options.copy()
        options['repoUsername'] = self.appRepoUsername
        options['repoPassword'] = self.appRepoPassword
        options['repoAddress'] = self.appRepoUrl
        options['uploadOption'] = ''
        uploader = Uploader(manifest, options)
        uploader.uploadFile(dummyFile, os.path.join('base',os.path.basename(dummyFile)))
        uploader.deleteFile(uploader.uploadedFile[-1])

    def _openDevNull(self):
        return open('/dev/null', 'w')

    def _generateDummyImage(self, filename, size=2):
        devNull = open('/dev/null', 'w')
        execute(['dd', 'if=/dev/zero', 'of=%s' % filename, 'bs=1000000', 'count=%s' % size],
        stdout=devNull, stderr=devNull)
        devNull.close()

    def registrarTest(self):
        '''Register a new node with ONE server, check that it is properly registered and remove it'''
        configHolder = self.configHolder.copy()
        configHolder.options['infoDriver'] = 'kvm'
        configHolder.options['virtDriver'] = 'kvm'
        configHolder.options['transfertDriver'] = 'nfs'
        configHolder.options['username'] = self.oneUsername
        configHolder.options['password'] = self.proxyOneadminPassword
        registrar = Registrar(configHolder)
        hostname = 'registrar.ip.test'
        id = registrar.register([hostname])
        monitor = Monitor(configHolder)
        info = monitor.nodeDetail([id])[0]
        self.assertEqual(hostname, info.name)
        registrar.deregister(hostname)
        self.assertRaises(Exception, monitor.nodeDetail,[id])

    def listAvalableTests(self):
        print 'Available tests:'
        for testName, testDoc in self._extractTestDescriptions():
            print '    - %s: %s' % (testName, testDoc)

    def _extractTestDescriptions(self):
        methods = []
        for attrib in self.__class__.__dict__:
            if self._isTestMethod(attrib):
                methods.append((attrib,self.__class__.__dict__[attrib].__doc__))
        return methods

    def _extractTestMethodNames(self):
        methods = []
        for attrib in self.__class__.__dict__:
            if self._isTestMethod(attrib):
                methods.append(attrib)
        return methods

    def _isTestMethod(self, attrib):
        return inspect.ismethod(getattr(self, attrib)) and \
               (attrib.lower().startswith('test') or attrib.lower().endswith('test')) and \
               not attrib.startswith('_')

<commit_msg>Minor change to test deb local install<commit_after>#
# Created as part of the StratusLab project (http://stratuslab.eu),
# co-funded by the European Commission under the Grant Agreement
# INFSO-RI-261552."
#
# Copyright (c) 2010, SixSq Sarl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import datetime
import inspect
import os.path
import time
import unittest
import urllib2

from stratuslab.Monitor import Monitor
from stratuslab.Registrar import Registrar
from stratuslab.Runner import Runner
from stratuslab.Uploader import Uploader
from stratuslab.Exceptions import NetworkException
from stratuslab.Exceptions import ConfigurationException
from stratuslab.Exceptions import ExecutionException
from stratuslab.ConfigHolder import ConfigHolder
from stratuslab.Util import execute
from stratuslab.Util import generateSshKeyPair
from stratuslab.Util import ping
from stratuslab.Util import printError
from stratuslab.Util import sshCmd
import Util

class Testor(unittest.TestCase):

    configHolder = None
    testNames = []

    def __init__(self, methodName='dummy'):
        super(Testor, self).__init__(methodName)

        self.vmIds = []
        self.sshKey = '/tmp/id_rsa_smoke_test'
        self.sshKeyPub = self.sshKey + '.pub'
        self.testsToRun = []

        Testor.configHolder.assign(self)
        self._setEnvVars()

    def _setEnvVars(self):
        self._setSingleEnvVar('appRepoUsername', 'STRATUSLAB_REPO_USERNAME')
        self._setSingleEnvVar('appRepoPassword', 'STRATUSLAB_REPO_PASSWORD')
        self._setSingleEnvVar('appRepoUrl', 'STRATUSLAB_REPO_ADDRESS')
        self._setSingleEnvVar('username', 'STRATUSLAB_USERNAME')
        self._setSingleEnvVar('password', 'STRATUSLAB_PASSWORD')
        self._fillEndpointOption()

    def _setSingleEnvVar(self, field, env):
        if env in os.environ:
            setattr(self, field, os.environ[env])

    def _setOption(self, key, value):
        Testor.configHolder.options[key] = value

    def _fillEndpointOption(self):
        if Util.envEndpoint in os.environ:
            return
        if not self.frontendIp:
            raise ConfigurationException('Missing environment variable %s or configuration parameter frontend_ip' % Util.envEndpoint)
        os.environ[Util.envEndpoint] = self.frontendIp

    def dummy(self):
        pass

    def runTests(self):
        suite = unittest.TestSuite()
        tests = []
        if self.testNames:
            tests = self.testNames
        else:
            tests = self._extractTestMethodNames()

        for test in tests:
            suite.addTest(Testor(test))

        testResult = unittest.TextTestRunner(verbosity=2).run(suite)
        return testResult.wasSuccessful()

    def runMethod(self, method):
        return method()

    def runInstancePublicNetworkTest(self):
        '''Start new instance, ping it via public network and ssh into it, then stop it.'''
        self._runInstanceTest()

    def runInstanceLocalNetworkTest(self):
        '''Start new instance, ping it via local network and ssh into it, then stop it.'''
        self._runInstanceTest(True)

    def _runInstanceTest(self, withLocalNetwork=False):
        runner = self._startVm(withLocalNetwork)
        self._repeatCall(self._ping, runner)
        self._repeatCall(self._loginViaSsh, runner)
        self._stopVm(runner)

    def _prepareLog(self, logFile):
        log = open(logFile,'aw')
        log.write('\n'*3 + '=' * 60 + '\n')
        log.write(str(datetime.datetime.now()) + '\n')
        log.write('=' * 60 + '\n'*3)
        return log

    def _startVm(self, withLocalNetwork=False):
        generateSshKeyPair(self.sshKey)

        options = Runner.defaultRunOptions()
        options['username'] = self.oneUsername
        options['password'] = self.proxyOneadminPassword
        options['userKey'] = self.sshKeyPub
        options['verboseLevel'] = self.verboseLevel

        if withLocalNetwork:
            options['isLocalIp'] = True

        configHolder = ConfigHolder(options)
        image = 'http://appliances.stratuslab.org/images/base/ttylinux-9.5-i486-base/1.0/ttylinux-9.5-i486-base-1.0.img.gz'

        runner = Runner(image, configHolder)
        self.vmIds = runner.runInstance()

        for id in self.vmIds:
            vmStarted = runner.waitUntilVmRunningOrTimeout(id)
            if not vmStarted:
                printError('Failed to start VM id: %s' % id)

        return runner

    def _repeatCall(self, method, args=[]):
        numberOfRepetition = 60
        for _ in range(numberOfRepetition):
            failed = False
            try:
                if args:
                    method(args)
                else:
                    method()
            except ExecutionException:
                failed = True
                time.sleep(10)
            else:
                break

        if failed:
            printError('Failed executing method %s %s times, giving-up' % (method, numberOfRepetition), exit=False)
            raise

    def _ping(self, runner):

        for vmId in self.vmIds:
            _, ip = runner.getNetworkDetail(vmId)
            res = ping(ip)
            if not res:
                raise ExecutionException('Failed to ping %s' % ip)

    def _loginViaSsh(self, runner):

        loginCommand = 'ls /tmp'

        for vmId in self.vmIds:
            _, ip = runner.getNetworkDetail(vmId)
            res = sshCmd(loginCommand, ip, self.sshKey)
            if res:
                raise ExecutionException('Failed to SSH into machine for %s with return code %s' % (ip, res))

    def _stopVm(self, runner):
        runner.killInstances(self.vmIds)

    def applianceRepositoryTest(self):
        '''Authenticate, then upload a dummy image to the appliance repository, and remove after'''

        self._checkAttributePresent(['appRepoUsername', 'appRepoPassword'])
        self._testRepoConnection()
        self._uploadAndDeleteDummyImage()

    def _checkAttributePresent(self, attrs):
        for attr in attrs:
            if attr not in self.__dict__:
                raise Exception('Missing attribute %s. Missing an option argument?' % attr)

    def _testRepoConnection(self):
        passwordMgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
        passwordMgr.add_password(None,
                                 self.appRepoUrl,
                                 self.appRepoUsername,
                                 self.appRepoPassword)

        handler = urllib2.HTTPBasicAuthHandler(passwordMgr)
        opener = urllib2.build_opener(handler)

        try:
            opener.open(self.appRepoUrl)
        except RuntimeError:
            raise NetworkException('Authentication to appliance repository failed')

    def _uploadAndDeleteDummyImage(self):
        dummyFile = '/tmp/stratus-dummy.img'
        self._generateDummyImage(dummyFile)

        manifest = ''
        options = self.configHolder.options.copy()
        options['repoUsername'] = self.appRepoUsername
        options['repoPassword'] = self.appRepoPassword
        options['repoAddress'] = self.appRepoUrl
        options['uploadOption'] = ''
        uploader = Uploader(manifest, options)
        uploader.uploadFile(dummyFile, os.path.join('base',os.path.basename(dummyFile)))
        uploader.deleteFile(uploader.uploadedFile[-1])

    def _openDevNull(self):
        return open('/dev/null', 'w')

    def _generateDummyImage(self, filename, size=2):
        devNull = open('/dev/null', 'w')
        execute(['dd', 'if=/dev/zero', 'of=%s' % filename, 'bs=1000000', 'count=%s' % size],
        stdout=devNull, stderr=devNull)
        devNull.close()

    def registrarTest(self):
        '''Register a new node with ONE server, check that it is properly registered and remove it'''
        configHolder = self.configHolder.copy()
        configHolder.options['infoDriver'] = 'kvm'
        configHolder.options['virtDriver'] = 'kvm'
        configHolder.options['transfertDriver'] = 'nfs'
        configHolder.options['username'] = self.oneUsername
        configHolder.options['password'] = self.proxyOneadminPassword
        registrar = Registrar(configHolder)
        hostname = 'registrar.ip.test'
        id = registrar.register([hostname])
        monitor = Monitor(configHolder)
        info = monitor.nodeDetail([id])[0]
        self.assertEqual(hostname, info.name)
        registrar.deregister(hostname)
        self.assertRaises(Exception, monitor.nodeDetail,[id])

    def listAvalableTests(self):
        print 'Available tests:'
        for testName, testDoc in self._extractTestDescriptions():
            print '    - %s: %s' % (testName, testDoc)

    def _extractTestDescriptions(self):
        methods = []
        for attrib in self.__class__.__dict__:
            if self._isTestMethod(attrib):
                methods.append((attrib,self.__class__.__dict__[attrib].__doc__))
        return methods

    def _extractTestMethodNames(self):
        methods = []
        for attrib in self.__class__.__dict__:
            if self._isTestMethod(attrib):
                methods.append(attrib)
        return methods

    def _isTestMethod(self, attrib):
        return inspect.ismethod(getattr(self, attrib)) and \
               (attrib.lower().startswith('test') or attrib.lower().endswith('test')) and \
               not attrib.startswith('_')

<|endoftext|>


<commit_before>from collections import defaultdict
from xml.etree import ElementTree
from corehq.apps.fixtures.models import FixtureDataItem, FixtureDataType
from corehq.apps.users.models import CommCareUser
from corehq.apps.products.fixtures import product_fixture_generator_json
from corehq.apps.programs.fixtures import program_fixture_generator_json

def item_lists_by_domain(domain):
    ret = list()
    for data_type in FixtureDataType.by_domain(domain):
        structure = {
            f.field_name: {
                'name': f.field_name,
                'no_option': True
            } for f in data_type.fields
        }

        for attr in data_type.item_attributes:
            structure['@' + attr] = {
                'name': attr,
                'no_option': True
            }

        uri = 'jr://fixture/%s:%s' % (ItemListsProvider.id, data_type.tag)
        ret.append({
            'id': data_type.tag,
            'uri': uri,
            'path': u"/{tag}_list/{tag}".format(tag=data_type.tag),
            'name': data_type.tag,
            'structure': structure,

            # DEPRECATED PROPERTIES
            'sourceUri': uri,
            'defaultId': data_type.tag,
            'initialQuery': u"instance('{tag}')/{tag}_list/{tag}".format(tag=data_type.tag),
        })

    products = product_fixture_generator_json(domain)
    if products:
        ret.append(products)
    programs = program_fixture_generator_json(domain)
    if programs:
        ret.append(programs)
    return ret

class ItemListsProvider(object):
    id = 'item-list'

    def __call__(self, user, version, last_sync=None, app=None):
        assert isinstance(user, CommCareUser)

        all_types = dict([(t._id, t) for t in FixtureDataType.by_domain(user.domain)])
        global_types = dict([(id, t) for id, t in all_types.items() if t.is_global])

        items_by_type = defaultdict(list)

        def _set_cached_type(item, data_type):
            # set the cached version used by the object so that it doesn't
            # have to do another db trip later
            item._data_type = data_type

        for global_fixture in global_types.values():
            items = list(FixtureDataItem.by_data_type(user.domain, global_fixture))
            _ = [_set_cached_type(item, global_fixture) for item in items]
            items_by_type[global_fixture._id] = items

        other_items = FixtureDataItem.by_user(user)
        data_types = {}

        for item in other_items:
            if item.data_type_id in global_types:
                continue  # was part of the global type so no need to add here
            if item.data_type_id not in data_types:
                try:
                    data_types[item.data_type_id] = all_types[item.data_type_id]
                except (AttributeError, KeyError):
                    continue
            items_by_type[item.data_type_id].append(item)
            _set_cached_type(item, data_types[item.data_type_id])

        fixtures = []
        all_types = data_types.values() + global_types.values()
        for data_type in all_types:
            xFixture = ElementTree.Element('fixture', attrib={'id': ':'.join((self.id, data_type.tag)),
                                                              'user_id': user.user_id})
            xItemList = ElementTree.Element('%s_list' % data_type.tag)
            xFixture.append(xItemList)
            for item in sorted(items_by_type[data_type.get_id], key=lambda x: x.sort_key):
                xItemList.append(item.to_xml())
            fixtures.append(xFixture)
        return fixtures

item_lists = ItemListsProvider()
<commit_msg>Don't change value of all_types<commit_after>from collections import defaultdict
from xml.etree import ElementTree
from corehq.apps.fixtures.models import FixtureDataItem, FixtureDataType
from corehq.apps.users.models import CommCareUser
from corehq.apps.products.fixtures import product_fixture_generator_json
from corehq.apps.programs.fixtures import program_fixture_generator_json

def item_lists_by_domain(domain):
    ret = list()
    for data_type in FixtureDataType.by_domain(domain):
        structure = {
            f.field_name: {
                'name': f.field_name,
                'no_option': True
            } for f in data_type.fields
        }

        for attr in data_type.item_attributes:
            structure['@' + attr] = {
                'name': attr,
                'no_option': True
            }

        uri = 'jr://fixture/%s:%s' % (ItemListsProvider.id, data_type.tag)
        ret.append({
            'id': data_type.tag,
            'uri': uri,
            'path': u"/{tag}_list/{tag}".format(tag=data_type.tag),
            'name': data_type.tag,
            'structure': structure,

            # DEPRECATED PROPERTIES
            'sourceUri': uri,
            'defaultId': data_type.tag,
            'initialQuery': u"instance('{tag}')/{tag}_list/{tag}".format(tag=data_type.tag),
        })

    products = product_fixture_generator_json(domain)
    if products:
        ret.append(products)
    programs = program_fixture_generator_json(domain)
    if programs:
        ret.append(programs)
    return ret

class ItemListsProvider(object):
    id = 'item-list'

    def __call__(self, user, version, last_sync=None, app=None):
        assert isinstance(user, CommCareUser)

        all_types = dict([(t._id, t) for t in FixtureDataType.by_domain(user.domain)])
        global_types = dict([(id, t) for id, t in all_types.items() if t.is_global])

        items_by_type = defaultdict(list)

        def _set_cached_type(item, data_type):
            # set the cached version used by the object so that it doesn't
            # have to do another db trip later
            item._data_type = data_type

        for global_fixture in global_types.values():
            items = list(FixtureDataItem.by_data_type(user.domain, global_fixture))
            _ = [_set_cached_type(item, global_fixture) for item in items]
            items_by_type[global_fixture._id] = items

        other_items = FixtureDataItem.by_user(user)
        data_types = {}

        for item in other_items:
            if item.data_type_id in global_types:
                continue  # was part of the global type so no need to add here
            if item.data_type_id not in data_types:
                try:
                    data_types[item.data_type_id] = all_types[item.data_type_id]
                except (AttributeError, KeyError):
                    continue
            items_by_type[item.data_type_id].append(item)
            _set_cached_type(item, data_types[item.data_type_id])

        fixtures = []
        all_types_to_sync = data_types.values() + global_types.values()
        for data_type in all_types_to_sync:
            xFixture = ElementTree.Element('fixture', attrib={'id': ':'.join((self.id, data_type.tag)),
                                                              'user_id': user.user_id})
            xItemList = ElementTree.Element('%s_list' % data_type.tag)
            xFixture.append(xItemList)
            for item in sorted(items_by_type[data_type.get_id], key=lambda x: x.sort_key):
                xItemList.append(item.to_xml())
            fixtures.append(xFixture)
        return fixtures

item_lists = ItemListsProvider()
<|endoftext|>


<commit_before>import pprint
import tempfile
import pytest
from counterpartylib.test import conftest  # this is require near the top to do setup of the test suite
from counterpartylib.test import util_test
from counterpartylib.test.util_test import CURR_DIR
from counterpartylib.test.fixtures.params import DP, ADDR

from counterpartylib.lib import util

FIXTURE_SQL_FILE = CURR_DIR + '/fixtures/scenarios/unittest_fixture.sql'
FIXTURE_DB = tempfile.gettempdir() + '/fixtures.unittest_fixture.db'

@pytest.mark.usefixtures("server_db")
@pytest.mark.usefixtures("api_server")
def test_alice_bob(server_db):
    alice = ADDR[0]
    bob = "miJqNkHhC5xsB61gsiSWXeTLnEGSQnWbXB"

    # check alices UTXOs
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == "ae241be7be83ebb14902757ad94854f787d9730fc553d6f695346c9375c0d8c1"
    assert utxos[0]['amount'] == 1.9990914
    assert utxos[0]['confirmations'] == 74

    # balance before send
    alice_balance = util.get_balance(server_db, alice, 'XCP')
    bob_balance = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance == 91875000000
    assert bob_balance == 0

    # create send
    v = int(100 * 1e8)
    send1hex = util.api('create_send', {'source': alice, 'destination': bob, 'asset': 'XCP', 'quantity': v})
    assert send1hex == "0100000001c1d8c075936c3495f6d653c50f73d987f75448d97a750249b1eb83bee71b24ae000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788acffffffff0336150000000000001976a9141e9d9c2c34d4dda3cd71603d9ce1e447c3cc5c0588ac00000000000000001e6a1c8a5dda15fb6f05628a061e67576e926dc71a7fa2f0cceb951120a9322f30ea0b000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788ac00000000"

    # insert send, this automatically also creates a block
    tx1hash, tx1 = util_test.insert_raw_transaction(send1hex, server_db)

    # balances after send
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance - v
    assert bob_balance2 == bob_balance + v

    # check API result
    result = util.api("get_balances", {"filters": [
        {'field': 'address', 'op': '==', 'value': alice},
        {'field': 'asset', 'op': '==', 'value': 'XCP'},
    ]})

    assert result[0]['quantity'] == alice_balance2

    # -- do another TX

    # check alices UTXOs
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx1['tx_hash']
    assert utxos[0]['amount'] == 1.99897135
    assert utxos[0]['confirmations'] == 1

    # balances before send
    alice_balance = util.get_balance(server_db, alice, 'XCP')
    bob_balance = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance == alice_balance2
    assert bob_balance == bob_balance2

    # create send
    v = int(100 * 1e8)
    send2hex = util.api('create_send', {'source': alice, 'destination': bob, 'asset': 'XCP', 'quantity': v})
    assert send2hex == "0100000001cd2d431037d1d0cfe05daeb1d08b975f27488e383f7f169e09d2f405fb618f39020000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788acffffffff0336150000000000001976a9141e9d9c2c34d4dda3cd71603d9ce1e447c3cc5c0588ac00000000000000001e6a1c8a5dda15fb6f05628a061e67576e926dc71a7fa2f0cceb951120a9324a01ea0b000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788ac00000000"

    # insert send, this automatically also creates a block
    tx2hash, tx2 = util_test.insert_raw_transaction(send2hex, server_db)

    # balances after send
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance - v
    assert bob_balance2 == bob_balance + v

    # -- do another TX, now unconfirmed

    # check alices UTXOs
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx2['tx_hash']
    assert utxos[0]['amount'] == 1.9988513
    assert utxos[0]['confirmations'] == 1

    # balances before send
    alice_balance = util.get_balance(server_db, alice, 'XCP')
    bob_balance = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance == alice_balance2
    assert bob_balance == bob_balance2

    # create send
    v = int(100 * 1e8)
    send3hex = util.api('create_send', {'source': alice, 'destination': bob, 'asset': 'XCP', 'quantity': v})
    assert send3hex == "01000000019aea7b78c8fffa50c51bbadb87824a202b3e6b53727e543e9c6846845205b5ce020000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788acffffffff0336150000000000001976a9141e9d9c2c34d4dda3cd71603d9ce1e447c3cc5c0588ac00000000000000001e6a1c8a5dda15fb6f05628a061e67576e926dc71a7fa2f0cceb951120a93265d2e90b000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788ac00000000"

    # insert send, as unconfirmed! won't create a block!
    tx3 = util_test.insert_unconfirmed_raw_transaction(send3hex, server_db)

    # balances after send, unaffected
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance
    assert bob_balance2 == bob_balance

    # no confirmed UTXOs left, we use the 1 we had
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 0

    # unconfirmed UTXO is there, we can use it!
    utxos = util.api('get_unspent_txouts', {"address": alice, "unconfirmed": True})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx3['tx_hash']
    assert utxos[0]['amount'] == 1.99873125
    assert utxos[0]['confirmations'] == 0

    # atm there's no way to confirm this unconfirmed TX
    # even doing this won't make it confirmed because it just mocks an empty block
    util_test.create_next_block(server_db)

    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx3['tx_hash']
    assert utxos[0]['amount'] == 1.99873125
    assert utxos[0]['confirmations'] == 1

    # we can eventually make this mocking better to be able to do that,
    # but for now you'll have to micro manage if you want to confirm a unconfirmed TX in 1 test
    # by just calling the `insert_raw_transaction` again for it
    tx3bhash, tx3b = util_test.insert_raw_transaction(send3hex, server_db)

    # balances after send
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance - v
    assert bob_balance2 == bob_balance + v
<commit_msg>fix complex_unit_test.py<commit_after>import pprint
import tempfile
import pytest

from counterpartylib.test import conftest  # this is require near the top to do setup of the test suite
from counterpartylib.test import util_test
from counterpartylib.test.util_test import CURR_DIR
from counterpartylib.test.fixtures.params import DP, ADDR

from counterpartylib.lib import util

FIXTURE_SQL_FILE = CURR_DIR + '/fixtures/scenarios/unittest_fixture.sql'
FIXTURE_DB = tempfile.gettempdir() + '/fixtures.unittest_fixture.db'

@pytest.mark.usefixtures("server_db")
@pytest.mark.usefixtures("api_server")
def test_alice_bob(server_db):
    alice = ADDR[0]
    bob = "miJqNkHhC5xsB61gsiSWXeTLnEGSQnWbXB"

    # check alices UTXOs
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == "ae241be7be83ebb14902757ad94854f787d9730fc553d6f695346c9375c0d8c1"
    assert utxos[0]['amount'] == 1.9990914
    assert utxos[0]['confirmations'] == 74

    # balance before send
    alice_balance = util.get_balance(server_db, alice, 'XCP')
    bob_balance = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance == 91875000000
    assert bob_balance == 0

    # create send
    v = int(100 * 1e8)
    send1hex = util.api('create_send', {'source': alice, 'destination': bob, 'asset': 'XCP', 'quantity': v, 'regular_dust_size': 5430})
    assert send1hex == "0100000001c1d8c075936c3495f6d653c50f73d987f75448d97a750249b1eb83bee71b24ae000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788acffffffff0336150000000000001976a9141e9d9c2c34d4dda3cd71603d9ce1e447c3cc5c0588ac00000000000000001e6a1c8a5dda15fb6f05628a061e67576e926dc71a7fa2f0cceb951120a9322f30ea0b000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788ac00000000"

    # insert send, this automatically also creates a block
    tx1hash, tx1 = util_test.insert_raw_transaction(send1hex, server_db)

    # balances after send
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance - v
    assert bob_balance2 == bob_balance + v

    # check API result
    result = util.api("get_balances", {"filters": [
        {'field': 'address', 'op': '==', 'value': alice},
        {'field': 'asset', 'op': '==', 'value': 'XCP'},
    ]})

    assert result[0]['quantity'] == alice_balance2

    # -- do another TX

    # check alices UTXOs
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx1['tx_hash']
    assert utxos[0]['amount'] == 1.99897135
    assert utxos[0]['confirmations'] == 1

    # balances before send
    alice_balance = util.get_balance(server_db, alice, 'XCP')
    bob_balance = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance == alice_balance2
    assert bob_balance == bob_balance2

    # create send
    v = int(100 * 1e8)
    send2hex = util.api('create_send', {'source': alice, 'destination': bob, 'asset': 'XCP', 'quantity': v, 'regular_dust_size': 5430})
    assert send2hex == "0100000001cd2d431037d1d0cfe05daeb1d08b975f27488e383f7f169e09d2f405fb618f39020000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788acffffffff0336150000000000001976a9141e9d9c2c34d4dda3cd71603d9ce1e447c3cc5c0588ac00000000000000001e6a1c8a5dda15fb6f05628a061e67576e926dc71a7fa2f0cceb951120a9324a01ea0b000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788ac00000000"

    # insert send, this automatically also creates a block
    tx2hash, tx2 = util_test.insert_raw_transaction(send2hex, server_db)

    # balances after send
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance - v
    assert bob_balance2 == bob_balance + v

    # -- do another TX, now unconfirmed

    # check alices UTXOs
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx2['tx_hash']
    assert utxos[0]['amount'] == 1.9988513
    assert utxos[0]['confirmations'] == 1

    # balances before send
    alice_balance = util.get_balance(server_db, alice, 'XCP')
    bob_balance = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance == alice_balance2
    assert bob_balance == bob_balance2

    # create send
    v = int(100 * 1e8)
    send3hex = util.api('create_send', {'source': alice, 'destination': bob, 'asset': 'XCP', 'quantity': v, 'regular_dust_size': 5430})
    assert send3hex == "01000000019aea7b78c8fffa50c51bbadb87824a202b3e6b53727e543e9c6846845205b5ce020000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788acffffffff0336150000000000001976a9141e9d9c2c34d4dda3cd71603d9ce1e447c3cc5c0588ac00000000000000001e6a1c8a5dda15fb6f05628a061e67576e926dc71a7fa2f0cceb951120a93265d2e90b000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788ac00000000"

    # insert send, as unconfirmed! won't create a block!
    tx3 = util_test.insert_unconfirmed_raw_transaction(send3hex, server_db)

    # balances after send, unaffected
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance
    assert bob_balance2 == bob_balance

    # no confirmed UTXOs left, we use the 1 we had
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 0

    # unconfirmed UTXO is there, we can use it!
    utxos = util.api('get_unspent_txouts', {"address": alice, "unconfirmed": True})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx3['tx_hash']
    assert utxos[0]['amount'] == 1.99873125
    assert utxos[0]['confirmations'] == 0

    # atm there's no way to confirm this unconfirmed TX
    # even doing this won't make it confirmed because it just mocks an empty block
    util_test.create_next_block(server_db)

    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx3['tx_hash']
    assert utxos[0]['amount'] == 1.99873125
    assert utxos[0]['confirmations'] == 1

    # we can eventually make this mocking better to be able to do that,
    # but for now you'll have to micro manage if you want to confirm a unconfirmed TX in 1 test
    # by just calling the `insert_raw_transaction` again for it
    tx3bhash, tx3b = util_test.insert_raw_transaction(send3hex, server_db)

    # balances after send
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance - v
    assert bob_balance2 == bob_balance + v
<|endoftext|>


<commit_before>#####################################################################################
#
#  Copyright (C) Tavendo GmbH
#
#  Unless a separate license agreement exists between you and Tavendo GmbH (e.g. you
#  have purchased a commercial license), the license terms below apply.
#
#  Should you enter into a separate license agreement after having received a copy of
#  this software, then the terms of such license agreement replace the terms below at
#  the time at which such license agreement becomes effective.
#
#  In case a separate license agreement ends, and such agreement ends without being
#  replaced by another separate license agreement, the license terms below apply
#  from the time at which said agreement ends.
#
#  LICENSE TERMS
#
#  This program is free software: you can redistribute it and/or modify it under the
#  terms of the GNU Affero General Public License, version 3, as published by the
#  Free Software Foundation. This program is distributed in the hope that it will be
#  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
#  See the GNU Affero General Public License Version 3 for more details.
#
#  You should have received a copy of the GNU Affero General Public license along
#  with this program. If not, see <http://www.gnu.org/licenses/agpl-3.0.en.html>.
#
#####################################################################################

from __future__ import absolute_import

from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks

from crossbar.adapter.rest import PusherResource
from crossbar.adapter.rest.test import MockPusherSession, testResource

pushBody = '{"topic": "com.test.messages", "args": [1]}'

class RequestBodyTestCase(TestCase):
    """
    Unit tests for the validation parts of L{_CommonResource}.
    """
    @inlineCallbacks
    def test_bad_content_type(self):
        """
        An incorrect content type will mean the request is rejected.
        """
        session = MockPusherSession(self)
        resource = PusherResource({}, session)

        request = yield testResource(
            resource, "/", method="POST",
            headers={"Content-Type": ["application/text"]},
            body=pushBody)

        self.assertEqual(request.code, 400)
        self.assertIn("bad or missing content type ('application/text')",
                      request.getWrittenData())

    @inlineCallbacks
    def test_bad_method(self):
        """
        An incorrect method will mean the request is rejected.
        """
        session = MockPusherSession(self)
        resource = PusherResource({}, session)

        request = yield testResource(
            resource, "/", method="PUT",
            headers={"Content-Type": ["application/jsn"]},
            body=pushBody)

        self.assertEqual(request.code, 405)
        self.assertIn("HTTP/PUT not allowed",
                      request.getWrittenData())

    @inlineCallbacks
    def test_too_large_body(self):
        """
        A too large body will mean the request is rejected.
        """
        session = MockPusherSession(self)
        resource = PusherResource({"post_body_limit": 1}, session)

        request = yield testResource(
            resource, "/", method="POST",
            headers={"Content-Type": ["application/json"]},
            body=pushBody)

        self.assertEqual(request.code, 400)
        self.assertIn("HTTP/POST body length ({}) exceeds maximum ({})".format(len(pushBody), 1),
                      request.getWrittenData())

    @inlineCallbacks
    def test_not_matching_bodylength(self):
        """
        A body length that is different than the Content-Length header will mean
        the request is rejected
        """
        session = MockPusherSession(self)
        resource = PusherResource({"post_body_limit": 1}, session)

        request = yield testResource(
            resource, "/", method="POST",
            headers={"Content-Type": ["application/json"],
                     "Content-Length": [1]},
            body=pushBody)

        self.assertEqual(request.code, 400)
        self.assertIn("HTTP/POST body length ({}) is different to Content-Length ({})".format(len(pushBody), 1),
                      request.getWrittenData())
<commit_msg>comma<commit_after>#####################################################################################
#
#  Copyright (C) Tavendo GmbH
#
#  Unless a separate license agreement exists between you and Tavendo GmbH (e.g. you
#  have purchased a commercial license), the license terms below apply.
#
#  Should you enter into a separate license agreement after having received a copy of
#  this software, then the terms of such license agreement replace the terms below at
#  the time at which such license agreement becomes effective.
#
#  In case a separate license agreement ends, and such agreement ends without being
#  replaced by another separate license agreement, the license terms below apply
#  from the time at which said agreement ends.
#
#  LICENSE TERMS
#
#  This program is free software: you can redistribute it and/or modify it under the
#  terms of the GNU Affero General Public License, version 3, as published by the
#  Free Software Foundation. This program is distributed in the hope that it will be
#  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
#  See the GNU Affero General Public License Version 3 for more details.
#
#  You should have received a copy of the GNU Affero General Public license along
#  with this program. If not, see <http://www.gnu.org/licenses/agpl-3.0.en.html>.
#
#####################################################################################

from __future__ import absolute_import

from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks

from crossbar.adapter.rest import PusherResource
from crossbar.adapter.rest.test import MockPusherSession, testResource

pushBody = '{"topic": "com.test.messages", "args": [1]}'

class RequestBodyTestCase(TestCase):
    """
    Unit tests for the validation parts of L{_CommonResource}.
    """
    @inlineCallbacks
    def test_bad_content_type(self):
        """
        An incorrect content type will mean the request is rejected.
        """
        session = MockPusherSession(self)
        resource = PusherResource({}, session)

        request = yield testResource(
            resource, "/", method="POST",
            headers={"Content-Type": ["application/text"]},
            body=pushBody)

        self.assertEqual(request.code, 400)
        self.assertIn("bad or missing content type ('application/text')",
                      request.getWrittenData())

    @inlineCallbacks
    def test_bad_method(self):
        """
        An incorrect method will mean the request is rejected.
        """
        session = MockPusherSession(self)
        resource = PusherResource({}, session)

        request = yield testResource(
            resource, "/", method="PUT",
            headers={"Content-Type": ["application/jsn"]},
            body=pushBody)

        self.assertEqual(request.code, 405)
        self.assertIn("HTTP/PUT not allowed",
                      request.getWrittenData())

    @inlineCallbacks
    def test_too_large_body(self):
        """
        A too large body will mean the request is rejected.
        """
        session = MockPusherSession(self)
        resource = PusherResource({"post_body_limit": 1}, session)

        request = yield testResource(
            resource, "/", method="POST",
            headers={"Content-Type": ["application/json"]},
            body=pushBody)

        self.assertEqual(request.code, 400)
        self.assertIn("HTTP/POST body length ({}) exceeds maximum ({})".format(len(pushBody), 1),
                      request.getWrittenData())

    @inlineCallbacks
    def test_not_matching_bodylength(self):
        """
        A body length that is different than the Content-Length header will mean
        the request is rejected.
        """
        session = MockPusherSession(self)
        resource = PusherResource({"post_body_limit": 1}, session)

        request = yield testResource(
            resource, "/", method="POST",
            headers={"Content-Type": ["application/json"],
                     "Content-Length": [1]},
            body=pushBody)

        self.assertEqual(request.code, 400)
        self.assertIn("HTTP/POST body length ({}) is different to Content-Length ({})".format(len(pushBody), 1),
                      request.getWrittenData())
<|endoftext|>