<commit_before>from django.contrib.auth.models import User

from rest_framework import serializers

from challenges.models import LeaderboardData
from participants.models import Participant, ParticipantTeam

from .models import Submission

class SubmissionSerializer(serializers.ModelSerializer):

    participant_team_name = serializers.SerializerMethodField()
    execution_time = serializers.SerializerMethodField()

    def __init__(self, *args, **kwargs):
        context = kwargs.get('context')
        if context and context.get('request').method == 'POST':
            created_by = context.get('request').user
            kwargs['data']['created_by'] = created_by.pk

            participant_team = context.get('participant_team').pk
            kwargs['data']['participant_team'] = participant_team

            challenge_phase = context.get('challenge_phase').pk
            kwargs['data']['challenge_phase'] = challenge_phase

        super(SubmissionSerializer, self).__init__(*args, **kwargs)

    class Meta:
        model = Submission
        fields = ('id', 'participant_team', 'participant_team_name', 'execution_time', 'challenge_phase',
                  'created_by', 'status', 'input_file', 'stdout_file', 'stderr_file', 'submitted_at',
                  'method_name', 'method_description', 'project_url', 'publication_url', 'is_public',
                  'submission_result_file',)

    def get_participant_team_name(self, obj):
        return obj.participant_team.team_name

    def get_execution_time(self, obj):
        return obj.execution_time

class LeaderboardDataSerializer(serializers.ModelSerializer):

    participant_team_name = serializers.SerializerMethodField()
    leaderboard_schema = serializers.SerializerMethodField()

    def __init__(self, *args, **kwargs):
        super(LeaderboardDataSerializer, self).__init__(*args, **kwargs)

    class Meta:
        model = LeaderboardData
        fields = "__all__"
        fields = ('id', 'participant_team_name', 'challenge_phase_split', 'leaderboard_schema', 'result')

    def get_participant_team_name(self, obj):
        return obj.submission.participant_team.team_name

    def get_leaderboard_schema(self, obj):
        return obj.leaderboard.schema

class ChallengeSubmissionManagementSerializer(serializers.ModelSerializer):

    participant_team = serializers.SerializerMethodField()
    challenge_phase = serializers.SerializerMethodField()
    created_by = serializers.SerializerMethodField()
    participant_team_members_email_id = serializers.SerializerMethodField()

    class Meta:
        model = Submission
        fields = ('id', 'participant_team', 'challenge_phase', 'created_by', 'status', 'is_public',
                  'submission_number', 'submitted_at', 'execution_time', 'input_file', 'stdout_file',
                  'stderr_file', 'submission_result_file', 'submission_metadata_file',
                  'participant_team_members_email_id',)

    def get_participant_team(self, obj):
        return obj.participant_team.team_name

    def get_challenge_phase(self, obj):
        return obj.challenge_phase.name

    def get_created_by(self, obj):
        return obj.created_by.username

    def get_participant_team_members_email_ids(self, obj):
        try:
            participant_team = ParticipantTeam.objects.get(team_name=obj.participant_team.team_name)
        except ParticipantTeam.DoesNotExist:
            return 'Participant team does not exist'

        participant_ids = Participant.objects.filter(team=participant_team).values_list('user_id', flat=True)
        return User.objects.filter(id__in=participant_ids).values_list('email', flat=True)
<commit_msg>Jobs: Fix variable name in ChallengeSubmissionManagementSerializer. (#1063)<commit_after>from django.contrib.auth.models import User

from rest_framework import serializers

from challenges.models import LeaderboardData
from participants.models import Participant, ParticipantTeam

from .models import Submission

class SubmissionSerializer(serializers.ModelSerializer):

    participant_team_name = serializers.SerializerMethodField()
    execution_time = serializers.SerializerMethodField()

    def __init__(self, *args, **kwargs):
        context = kwargs.get('context')
        if context and context.get('request').method == 'POST':
            created_by = context.get('request').user
            kwargs['data']['created_by'] = created_by.pk

            participant_team = context.get('participant_team').pk
            kwargs['data']['participant_team'] = participant_team

            challenge_phase = context.get('challenge_phase').pk
            kwargs['data']['challenge_phase'] = challenge_phase

        super(SubmissionSerializer, self).__init__(*args, **kwargs)

    class Meta:
        model = Submission
        fields = ('id', 'participant_team', 'participant_team_name', 'execution_time', 'challenge_phase',
                  'created_by', 'status', 'input_file', 'stdout_file', 'stderr_file', 'submitted_at',
                  'method_name', 'method_description', 'project_url', 'publication_url', 'is_public',
                  'submission_result_file',)

    def get_participant_team_name(self, obj):
        return obj.participant_team.team_name

    def get_execution_time(self, obj):
        return obj.execution_time

class LeaderboardDataSerializer(serializers.ModelSerializer):

    participant_team_name = serializers.SerializerMethodField()
    leaderboard_schema = serializers.SerializerMethodField()

    def __init__(self, *args, **kwargs):
        super(LeaderboardDataSerializer, self).__init__(*args, **kwargs)

    class Meta:
        model = LeaderboardData
        fields = "__all__"
        fields = ('id', 'participant_team_name', 'challenge_phase_split', 'leaderboard_schema', 'result')

    def get_participant_team_name(self, obj):
        return obj.submission.participant_team.team_name

    def get_leaderboard_schema(self, obj):
        return obj.leaderboard.schema

class ChallengeSubmissionManagementSerializer(serializers.ModelSerializer):

    participant_team = serializers.SerializerMethodField()
    challenge_phase = serializers.SerializerMethodField()
    created_by = serializers.SerializerMethodField()
    get_participant_team_members_email_ids = serializers.SerializerMethodField()

    class Meta:
        model = Submission
        fields = ('id', 'participant_team', 'challenge_phase', 'created_by', 'status', 'is_public',
                  'submission_number', 'submitted_at', 'execution_time', 'input_file', 'stdout_file',
                  'stderr_file', 'submission_result_file', 'submission_metadata_file',
                  'get_participant_team_members_email_ids',)

    def get_participant_team(self, obj):
        return obj.participant_team.team_name

    def get_challenge_phase(self, obj):
        return obj.challenge_phase.name

    def get_created_by(self, obj):
        return obj.created_by.username

    def get_participant_team_members_email_ids(self, obj):
        try:
            participant_team = ParticipantTeam.objects.get(team_name=obj.participant_team.team_name)
        except ParticipantTeam.DoesNotExist:
            return 'Participant team does not exist'

        participant_ids = Participant.objects.filter(team=participant_team).values_list('user_id', flat=True)
        return User.objects.filter(id__in=participant_ids).values_list('email', flat=True)
<|endoftext|>


<commit_before># this module defines classes that can be used to massage the content of an
# article, mostly to remove gunk like ads

import sys, re, urllib2
import param, util

class Filter:
  """Virtual class with the interface for all degunking filters"""
  def apply(self, content, feed, item):
    raise NotImplementedError

class Re(Filter):
  """Strip text, or rewrite a substitution for it, using regular expressions
  regex: regex to search for in article text
  flags: re compilation flags
  sub: optional substitution. The default just removes the offending match
       instead of rewriting it
  iterate: keep reiterating the process until no more matches are found
  """
  def __init__(self, regex, flags=0, sub='', iterate=False):
    self.re = re.compile(regex, flags)
    self.sub = sub
    self.iterate = iterate
  def apply(self, content, *args, **kwargs):
    content, matches = self.re.subn(self.sub, content)
    while self.iterate and matches:
      content, matches = self.re.subn(self.sub, content)
    return content

class ReUrl(Filter):
  """Find an alternative item link by either rewriting the article link, and/or
  searching for one in the contents (the URL is searched first, then contents.
  This is useful to skip summary pages and go directly to full contents
  url: substitution to use as the article link (can use backreferences like \1)
  regex_url: regex to search for in original article link
  regex_content: regex to search for in the article content
                 (only first match matters)
  flags: flags for regex compilation
  """
  def __init__(self, url, regex_url=None, regex_content=None, flags=0):
    self.url = url
    if regex_content:
      self.re_content = re.compile(regex_content, flags)
    else:
      self.re_content= None
    if regex_url:
      self.re_url = re.compile(regex_url, flags)
    else:
      self.re_url= None
  def apply(self, content, *args, **kwargs):
    item = args[1]
    if self.re_url:
      m = self.re_url.search(item['link'])
      if m:
        url = m.expand(self.url)
        item = args[1]
        item['link'] = url
        return content
    if self.re_content:
      m = self.re_content.search(content)
      if m:
        url = m.expand(self.url)
        item = args[1]
        item['link'] = url
    return content

class ReTitle(Filter):
  """Find an alternative item title
  title: substitution to use as the title (can use backreferences like \1)
  regex: regex to search for in the article content (only first match matters)
  flags: flags for regex compilation
  """
  def __init__(self, title, regex, flags=0):
    self.title = title
    self.re_content = re.compile(regex, flags)
  def apply(self, content, *args, **kwargs):
    item = args[1]
    if not item['title'] or item['title'] == 'Untitled':
      item = args[1]
      m = self.re_content.search(content)
      if m:
        title = m.expand(self.title).strip()
        if title:
          item['title'] = title
    return content

class UseFirstLink(Filter):
  """Use the first link in the body as the article link.
  Originally for the Daily Python URL feed, where the item link in the feed
  is the (useless) anchor to that item on the page, rather than the article
  itself.
  """
  url_re = re.compile('(?:href|src)="([^"]*)"', re.IGNORECASE)
  def __init__(self, prefix):
    self.prefix = prefix
  def apply(self, content, *args, **kwargs):
    item = args[1]
    urls = self.url_re.findall(content)
    if item['link'].startswith(self.prefix) and len(urls):
      item['link'] = urls[0]
    return content

class Dereference(Filter):
  """Dereference the item link and use a regex to find a better link. Useful
  for those annoying sites that try to force you to visit them first for banner
  impressions (e.g. Digg).
  The regex should include one group, used to extract the URL, If the regex
  fails to match, the link is unchanged, so you can be as tight as you want.
  """
  url_re = re.compile('(?:href|src)="([^"]*)"', re.IGNORECASE)
  def __init__(self, link_substr, regex):
    self.link_substr = link_substr
    self.re = re.compile(regex)
  def apply(self, content, *args, **kwargs):
    item = args[1]
    if self.link_substr in item['link']:
      try:
        # check if this item has not already been loaded before
        guid = item['id']
        from singleton import db, sqlite
        c = db.cursor()
        if sqlite.paramstyle == 'qmark':
          c.execute("select item_link from fm_items where item_guid=?", [guid])
        elif sqlite.paramstyle == 'pyformat':
          c.execute("select item_link from fm_items where item_guid=%guid)s",
                    {'guid': guid})
        link = c.fetchone()
        c.close()
        if link:
          print >> param.log, 'not dereferencing', guid, '->', link[0]
          item['link'] = link[0]
          return content
        # we haven't seen this article before, buck up and load it
        deref = urllib2.urlopen(item['link']).read()
        m = self.re.search(deref)
        if m and m.groups():
          item['link'] = m.groups()[0]
      except:
        util.print_stack()
    return content
<commit_msg>optimization<commit_after># this module defines classes that can be used to massage the content of an
# article, mostly to remove gunk like ads

import sys, re, urllib2
import param, util

class Filter:
  """Virtual class with the interface for all degunking filters"""
  def apply(self, content, feed, item):
    raise NotImplementedError

class Re(Filter):
  """Strip text, or rewrite a substitution for it, using regular expressions
  regex: regex to search for in article text
  flags: re compilation flags
  sub: optional substitution. The default just removes the offending match
       instead of rewriting it
  iterate: keep reiterating the process until no more matches are found
  """
  def __init__(self, regex, flags=0, sub='', iterate=False):
    self.re = re.compile(regex, flags)
    self.sub = sub
    self.iterate = iterate
  def apply(self, content, *args, **kwargs):
    content, matches = self.re.subn(self.sub, content)
    while self.iterate and matches:
      content, matches = self.re.subn(self.sub, content)
    return content

class ReUrl(Filter):
  """Find an alternative item link by either rewriting the article link, and/or
  searching for one in the contents (the URL is searched first, then contents.
  This is useful to skip summary pages and go directly to full contents
  url: substitution to use as the article link (can use backreferences like \1)
  regex_url: regex to search for in original article link
  regex_content: regex to search for in the article content
                 (only first match matters)
  flags: flags for regex compilation
  """
  def __init__(self, url, regex_url=None, regex_content=None, flags=0):
    self.url = url
    if regex_content:
      self.re_content = re.compile(regex_content, flags)
    else:
      self.re_content= None
    if regex_url:
      self.re_url = re.compile(regex_url, flags)
    else:
      self.re_url= None
  def apply(self, content, *args, **kwargs):
    item = args[1]
    if self.re_url:
      m = self.re_url.search(item['link'])
      if m:
        url = m.expand(self.url)
        item = args[1]
        item['link'] = url
        return content
    if self.re_content:
      m = self.re_content.search(content)
      if m:
        url = m.expand(self.url)
        item = args[1]
        item['link'] = url
    return content

class ReTitle(Filter):
  """Find an alternative item title
  title: substitution to use as the title (can use backreferences like \1)
  regex: regex to search for in the article content (only first match matters)
  flags: flags for regex compilation
  """
  def __init__(self, title, regex, flags=0):
    self.title = title
    self.re_content = re.compile(regex, flags)
  def apply(self, content, *args, **kwargs):
    item = args[1]
    if not item['title'] or item['title'] == 'Untitled':
      item = args[1]
      m = self.re_content.search(content)
      if m:
        title = m.expand(self.title).strip()
        if title:
          item['title'] = title
    return content

class UseFirstLink(Filter):
  """Use the first link in the body as the article link.
  Originally for the Daily Python URL feed, where the item link in the feed
  is the (useless) anchor to that item on the page, rather than the article
  itself.
  """
  url_re = re.compile('(?:href|src)="([^"]*)"', re.IGNORECASE)
  def __init__(self, prefix):
    self.prefix = prefix
  def apply(self, content, *args, **kwargs):
    item = args[1]
    if item['link'].startswith(self.prefix):
      urls = self.url_re.findall(content)
      if len(urls):
        item['link'] = urls[0]
    return content

class Dereference(Filter):
  """Dereference the item link and use a regex to find a better link. Useful
  for those annoying sites that try to force you to visit them first for banner
  impressions (e.g. Digg).
  The regex should include one group, used to extract the URL, If the regex
  fails to match, the link is unchanged, so you can be as tight as you want.
  """
  url_re = re.compile('(?:href|src)="([^"]*)"', re.IGNORECASE)
  def __init__(self, link_substr, regex):
    self.link_substr = link_substr
    self.re = re.compile(regex)
  def apply(self, content, *args, **kwargs):
    item = args[1]
    if self.link_substr in item['link']:
      try:
        # check if this item has not already been loaded before
        guid = item['id']
        from singleton import db, sqlite
        c = db.cursor()
        if sqlite.paramstyle == 'qmark':
          c.execute("select item_link from fm_items where item_guid=?", [guid])
        elif sqlite.paramstyle == 'pyformat':
          c.execute("select item_link from fm_items where item_guid=%guid)s",
                    {'guid': guid})
        link = c.fetchone()
        c.close()
        if link:
          print >> param.log, 'not dereferencing', guid, '->', link[0]
          item['link'] = link[0]
          return content
        # we haven't seen this article before, buck up and load it
        deref = urllib2.urlopen(item['link']).read()
        m = self.re.search(deref)
        if m and m.groups():
          item['link'] = m.groups()[0]
      except:
        util.print_stack()
    return content
<|endoftext|>


<commit_before>"""Testing osa-differ."""
import argparse
import json

from git import Repo
import httpretty
from osa_differ import osa_differ
from pytest import raises

class TestOSADiffer(object):
    """Testing osa-differ."""

    def test_arguments_not_enough(self, capsys):
        """Verify that we get an error with missing args."""
        with raises(SystemExit):
            parser = osa_differ.create_parser()
            parser.parse_args([])
        out, err = capsys.readouterr()
        assert "usage" in err

    def test_arguments_sufficient(self):
        """Verify that we get args properly with right args passed."""
        parser = osa_differ.create_parser()
        args = vars(parser.parse_args(['13.3.0', '13.3.1']))
        assert args['old_commit'][0] == '13.3.0'
        assert args['new_commit'][0] == '13.3.1'

    def test_arguments_parse(self, monkeypatch):
        """Test if we can parse arguments."""
        def mockreturn(test):
            return {'test': 'test'}
        monkeypatch.setattr('argparse.ArgumentParser.parse_args', mockreturn)
        result = osa_differ.parse_arguments()
        assert result['test'] == 'test'

    def test_create_parser(self):
        """Verify that we can create an argument parser."""
        parser = osa_differ.create_parser()
        assert isinstance(parser, argparse.ArgumentParser)

    def test_commit_url_github(self):
        """Verify that GitHub URLs are unaltered."""
        repo_url = "https://github.com/openstack/openstack-ansible"
        result = osa_differ.get_commit_url(repo_url)
        assert result == repo_url

    def test_commit_url_openstack(self):
        """Verify that OpenStack URLs are fixed."""
        repo_url = "https://git.openstack.org/cgit/openstack/openstack-ansible"
        result = osa_differ.get_commit_url(repo_url)
        assert result == "https://github.com/openstack/openstack-ansible"

    def test_commit_url_unknown(self):
        """Verify that unknown URLs are unaltered."""
        repo_url = "https://reddit.com"
        result = osa_differ.get_commit_url(repo_url)
        assert result == repo_url

    def test_get_commits(self, tmpdir):
        """Verify that we can get commits for a repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        for x in range(0, 10):
            file = p / "test{0}.txt".format(x)
            file.write_text(u"Test", encoding='utf-8')
            repo.index.add(['test{0}.txt'.format(x)])
            repo.index.commit("Commit #{0}".format(x))

        commits = osa_differ.get_commits(path, 'HEAD~2', 'HEAD')
        assert len(list(commits)) == 2

    def test_get_commits_hide_merges(self, tmpdir):
        """Verify that we can get commits for a repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        for x in range(0, 10):
            file = p / "test{0}.txt".format(x)
            file.write_text(u"Test", encoding='utf-8')
            repo.index.add(['test{0}.txt'.format(x)])
            repo.index.commit("Merge #{0}".format(x))

        commits = osa_differ.get_commits(path, 'HEAD~2', 'HEAD',
                                         hide_merges=True)
        assert len(list(commits)) == 0

    def test_get_commits_include_merges(self, tmpdir):
        """Verify that we can get commits for a repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        for x in range(0, 10):
            file = p / "test{0}.txt".format(x)
            file.write_text(u"Test", encoding='utf-8')
            repo.index.add(['test{0}.txt'.format(x)])
            repo.index.commit("Merge #{0}".format(x))

        commits = osa_differ.get_commits(path, 'HEAD~2', 'HEAD',
                                         hide_merges=False)
        assert len(list(commits)) == 2

    def test_get_projects(self, tmpdir):
        """Verify that we can retrieve projects."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test1.yml'
        file.write_text(u"""---
tempest_git_repo: https://git.openstack.org/openstack/tempest
tempest_git_install_branch: 1493c7f0ba49bfccb9ff8516b10a65d949d7462e
tempest_git_project_group: utility_all
""", encoding='utf-8')
        file = p / 'test2.yml'
        file.write_text(u"""---
novncproxy_git_repo: https://github.com/kanaka/novnc
novncproxy_git_install_branch: da82b3426c27bf1a79f671c5825d68ab8c0c5d9f
novncproxy_git_project_group: nova_console
""", encoding='utf-8')
        repo.index.add(['test1.yml', 'test2.yml'])
        repo.index.commit("Test")
        projects = osa_differ.get_projects(path,
                                           ['test1.yml', 'test2.yml'],
                                           'HEAD')
        assert isinstance(projects, list)
        assert 'novncproxy' in [x[0] for x in projects]
        assert 'tempest' in [x[0] for x in projects]

    def test_get_roles(self, tmpdir):
        """Verify that we can get OSA role information."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'ansible-role-requirements.yml'
        file.write_text(u"""
- name: apt_package_pinning
  scm: git
  src: https://github.com/openstack/openstack-ansible-apt_package_pinning
  version: master
""", encoding='utf-8')
        repo.index.add(['ansible-role-requirements.yml'])
        repo.index.commit("Test")

        roles = osa_differ.get_roles(path, 'HEAD')
        assert isinstance(roles, list)
        assert roles[0][0] == 'apt_package_pinning'

    @httpretty.activate
    def test_post_gist(self):
        """Verify that posting gists works."""
        json_body = {
            'html_url': 'https://example.com/'
        }
        httpretty.register_uri(httpretty.POST,
                               "https://api.github.com/gists",
                               body=json.dumps(json_body))
        result = osa_differ.post_gist("report text", 'HEAD~1', 'HEAD')
        assert result == 'https://example.com/'

    def test_commit_valid(self, tmpdir):
        """Verify that we can find valid commits."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        result = osa_differ.validate_commits(path, ['HEAD'])
        assert result

    def test_commit_invalid(self, tmpdir):
        """Verify that we can find valid commits."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        with raises(Exception):
            osa_differ.validate_commits(path, ['HEAD~1'])

    def test_make_osa_report(self, tmpdir):
        """Verify that we can make the OSA header report."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing1', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 1')
        file.write_text(u'Testing2', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 2')

        parser = osa_differ.create_parser()
        args = parser.parse_args(['HEAD~1', 'HEAD'])
        report = osa_differ.make_osa_report(path,
                                            'HEAD~1',
                                            "HEAD",
                                            args)
        assert "HEAD~1" in report
        assert "OpenStack-Ansible Diff Generator" in report

    def test_make_report(self, tmpdir):
        """Verify that we can make a report."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing1', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 1')
        file.write_text(u'Testing2', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 2')

        new_pins = [("test", "http://example.com", "HEAD")]
        old_pins = [("test", "http://example.com", "HEAD~1")]

        report = osa_differ.make_report(str(tmpdir), old_pins, new_pins)

        assert "1 commit was found in `test <http://example.com>`" in report
        assert "Testing 2" in report

    def test_make_report_old_pin_missing(self, tmpdir):
        """Verify that we can make a report when the old pin is missing."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing1', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 1')
        file.write_text(u'Testing2', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 2')

        new_pins = [("test", "http://example.com", "HEAD")]
        old_pins = []

        report = osa_differ.make_report(str(tmpdir), old_pins, new_pins)

        assert report == ''

    def test_prepare_storage_directory_exists(self, tmpdir):
        """Verify that we can create a storage directory."""
        p = tmpdir.mkdir("test")
        storagedir = osa_differ.prepare_storage_dir(str(p))
        assert storagedir == p

    def test_prepare_storage_directory_create(self, tmpdir):
        """Verify that we can create a storage directory."""
        p = tmpdir.mkdir("test")
        newdir = "{0}/subdir".format(str(p))
        storagedir = osa_differ.prepare_storage_dir(newdir)
        assert storagedir == newdir

    def test_prepare_storage_directory_exception(self, tmpdir):
        """Verify that we can create a storage directory."""
        p = tmpdir.mkdir("test")
        newdir = "{0}/subdir/subdir/subdir".format(str(p))

        with raises(OSError):
            osa_differ.prepare_storage_dir(newdir)

    def test_render_template(self, tmpdir):
        """Verify that we can render a jinja template."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        for x in range(0, 10):
            file = p / "test{0}.txt".format(x)
            file.write_text(u"Test", encoding='utf-8')
            repo.index.add(['test{0}.txt'.format(x)])
            repo.index.commit("Commit #{0}".format(x))

        commits = osa_differ.get_commits(path, 'HEAD~2', 'HEAD')

        template_vars = {
            'repo': 'openstack-ansible',
            'commits': commits,
            'commit_base_url': 'http://example.com',
            'old_sha': 'HEAD~10',
            'new_sha': 'HEAD~1'
        }
        template_filename = "offline-repo-changes.j2"
        rst = osa_differ.render_template(template_filename,
                                         template_vars)
        assert "openstack-ansible" in rst
        assert "2 commits were found" in rst
        assert "http://example.com" in rst
        assert "HEAD~10" in rst
        assert "HEAD~1" in rst

    def test_repo_clone(self, tmpdir, monkeypatch):
        """Verify that we can clone a repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        def mockclone(x, y):
            return path

        def mockwait(*args, **kwargs):
            return True

        monkeypatch.setattr("git.repo.base.Repo.clone", mockclone)
        monkeypatch.setattr("git.cmd.Git.AutoInterrupt.wait", mockwait)
        result = osa_differ.repo_clone(path,
                                       "http://example.com")

        assert result.active_branch.name == 'master'
        assert not result.is_dirty()

    def test_repo_clone_update(self, tmpdir):
        """Verify that we can clone a repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        p = tmpdir.mkdir("test2")
        path_clonefrom = "{0}/testrepodoesntexist".format(str(p))
        result = osa_differ.update_repo(path_clonefrom, path)

        assert result.active_branch.name == 'master'
        assert not result.is_dirty()

    def test_repo_update_update(self, tmpdir):
        """Verify that update_repo tries to update the repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        result = osa_differ.update_repo(path, path)

        assert result.active_branch.name == 'master'
        assert not result.is_dirty()

    def test_repo_update_without_fetch(self, tmpdir):
        """Verify that we can get a repo ready without fetching it."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        result = osa_differ.repo_pull(path,
                                      "http://example.com",
                                      fetch=False)
        assert result.active_branch.name == 'master'
        assert not result.is_dirty()

    def test_repo_update_with_fetch(self, tmpdir, monkeypatch):
        """Verify that we can get a repo ready and update it."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')
        repo.create_remote('origin', url='http://example.com')

        monkeypatch.setattr("git.remote.Remote.pull", lambda x: True)
        result = osa_differ.repo_pull(path,
                                      "http://example.com",
                                      fetch=True)
        assert result.active_branch.name == 'master'
        assert not result.is_dirty()
<commit_msg>Add tests for making report with old pin missing<commit_after>"""Testing osa-differ."""
import argparse
import json

from git import Repo
import httpretty
from osa_differ import osa_differ
from pytest import raises

class TestOSADiffer(object):
    """Testing osa-differ."""

    def test_arguments_not_enough(self, capsys):
        """Verify that we get an error with missing args."""
        with raises(SystemExit):
            parser = osa_differ.create_parser()
            parser.parse_args([])
        out, err = capsys.readouterr()
        assert "usage" in err

    def test_arguments_sufficient(self):
        """Verify that we get args properly with right args passed."""
        parser = osa_differ.create_parser()
        args = vars(parser.parse_args(['13.3.0', '13.3.1']))
        assert args['old_commit'][0] == '13.3.0'
        assert args['new_commit'][0] == '13.3.1'

    def test_arguments_parse(self, monkeypatch):
        """Test if we can parse arguments."""
        def mockreturn(test):
            return {'test': 'test'}
        monkeypatch.setattr('argparse.ArgumentParser.parse_args', mockreturn)
        result = osa_differ.parse_arguments()
        assert result['test'] == 'test'

    def test_create_parser(self):
        """Verify that we can create an argument parser."""
        parser = osa_differ.create_parser()
        assert isinstance(parser, argparse.ArgumentParser)

    def test_commit_url_github(self):
        """Verify that GitHub URLs are unaltered."""
        repo_url = "https://github.com/openstack/openstack-ansible"
        result = osa_differ.get_commit_url(repo_url)
        assert result == repo_url

    def test_commit_url_openstack(self):
        """Verify that OpenStack URLs are fixed."""
        repo_url = "https://git.openstack.org/cgit/openstack/openstack-ansible"
        result = osa_differ.get_commit_url(repo_url)
        assert result == "https://github.com/openstack/openstack-ansible"

    def test_commit_url_unknown(self):
        """Verify that unknown URLs are unaltered."""
        repo_url = "https://reddit.com"
        result = osa_differ.get_commit_url(repo_url)
        assert result == repo_url

    def test_get_commits(self, tmpdir):
        """Verify that we can get commits for a repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        for x in range(0, 10):
            file = p / "test{0}.txt".format(x)
            file.write_text(u"Test", encoding='utf-8')
            repo.index.add(['test{0}.txt'.format(x)])
            repo.index.commit("Commit #{0}".format(x))

        commits = osa_differ.get_commits(path, 'HEAD~2', 'HEAD')
        assert len(list(commits)) == 2

    def test_get_commits_hide_merges(self, tmpdir):
        """Verify that we can get commits for a repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        for x in range(0, 10):
            file = p / "test{0}.txt".format(x)
            file.write_text(u"Test", encoding='utf-8')
            repo.index.add(['test{0}.txt'.format(x)])
            repo.index.commit("Merge #{0}".format(x))

        commits = osa_differ.get_commits(path, 'HEAD~2', 'HEAD',
                                         hide_merges=True)
        assert len(list(commits)) == 0

    def test_get_commits_include_merges(self, tmpdir):
        """Verify that we can get commits for a repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        for x in range(0, 10):
            file = p / "test{0}.txt".format(x)
            file.write_text(u"Test", encoding='utf-8')
            repo.index.add(['test{0}.txt'.format(x)])
            repo.index.commit("Merge #{0}".format(x))

        commits = osa_differ.get_commits(path, 'HEAD~2', 'HEAD',
                                         hide_merges=False)
        assert len(list(commits)) == 2

    def test_get_projects(self, tmpdir):
        """Verify that we can retrieve projects."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test1.yml'
        file.write_text(u"""---
tempest_git_repo: https://git.openstack.org/openstack/tempest
tempest_git_install_branch: 1493c7f0ba49bfccb9ff8516b10a65d949d7462e
tempest_git_project_group: utility_all
""", encoding='utf-8')
        file = p / 'test2.yml'
        file.write_text(u"""---
novncproxy_git_repo: https://github.com/kanaka/novnc
novncproxy_git_install_branch: da82b3426c27bf1a79f671c5825d68ab8c0c5d9f
novncproxy_git_project_group: nova_console
""", encoding='utf-8')
        repo.index.add(['test1.yml', 'test2.yml'])
        repo.index.commit("Test")
        projects = osa_differ.get_projects(path,
                                           ['test1.yml', 'test2.yml'],
                                           'HEAD')
        assert isinstance(projects, list)
        assert 'novncproxy' in [x[0] for x in projects]
        assert 'tempest' in [x[0] for x in projects]

    def test_get_roles(self, tmpdir):
        """Verify that we can get OSA role information."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'ansible-role-requirements.yml'
        file.write_text(u"""
- name: apt_package_pinning
  scm: git
  src: https://github.com/openstack/openstack-ansible-apt_package_pinning
  version: master
""", encoding='utf-8')
        repo.index.add(['ansible-role-requirements.yml'])
        repo.index.commit("Test")

        roles = osa_differ.get_roles(path, 'HEAD')
        assert isinstance(roles, list)
        assert roles[0][0] == 'apt_package_pinning'

    @httpretty.activate
    def test_post_gist(self):
        """Verify that posting gists works."""
        json_body = {
            'html_url': 'https://example.com/'
        }
        httpretty.register_uri(httpretty.POST,
                               "https://api.github.com/gists",
                               body=json.dumps(json_body))
        result = osa_differ.post_gist("report text", 'HEAD~1', 'HEAD')
        assert result == 'https://example.com/'

    def test_commit_valid(self, tmpdir):
        """Verify that we can find valid commits."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        result = osa_differ.validate_commits(path, ['HEAD'])
        assert result

    def test_commit_invalid(self, tmpdir):
        """Verify that we can find valid commits."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        with raises(Exception):
            osa_differ.validate_commits(path, ['HEAD~1'])

    def test_make_osa_report(self, tmpdir):
        """Verify that we can make the OSA header report."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing1', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 1')
        file.write_text(u'Testing2', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 2')

        parser = osa_differ.create_parser()
        args = parser.parse_args(['HEAD~1', 'HEAD'])
        report = osa_differ.make_osa_report(path,
                                            'HEAD~1',
                                            "HEAD",
                                            args)
        assert "HEAD~1" in report
        assert "OpenStack-Ansible Diff Generator" in report

    def test_make_report(self, tmpdir):
        """Verify that we can make a report."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing1', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 1')
        file.write_text(u'Testing2', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 2')

        new_pins = [("test", "http://example.com", "HEAD")]
        old_pins = [("test", "http://example.com", "HEAD~1")]

        report = osa_differ.make_report(str(tmpdir), old_pins, new_pins)

        assert "1 commit was found in `test <http://example.com>`" in report
        assert "Testing 2" in report

    def test_make_report_old_pin_missing(self, tmpdir):
        """Verify that we can make a report when the old pin is missing."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing1', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 1')
        file.write_text(u'Testing2', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing 2')

        new_pins = [("test", "http://example.com", "HEAD")]
        old_pins = []

        report = osa_differ.make_report(str(tmpdir), old_pins, new_pins)

        assert report == ''

    def publish_report(self):
        """Verify that we can publish a report to stdout."""
        report = "Sample report"

        parser = osa_differ.create_parser()
        args = parser.parse_args(['HEAD~1', 'HEAD'])

        result = osa_differ.publish_report(report, args)

        assert result == 'Sample report'

    def test_prepare_storage_directory_exists(self, tmpdir):
        """Verify that we can create a storage directory."""
        p = tmpdir.mkdir("test")
        storagedir = osa_differ.prepare_storage_dir(str(p))
        assert storagedir == p

    def test_prepare_storage_directory_create(self, tmpdir):
        """Verify that we can create a storage directory."""
        p = tmpdir.mkdir("test")
        newdir = "{0}/subdir".format(str(p))
        storagedir = osa_differ.prepare_storage_dir(newdir)
        assert storagedir == newdir

    def test_prepare_storage_directory_exception(self, tmpdir):
        """Verify that we can create a storage directory."""
        p = tmpdir.mkdir("test")
        newdir = "{0}/subdir/subdir/subdir".format(str(p))

        with raises(OSError):
            osa_differ.prepare_storage_dir(newdir)

    def test_render_template(self, tmpdir):
        """Verify that we can render a jinja template."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        for x in range(0, 10):
            file = p / "test{0}.txt".format(x)
            file.write_text(u"Test", encoding='utf-8')
            repo.index.add(['test{0}.txt'.format(x)])
            repo.index.commit("Commit #{0}".format(x))

        commits = osa_differ.get_commits(path, 'HEAD~2', 'HEAD')

        template_vars = {
            'repo': 'openstack-ansible',
            'commits': commits,
            'commit_base_url': 'http://example.com',
            'old_sha': 'HEAD~10',
            'new_sha': 'HEAD~1'
        }
        template_filename = "offline-repo-changes.j2"
        rst = osa_differ.render_template(template_filename,
                                         template_vars)
        assert "openstack-ansible" in rst
        assert "2 commits were found" in rst
        assert "http://example.com" in rst
        assert "HEAD~10" in rst
        assert "HEAD~1" in rst

    def test_repo_clone(self, tmpdir, monkeypatch):
        """Verify that we can clone a repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        def mockclone(x, y):
            return path

        def mockwait(*args, **kwargs):
            return True

        monkeypatch.setattr("git.repo.base.Repo.clone", mockclone)
        monkeypatch.setattr("git.cmd.Git.AutoInterrupt.wait", mockwait)
        result = osa_differ.repo_clone(path,
                                       "http://example.com")

        assert result.active_branch.name == 'master'
        assert not result.is_dirty()

    def test_repo_clone_update(self, tmpdir):
        """Verify that we can clone a repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        p = tmpdir.mkdir("test2")
        path_clonefrom = "{0}/testrepodoesntexist".format(str(p))
        result = osa_differ.update_repo(path_clonefrom, path)

        assert result.active_branch.name == 'master'
        assert not result.is_dirty()

    def test_repo_update_update(self, tmpdir):
        """Verify that update_repo tries to update the repo."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        result = osa_differ.update_repo(path, path)

        assert result.active_branch.name == 'master'
        assert not result.is_dirty()

    def test_repo_update_without_fetch(self, tmpdir):
        """Verify that we can get a repo ready without fetching it."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')

        result = osa_differ.repo_pull(path,
                                      "http://example.com",
                                      fetch=False)
        assert result.active_branch.name == 'master'
        assert not result.is_dirty()

    def test_repo_update_with_fetch(self, tmpdir, monkeypatch):
        """Verify that we can get a repo ready and update it."""
        p = tmpdir.mkdir('test')
        path = str(p)
        repo = Repo.init(path)
        file = p / 'test.txt'
        file.write_text(u'Testing', encoding='utf-8')
        repo.index.add(['test.txt'])
        repo.index.commit('Testing')
        repo.create_remote('origin', url='http://example.com')

        monkeypatch.setattr("git.remote.Remote.pull", lambda x: True)
        result = osa_differ.repo_pull(path,
                                      "http://example.com",
                                      fetch=True)
        assert result.active_branch.name == 'master'
        assert not result.is_dirty()
<|endoftext|>


<commit_before># Create your views here.
<commit_msg>Updating home views.<commit_after>from django.shortcuts import render_to_response
from django.template import RequestContext

def index(request):
  return render_to_response("index.html", {}, context_instance=RequestContext(request))<|endoftext|>


<commit_before>from simulocloud import PointCloud, Bounds
from simulocloud.exceptions import EmptyPointCloud
import pytest
import numpy as np
import cPickle as pkl
import os

""" Test data """
# Data type used for arrays
_DTYPE = np.float64
_INPUT_DATA = [[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
               [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9],
               [2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9]]

@pytest.fixture
def expected_DATA_points():
    """The points array that should be generated from `input_list`."""
    return np.array([( 0. ,  1. ,  2. ),
                     ( 0.1,  1.1,  2.1),
                     ( 0.2,  1.2,  2.2),
                     ( 0.3,  1.3,  2.3),
                     ( 0.4,  1.4,  2.4),
                     ( 0.5,  1.5,  2.5),
                     ( 0.6,  1.6,  2.6),
                     ( 0.7,  1.7,  2.7),
                     ( 0.8,  1.8,  2.8),
                     ( 0.9,  1.9,  2.9)],
                    dtype=[('x', _DTYPE), ('y', _DTYPE), ('z', _DTYPE)])

@pytest.fixture
def input_array():
    """Simple [xs, ys, zs] numpy.ndarray of `input_list`."""
    return np.array(_INPUT_DATA, dtype=_DTYPE)

@pytest.fixture
def expected_las_points(fname='ALS_points.pkl'):
    """The points array that should be generated from the example las data."""
    with open(abspath(fname), 'rb') as o:
        points = pkl.load(o)
    return points

@pytest.fixture
def pc(input_array):
    """Set up a `PointCloud` instance using test data."""
    return PointCloud(input_array)

@pytest.fixture
def none_bounds():
    """A Bounds nametuple with all bounds set to None."""
    return Bounds(*(None,)*6)

@pytest.fixture
def inf_bounds():
    """A Bounds namedtuple with all bounds set to inf/-inf."""
    return Bounds(*(np.inf,)*3 + (np.inf,)*3)
""" Helper functions """

def abspath(fname, fdir='data'):
    """Return the absolute filepath of filename in (relative) directory."""
    return os.path.join(os.path.dirname(__file__), fdir, fname)

""" Test functions """

def test_PointCloud_read_from_array(input_array, expected_DATA_points):
    """Can PointCloud initialise directly from a `[xs, ys, zs]` array?"""
    assert np.all(PointCloud(input_array).points == expected_DATA_points)

def test_PointCloud_read_from_las(expected_las_points, fname='ALS.las'):
    """Can PointCloud be constructed from a `.las` file?"""
    assert np.all(PointCloud.from_las(abspath(fname)).points == expected_las_points)

def test_arr_generation(pc, input_array):
    """Does PointCloud.arr work as expected?."""
    assert np.all(pc.arr == input_array.T)

def test_bounds_returns_accurate_boundary_box(pc):
    """Does PointCloud.bounds accurately describe the bounding box?"""
    assert pc.bounds == tuple((f(c) for f in (min, max) for c in _INPUT_DATA))

def test_len_works(pc):
    """Does __len__() report the correct number of points?"""
    # Assumes lists in _INPUT_DATA are consistent length
    assert len(pc) == len(_INPUT_DATA[0])

def test_cropping_with_none_bounds(pc, none_bounds):
    """Does no PointCloud cropping occur when bounds of None are used?"""
    assert np.all(pc.crop(none_bounds).points == pc.points)

@pytest.mark.parametrize('c', ('x', 'y', 'z'))
def test_cropping_is_lower_bounds_inclusive(pc, none_bounds, c):
    """Does PointCloud cropping preserve values at lower bounds?"""
    # Ensure a unique point used as minimum bound
    sorted_points = np.sort(pc.points, order=[c])
    for i, minc in enumerate(sorted_points[c]):
        if i < 1: continue # at least one point must be out of bounds
        if minc != sorted_points[i-1][c]:
            lowest_point = sorted_points[i]
            break

    # Apply lower bound cropping to a single dimension
    pc = pc.crop(none_bounds._replace(**{'min'+c: minc}))

    assert np.sort(pc.points, order=c)[0] == lowest_point

@pytest.mark.parametrize('c', ('x', 'y', 'z'))
def test_cropping_is_upper_bounds_exclusive(pc, none_bounds, c):
    """Does PointCloud cropping omit values at upper bounds?"""
    # Ensure a unique point used as maximum bound
    rev_sorted_points = np.sort(pc.points, order=[c])[::-1]
    for i, maxc in enumerate(rev_sorted_points[c]):
        if maxc != rev_sorted_points[i+1][c]:
            oob_point = rev_sorted_points[i]
            highest_point = rev_sorted_points[i+1]
            break

    # Apply upper bound cropping to a single dimension
    pc = pc.crop(none_bounds._replace(**{'max'+c: maxc}))

    assert np.sort(pc.points, order=c)[-1] == highest_point and oob_point not in pc.points

def test_cropping_to_nothing_raises_exception_when_specified(pc, inf_bounds):
    """Does PointCloud cropping refuse to return an empty PointCloud?"""
    with pytest.raises(EmptyPointCloud):
        pc.crop(inf_bounds, return_empty=False)

def test_cropping_to_nothing_returns_empty(pc, inf_bounds):
    """Does PointCloud cropping return an empty PointCloud when asked?"""
    assert not len(pc.crop(inf_bounds, return_empty=True))

def test_PointCloud_exports_transparently_to_txt(pc, tmpdir):
    """Is the file output by PointCloud.to_txt identical to the input?"""
    fpath = tmpdir.join("_INPUT_DATA.txt").strpath
    pc.to_txt(fpath)
    assert np.all(pc.points == PointCloud(np.loadtxt(fpath)).points)
<commit_msg>Make pointcloud origin explicit in tests<commit_after>from simulocloud import PointCloud, Bounds
from simulocloud.exceptions import EmptyPointCloud
import pytest
import numpy as np
import cPickle as pkl
import os

""" Test data """
# Data type used for arrays
_DTYPE = np.float64
_INPUT_DATA = [[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
               [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9],
               [2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9]]

@pytest.fixture
def expected_DATA_points():
    """The points array that should be generated from `input_list`."""
    return np.array([( 0. ,  1. ,  2. ),
                     ( 0.1,  1.1,  2.1),
                     ( 0.2,  1.2,  2.2),
                     ( 0.3,  1.3,  2.3),
                     ( 0.4,  1.4,  2.4),
                     ( 0.5,  1.5,  2.5),
                     ( 0.6,  1.6,  2.6),
                     ( 0.7,  1.7,  2.7),
                     ( 0.8,  1.8,  2.8),
                     ( 0.9,  1.9,  2.9)],
                    dtype=[('x', _DTYPE), ('y', _DTYPE), ('z', _DTYPE)])

@pytest.fixture
def input_array():
    """Simple [xs, ys, zs] numpy.ndarray of `input_list`."""
    return np.array(_INPUT_DATA, dtype=_DTYPE)

@pytest.fixture
def expected_las_points(fname='ALS_points.pkl'):
    """The points array that should be generated from the example las data."""
    with open(abspath(fname), 'rb') as o:
        points = pkl.load(o)
    return points

@pytest.fixture
def pc_arr(input_array):
    """Set up a `PointCloud` instance using array test data."""
    return PointCloud(input_array)

@pytest.fixture
def none_bounds():
    """A Bounds nametuple with all bounds set to None."""
    return Bounds(*(None,)*6)

@pytest.fixture
def inf_bounds():
    """A Bounds namedtuple with all bounds set to inf/-inf."""
    return Bounds(*(np.inf,)*3 + (np.inf,)*3)
""" Helper functions """

def abspath(fname, fdir='data'):
    """Return the absolute filepath of filename in (relative) directory."""
    return os.path.join(os.path.dirname(__file__), fdir, fname)

""" Test functions """

def test_PointCloud_read_from_array(input_array, expected_DATA_points):
    """Can PointCloud initialise directly from a `[xs, ys, zs]` array?"""
    assert np.all(PointCloud(input_array).points == expected_DATA_points)

def test_PointCloud_read_from_las(expected_las_points, fname='ALS.las'):
    """Can PointCloud be constructed from a `.las` file?"""
    assert np.all(PointCloud.from_las(abspath(fname)).points == expected_las_points)

def test_arr_generation(pc_arr, input_array):
    """Does PointCloud.arr work as expected?."""
    assert np.all(pc_arr.arr == input_array.T)

def test_bounds_returns_accurate_boundary_box(pc_arr):
    """Does PointCloud.bounds accurately describe the bounding box?"""
    assert pc_arr.bounds == tuple((f(c) for f in (min, max) for c in _INPUT_DATA))

def test_len_works(pc_arr):
    """Does __len__() report the correct number of points?"""
    # Assumes lists in _INPUT_DATA are consistent length
    assert len(pc_arr) == len(_INPUT_DATA[0])

def test_cropping_with_none_bounds(pc_arr, none_bounds):
    """Does no PointCloud cropping occur when bounds of None are used?"""
    assert np.all(pc_arr.crop(none_bounds).points == pc_arr.points)

@pytest.mark.parametrize('c', ('x', 'y', 'z'))
def test_cropping_is_lower_bounds_inclusive(pc_arr, none_bounds, c):
    """Does PointCloud cropping preserve values at lower bounds?"""
    # Ensure a unique point used as minimum bound
    sorted_points = np.sort(pc_arr.points, order=[c])
    for i, minc in enumerate(sorted_points[c]):
        if i < 1: continue # at least one point must be out of bounds
        if minc != sorted_points[i-1][c]:
            lowest_point = sorted_points[i]
            break

    # Apply lower bound cropping to a single dimension
    pc_cropped = pc_arr.crop(none_bounds._replace(**{'min'+c: minc}))

    assert np.sort(pc_cropped.points, order=c)[0] == lowest_point

@pytest.mark.parametrize('c', ('x', 'y', 'z'))
def test_cropping_is_upper_bounds_exclusive(pc_arr, none_bounds, c):
    """Does PointCloud cropping omit values at upper bounds?"""
    # Ensure a unique point used as maximum bound
    rev_sorted_points = np.sort(pc_arr.points, order=[c])[::-1]
    for i, maxc in enumerate(rev_sorted_points[c]):
        if maxc != rev_sorted_points[i+1][c]:
            oob_point = rev_sorted_points[i]
            highest_point = rev_sorted_points[i+1]
            break

    # Apply upper bound cropping to a single dimension
    pc_arr = pc_arr.crop(none_bounds._replace(**{'max'+c: maxc}))

    assert (np.sort(pc_arr.points, order=c)[-1] == highest_point) and (
           oob_point not in pc_arr.points)

def test_cropping_to_nothing_raises_exception_when_specified(pc_arr, inf_bounds):
    """Does PointCloud cropping refuse to return an empty PointCloud?"""
    with pytest.raises(EmptyPointCloud):
        pc_arr.crop(inf_bounds, return_empty=False)

def test_cropping_to_nothing_returns_empty(pc_arr, inf_bounds):
    """Does PointCloud cropping return an empty PointCloud when asked?"""
    assert not len(pc_arr.crop(inf_bounds, return_empty=True))

def test_PointCloud_exports_transparently_to_txt(pc_arr, tmpdir):
    """Is the file output by PointCloud.to_txt identical to the input?"""
    fpath = tmpdir.join("_INPUT_DATA.txt").strpath
    pc_arr.to_txt(fpath)
    assert np.all(pc_arr.points == PointCloud(np.loadtxt(fpath)).points)

<|endoftext|>


<commit_before>"""
    Tests for the NURBS-Python package
    Released under The MIT License. See LICENSE file for details.
    Copyright (c) 2018 Onur Rauf Bingol

    Tests file I/O operations. Requires "pytest" to run.
"""

import os
from geomdl import BSpline, NURBS
from geomdl import exchange

FILE_NAME = 'testing'
SAMPLE_SIZE = 25
C_DEGREE = 2
C_CTRLPTS3D = [[1, 1, 0], [2, 1, -1], [2, 2, 0]]
C_KV = [0, 0, 0, 1, 1, 1]

S_DEGREE_U = 2
S_DEGREE_V = 2
S_CTRLPTS = [[0, 0, 0], [0, 1, 0], [0, 2, -3],
             [1, 0, 6], [1, 1, 0], [1, 2, 0],
             [2, 0, 0], [2, 1, 0], [2, 2, 3]]
S_KV_U = [0, 0, 0, 1, 1, 1]
S_KV_V = [0, 0, 0, 1, 1, 1]

# Tests pickled load-save operations on curves
def test_bspline_curve_loadsave():
    fname = FILE_NAME + ".pickle"

    curve_save = BSpline.Curve()
    curve_save.degree = C_DEGREE
    curve_save.ctrlpts = C_CTRLPTS3D
    curve_save.knotvector = C_KV
    curve_save.save(fname)

    curve_load = BSpline.Curve()
    curve_load.load(fname)

    # Remove save file
    os.remove(fname)

    assert curve_save.degree == curve_load.degree
    assert curve_save.knotvector == curve_load.knotvector
    assert curve_save.ctrlpts == curve_load.ctrlpts
    assert curve_save.dimension == curve_load.dimension

# Tests pickled load-save operations on surfaces
def test_bspline_surface_loadsave():
    fname = FILE_NAME + ".pickle"

    surf_save = BSpline.Surface()
    surf_save.degree_u = S_DEGREE_U
    surf_save.degree_v = S_DEGREE_V
    surf_save.ctrlpts_size_u = 3
    surf_save.ctrlpts_size_v = 3
    surf_save.ctrlpts = S_CTRLPTS
    surf_save.knotvector_u = S_KV_U
    surf_save.knotvector_v = S_KV_V
    surf_save.save(fname)

    surf_load = BSpline.Surface()
    surf_load.load(fname)

    # Remove save file
    os.remove(fname)

    assert surf_save.degree_u == surf_load.degree_u
    assert surf_save.degree_v == surf_load.degree_v
    assert surf_save.knotvector_u == surf_load.knotvector_u
    assert surf_save.knotvector_v == surf_load.knotvector_v
    assert surf_save.ctrlpts == surf_load.ctrlpts
    assert surf_save.ctrlpts_size_u == surf_load.ctrlpts_size_u
    assert surf_save.ctrlpts_size_v == surf_load.ctrlpts_size_v
    assert surf_save.dimension == surf_load.dimension

# Tests if the .obj file exists
def test_export_obj_single():
    fname = FILE_NAME + ".obj"

    surf_save = NURBS.Surface()
    surf_save.degree_u = S_DEGREE_U
    surf_save.degree_v = S_DEGREE_V
    surf_save.ctrlpts_size_u = 3
    surf_save.ctrlpts_size_v = 3
    surf_save.ctrlpts = S_CTRLPTS
    surf_save.knotvector_u = S_KV_U
    surf_save.knotvector_v = S_KV_V

    surf_save.sample_size = SAMPLE_SIZE
    exchange.export_obj(surf_save, fname)

    assert os.path.isfile(fname)
    assert os.path.getsize(fname) > 0

    # Clean up temporary file if exists
    if os.path.isfile(fname):
        os.remove(fname)

# Tests if the .off file exists
def test_export_off_single():
    fname = FILE_NAME + ".off"

    surf_save = NURBS.Surface()
    surf_save.degree_u = S_DEGREE_U
    surf_save.degree_v = S_DEGREE_V
    surf_save.ctrlpts_size_u = 3
    surf_save.ctrlpts_size_v = 3
    surf_save.ctrlpts = S_CTRLPTS
    surf_save.knotvector_u = S_KV_U
    surf_save.knotvector_v = S_KV_V

    surf_save.sample_size = SAMPLE_SIZE
    exchange.export_off(surf_save, fname)

    assert os.path.isfile(fname)
    assert os.path.getsize(fname) > 0

    # Clean up temporary file if exists
    if os.path.isfile(fname):
        os.remove(fname)

# Tests if the .stl file exists
def test_export_stl_single():
    fname = FILE_NAME + ".stl"

    surf_save = NURBS.Surface()
    surf_save.degree_u = S_DEGREE_U
    surf_save.degree_v = S_DEGREE_V
    surf_save.ctrlpts_size_u = 3
    surf_save.ctrlpts_size_v = 3
    surf_save.ctrlpts = S_CTRLPTS
    surf_save.knotvector_u = S_KV_U
    surf_save.knotvector_v = S_KV_V

    surf_save.sample_size = SAMPLE_SIZE
    exchange.export_stl(surf_save, fname)

    assert os.path.isfile(fname)
    assert os.path.getsize(fname) > 0

    # Clean up temporary file if exists
    if os.path.isfile(fname):
        os.remove(fname)

# Tests if the .stl file exists (ascii)
def test_export_stl_ascii_single():
    fname = FILE_NAME + ".stl"

    surf_save = NURBS.Surface()
    surf_save.degree_u = S_DEGREE_U
    surf_save.degree_v = S_DEGREE_V
    surf_save.ctrlpts_size_u = 3
    surf_save.ctrlpts_size_v = 3
    surf_save.ctrlpts = S_CTRLPTS
    surf_save.knotvector_u = S_KV_U
    surf_save.knotvector_v = S_KV_V

    surf_save.sample_size = SAMPLE_SIZE
    exchange.export_stl(surf_save, fname, binary=False)

    assert os.path.isfile(fname)
    assert os.path.getsize(fname) > 0

    # Clean up temporary file if exists
    if os.path.isfile(fname):
        os.remove(fname)
<commit_msg>Start using pytest fixtures in test_read_write.py<commit_after>"""
    Tests for the NURBS-Python package
    Released under The MIT License. See LICENSE file for details.
    Copyright (c) 2018 Onur Rauf Bingol

    Tests file I/O operations. Requires "pytest" to run.
"""

import os
import pytest
from geomdl import BSpline, NURBS
from geomdl import exchange

FILE_NAME = 'testing'
SAMPLE_SIZE = 25

@pytest.fixture
def bspline_curve3d():
    """ Creates a B-Spline 3-dimensional curve instance """
    curve = BSpline.Curve()
    curve.degree = 2
    curve.ctrlpts = [[1, 1, 0], [2, 1, -1], [2, 2, 0]]
    curve.knotvector = [0, 0, 0, 1, 1, 1]
    return curve

@pytest.fixture
def bspline_surface():
    """ Creates a B-Spline surface instance """
    surf = BSpline.Surface()
    surf.degree_u = 2
    surf.degree_v = 2
    surf.ctrlpts_size_u = 3
    surf.ctrlpts_size_v = 3
    surf.ctrlpts = [[0, 0, 0], [0, 1, 0], [0, 2, -3],
                    [1, 0, 6], [1, 1, 0], [1, 2, 0],
                    [2, 0, 0], [2, 1, 0], [2, 2, 3]]
    surf.knotvector_u = [0, 0, 0, 1, 1, 1]
    surf.knotvector_v = [0, 0, 0, 1, 1, 1]
    return surf

@pytest.fixture
def nurbs_surface():
    """ Creates a NURBS surface instance """
    surf = NURBS.Surface()
    surf.degree_u = 2
    surf.degree_v = 2
    surf.ctrlpts_size_u = 3
    surf.ctrlpts_size_v = 3
    surf.ctrlpts = [[0, 0, 0], [0, 1, 0], [0, 2, -3],
                    [1, 0, 6], [1, 1, 0], [1, 2, 0],
                    [2, 0, 0], [2, 1, 0], [2, 2, 3]]
    # use the auto-generated weights vector
    surf.knotvector_u = [0, 0, 0, 1, 1, 1]
    surf.knotvector_v = [0, 0, 0, 1, 1, 1]
    return surf

# Tests pickled load-save operations on curves
def test_bspline_curve_loadsave(bspline_curve3d):
    fname = FILE_NAME + ".pickle"

    bspline_curve3d.save(fname)

    curve_load = BSpline.Curve()
    curve_load.load(fname)

    # Remove save file
    os.remove(fname)

    assert bspline_curve3d.degree == curve_load.degree
    assert bspline_curve3d.knotvector == curve_load.knotvector
    assert bspline_curve3d.ctrlpts == curve_load.ctrlpts
    assert bspline_curve3d.dimension == curve_load.dimension

# Tests pickled load-save operations on surfaces
def test_bspline_surface_loadsave(bspline_surface):
    fname = FILE_NAME + ".pickle"

    bspline_surface.save(fname)

    surf_load = BSpline.Surface()
    surf_load.load(fname)

    # Remove save file
    os.remove(fname)

    assert bspline_surface.degree_u == surf_load.degree_u
    assert bspline_surface.degree_v == surf_load.degree_v
    assert bspline_surface.knotvector_u == surf_load.knotvector_u
    assert bspline_surface.knotvector_v == surf_load.knotvector_v
    assert bspline_surface.ctrlpts == surf_load.ctrlpts
    assert bspline_surface.ctrlpts_size_u == surf_load.ctrlpts_size_u
    assert bspline_surface.ctrlpts_size_v == surf_load.ctrlpts_size_v
    assert bspline_surface.dimension == surf_load.dimension

# Tests if the .obj file exists
def test_export_obj_single(nurbs_surface):
    fname = FILE_NAME + ".obj"

    nurbs_surface.sample_size = SAMPLE_SIZE
    exchange.export_obj(nurbs_surface, fname)

    assert os.path.isfile(fname)
    assert os.path.getsize(fname) > 0

    # Clean up temporary file if exists
    if os.path.isfile(fname):
        os.remove(fname)

# Tests if the .off file exists
def test_export_off_single(nurbs_surface):
    fname = FILE_NAME + ".off"

    nurbs_surface.sample_size = SAMPLE_SIZE
    exchange.export_off(nurbs_surface, fname)

    assert os.path.isfile(fname)
    assert os.path.getsize(fname) > 0

    # Clean up temporary file if exists
    if os.path.isfile(fname):
        os.remove(fname)

# Tests if the .stl file exists
def test_export_stl_single(nurbs_surface):
    fname = FILE_NAME + ".stl"

    nurbs_surface.sample_size = SAMPLE_SIZE
    exchange.export_stl(nurbs_surface, fname)

    assert os.path.isfile(fname)
    assert os.path.getsize(fname) > 0

    # Clean up temporary file if exists
    if os.path.isfile(fname):
        os.remove(fname)

# Tests if the .stl file exists (ascii)
def test_export_stl_ascii_single(nurbs_surface):
    fname = FILE_NAME + ".stl"

    nurbs_surface.sample_size = SAMPLE_SIZE
    exchange.export_stl(nurbs_surface, fname, binary=False)

    assert os.path.isfile(fname)
    assert os.path.getsize(fname) > 0

    # Clean up temporary file if exists
    if os.path.isfile(fname):
        os.remove(fname)
<|endoftext|>


<commit_before># This file contains unit tests of the regression implementatipon in
# mlroots/supervised/regression.py
#
# Part of mlroots, a project to implement well-known machine learning
# algorithms from scratch
#
# Originally authored by Adams Rosales <adamisto92@gmail.com>
#
# Licensed under MIT License (see LICENSE.txt for details)

import unittest
import numpy as np
import pandas as pd

from mlroots.supervised import regression
from mlroots.errors.errors import *

class BaseTests:

    class FitTest(unittest.TestCase):
        """ Test cases to test the result of fitting univariate and
        multivariate regressions on dummy data

        Verifies that the coefficients, MSE, R-squared, F score,
        confidence intervals, and p-value estimates are correct
        """

        def setUp(self):
            """ Instantiates regression objects and gets data to test """
            self.uni_x = np.array([1,2,3,4,5,6,7,8,9,10])
            self.multi_x = np.array([[1,2,3,4,5,6,7,8,9,10],
                                     [20,17,18,2,3,4,1,19,17,8]])
            self.multi_x_df = pd.DataFrame(np.transpose(self.multi_x))
            self.y = np.array([-3.2,-36.3,100.4,55.9,
                                73.4,69.9,90.1,-85.5,
                               -39.1, 126.1])

            # Linear model with one feature
            self.uni_coeff = np.array([24.713,1.901])
            self.multi_coeff = np.array([110.397,-1.885,-5.950])

            self.uni_model = regression.LinMod(self.y, self.uni_x)
            self.uni_fit = self.uni_model.fit()

            # Linear model with more than 1 feature
            self.multi_model = regression.LinMod(
                self.y,
                np.transpose(self.multi_x)
            )
            self.multi_fit = self.multi_model.fit()

            # Linear model from data frame predictor data
            self.multi_model_df = regression.LinMod.from_df(
                self.y,
                self.multi_x_df
            )
            self.multi_df_fit = self.multi_model_df.fit()

            # Other regression parameters
            self.multi_r_sq = 0.4252
            self.adj_r_sq = 0.2609
            self.mse = 3719.78
            self.multi_p = np.array([0.0910, 0.7932, 0.0585])

        def test_uni_coeff(self):
            """ Tests univariate coefficients """
            for idx, coeff in enumerate(self.uni_fit):
                self.assertAlmostEqual(coeff, self.uni_coeff[idx], places = 3,
                    msg = "Univariate regression coefficient incorrectly calculated"
                )

        def test_multi_coeff(self):
            """ Tests multivariate coefficients """
            for idx, coeff in enumerate(self.multi_fit):
                self.assertAlmostEqual(coeff, self.multi_coeff[idx], places = 3,
                    msg = "Multivariate regression coefficient incorrectly calculated"
                )

        def test_multi_df_coeff(self):
            """ Tests multivariate coefficients from df formatted model """
            for idx, coeff in enumerate(self.multi_df_fit):
                self.assertAlmostEqual(coeff, self.multi_coeff[idx], places = 3,
                    msg = "From df LinMod class method yielding wrong coefficient"
                )

        def test_rsq(self):
            """ Tests multiple R-squared """
            self.assertAlmostEqual(self.multi_model.r_sq, self.multi_r_sq, places = 3,
                msg = "Multiple R squared incorrectly calculated"
            )

        def test_adj_rsq(self):
            """ Tests adjusted R-squared """
            self.assertAlmostEqual(self.multi_model.adj_r_sq, self.adj_r_sq, places = 3,
                msg = "Adjusted R squared incorrectly calculated"
            )

        def test_p_val(self):
            """ Tests coefficient p values """
            for idx, p in enumerate(self.multi_p):
                self.assertAlmostEqual(self.multi_model.coeff_p[idx], p, places = 3,
                    msg = "Coefficient p-value incorrectly calculated"
                )

        def test_mse(self):
            """ Tests MSE """
            self.assertAlmostEqual(self.multi_model.mse, self.mse, places = 0,
                msg = "MSE incorrectly calculated"
            )

class PredictionTest(BaseTests.FitTest):
    """ Test cases to test that the regression models are correctly predicting
    based on training data
    """

    def test_dimension_error(self):
        """ Test to see if a dimension error is raised upon receiving an input
        with a different number of features than the training data has """
        test_matrix = np.array([[1,2],[2,3]])

        self.assertRaises(LengthMismatchError, self.multi_model.predict, test_matrix)

    def test_input_error(self):
        """ Test to see if an input error is raised when something other than a list
        or numpy array is received """
        self.assertRaises(DataInputError, self.multi_model.predict, "test")

if __name__ == "__main__":
    unittest.main()
<commit_msg>Update test_regression.py<commit_after># This file contains unit tests of the regression implementation in mlroots/
# supervised/regression.py
#
# Part of mlroots, a project to implement well-known machine learning  algorithms
# from scratch
#
# Originally authored by Adams Rosales <adamisto92@gmail.com>
#
# Licensed under MIT License (see LICENSE.txt for details)

import unittest
import numpy as np
import pandas as pd

from mlroots.supervised import regression
from mlroots.errors.errors import *

class BaseTests:

    class FitTest(unittest.TestCase):
        """ Test cases to test the result of fitting univariate and
        multivariate regressions on dummy data

        Verifies that the coefficients, MSE, R-squared, F score,
        confidence intervals, and p-value estimates are correct
        """

        def setUp(self):
            """ Instantiates regression objects and gets data to test """
            self.uni_x = np.array([1,2,3,4,5,6,7,8,9,10])
            self.multi_x = np.array([[1,2,3,4,5,6,7,8,9,10],
                                     [20,17,18,2,3,4,1,19,17,8]])
            self.multi_x_df = pd.DataFrame(np.transpose(self.multi_x))
            self.y = np.array([-3.2,-36.3,100.4,55.9,
                                73.4,69.9,90.1,-85.5,
                               -39.1, 126.1])

            # Linear model with one feature
            self.uni_coeff = np.array([24.713,1.901])
            self.multi_coeff = np.array([110.397,-1.885,-5.950])

            self.uni_model = regression.LinMod(self.y, self.uni_x)
            self.uni_fit = self.uni_model.fit()

            # Linear model with more than 1 feature
            self.multi_model = regression.LinMod(
                self.y,
                np.transpose(self.multi_x)
            )
            self.multi_fit = self.multi_model.fit()

            # Linear model from data frame predictor data
            self.multi_model_df = regression.LinMod.from_df(
                self.y,
                self.multi_x_df
            )
            self.multi_df_fit = self.multi_model_df.fit()

            # Other regression parameters
            self.multi_r_sq = 0.4252
            self.adj_r_sq = 0.2609
            self.mse = 3719.78
            self.multi_p = np.array([0.0910, 0.7932, 0.0585])

        def test_uni_coeff(self):
            """ Tests univariate coefficients """
            for idx, coeff in enumerate(self.uni_fit):
                self.assertAlmostEqual(coeff, self.uni_coeff[idx], places = 3,
                    msg = "Univariate regression coefficient incorrectly calculated"
                )

        def test_multi_coeff(self):
            """ Tests multivariate coefficients """
            for idx, coeff in enumerate(self.multi_fit):
                self.assertAlmostEqual(coeff, self.multi_coeff[idx], places = 3,
                    msg = "Multivariate regression coefficient incorrectly calculated"
                )

        def test_multi_df_coeff(self):
            """ Tests multivariate coefficients from df formatted model """
            for idx, coeff in enumerate(self.multi_df_fit):
                self.assertAlmostEqual(coeff, self.multi_coeff[idx], places = 3,
                    msg = "From df LinMod class method yielding wrong coefficient"
                )

        def test_rsq(self):
            """ Tests multiple R-squared """
            self.assertAlmostEqual(self.multi_model.r_sq, self.multi_r_sq, places = 3,
                msg = "Multiple R squared incorrectly calculated"
            )

        def test_adj_rsq(self):
            """ Tests adjusted R-squared """
            self.assertAlmostEqual(self.multi_model.adj_r_sq, self.adj_r_sq, places = 3,
                msg = "Adjusted R squared incorrectly calculated"
            )

        def test_p_val(self):
            """ Tests coefficient p values """
            for idx, p in enumerate(self.multi_p):
                self.assertAlmostEqual(self.multi_model.coeff_p[idx], p, places = 3,
                    msg = "Coefficient p-value incorrectly calculated"
                )

        def test_mse(self):
            """ Tests MSE """
            self.assertAlmostEqual(self.multi_model.mse, self.mse, places = 0,
                msg = "MSE incorrectly calculated"
            )

class PredictionTest(BaseTests.FitTest):
    """ Test cases to test that the regression models are correctly predicting
    based on training data
    """

    def test_dimension_error(self):
        """ Test to see if a dimension error is raised upon receiving an input
        with a different number of features than the training data has """
        test_matrix = np.array([[1,2],[2,3]])

        self.assertRaises(LengthMismatchError, self.multi_model.predict, test_matrix)

    def test_input_error(self):
        """ Test to see if an input error is raised when something other than a list
        or numpy array is received """
        self.assertRaises(DataInputError, self.multi_model.predict, "test")

if __name__ == "__main__":
    unittest.main()
<|endoftext|>


<commit_before>from apps.bluebottle_utils.managers import GenericForeignKeyManagerMixin
from django.db import models
from django.utils.text import Truncator
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django_extensions.db.fields import ModificationDateTimeField, CreationDateTimeField
from polymorphic import PolymorphicModel, PolymorphicManager

class WallPostManager(GenericForeignKeyManagerMixin, PolymorphicManager):
    pass

# This base class will never be used directly because  the content of the wall posts is always defined in the child
# classes. Normally this would be an abstract class but it's not possible to make this an abstract class and have the
# polymorphic behaviour of sorting on the common fields.
class WallPost(PolymorphicModel):
    # The user who wrote the wall post. This can be empty to support wall posts without users (e.g. anonymous text wall
    # posts, system wall posts)
    author = models.ForeignKey('auth.User', verbose_name=_('author'), related_name="%(class)s_reactions", blank=True, null=True)

    # The metadata for the wall post.
    created = CreationDateTimeField()
    updated = ModificationDateTimeField()
    deleted = models.DateTimeField(blank=True, null=True)
    ip_address = models.IPAddressField(_('IP address'))

    # Generic foreign key so we can connect it to any object.
    content_type = models.ForeignKey(ContentType, verbose_name=_('content type'), related_name="content_type_set_for_%(class)s")
    object_id = models.PositiveIntegerField(_('object ID'))
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    # Manager
    objects = WallPostManager()

    class Meta:
        ordering = ('created',)

class MediaWallPost(WallPost):
    # The content of the wall post.
    title = models.CharField(max_length=60)
    text = models.TextField(max_length=300, blank=True)
    video_url = models.URLField(max_length=100, blank=True)

    def __unicode__(self):
        s = "MediaWallPost: {0}".format(self.text)
        return Truncator(s).words(10)

class TextWallPost(WallPost):
    # The content of the wall post.
    text = models.TextField(max_length=300)

    def __unicode__(self):
        s = "TextWallPost: {0}".format(self.text)
        return Truncator(s).words(10)
<commit_msg>Allow IP to be Null in wallposts<commit_after>from apps.bluebottle_utils.managers import GenericForeignKeyManagerMixin
from django.db import models
from django.utils.text import Truncator
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django_extensions.db.fields import ModificationDateTimeField, CreationDateTimeField
from polymorphic import PolymorphicModel, PolymorphicManager

class WallPostManager(GenericForeignKeyManagerMixin, PolymorphicManager):
    pass

# This base class will never be used directly because  the content of the wall posts is always defined in the child
# classes. Normally this would be an abstract class but it's not possible to make this an abstract class and have the
# polymorphic behaviour of sorting on the common fields.
class WallPost(PolymorphicModel):
    # The user who wrote the wall post. This can be empty to support wall posts without users (e.g. anonymous text wall
    # posts, system wall posts)
    author = models.ForeignKey('auth.User', verbose_name=_('author'), related_name="%(class)s_reactions", blank=True, null=True)

    # The metadata for the wall post.
    created = CreationDateTimeField()
    updated = ModificationDateTimeField()
    deleted = models.DateTimeField(blank=True, null=True)
    ip_address = models.IPAddressField(_('IP address'), blank=True, null=True)

    # Generic foreign key so we can connect it to any object.
    content_type = models.ForeignKey(ContentType, verbose_name=_('content type'), related_name="content_type_set_for_%(class)s")
    object_id = models.PositiveIntegerField(_('object ID'))
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    # Manager
    objects = WallPostManager()

    class Meta:
        ordering = ('created',)

class MediaWallPost(WallPost):
    # The content of the wall post.
    title = models.CharField(max_length=60)
    text = models.TextField(max_length=300, blank=True)
    video_url = models.URLField(max_length=100, blank=True)

    def __unicode__(self):
        s = "MediaWallPost: {0}".format(self.text)
        return Truncator(s).words(10)

class TextWallPost(WallPost):
    # The content of the wall post.
    text = models.TextField(max_length=300)

    def __unicode__(self):
        s = "TextWallPost: {0}".format(self.text)
        return Truncator(s).words(10)
<|endoftext|>


<commit_before>import os
from contextlib import contextmanager
from textwrap import dedent

from _stbt.config import _config_init, _sponge, get_config, set_config

@contextmanager
def _directory_sandbox():
    from tempfile import mkdtemp
    from shutil import rmtree
    from os import chdir
    d = mkdtemp()
    try:
        chdir(d)
        yield d
    finally:
        rmtree(d, ignore_errors=True)

def test_sponge_that_new_data_end_up_in_file():
    with _directory_sandbox():
        with _sponge('hello') as f:
            f.write('hello')
        assert open('hello').read() == 'hello'

def test_sponge_that_on_exception_file_isnt_modified():
    with _directory_sandbox():
        open('foo', 'w').write('bar')
        try:
            with _sponge('foo') as f:
                f.write('hello')
                raise RuntimeError()
        except RuntimeError:
            pass
        assert open('foo').read() == 'bar'

test_config = dedent("""\
    [global]
    # A comment
    test=hello
    another_test = goodbye""")

@contextmanager
def set_config_test():
    with _directory_sandbox() as d:
        test_cfg = d + '/test.cfg'
        os.environ['STBT_CONFIG_FILE'] = test_cfg
        with open(test_cfg, 'w') as f:
            f.write(test_config)
        yield

def test_that_set_config_modifies_config_value():
    with set_config_test():
        set_config('global', 'test', 'goodbye')
        assert get_config('global', 'test', 'goodbye')
        _config_init(force=True)
        assert get_config('global', 'test', 'goodbye')

def test_that_set_config_creates_new_sections_if_required():
    with set_config_test():
        set_config('non_existent_section', 'test', 'goodbye')
        assert get_config('non_existent_section', 'test', 'goodbye')
        _config_init(force=True)
        assert get_config('non_existent_section', 'test', 'goodbye')

def test_that_set_config_preserves_file_comments_and_formatting():
    # pylint:disable=W0511,W0101
    # FIXME: Preserve comments and formatting.  This is fairly tricky as
    # comments and whitespace are not currently stored in Python's internal
    # ConfigParser representation and multiline values makes just using regex
    # tricky.
    from nose import SkipTest
    raise SkipTest("set_config doesn't currently preserve formatting")
    with set_config_test():
        set_config('global', 'test', 'goodbye')
        assert open('test.cfg', 'r').read() == test_config.replace(
            'hello', 'goodbye')

def test_that_set_config_creates_directories_if_required():
    with _directory_sandbox() as d:
        os.environ['XDG_CONFIG_HOME'] = d + '/.config'
        if 'STBT_CONFIG_FILE' in os.environ:
            del os.environ['STBT_CONFIG_FILE']
        set_config('global', 'test', 'hello2')
        assert os.path.isfile(d + '/.config/stbt/stbt.conf')
        _config_init(force=True)
        assert get_config('global', 'test') == 'hello2'

def test_that_set_config_writes_to_the_first_stbt_config_file():
    with _directory_sandbox() as d:
        filled_cfg = d + '/test.cfg'
        empty_cfg = d + '/empty.cfg'
        os.environ['STBT_CONFIG_FILE'] = '%s:%s' % (filled_cfg, empty_cfg)
        open(filled_cfg, 'w')
        open(empty_cfg, 'w')
        set_config('global', 'test', 'goodbye')
        assert open(filled_cfg).read().startswith('[global]')
        assert open(empty_cfg).read() == ''
<commit_msg>tests: Fix `set_config` tests<commit_after>import os
from contextlib import contextmanager
from textwrap import dedent

from _stbt.config import _config_init, _sponge, get_config, set_config

@contextmanager
def _directory_sandbox():
    from tempfile import mkdtemp
    from shutil import rmtree
    from os import chdir
    d = mkdtemp()
    try:
        chdir(d)
        yield d
    finally:
        rmtree(d, ignore_errors=True)

def test_sponge_that_new_data_end_up_in_file():
    with _directory_sandbox():
        with _sponge('hello') as f:
            f.write('hello')
        assert open('hello').read() == 'hello'

def test_sponge_that_on_exception_file_isnt_modified():
    with _directory_sandbox():
        open('foo', 'w').write('bar')
        try:
            with _sponge('foo') as f:
                f.write('hello')
                raise RuntimeError()
        except RuntimeError:
            pass
        assert open('foo').read() == 'bar'

test_config = dedent("""\
    [global]
    # A comment
    test=hello
    another_test = goodbye""")

@contextmanager
def set_config_test():
    with _directory_sandbox() as d:
        test_cfg = d + '/test.cfg'
        os.environ['STBT_CONFIG_FILE'] = test_cfg
        with open(test_cfg, 'w') as f:
            f.write(test_config)
        yield

def test_that_set_config_modifies_config_value():
    with set_config_test():
        set_config('global', 'test', 'goodbye')
        assert get_config('global', 'test') == 'goodbye'
        _config_init(force=True)
        assert get_config('global', 'test') == 'goodbye'

def test_that_set_config_creates_new_sections_if_required():
    with set_config_test():
        set_config('non_existent_section', 'test', 'goodbye')
        assert get_config('non_existent_section', 'test') == 'goodbye'
        _config_init(force=True)
        assert get_config('non_existent_section', 'test') == 'goodbye'

def test_that_set_config_preserves_file_comments_and_formatting():
    # pylint:disable=W0511,W0101
    # FIXME: Preserve comments and formatting.  This is fairly tricky as
    # comments and whitespace are not currently stored in Python's internal
    # ConfigParser representation and multiline values makes just using regex
    # tricky.
    from nose import SkipTest
    raise SkipTest("set_config doesn't currently preserve formatting")
    with set_config_test():
        set_config('global', 'test', 'goodbye')
        assert open('test.cfg', 'r').read() == test_config.replace(
            'hello', 'goodbye')

def test_that_set_config_creates_directories_if_required():
    with _directory_sandbox() as d:
        os.environ['XDG_CONFIG_HOME'] = d + '/.config'
        if 'STBT_CONFIG_FILE' in os.environ:
            del os.environ['STBT_CONFIG_FILE']
        set_config('global', 'test', 'hello2')
        assert os.path.isfile(d + '/.config/stbt/stbt.conf')
        _config_init(force=True)
        assert get_config('global', 'test') == 'hello2'

def test_that_set_config_writes_to_the_first_stbt_config_file():
    with _directory_sandbox() as d:
        filled_cfg = d + '/test.cfg'
        empty_cfg = d + '/empty.cfg'
        os.environ['STBT_CONFIG_FILE'] = '%s:%s' % (filled_cfg, empty_cfg)
        open(filled_cfg, 'w')
        open(empty_cfg, 'w')
        set_config('global', 'test', 'goodbye')
        assert open(filled_cfg).read().startswith('[global]')
        assert open(empty_cfg).read() == ''
<|endoftext|>