<commit_before>#!/usr/bin/env python

###########################################################################################################################################
#1. user defined root(may or may not be a mutated gene for the sample); get all interacting partners from the variant list of the sample
#2. For a root fixed to a lof gene in each sample, get the interacting partners
#3. a network graph of mutated genes for each sample, a subnetwork for any gene in that list (not executed here)
###########################################################################################################################################

import os
import sys
import sqlite3
import numpy as np
import zlib
import cPickle
from gemini.config import read_gemini_config
from pygraph.classes.graph import graph
from pygraph.readwrite.dot import write
from pygraph.algorithms.searching import breadth_first_search
from pygraph.algorithms.minmax import shortest_path
from pygraph.classes.exceptions import AdditionError
from pygraph.algorithms.filters.radius import radius
from pygraph.classes.digraph import digraph
import gemini_utils as util
from gemini_constants import *
from collections import defaultdict

config = read_gemini_config()
path_dirname = config["annotation_dir"]
out = open("file.dot", 'w')
samples = defaultdict(list)
lof = defaultdict(list)

def get_variant_genes(c, args, idx_to_sample):
    for r in c:
        gt_types = np.array(cPickle.loads(zlib.decompress(r['gt_types'])))
        gts      = np.array(cPickle.loads(zlib.decompress(r['gts'])))
        var_id = str(r['variant_id'])
        chrom = str(r['chrom'])
        start = str(r['start'])
        end = str(r['end'])
        gene     = str(r['gene'])
        impact = str(r['impact'])
        biotype = str(r['biotype'])
        in_dbsnp = str(r['in_dbsnp'])
        clin_sigs = str(r['clin_sigs'])
        aaf_1kg_all = str(r['aaf_1kg_all'])
        aaf_esp_all = str(r['aaf_esp_all'])

        for idx, gt_type in enumerate(gt_types):
            if (gt_type == HET or gt_type == HOM_ALT):
                if gene != "None":
                    (key, value) = (idx_to_sample[idx], \
                                   (gene,var_id,chrom,start,end,impact, \
                                   biotype,in_dbsnp,clin_sigs,aaf_1kg_all, \
                                   aaf_esp_all))
                    samples[idx_to_sample[idx]].append(value)
    return samples

def get_lof_genes(c, args, idx_to_sample):
    for r in c:
        gt_types = np.array(cPickle.loads(zlib.decompress(r['gt_types'])))
        gts      = np.array(cPickle.loads(zlib.decompress(r['gts'])))
        gene     = str(r['gene'])

        for idx, gt_type in enumerate(gt_types):
            if (gt_type == HET or gt_type == HOM_ALT):
                if gene != "None":
                    (key, value) = (idx_to_sample[idx], gene)
                    lof[idx_to_sample[idx]].append(gene)
    return lof

def sample_gene_interactions(c, args, idx_to_sample):
    #fetch variant gene dict for all samples
    get_variant_genes(c, args, idx_to_sample)
    #file handle for fetching the hprd graph
    file_graph = os.path.join(path_dirname, 'hprd_interaction_graph')
    #load the graph using cPickle and close file handle
    gr = graph()
    f = open(file_graph, 'rb')
    gr = cPickle.load(f)
    f.close()
    k = []
    variants = []
    #calculate nodes from the graph
    hprd_genes = gr.nodes()
    if args.gene == None or args.gene not in hprd_genes:
        sys.stderr.write("Gene name not found or")
        sys.stderr.write(" gene not in p-p interaction file\n")

    elif args.gene in hprd_genes:
        x, y = \
            breadth_first_search(gr,root=args.gene,filter=radius(args.radius))
        gst = digraph()
        gst.add_spanning_tree(x)
        dot = write(gst)
        out.write(dot)
        st, sd = shortest_path(gst, args.gene)

        if args.var_mode:
            for sample in samples.iterkeys():
                var = samples[str(sample)]
                #for each level return interacting genes if they are
                # variants in the sample.
                # 0th order would be returned if the user chosen
                # gene is a variant in the sample
                for x in range(0, (args.radius+1)):
                    for each in var:
                        for key, value in sd.iteritems():
                            if value == x and key == each[0]:
                                print "\t".join([str(sample),str(args.gene), \
                                          str(x), \
                                          str(key), \
                                          str(each[1]), \
                                          str(each[2]), \
                                          str(each[3]), \
                                          str(each[4]), \
                                          str(each[5]), \
                                          str(each[6]), \
                                          str(each[7]), \
                                          str(each[8]), \
                                          str(each[9]), \
                                          str(each[10])])
        elif (not args.var_mode):
            for sample in samples.iterkeys():
                for each in samples[str(sample)]:
                    variants.append(each[0])
                for x in range(0, (args.radius+1)):
                    for key, value in sd.iteritems():
                        if value == x and key in set(variants):
                            k.append(key)
                    if k:
                        print "\t".join([str(sample), str(args.gene), \
                                 str(x)+"_order:",
                                 ",".join(k)])
                    else:
                        print "\t".join([str(sample), str(args.gene), \
                                         str(x)+"_order:", "none"])
                    #initialize keys for next iteration
                    k = []

def sample_lof_interactions(c, args, idx_to_sample):
    get_lof_genes(c, args, idx_to_sample)
    get_variant_genes(c, args, idx_to_sample)
    #file handle for fetching the hprd graph
    file_graph = os.path.join(path_dirname, 'hprd_interaction_graph')
    #load the graph using cPickle and close file handle
    gr = graph()
    f = open(file_graph, 'rb')
    gr = cPickle.load(f)
    f.close()
    #calculate nodes from the graph
    hprd_genes = gr.nodes()
    #initialize keys
    k = []
    variants = []

    if (not args.var_mode):
        for sample in lof.iterkeys():
            lofvariants = list(set(lof[str(sample)]))
            for each in samples[str(sample)]:
                variants.append(each[0])

            for gene in lofvariants:
                if gene in hprd_genes:
                    x, y = \
                        breadth_first_search(gr,root=gene,\
                        filter=radius(args.radius))

                    gst = digraph()
                    gst.add_spanning_tree(x)
                    st, sd = shortest_path(gst, gene)
                    # for each level return interacting genes
                    # if they are variants in the sample.
                    for rad in range(1, (args.radius+1)):
                        for key, value in sd.iteritems():
                            if (value == rad) and key in set(variants):
                                k.append(key)
                        if k:
                            print "\t".join([str(sample), \
                                       str(gene), \
                                       str(rad)+"_order:",
                                       ",".join(k)])
                        else:
                            print "\t".join([str(sample), \
                                       str(gene), \
                                       str(rad)+"_order:", \
                                       "none"])
                        #initialize k
                        k = []
    elif args.var_mode:
        for sample in lof.iterkeys():
            lofvariants = list(set(lof[str(sample)]))
            var = samples[str(sample)]
            for gene in lofvariants:
                if gene in hprd_genes:
                    x, y = \
                         breadth_first_search(gr,root=gene, \
                         filter=radius(args.radius))
                    gst = digraph()
                    gst.add_spanning_tree(x)
                    st, sd = shortest_path(gst, gene)
                    for rad in range(1, (args.radius+1)):
                        for each in var:
                            for key, value in sd.iteritems():
                                if value == rad and key == each[0]:
                                    print "\t".join([str(sample), \
                                               str(gene), \
                                               str(rad), \
                                               str(key), \
                                               str(each[1]), \
                                               str(each[2]), \
                                               str(each[3]), \
                                               str(each[4]), \
                                               str(each[5]), \
                                               str(each[6]), \
                                               str(each[7]), \
                                               str(each[8]), \
                                               str(each[9]), \
                                               str(each[10])])

def sample_variants(c, args):
    idx_to_sample = util.map_indicies_to_samples(c)
    query = "SELECT variant_id, gt_types, gts, gene, impact, biotype, \
           in_dbsnp, clin_sigs, aaf_1kg_all, aaf_esp_all, chrom, start, end  \
          FROM variants"
    c.execute(query)

    if args.var_mode:
        print "\t".join(['sample','gene','order_of_interaction', \
                         'interacting_gene', 'var_id', 'chrom', 'start', \
                         'end', 'impact','biotype', 'in_dbsnp', 'clin_sigs', \
                         'aaf_1kg_all','aaf_esp_all'])
    elif (not args.var_mode):
        print "\t".join(['sample','gene','order_of_interaction', \
                     'interacting_gene'])
    sample_gene_interactions(c, args, idx_to_sample)

def sample_lof_variants(c, args):
    idx_to_sample = util.map_indicies_to_samples(c)
    query = "SELECT chrom, start, end, \
                             gt_types, gts, gene \
             FROM variants \
             WHERE is_lof='1'"
    c.execute(query)

    sample_lof_interactions(c, args, idx_to_sample)

def variants(c, args):
    idx_to_sample = util.map_indicies_to_samples(c)
    query = "SELECT variant_id, gt_types, gts, gene, \
             impact, biotype, in_dbsnp, clin_sigs, aaf_1kg_all, \
             aaf_esp_all, chrom, start, end \
             FROM variants"
    c.execute(query)

    if args.var_mode:
        print "\t".join(['sample','lof_gene','order_of_interaction', \
                    'interacting_gene', 'var_id', 'chrom', 'start', 'end', \
                    'impact','biotype','in_dbsnp', 'clin_sigs', \
                    'aaf_1kg_all','aaf_esp_all'])
    elif (not args.var_mode):
        print "\t".join(['sample','lof_gene','order_of_interaction', \
                    'interacting_gene'])

    get_variant_genes(c, args, idx_to_sample)

def genequery(parser, args):
    if os.path.exists(args.db):
        conn = sqlite3.connect(args.db)
        conn.isolation_level = None
        conn.row_factory = sqlite3.Row
        c = conn.cursor()
        sample_variants(c, args)

def lofgenequery(parser, args):
    if os.path.exists(args.db):
        conn = sqlite3.connect(args.db)
        conn.isolation_level = None
        conn.row_factory = sqlite3.Row
        c = conn.cursor()
        variants(c, args)
        sample_lof_variants(c, args)
<commit_msg>change logic to handle global variables<commit_after>#!/usr/bin/env python

###########################################################################################################################################
#1. user defined root(may or may not be a mutated gene for the sample); get all interacting partners from the variant list of the sample
#2. For a root fixed to a lof gene in each sample, get the interacting partners
#3. a network graph of mutated genes for each sample, a subnetwork for any gene in that list (not executed here)
###########################################################################################################################################

import os
import sys
import sqlite3
import numpy as np
import zlib
import cPickle
from gemini.config import read_gemini_config
from pygraph.classes.graph import graph
from pygraph.readwrite.dot import write
from pygraph.algorithms.searching import breadth_first_search
from pygraph.algorithms.minmax import shortest_path
from pygraph.classes.exceptions import AdditionError
from pygraph.algorithms.filters.radius import radius
from pygraph.classes.digraph import digraph
import gemini_utils as util
from gemini_constants import *
from collections import defaultdict

config = read_gemini_config()
path_dirname = config["annotation_dir"]

def get_variant_genes(c, args, idx_to_sample):
    samples = defaultdict(list)
    for r in c:
        gt_types = np.array(cPickle.loads(zlib.decompress(r['gt_types'])))
        gts      = np.array(cPickle.loads(zlib.decompress(r['gts'])))
        var_id = str(r['variant_id'])
        chrom = str(r['chrom'])
        start = str(r['start'])
        end = str(r['end'])
        gene     = str(r['gene'])
        impact = str(r['impact'])
        biotype = str(r['biotype'])
        in_dbsnp = str(r['in_dbsnp'])
        clin_sigs = str(r['clin_sigs'])
        aaf_1kg_all = str(r['aaf_1kg_all'])
        aaf_esp_all = str(r['aaf_esp_all'])

        for idx, gt_type in enumerate(gt_types):
            if (gt_type == HET or gt_type == HOM_ALT):
                if gene != "None":
                    (key, value) = (idx_to_sample[idx], \
                                   (gene,var_id,chrom,start,end,impact, \
                                   biotype,in_dbsnp,clin_sigs,aaf_1kg_all, \
                                   aaf_esp_all))
                    samples[idx_to_sample[idx]].append(value)
    return samples

def get_lof_genes(c, args, idx_to_sample):
    lof = defaultdict(list)
    for r in c:
        gt_types = np.array(cPickle.loads(zlib.decompress(r['gt_types'])))
        gts      = np.array(cPickle.loads(zlib.decompress(r['gts'])))
        gene     = str(r['gene'])

        for idx, gt_type in enumerate(gt_types):
            if (gt_type == HET or gt_type == HOM_ALT):
                if gene != "None":
                    (key, value) = (idx_to_sample[idx], gene)
                    lof[idx_to_sample[idx]].append(gene)
    return lof

def sample_gene_interactions(c, args, idx_to_sample):
    out = open("file.dot", 'w')
    #fetch variant gene dict for all samples
    samples = get_variant_genes(c, args, idx_to_sample)
    #file handle for fetching the hprd graph
    file_graph = os.path.join(path_dirname, 'hprd_interaction_graph')
    #load the graph using cPickle and close file handle
    gr = graph()
    f = open(file_graph, 'rb')
    gr = cPickle.load(f)
    f.close()
    k = []
    variants = []
    #calculate nodes from the graph
    hprd_genes = gr.nodes()
    if args.gene == None or args.gene not in hprd_genes:
        sys.stderr.write("Gene name not found or")
        sys.stderr.write(" gene not in p-p interaction file\n")

    elif args.gene in hprd_genes:
        x, y = \
            breadth_first_search(gr,root=args.gene,filter=radius(args.radius))
        gst = digraph()
        gst.add_spanning_tree(x)
        dot = write(gst)
        out.write(dot)
        st, sd = shortest_path(gst, args.gene)

        if args.var_mode:
            for sample in samples.iterkeys():
                var = samples[str(sample)]
                #for each level return interacting genes if they are
                # variants in the sample.
                # 0th order would be returned if the user chosen
                # gene is a variant in the sample
                for x in range(0, (args.radius+1)):
                    for each in var:
                        for key, value in sd.iteritems():
                            if value == x and key == each[0]:
                                print "\t".join([str(sample),str(args.gene), \
                                          str(x), \
                                          str(key), \
                                          str(each[1]), \
                                          str(each[2]), \
                                          str(each[3]), \
                                          str(each[4]), \
                                          str(each[5]), \
                                          str(each[6]), \
                                          str(each[7]), \
                                          str(each[8]), \
                                          str(each[9]), \
                                          str(each[10])])
        elif (not args.var_mode):
            for sample in samples.iterkeys():
                for each in samples[str(sample)]:
                    variants.append(each[0])
                for x in range(0, (args.radius+1)):
                    for key, value in sd.iteritems():
                        if value == x and key in set(variants):
                            k.append(key)
                    if k:
                        print "\t".join([str(sample), str(args.gene), \
                                 str(x)+"_order:",
                                 ",".join(k)])
                    else:
                        print "\t".join([str(sample), str(args.gene), \
                                         str(x)+"_order:", "none"])
                    #initialize keys for next iteration
                    k = []

def sample_lof_interactions(c, args, idx_to_sample, samples):
    lof = get_lof_genes(c, args, idx_to_sample)
    #file handle for fetching the hprd graph
    file_graph = os.path.join(path_dirname, 'hprd_interaction_graph')
    #load the graph using cPickle and close file handle
    gr = graph()
    f = open(file_graph, 'rb')
    gr = cPickle.load(f)
    f.close()
    #calculate nodes from the graph
    hprd_genes = gr.nodes()
    #initialize keys
    k = []
    variants = []

    if (not args.var_mode):
        for sample in lof.iterkeys():
            lofvariants = list(set(lof[str(sample)]))
            for each in samples[str(sample)]:
                variants.append(each[0])

            for gene in lofvariants:
                if gene in hprd_genes:
                    x, y = \
                        breadth_first_search(gr,root=gene,\
                        filter=radius(args.radius))

                    gst = digraph()
                    gst.add_spanning_tree(x)
                    st, sd = shortest_path(gst, gene)
                    # for each level return interacting genes
                    # if they are variants in the sample.
                    for rad in range(1, (args.radius+1)):
                        for key, value in sd.iteritems():
                            if (value == rad) and key in set(variants):
                                k.append(key)
                        if k:
                            print "\t".join([str(sample), \
                                       str(gene), \
                                       str(rad)+"_order:",
                                       ",".join(k)])
                        else:
                            print "\t".join([str(sample), \
                                       str(gene), \
                                       str(rad)+"_order:", \
                                       "none"])
                        #initialize k
                        k = []
    elif args.var_mode:
        for sample in lof.iterkeys():
            lofvariants = list(set(lof[str(sample)]))
            var = samples[str(sample)]
            for gene in lofvariants:
                if gene in hprd_genes:
                    x, y = \
                         breadth_first_search(gr,root=gene, \
                         filter=radius(args.radius))
                    gst = digraph()
                    gst.add_spanning_tree(x)
                    st, sd = shortest_path(gst, gene)
                    for rad in range(1, (args.radius+1)):
                        for each in var:
                            for key, value in sd.iteritems():
                                if value == rad and key == each[0]:
                                    print "\t".join([str(sample), \
                                               str(gene), \
                                               str(rad), \
                                               str(key), \
                                               str(each[1]), \
                                               str(each[2]), \
                                               str(each[3]), \
                                               str(each[4]), \
                                               str(each[5]), \
                                               str(each[6]), \
                                               str(each[7]), \
                                               str(each[8]), \
                                               str(each[9]), \
                                               str(each[10])])

def sample_variants(c, args):
    idx_to_sample = util.map_indicies_to_samples(c)
    query = "SELECT variant_id, gt_types, gts, gene, impact, biotype, \
                    in_dbsnp, clin_sigs, aaf_1kg_all, aaf_esp_all, chrom, \
                    start, end  \
             FROM variants"
    c.execute(query)

    if args.command == 'interactions':
        #header
        if args.var_mode:
            print "\t".join(['sample','gene','order_of_interaction', \
                             'interacting_gene', 'var_id', 'chrom', 'start', \
                             'end', 'impact', 'biotype', 'in_dbsnp', \
                             'clin_sigs', 'aaf_1kg_all','aaf_esp_all'])

        if (not args.var_mode):
            print "\t".join(['sample','gene','order_of_interaction', \
                     'interacting_gene'])
        sample_gene_interactions(c, args, idx_to_sample)

    elif args.command == 'lof_interactions':
        samples = get_variant_genes(c, args, idx_to_sample)
        return samples

def sample_lof_variants(c, args, samples):
    idx_to_sample = util.map_indicies_to_samples(c)
    query = "SELECT chrom, start, end, \
                             gt_types, gts, gene \
             FROM variants \
             WHERE is_lof='1'"
    c.execute(query)

    #header
    if args.var_mode:
        print "\t".join(['sample','lof_gene','order_of_interaction', \
                    'interacting_gene', 'var_id', 'chrom', 'start', \
                    'end', 'impact','biotype','in_dbsnp', 'clin_sigs', \
                    'aaf_1kg_all','aaf_esp_all'])

    elif (not args.var_mode):
        print "\t".join(['sample','lof_gene','order_of_interaction', \
                         'interacting_gene'])

    sample_lof_interactions(c, args, idx_to_sample, samples)

def genequery(parser, args):
    if os.path.exists(args.db):
        conn = sqlite3.connect(args.db)
        conn.isolation_level = None
        conn.row_factory = sqlite3.Row
        c = conn.cursor()
        sample_variants(c, args)

def lofgenequery(parser, args):
    if os.path.exists(args.db):
        conn = sqlite3.connect(args.db)
        conn.isolation_level = None
        conn.row_factory = sqlite3.Row
        c = conn.cursor()
        samples = sample_variants(c, args)
        sample_lof_variants(c, args, samples)
<|endoftext|>


<commit_before>import numpy as np
import pytest
from hypothesis import given, strategies as st, example, assume

from utils.parse import parse_paramfile
from utils.rv_utils import RV

def test_rv_object_instance_of_rv_class():
    rv = RV()
    assert isinstance(rv, RV)

def test_initalize_rv_class_from_dict():
    params = {"k1": 1, "period": 2, "tau": 5000, "omega": 1, "eccentricity": 0.5, "mean_val": 5}
    rv = RV.from_dict(params)
    assert rv.semi_amp == params["k1"]
    assert rv.period == params["period"]
    assert rv.ecc == params["eccentricity"]
    assert rv.tau == params["tau"]
    assert rv.gamma == params["mean_val"]
    assert rv.omega == params["omega"]

def test_initalize_rv_class_from_file():
    paramfile = "tests/test_params.txt"
    rv = RV.from_file(paramfile)
    params = parse_paramfile(paramfile)

    assert rv.semi_amp == params["k1"]
    assert rv.period == params["period"]
    assert rv.ecc == params["eccentricity"]
    assert rv.tau == params["tau"]
    assert rv.gamma == params["mean_val"]
    assert rv.omega == params["omega"]

@pytest.mark.parametrize("semi_amp, period, tau, gamma, omega", [
    (1, 1, 1000, 10, 0),
    (10, 10, 3800, 100, 100),
])
def test_rv_class_max_amp_on_circle(semi_amp, period, tau, gamma, omega):
    ecc = 0
    rv = RV(semi_amp, period, ecc, tau, gamma, omega)
    assert rv.max_amp() == semi_amp

@pytest.mark.parametrize("semi_amp, period, ecc, tau, gamma, omega, expected_amp", [
    (1, 1, 0.25, 1000, 10, 0, 1.25),
    (10, 10, 0.5, 2800, 100, 300, 12.5),
    (20, 10, 0.75, 5800, 100, 180, 35.0),
])
def test_rv_class_max_amp_on_elipse(semi_amp, period, ecc, tau, gamma, omega, expected_amp):
    rv = RV(semi_amp, period, ecc, tau, gamma, omega)
    assert rv.max_amp() <= abs(semi_amp * (1 + ecc))  # omega = 0, 2pi etc
    assert rv.max_amp() == expected_amp

@given(st.floats(min_value=0, max_value=np.pi), st.floats(min_value=0.001, max_value=0.999))
@example(2, 0.5)  # example with an integer
def test_true_anomaly_with_scalar(ma, ecc):
    assume(abs(ma) > 0.001)
    ta = RV.true_anomaly(ma, ecc)
    E = np.arccos((ecc + np.cos(ta)) / (1 + ecc * np.cos(ta)))
    assert np.allclose(ma, E - ecc * np.sin(E))
    assert len(ta) == 1

@given(st.floats(min_value=0.01, max_value=0.99))
def test_true_anomaly_errors(ecc):
    with pytest.raises(TypeError):
        RV.true_anomaly([], ecc)

    with pytest.raises(ValueError):
        RV.true_anomaly(np.array([]), ecc)

@given(st.lists(st.floats(min_value=1, max_value=3e6), min_size=1),
       st.floats(min_value=0, max_value=3e6),
       st.floats(min_value=0.01, max_value=1e6))
def test_mean_anomaly_shape(t, t0, p):
    """Mean anomaly is an angle, doesn't have a constraint value."""
    t = np.array(t)
    ma = RV.mean_anomaly(t, t0, p)

    assert len(t) == len(ma)
    assert isinstance(t, np.ndarray)

@given(st.lists(st.floats(min_value=0, max_value=np.pi), min_size=1), st.floats(min_value=0.001, max_value=0.9999))
def test_true_anomaly(ma, ecc):
    ma = np.asarray(ma)
    assume(np.all(np.abs(ma) > 0.0001))
    ta = RV.true_anomaly(ma, ecc)
    E = np.arccos((ecc + np.cos(ta)) / (1 + ecc * np.cos(ta)))

    assert np.allclose(ma, E - ecc * np.sin(E), rtol=0.05)
    assert len(ta) == len(ma)

def test_create_companion():
    host = RV(semi_amp=1, m1=10, m2=5)
    companion = host.create_companion()

    assert companion.semi_amp == -host.semi_amp * host._params["m1"] / host._params["m2"]
    assert companion._params["k2"] == host._params["k1"]
    assert companion._params["k1"] == companion.semi_amp

@pytest.mark.parametrize("k2", [1, 10, 30])
def test_create_companion_with_k2(k2):
    host = RV(semi_amp=1, m1=10, m2=5, k2=k2)
    companion = host.create_companion()

    assert companion.semi_amp == k2  # does not depend on m1 and m2 if k2 given

    assert companion._params["k2"] == host._params["k1"]
    assert companion._params["k1"] == companion.semi_amp

@pytest.mark.parametrize("semi_amp, mass_ratio", [
    (1, 5.0), (0.8, 10), (10.2, 1024)])
def test_create_companion_with_mass_ratio(semi_amp, mass_ratio):
    host = RV(semi_amp=1, k1=1, m1=10, m2=5)
    print("host params", host._params)

    companion = host.create_companion(mass_ratio=mass_ratio)
    print("companion params", companion._params)
    assert companion.semi_amp == (- host.semi_amp * mass_ratio)
    assert host.period == companion.period
    assert host.gamma == companion.gamma
    assert host.ecc == companion.ecc
    assert host.tau == companion.tau

    assert companion._params["k2"] == host._params["k1"]
    assert companion._params["k1"] == companion.semi_amp

def test_double_create_companion_returns_host():
    host = RV(semi_amp=1, m1=10, m2=5)
    companion = host.create_companion()

    host_2 = companion.create_companion()

    assert host_2.semi_amp == host.semi_amp
    assert host == host_2
    assert host != companion

@pytest.mark.parametrize("mass_ratio", [
    5.0, 10, 1024])
def test_double_create_companion_with_ratio_returns_host(mass_ratio):
    host = RV(semi_amp=1, m1=10, m2=5)
    print("host parsm", host._params)
    companion = host.create_companion(mass_ratio=mass_ratio)
    print("comp params", companion._params)
    host_2 = companion.create_companion(1.0 / mass_ratio)
    print("host_2 params", host_2._params)
    assert host_2.semi_amp == host.semi_amp
    assert host == host_2
    assert host != companion

def test_from_dict_works_properly():
    params = {"k1": 10, "eccentricity": 0.5, "period": 5, "mean_val": 4, "tau": 1, "omega": 1, "m1": 4, "m2": 6}

    rv = RV.from_dict(params)
    assert rv._params.get("other_params") is None
    assert rv._params.get("m1") == 4
    assert rv._params.get("m2") == 6

def test_param_from_dict_and_to_dict_give_the_same_dict():
    params = {"k1": 10, "eccentricity": 0.5, "period": 5, "mean_val": 4, "tau": 1, "omega": 1,
              "m1": 4, "m2": 6, "k2": 100, "name": "test", "ignore_mean": True}

    rv1 = RV.from_dict(params)
    rv1_params = rv1.to_dict()
    assert params == rv1_params
    assert params == rv1._params
    assert rv1_params == rv1._params

    rv2 = RV.from_dict(rv1_params)
    assert rv2 == rv1

def test__repr__():
    rv = RV(semi_amp=1.0, period=1, omega=35, k2=7, m1=0.81, tau=3561.51)
    assert isinstance(rv.__repr__(), str)
    assert rv.__repr__() == "RV(semi_amp=1.0, period=1, ecc=0.0, tau=3561.51, omega=35, gamma=0.0, k2=7, m1=0.81)"

    assert RV().__repr__() == "RV(semi_amp=0.0, period=0.0, ecc=0.0, tau=0.0, omega=0.0, gamma=0.0)"

def test_companion_without_mass_gives_errors():
    rv = RV()
    with pytest.raises(ValueError):
        # Needs mass parameters
        rv.create_companion()

@pytest.mark.parametrize("center, npoints", [(0, 100), (0.5, 150)])
def test_full_phase(center, npoints):
    params = {"semi_amp": 1, "period": 15, "tau": 2400000, "omega": 0, "ecc": 0.1}
    rv1 = RV(**params).rv_full_phase(center, npoints)
    rv2 = RV(**params).rv_full_phase(center + 1, npoints)

    assert len(rv1) == npoints
    assert np.allclose(rv1, rv2)
    assert np.allclose(rv1[0], rv1[-1])

def test_anomaly_gives_runtime_error():
    with pytest.raises(RuntimeError):
        RV.true_anomaly(0.0000001, 0.9999999, niterationmax=5)

def test_set_ignore_mean():
    rv = RV()
    assert rv.ignore_mean is False
    rv.ignore_mean = True
    assert rv.ignore_mean is True
    rv.ignore_mean = False
    assert rv.ignore_mean is False

@pytest.mark.parametrize("ignore", [False, True])
def test_RV_can_handle_ignore_mean_as_input(ignore):
    rv = RV(ignore_mean=ignore)
    assert rv.ignore_mean == ignore
    assert rv._params["ignore_mean"] == ignore
    assert "ignore_mean" in rv.to_dict().keys()

def test_RV_to_dict_updates_parameters_in_params():
    rv = RV(semi_amp=1.0, period=3, tau=4, omega=5, ecc=0.5, mean_val=8, ignore_mean=True)
    assert rv.ignore_mean == True
    assert rv._params["ignore_mean"] == True
    assert rv.semi_amp == 1.0

    # _params not updated (yet)
    rv.semi_amp = 2
    rv.ignore_mean = False
    assert rv._params["k1"] == 1
    assert rv._params["ignore_mean"] == True
    assert rv.ignore_mean == False

    # RV.to_dict() updates _params
    param_dict = rv.to_dict()
    assert param_dict["ignore_mean"] == False
    assert rv._params["ignore_mean"] == False
    assert rv.semi_amp == 2
    assert param_dict["k1"] == 2
    assert rv._params["k1"] == 2
<commit_msg>reduce true anomly limits again<commit_after>import numpy as np
import pytest
from hypothesis import given, strategies as st, example, assume

from utils.parse import parse_paramfile
from utils.rv_utils import RV

def test_rv_object_instance_of_rv_class():
    rv = RV()
    assert isinstance(rv, RV)

def test_initalize_rv_class_from_dict():
    params = {"k1": 1, "period": 2, "tau": 5000, "omega": 1, "eccentricity": 0.5, "mean_val": 5}
    rv = RV.from_dict(params)
    assert rv.semi_amp == params["k1"]
    assert rv.period == params["period"]
    assert rv.ecc == params["eccentricity"]
    assert rv.tau == params["tau"]
    assert rv.gamma == params["mean_val"]
    assert rv.omega == params["omega"]

def test_initalize_rv_class_from_file():
    paramfile = "tests/test_params.txt"
    rv = RV.from_file(paramfile)
    params = parse_paramfile(paramfile)

    assert rv.semi_amp == params["k1"]
    assert rv.period == params["period"]
    assert rv.ecc == params["eccentricity"]
    assert rv.tau == params["tau"]
    assert rv.gamma == params["mean_val"]
    assert rv.omega == params["omega"]

@pytest.mark.parametrize("semi_amp, period, tau, gamma, omega", [
    (1, 1, 1000, 10, 0),
    (10, 10, 3800, 100, 100),
])
def test_rv_class_max_amp_on_circle(semi_amp, period, tau, gamma, omega):
    ecc = 0
    rv = RV(semi_amp, period, ecc, tau, gamma, omega)
    assert rv.max_amp() == semi_amp

@pytest.mark.parametrize("semi_amp, period, ecc, tau, gamma, omega, expected_amp", [
    (1, 1, 0.25, 1000, 10, 0, 1.25),
    (10, 10, 0.5, 2800, 100, 300, 12.5),
    (20, 10, 0.75, 5800, 100, 180, 35.0),
])
def test_rv_class_max_amp_on_elipse(semi_amp, period, ecc, tau, gamma, omega, expected_amp):
    rv = RV(semi_amp, period, ecc, tau, gamma, omega)
    assert rv.max_amp() <= abs(semi_amp * (1 + ecc))  # omega = 0, 2pi etc
    assert rv.max_amp() == expected_amp

@given(st.floats(min_value=0, max_value=np.pi/2), st.floats(min_value=0.01, max_value=0.9))
@example(2, 0.5)  # example with an integer
def test_true_anomaly_with_scalar(ma, ecc):
    assume(abs(ma) > 0.001)
    ta = RV.true_anomaly(ma, ecc)
    E = np.arccos((ecc + np.cos(ta)) / (1 + ecc * np.cos(ta)))
    assert np.allclose(ma, E - ecc * np.sin(E))
    assert len(ta) == 1

@given(st.floats(min_value=0.01, max_value=0.99))
def test_true_anomaly_errors(ecc):
    with pytest.raises(TypeError):
        RV.true_anomaly([], ecc)

    with pytest.raises(ValueError):
        RV.true_anomaly(np.array([]), ecc)

@given(st.lists(st.floats(min_value=1, max_value=3e6), min_size=1),
       st.floats(min_value=0, max_value=3e6),
       st.floats(min_value=0.01, max_value=1e6))
def test_mean_anomaly_shape(t, t0, p):
    """Mean anomaly is an angle, doesn't have a constraint value."""
    t = np.array(t)
    ma = RV.mean_anomaly(t, t0, p)

    assert len(t) == len(ma)
    assert isinstance(t, np.ndarray)

@given(st.lists(st.floats(min_value=0, max_value=np.pi/2), min_size=1), st.floats(min_value=0.01, max_value=0.9))
def test_true_anomaly(ma, ecc):
    ma = np.asarray(ma)
    assume(np.all(np.abs(ma) > 0.0001))
    ta = RV.true_anomaly(ma, ecc)
    E = np.arccos((ecc + np.cos(ta)) / (1 + ecc * np.cos(ta)))

    assert np.allclose(ma, E - ecc * np.sin(E), rtol=0.05)
    assert len(ta) == len(ma)

def test_create_companion():
    host = RV(semi_amp=1, m1=10, m2=5)
    companion = host.create_companion()

    assert companion.semi_amp == -host.semi_amp * host._params["m1"] / host._params["m2"]
    assert companion._params["k2"] == host._params["k1"]
    assert companion._params["k1"] == companion.semi_amp

@pytest.mark.parametrize("k2", [1, 10, 30])
def test_create_companion_with_k2(k2):
    host = RV(semi_amp=1, m1=10, m2=5, k2=k2)
    companion = host.create_companion()

    assert companion.semi_amp == k2  # does not depend on m1 and m2 if k2 given

    assert companion._params["k2"] == host._params["k1"]
    assert companion._params["k1"] == companion.semi_amp

@pytest.mark.parametrize("semi_amp, mass_ratio", [
    (1, 5.0), (0.8, 10), (10.2, 1024)])
def test_create_companion_with_mass_ratio(semi_amp, mass_ratio):
    host = RV(semi_amp=1, k1=1, m1=10, m2=5)
    print("host params", host._params)

    companion = host.create_companion(mass_ratio=mass_ratio)
    print("companion params", companion._params)
    assert companion.semi_amp == (- host.semi_amp * mass_ratio)
    assert host.period == companion.period
    assert host.gamma == companion.gamma
    assert host.ecc == companion.ecc
    assert host.tau == companion.tau

    assert companion._params["k2"] == host._params["k1"]
    assert companion._params["k1"] == companion.semi_amp

def test_double_create_companion_returns_host():
    host = RV(semi_amp=1, m1=10, m2=5)
    companion = host.create_companion()

    host_2 = companion.create_companion()

    assert host_2.semi_amp == host.semi_amp
    assert host == host_2
    assert host != companion

@pytest.mark.parametrize("mass_ratio", [
    5.0, 10, 1024])
def test_double_create_companion_with_ratio_returns_host(mass_ratio):
    host = RV(semi_amp=1, m1=10, m2=5)
    print("host parsm", host._params)
    companion = host.create_companion(mass_ratio=mass_ratio)
    print("comp params", companion._params)
    host_2 = companion.create_companion(1.0 / mass_ratio)
    print("host_2 params", host_2._params)
    assert host_2.semi_amp == host.semi_amp
    assert host == host_2
    assert host != companion

def test_from_dict_works_properly():
    params = {"k1": 10, "eccentricity": 0.5, "period": 5, "mean_val": 4, "tau": 1, "omega": 1, "m1": 4, "m2": 6}

    rv = RV.from_dict(params)
    assert rv._params.get("other_params") is None
    assert rv._params.get("m1") == 4
    assert rv._params.get("m2") == 6

def test_param_from_dict_and_to_dict_give_the_same_dict():
    params = {"k1": 10, "eccentricity": 0.5, "period": 5, "mean_val": 4, "tau": 1, "omega": 1,
              "m1": 4, "m2": 6, "k2": 100, "name": "test", "ignore_mean": True}

    rv1 = RV.from_dict(params)
    rv1_params = rv1.to_dict()
    assert params == rv1_params
    assert params == rv1._params
    assert rv1_params == rv1._params

    rv2 = RV.from_dict(rv1_params)
    assert rv2 == rv1

def test__repr__():
    rv = RV(semi_amp=1.0, period=1, omega=35, k2=7, m1=0.81, tau=3561.51)
    assert isinstance(rv.__repr__(), str)
    assert rv.__repr__() == "RV(semi_amp=1.0, period=1, ecc=0.0, tau=3561.51, omega=35, gamma=0.0, k2=7, m1=0.81)"

    assert RV().__repr__() == "RV(semi_amp=0.0, period=0.0, ecc=0.0, tau=0.0, omega=0.0, gamma=0.0)"

def test_companion_without_mass_gives_errors():
    rv = RV()
    with pytest.raises(ValueError):
        # Needs mass parameters
        rv.create_companion()

@pytest.mark.parametrize("center, npoints", [(0, 100), (0.5, 150)])
def test_full_phase(center, npoints):
    params = {"semi_amp": 1, "period": 15, "tau": 2400000, "omega": 0, "ecc": 0.1}
    rv1 = RV(**params).rv_full_phase(center, npoints)
    rv2 = RV(**params).rv_full_phase(center + 1, npoints)

    assert len(rv1) == npoints
    assert np.allclose(rv1, rv2)
    assert np.allclose(rv1[0], rv1[-1])

def test_anomaly_gives_runtime_error():
    with pytest.raises(RuntimeError):
        RV.true_anomaly(0.0000001, 0.9999999, niterationmax=5)

def test_set_ignore_mean():
    rv = RV()
    assert rv.ignore_mean is False
    rv.ignore_mean = True
    assert rv.ignore_mean is True
    rv.ignore_mean = False
    assert rv.ignore_mean is False

@pytest.mark.parametrize("ignore", [False, True])
def test_RV_can_handle_ignore_mean_as_input(ignore):
    rv = RV(ignore_mean=ignore)
    assert rv.ignore_mean == ignore
    assert rv._params["ignore_mean"] == ignore
    assert "ignore_mean" in rv.to_dict().keys()

def test_RV_to_dict_updates_parameters_in_params():
    rv = RV(semi_amp=1.0, period=3, tau=4, omega=5, ecc=0.5, mean_val=8, ignore_mean=True)
    assert rv.ignore_mean == True
    assert rv._params["ignore_mean"] == True
    assert rv.semi_amp == 1.0

    # _params not updated (yet)
    rv.semi_amp = 2
    rv.ignore_mean = False
    assert rv._params["k1"] == 1
    assert rv._params["ignore_mean"] == True
    assert rv.ignore_mean == False

    # RV.to_dict() updates _params
    param_dict = rv.to_dict()
    assert param_dict["ignore_mean"] == False
    assert rv._params["ignore_mean"] == False
    assert rv.semi_amp == 2
    assert param_dict["k1"] == 2
    assert rv._params["k1"] == 2
<|endoftext|>


<commit_before>__version__ = '0.25.0a1'
<commit_msg>Set version to 0.25.0<commit_after>__version__ = '0.25.0'
<|endoftext|>


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

#
#    Copyright 2014-2015
#
#      STIC-Investigación - Universidad de La Laguna (ULL) <gesinv@ull.edu.es>
#
#    This file is part of CVN.
#
#    CVN is free software: you can redistribute it and/or modify it under
#    the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    CVN 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 for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with CVN.  If not, see
#    <http://www.gnu.org/licenses/>.
#

from core.tests.helpers import init, clean
from django import test
from django.conf import settings as st
from django.contrib.auth.models import User
from selenium import webdriver
from selenium.common.exceptions import (NoSuchElementException,
                                        NoAlertPresentException)
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import unittest
from pyvirtualdisplay import Display
from utils import get_cvn_path

class LoginCAS(test.LiveServerTestCase):

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

    def setUp(self):
        display = Display(visible=0, size=(1280, 1024))
        display.start()
        self.display = display
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(8)
        self.driver.set_page_load_timeout(30)
        self.base_url = st.CAS_SERVER_URL
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_login_cas(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME, "page-header"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_login_no_cas(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invNoCas")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        self.assertTrue(self.is_element_present(By.ID, "status"))

    def test_upload_cvn_fecyt(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
#        driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            get_cvn_path('CVN-Test_2'))
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "alert-success"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_upload_cvn_no_fecyt(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            get_cvn_path('CVN-NO-FECYT'))
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "errorlist"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_upload_cvn_user_admin(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invipas")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # Add admin permissions after login CAS
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
            (By.CLASS_NAME, 'btn-signout')))
        u = User.objects.get(username="invipas")
        u.is_staff = True
        u.is_superuser = True
        u.save()
#        driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            get_cvn_path('CVN-Test'))
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "alert-success"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_user_download_cvn(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # First upload CVN
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            get_cvn_path('CVN-Test_2'))
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        # Download CVN
        driver.find_element_by_link_text(
            u'Descargar una copia de mi CVN').click()
        # Check if CVN window is open
        self.assertEqual(len(driver.window_handles), 2)
        # Change to CVN window
        driver.switch_to_window(driver.window_handles[-1])
        # Return to 'Main' windows
        driver.switch_to_window(driver.window_handles[0])
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_user_rrhh(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            get_cvn_path('CVN-Test_2'))
        self.assertFalse(self.is_element_present(By.ID, "dept"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_selenium_not_info(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        self.assertFalse(self.is_element_present(
            By.LINK_TEXT, u"Exportar mi información ULL"))

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(by=how, value=what)
        except NoSuchElementException:  # , e:
            return False
        return True

    def is_alert_present(self):
        try:
            self.driver.switch_to_alert()
        except NoAlertPresentException:  # , e:
            return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally:
            self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.display.stop()
        self.assertEqual([], self.verificationErrors)

    @classmethod
    def tearDownClass(cls):
        clean()

if __name__ == "__main__":
    unittest.main()
<commit_msg>Move selenium tests to cvn<commit_after># -*- encoding: UTF-8 -*-

#
#    Copyright 2014-2015
#
#      STIC-Investigación - Universidad de La Laguna (ULL) <gesinv@ull.edu.es>
#
#    This file is part of CVN.
#
#    CVN is free software: you can redistribute it and/or modify it under
#    the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    CVN 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 for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with CVN.  If not, see
#    <http://www.gnu.org/licenses/>.
#

from core.tests.helpers import init, clean
from django import test
from django.conf import settings as st
from django.contrib.auth.models import User
from selenium import webdriver
from selenium.common.exceptions import (NoSuchElementException,
                                        NoAlertPresentException)
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import unittest
from pyvirtualdisplay import Display
from utils import get_cvn_path
from core.models import UserProfile
from selenium.webdriver.support.ui import Select

class LoginCAS(test.LiveServerTestCase):

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

    def setUp(self):
        display = Display(visible=0, size=(1280, 1024))
        display.start()
        self.display = display
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(8)
        self.driver.set_page_load_timeout(30)
        self.base_url = st.CAS_SERVER_URL
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_login_cas(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME, "page-header"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_login_no_cas(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invNoCas")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        self.assertTrue(self.is_element_present(By.ID, "status"))

    def test_upload_cvn_fecyt(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
#        driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            get_cvn_path('CVN-Test_2'))
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "alert-success"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_upload_cvn_no_fecyt(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            get_cvn_path('CVN-NO-FECYT'))
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "errorlist"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_upload_cvn_user_admin(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invipas")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # Add admin permissions after login CAS
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
            (By.CLASS_NAME, 'btn-signout')))
        u = User.objects.get(username="invipas")
        u.is_staff = True
        u.is_superuser = True
        u.save()
#        driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            get_cvn_path('CVN-Test'))
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "alert-success"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_user_download_cvn(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # First upload CVN
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            get_cvn_path('CVN-Test_2'))
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        # Download CVN
        driver.find_element_by_link_text(
            u'Descargar una copia de mi CVN').click()
        # Check if CVN window is open
        self.assertEqual(len(driver.window_handles), 2)
        # Change to CVN window
        driver.switch_to_window(driver.window_handles[-1])
        # Return to 'Main' windows
        driver.switch_to_window(driver.window_handles[0])
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_user_rrhh(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            get_cvn_path('CVN-Test_2'))
        self.assertFalse(self.is_element_present(By.ID, "dept"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_selenium_not_info(self):
        driver = self.driver
        driver.get(self.base_url +
                   "login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        self.assertFalse(self.is_element_present(
            By.LINK_TEXT, u"Exportar mi información ULL"))

    def test_selenium_cambio_correcto_dni(self):
        user = UserProfile.get_or_create_user('invipas', '72693103Q')[0]
        user.set_password("pruebasINV1")
        user.is_staff = True
        user.is_superuser = True
        user.save()
        driver = self.driver
        driver.get(self.base_url + "/cas-1/login?service=http%3A%2F%2Flocalhost"
                                   "%3A8081%2Finvestigacion%2Faccounts%2Flogin"
                                   "%2F%3Fnext%3D%252Finvestigacion%252Fadmin"
                                   "%252Flogin%252F%253Fnext%253D%252F"
                                   "investigacion%252Fadmin%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invipas")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        driver.find_element_by_link_text("User profiles").click()
        driver.find_element_by_id("searchbar").clear()
        driver.find_element_by_id("searchbar").send_keys(user.username)
        driver.find_element_by_css_selector("input[type=\"submit\"]").click()
        driver.find_element_by_css_selector("tr.row1 > td.action-checkbox > "
                                            "input[name=\"_selected_action\"]"
                                            ).click()
        Select(driver.find_element_by_name("action")
               ).select_by_visible_text("Change user's DNI")
        driver.find_element_by_name("index").click()
        driver.find_element_by_id("id_new_dni").clear()
        driver.find_element_by_id("id_new_dni").send_keys("08030254B")
        driver.find_element_by_name("apply").click()
        try:
            result = driver.find_element_by_class_name("info").text
        except NoSuchElementException:
            result = ''
        self.assertTrue((u'Successfully changed dni.' in result))
        self.assertEqual("08030254B", UserProfile.objects.get(
            user__username='invipas').documento)

    def test_selenium_cambio_incorrecto_dni(self):
        user = UserProfile.get_or_create_user('invipas', '72693103Q')[0]
        user.set_password("pruebasINV1")
        user.is_staff = True
        user.is_superuser = True
        user.save()
        driver = self.driver
        driver.get(self.base_url + "/cas-1/login?service=http%3A%2F%2Flocalhost"
                                   "%3A8081%2Finvestigacion%2Faccounts%2Flogin"
                                   "%2F%3Fnext%3D%252Finvestigacion%252Fadmin"
                                   "%252Flogin%252F%253Fnext%253D%252F"
                                   "investigacion%252Fadmin%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invipas")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        driver.find_element_by_link_text("User profiles").click()
        driver.find_element_by_id("searchbar").clear()
        driver.find_element_by_id("searchbar").send_keys(user.username)
        driver.find_element_by_css_selector("input[type=\"submit\"]").click()
        driver.find_element_by_css_selector("tr.row1 > td.action-checkbox > "
                                            "input[name=\"_selected_action\"]"
                                            ).click()
        Select(driver.find_element_by_name("action")
               ).select_by_visible_text("Change user's DNI")
        driver.find_element_by_name("index").click()
        driver.find_element_by_id("id_new_dni").clear()
        driver.find_element_by_id("id_new_dni").send_keys("88888888B")
        driver.find_element_by_name("apply").click()
        try:
            result = driver.find_element_by_class_name("info").text
        except NoSuchElementException:
            result = ''
        self.assertFalse(u'Successfully changed dni.' in result)
        self.assertEqual("72693103Q", UserProfile.objects.get(
            user__username='invipas').documento)

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(by=how, value=what)
        except NoSuchElementException:  # , e:
            return False
        return True

    def is_alert_present(self):
        try:
            self.driver.switch_to_alert()
        except NoAlertPresentException:  # , e:
            return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally:
            self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.display.stop()
        self.assertEqual([], self.verificationErrors)

    @classmethod
    def tearDownClass(cls):
        clean()

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


<commit_before>import os
import io
import asyncio
from unittest import mock

import pytest
import aiohttpretty

from tests import utils
from tests.utils import async

from waterbutler.core import streams
from waterbutler.core import exceptions
from waterbutler.server import settings
from waterbutler.core.path import WaterButlerPath
from waterbutler.providers.osfstorage import OSFStorageProvider
from waterbutler.providers.osfstorage.settings import FILE_PATH_COMPLETE

@pytest.fixture
def file_content():
    return b'sleepy'

@pytest.fixture
def file_like(file_content):
    return io.BytesIO(file_content)

@pytest.fixture
def file_stream(file_like):
    return streams.FileStreamReader(file_like)

@pytest.fixture
def auth():
    return {
        'id': 'cat',
        'name': 'cat',
        'email': 'cat@cat.com',
    }

@pytest.fixture
def credentials():
    return {
        'storage': {
            'access_key': 'Dont dead',
            'secret_key': 'open inside',
        },
        'archive': {},
        'parity': {},
    }

@pytest.fixture
def settings():
    return {
        'justa': 'setting',
        'nid': 'foo',
        'rootId': 'rootId',
        'baseUrl': 'https://waterbutler.io',
        'storage': {
            'provider': 'mock',
        },
        'archive': {},
        'parity': {},
    }

@pytest.fixture
def provider_and_mock(monkeypatch, auth, credentials, settings):
    mock_provider = utils.MockProvider1({}, {}, {})

    mock_provider.copy = utils.MockCoroutine()
    mock_provider.move = utils.MockCoroutine()
    mock_provider.delete = utils.MockCoroutine()
    mock_provider.upload = utils.MockCoroutine()
    mock_provider.download = utils.MockCoroutine()
    mock_provider.metadata = utils.MockCoroutine()

    mock_make_provider = mock.Mock(return_value=mock_provider)
    monkeypatch.setattr(OSFStorageProvider, 'make_provider', mock_make_provider)
    return OSFStorageProvider(auth, credentials, settings), mock_provider

@pytest.fixture
def provider(provider_and_mock):
    provider, _ = provider_and_mock
    return provider

@pytest.fixture
def osf_response():
    return {
        'data': {
            'path': 'test/path',
            'name': 'unrelatedpath',
        },
        'settings': {
            'justa': 'settings'
        }
    }

@pytest.fixture
def upload_response():
    return {
        'version': 'vid',
        'status': 'success',
        'data': {
            'downloads': 10,
            'version': 8,
            'path': '/dfl893b1pdn11kd28b',
            'md5': 'abcdabcdabcdabcdabcdabcdabcd'
        }
    }

@pytest.fixture
def mock_path():
    return WaterButlerPath('/unrelatedpath', _ids=('rootId', 'another'))

@pytest.fixture
def mock_folder_path():
    return WaterButlerPath('/unrelatedfolder/', _ids=('rootId', 'another'))

@async
@pytest.mark.aiohttpretty
def test_download(monkeypatch, provider_and_mock, osf_response, mock_path):
    provider, inner_provider = provider_and_mock

    url = 'https://waterbutler.io/{}/download/?mode&version'.format(mock_path.identifier)

    aiohttpretty.register_json_uri('GET', url, body=osf_response)

    yield from provider.download(mock_path)

    assert provider.make_provider.called
    assert inner_provider.download.called

    assert aiohttpretty.has_call(method='GET', uri=url, check_params=False)
    provider.make_provider.assert_called_once_with(osf_response['settings'])
    inner_provider.download.assert_called_once_with(path=WaterButlerPath('/test/path'), displayName='unrelatedpath')

@async
@pytest.mark.aiohttpretty
def test_delete(monkeypatch, provider, mock_path):
    path = WaterButlerPath('/unrelatedpath', _ids=('Doesntmatter', 'another'))
    aiohttpretty.register_uri('DELETE', 'https://waterbutler.io/another/', status_code=200)

    yield from provider.delete(path)

    assert aiohttpretty.has_call(method='DELETE', uri='https://waterbutler.io/another/', check_params=False)

@async
@pytest.mark.aiohttpretty
def test_provider_metadata_empty(monkeypatch, provider, mock_folder_path):
    url = 'https://waterbutler.io/{}/children/'.format(mock_folder_path.identifier)
    aiohttpretty.register_json_uri('GET', url, status_code=200, body=[])

    res = yield from provider.metadata(mock_folder_path)

    assert res == []

    assert aiohttpretty.has_call(method='GET', uri=url)

@async
@pytest.mark.aiohttpretty
def test_provider_metadata(monkeypatch, provider, mock_folder_path):
    items = [
        {
            'name': 'foo',
            'path': '/foo',
            'kind': 'file',
            'version': 10,
            'downloads': 1,
            'md5': '1234'
        },
        {
            'name': 'bar',
            'path': '/bar',
            'kind': 'file',
            'version': 10,
            'downloads': 1,
            'md5': '1234'
        },
        {
            'name': 'baz',
            'path': '/baz',
            'kind': 'folder'
        }
    ]
    url = 'https://waterbutler.io/{}/children/'.format(mock_folder_path.identifier)
    aiohttpretty.register_json_uri('GET', url, status=200, body=items)

    res = yield from provider.metadata(mock_folder_path)

    assert isinstance(res, list)

    for item in res:
        assert isinstance(item, dict)
        assert item['name'] is not None
        assert item['path'] is not None
        assert item['provider'] == 'osfstorage'

    assert aiohttpretty.has_call(method='GET', uri=url)

class TestUploads:

    def patch_tasks(self, monkeypatch):
        basepath = 'waterbutler.providers.osfstorage.provider.{}'
        monkeypatch.setattr(basepath.format('os.rename'), lambda *_: None)
        monkeypatch.setattr(basepath.format('settings.RUN_TASKS'), False)
        monkeypatch.setattr(basepath.format('uuid.uuid4'), lambda: 'uniquepath')

    @async
    @pytest.mark.aiohttpretty
    def test_upload_new(self, monkeypatch, provider_and_mock, file_stream, upload_response):
        self.patch_tasks(monkeypatch)

        path = WaterButlerPath('/newfile', _ids=('rootId', None))
        url = 'https://waterbutler.io/{}/children/'.format(path.parent.identifier)
        aiohttpretty.register_json_uri('POST', url, status=201, body=upload_response)

        provider, inner_provider = provider_and_mock
        inner_provider.metadata = utils.MockCoroutine(return_value={})

        res, created = yield from provider.upload(file_stream, path)

        assert created is True
        assert res['name'] == 'newfile'
        assert res['extra']['version'] == 8
        assert res['provider'] == 'osfstorage'
        assert res['extra']['downloads'] == 10

        inner_provider.delete.assert_called_once_with(WaterButlerPath('/uniquepath'))
        inner_provider.metadata.assert_called_once_with(WaterButlerPath('/' + file_stream.writers['sha256'].hexdigest))
        inner_provider.upload.assert_called_once_with(file_stream, WaterButlerPath('/uniquepath'), check_created=False, fetch_metadata=False)

    @async
    @pytest.mark.aiohttpretty
    def test_upload_existing(self, monkeypatch, provider_and_mock, file_stream):
        self.patch_tasks(monkeypatch)
        provider, inner_provider = provider_and_mock

        path = WaterButlerPath('/foopath', _ids=('Test', 'OtherTest'))
        url = 'https://waterbutler.io/{}/children/'.format(path.parent.identifier)

        inner_provider.move.return_value = ({}, True)
        inner_provider.metadata.side_effect = exceptions.MetadataError('Boom!', code=404)

        aiohttpretty.register_json_uri('POST', url, status=200, body={'data': {'downloads': 10, 'version': 8, 'path': '/24601'}})

        res, created = yield from provider.upload(file_stream, path)

        assert created is False
        assert res['name'] == 'foopath'
        assert res['path'] == '/24601'
        assert res['extra']['version'] == 8
        assert res['provider'] == 'osfstorage'
        assert res['extra']['downloads'] == 10

        inner_provider.metadata.assert_called_once_with(WaterButlerPath('/' + file_stream.writers['sha256'].hexdigest))
        inner_provider.upload.assert_called_once_with(file_stream, WaterButlerPath('/uniquepath'), check_created=False, fetch_metadata=False)
        inner_provider.move.assert_called_once_with(inner_provider, WaterButlerPath('/uniquepath'), WaterButlerPath('/' + file_stream.writers['sha256'].hexdigest))

    @async
    @pytest.mark.aiohttpretty
    def test_upload_and_tasks(self, monkeypatch, provider_and_mock, file_stream, credentials, settings):
        provider, inner_provider = provider_and_mock
        basepath = 'waterbutler.providers.osfstorage.provider.{}'
        path = WaterButlerPath('/foopath', _ids=('Test', 'OtherTest'))
        url = 'https://waterbutler.io/{}/children/'.format(path.parent.identifier)

        mock_parity = mock.Mock()
        mock_backup = mock.Mock()
        inner_provider.move.return_value = ({}, True)
        inner_provider.metadata.side_effect = exceptions.MetadataError('Boom!', code=404)

        aiohttpretty.register_json_uri('POST', url, status=201, body={'version': 'versionpk', 'data': {'version': 42, 'downloads': 30, 'path': '/alkjdaslke09'}})

        monkeypatch.setattr(basepath.format('backup.main'), mock_backup)
        monkeypatch.setattr(basepath.format('parity.main'), mock_parity)
        monkeypatch.setattr(basepath.format('settings.RUN_TASKS'), True)
        monkeypatch.setattr(basepath.format('os.rename'), lambda *_: None)
        monkeypatch.setattr(basepath.format('uuid.uuid4'), lambda: 'uniquepath')

        res, created = yield from provider.upload(file_stream, path)

        assert created is True
        assert res['name'] == 'foopath'
        assert res['extra']['version'] == 42
        assert res['provider'] == 'osfstorage'
        assert res['extra']['downloads'] == 30

        inner_provider.upload.assert_called_once_with(file_stream, WaterButlerPath('/uniquepath'), check_created=False, fetch_metadata=False)
        complete_path = os.path.join(FILE_PATH_COMPLETE, file_stream.writers['sha256'].hexdigest)
        mock_parity.assert_called_once_with(complete_path, credentials['parity'], settings['parity'])
        mock_backup.assert_called_once_with(complete_path, 'versionpk', 'https://waterbutler.io/hooks/metadata/', credentials['archive'], settings['parity'])
        inner_provider.metadata.assert_called_once_with(WaterButlerPath('/' + file_stream.writers['sha256'].hexdigest))
        inner_provider.move.assert_called_once_with(inner_provider, WaterButlerPath('/uniquepath'), WaterButlerPath('/' + file_stream.writers['sha256'].hexdigest))
<commit_msg>When will tests work :(<commit_after>import os
import io
import asyncio
from unittest import mock

import pytest
import aiohttpretty

from tests import utils
from tests.utils import async

from waterbutler.core import streams
from waterbutler.core import exceptions
from waterbutler.server import settings
from waterbutler.core.path import WaterButlerPath
from waterbutler.providers.osfstorage import OSFStorageProvider
from waterbutler.providers.osfstorage.settings import FILE_PATH_COMPLETE

@pytest.fixture
def file_content():
    return b'sleepy'

@pytest.fixture
def file_like(file_content):
    return io.BytesIO(file_content)

@pytest.fixture
def file_stream(file_like):
    return streams.FileStreamReader(file_like)

@pytest.fixture
def auth():
    return {
        'id': 'cat',
        'name': 'cat',
        'email': 'cat@cat.com',
    }

@pytest.fixture
def credentials():
    return {
        'storage': {
            'access_key': 'Dont dead',
            'secret_key': 'open inside',
        },
        'archive': {},
        'parity': {},
    }

@pytest.fixture
def settings():
    return {
        'justa': 'setting',
        'nid': 'foo',
        'rootId': 'rootId',
        'baseUrl': 'https://waterbutler.io',
        'storage': {
            'provider': 'mock',
        },
        'archive': {},
        'parity': {},
    }

@pytest.fixture
def provider_and_mock(monkeypatch, auth, credentials, settings):
    mock_provider = utils.MockProvider1({}, {}, {})

    mock_provider.copy = utils.MockCoroutine()
    mock_provider.move = utils.MockCoroutine()
    mock_provider.delete = utils.MockCoroutine()
    mock_provider.upload = utils.MockCoroutine()
    mock_provider.download = utils.MockCoroutine()
    mock_provider.metadata = utils.MockCoroutine()

    mock_make_provider = mock.Mock(return_value=mock_provider)
    monkeypatch.setattr(OSFStorageProvider, 'make_provider', mock_make_provider)
    return OSFStorageProvider(auth, credentials, settings), mock_provider

@pytest.fixture
def provider(provider_and_mock):
    provider, _ = provider_and_mock
    return provider

@pytest.fixture
def osf_response():
    return {
        'data': {
            'path': 'test/path',
            'name': 'unrelatedpath',
        },
        'settings': {
            'justa': 'settings'
        }
    }

@pytest.fixture
def upload_response():
    return {
        'version': 'vid',
        'status': 'success',
        'data': {
            'downloads': 10,
            'version': 8,
            'path': '/dfl893b1pdn11kd28b',
            'md5': 'abcdabcdabcdabcdabcdabcdabcd'
        }
    }

@pytest.fixture
def mock_path():
    return WaterButlerPath('/unrelatedpath', _ids=('rootId', 'another'))

@pytest.fixture
def mock_folder_path():
    return WaterButlerPath('/unrelatedfolder/', _ids=('rootId', 'another'))

@async
@pytest.mark.aiohttpretty
def test_download(monkeypatch, provider_and_mock, osf_response, mock_path):
    provider, inner_provider = provider_and_mock

    url = 'https://waterbutler.io/{}/download/?mode&version'.format(mock_path.identifier)

    aiohttpretty.register_json_uri('GET', url, body=osf_response)

    yield from provider.download(mock_path)

    assert provider.make_provider.called
    assert inner_provider.download.called

    assert aiohttpretty.has_call(method='GET', uri=url, check_params=False)
    provider.make_provider.assert_called_once_with(osf_response['settings'])
    inner_provider.download.assert_called_once_with(path=WaterButlerPath('/test/path'), displayName='unrelatedpath')

@async
@pytest.mark.aiohttpretty
def test_delete(monkeypatch, provider, mock_path):
    path = WaterButlerPath('/unrelatedpath', _ids=('Doesntmatter', 'another'))
    aiohttpretty.register_uri('DELETE', 'https://waterbutler.io/another/', status_code=200)

    yield from provider.delete(path)

    assert aiohttpretty.has_call(method='DELETE', uri='https://waterbutler.io/another/', check_params=False)

@async
@pytest.mark.aiohttpretty
def test_provider_metadata_empty(monkeypatch, provider, mock_folder_path):
    url = 'https://waterbutler.io/{}/children/'.format(mock_folder_path.identifier)
    aiohttpretty.register_json_uri('GET', url, status_code=200, body=[])

    res = yield from provider.metadata(mock_folder_path)

    assert res == []

    assert aiohttpretty.has_call(method='GET', uri=url)

@async
@pytest.mark.aiohttpretty
def test_provider_metadata(monkeypatch, provider, mock_folder_path):
    items = [
        {
            'name': 'foo',
            'path': '/foo',
            'kind': 'file',
            'version': 10,
            'downloads': 1,
            'md5': '1234'
        },
        {
            'name': 'bar',
            'path': '/bar',
            'kind': 'file',
            'version': 10,
            'downloads': 1,
            'md5': '1234'
        },
        {
            'name': 'baz',
            'path': '/baz',
            'kind': 'folder'
        }
    ]
    url = 'https://waterbutler.io/{}/children/'.format(mock_folder_path.identifier)
    aiohttpretty.register_json_uri('GET', url, status=200, body=items)

    res = yield from provider.metadata(mock_folder_path)

    assert isinstance(res, list)

    for item in res:
        assert isinstance(item, dict)
        assert item['name'] is not None
        assert item['path'] is not None
        assert item['provider'] == 'osfstorage'

    assert aiohttpretty.has_call(method='GET', uri=url)

class TestUploads:

    def patch_tasks(self, monkeypatch):
        basepath = 'waterbutler.providers.osfstorage.provider.{}'
        monkeypatch.setattr(basepath.format('os.rename'), lambda *_: None)
        monkeypatch.setattr(basepath.format('settings.RUN_TASKS'), False)
        monkeypatch.setattr(basepath.format('uuid.uuid4'), lambda: 'uniquepath')

    @async
    @pytest.mark.aiohttpretty
    def test_upload_new(self, monkeypatch, provider_and_mock, file_stream, upload_response):
        self.patch_tasks(monkeypatch)

        path = WaterButlerPath('/newfile', _ids=('rootId', None))
        url = 'https://waterbutler.io/{}/children/'.format(path.parent.identifier)
        aiohttpretty.register_json_uri('POST', url, status=201, body=upload_response)

        provider, inner_provider = provider_and_mock
        inner_provider.metadata = utils.MockCoroutine(return_value={})

        res, created = yield from provider.upload(file_stream, path)

        assert created is True
        assert res['name'] == 'newfile'
        assert res['extra']['version'] == 8
        assert res['provider'] == 'osfstorage'
        assert res['extra']['downloads'] == 10

        inner_provider.delete.assert_called_once_with(WaterButlerPath('/uniquepath'))
        inner_provider.metadata.assert_called_once_with(WaterButlerPath('/' + file_stream.writers['sha256'].hexdigest))
        inner_provider.upload.assert_called_once_with(file_stream, WaterButlerPath('/uniquepath'), check_created=False, fetch_metadata=False)

    @async
    @pytest.mark.aiohttpretty
    def test_upload_existing(self, monkeypatch, provider_and_mock, file_stream):
        self.patch_tasks(monkeypatch)
        provider, inner_provider = provider_and_mock

        path = WaterButlerPath('/foopath', _ids=('Test', 'OtherTest'))
        url = 'https://waterbutler.io/{}/children/'.format(path.parent.identifier)

        inner_provider.move.return_value = ({}, True)
        inner_provider.metadata.side_effect = exceptions.MetadataError('Boom!', code=404)

        aiohttpretty.register_json_uri('POST', url, status=200, body={'data': {'downloads': 10, 'version': 8, 'path': '/24601', 'md5': '1234'}})

        res, created = yield from provider.upload(file_stream, path)

        assert created is False
        assert res['name'] == 'foopath'
        assert res['path'] == '/24601'
        assert res['extra']['version'] == 8
        assert res['provider'] == 'osfstorage'
        assert res['extra']['downloads'] == 10

        inner_provider.metadata.assert_called_once_with(WaterButlerPath('/' + file_stream.writers['sha256'].hexdigest))
        inner_provider.upload.assert_called_once_with(file_stream, WaterButlerPath('/uniquepath'), check_created=False, fetch_metadata=False)
        inner_provider.move.assert_called_once_with(inner_provider, WaterButlerPath('/uniquepath'), WaterButlerPath('/' + file_stream.writers['sha256'].hexdigest))

    @async
    @pytest.mark.aiohttpretty
    def test_upload_and_tasks(self, monkeypatch, provider_and_mock, file_stream, credentials, settings):
        provider, inner_provider = provider_and_mock
        basepath = 'waterbutler.providers.osfstorage.provider.{}'
        path = WaterButlerPath('/foopath', _ids=('Test', 'OtherTest'))
        url = 'https://waterbutler.io/{}/children/'.format(path.parent.identifier)

        mock_parity = mock.Mock()
        mock_backup = mock.Mock()
        inner_provider.move.return_value = ({}, True)
        inner_provider.metadata.side_effect = exceptions.MetadataError('Boom!', code=404)

        aiohttpretty.register_json_uri('POST', url, status=201, body={'version': 'versionpk', 'data': {'version': 42, 'downloads': 30, 'path': '/alkjdaslke09', 'md5': 'abcd'}})

        monkeypatch.setattr(basepath.format('backup.main'), mock_backup)
        monkeypatch.setattr(basepath.format('parity.main'), mock_parity)
        monkeypatch.setattr(basepath.format('settings.RUN_TASKS'), True)
        monkeypatch.setattr(basepath.format('os.rename'), lambda *_: None)
        monkeypatch.setattr(basepath.format('uuid.uuid4'), lambda: 'uniquepath')

        res, created = yield from provider.upload(file_stream, path)

        assert created is True
        assert res['name'] == 'foopath'
        assert res['extra']['version'] == 42
        assert res['provider'] == 'osfstorage'
        assert res['extra']['downloads'] == 30

        inner_provider.upload.assert_called_once_with(file_stream, WaterButlerPath('/uniquepath'), check_created=False, fetch_metadata=False)
        complete_path = os.path.join(FILE_PATH_COMPLETE, file_stream.writers['sha256'].hexdigest)
        mock_parity.assert_called_once_with(complete_path, credentials['parity'], settings['parity'])
        mock_backup.assert_called_once_with(complete_path, 'versionpk', 'https://waterbutler.io/hooks/metadata/', credentials['archive'], settings['parity'])
        inner_provider.metadata.assert_called_once_with(WaterButlerPath('/' + file_stream.writers['sha256'].hexdigest))
        inner_provider.move.assert_called_once_with(inner_provider, WaterButlerPath('/uniquepath'), WaterButlerPath('/' + file_stream.writers['sha256'].hexdigest))
<|endoftext|>


<commit_before>import pytest
from tests.support.helpers import PRE_PYTEST_SKIP_REASON

@pytest.fixture(autouse=True)
def skip_pre_pytest_platforms(grains):
    os = grains["os"]
    os_major_version = int(grains.get("osmajorrelease") or 0)
    centos_7 = os == "CentOS" and os_major_version == 7
    ubuntu_16 = os == "Ubuntu" and os_major_version == 16
    if not centos_7 and not ubuntu_16:
        pytest.skip(PRE_PYTEST_SKIP_REASON)
<commit_msg>Proxy tests can run on all platforms<commit_after><|endoftext|>


<commit_before>import os
import re
from subprocess import Popen, PIPE

def find_command(cmd, path=None, pathext=None):
    if path is None:
        path = os.environ.get('PATH', []).split(os.pathsep)
    if isinstance(path, basestring):
        path = [path]
    # check if there are funny path extensions for executables, e.g. Windows
    if pathext is None:
        pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD').split(os.pathsep)
    # don't use extensions if the command ends with one of them
    for ext in pathext:
        if cmd.endswith(ext):
            pathext = ['']
            break
    # check if we find the command on PATH
    for p in path:
        f = os.path.join(p, cmd)
        if os.path.isfile(f):
            return f
        for ext in pathext:
            fext = f + ext
            if os.path.isfile(fext):
                return fext
    return None

# checks if it can find xgettext on the PATH and
# imports the extraction tests if yes
xgettext_cmd = find_command('xgettext')
if xgettext_cmd:
    p = Popen('%s --version' % xgettext_cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=os.name != 'nt', universal_newlines=True)
    output = p.communicate()[0]
    match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', output)
    if match:
        xversion = (int(match.group('major')), int(match.group('minor')))
        if xversion > (0, 15):
            from extraction import *
<commit_msg>Fixed a typo in last commit.<commit_after>import os
import re
from subprocess import Popen, PIPE

def find_command(cmd, path=None, pathext=None):
    if path is None:
        path = os.environ.get('PATH', []).split(os.pathsep)
    if isinstance(path, basestring):
        path = [path]
    # check if there are funny path extensions for executables, e.g. Windows
    if pathext is None:
        pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD').split(os.pathsep)
    # don't use extensions if the command ends with one of them
    for ext in pathext:
        if cmd.endswith(ext):
            pathext = ['']
            break
    # check if we find the command on PATH
    for p in path:
        f = os.path.join(p, cmd)
        if os.path.isfile(f):
            return f
        for ext in pathext:
            fext = f + ext
            if os.path.isfile(fext):
                return fext
    return None

# checks if it can find xgettext on the PATH and
# imports the extraction tests if yes
xgettext_cmd = find_command('xgettext')
if xgettext_cmd:
    p = Popen('%s --version' % xgettext_cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=os.name != 'nt', universal_newlines=True)
    output = p.communicate()[0]
    match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', output)
    if match:
        xversion = (int(match.group('major')), int(match.group('minor')))
        if xversion >= (0, 15):
            from extraction import *
<|endoftext|>


<commit_before>"""
>>> from django.db.models.fields import *

# DecimalField

>>> f = DecimalField()

>>> f.to_python(3)
Decimal("3")

>>> f.to_python("3.14")
Decimal("3.14")

>>> f.to_python("abc")
Traceback (most recent call last):
...
ValidationError: [u'This value must be a decimal number.']

>>> f = DecimalField(max_digits=5, decimal_places=1)
>>> x = f.to_python(2)
>>> y = f.to_python('2.6')

>>> f._format(x)
u'2.0'
>>> f._format(y)
u'2.6'
>>> f._format(None)
>>> f.get_db_prep_lookup('exact', None)
[None]

# DateTimeField and TimeField to_python should support usecs:
>>> f = DateTimeField()
>>> f.to_python('2001-01-02 03:04:05.000006')
datetime.datetime(2001, 1, 2, 3, 4, 5, 6)
>>> f.to_python('2001-01-02 03:04:05.999999')
datetime.datetime(2001, 1, 2, 3, 4, 5, 999999)

>>> f = TimeField()
>>> f.to_python('01:02:03.000004')
datetime.time(1, 2, 3, 4)
>>> f.to_python('01:02:03.999999')
datetime.time(1, 2, 3, 999999)

"""
<commit_msg>Since max_digits and decimal_places are required on django.db.models.DecimalField, one of the tests was a bit bogus. Fixed that.<commit_after>"""
>>> from django.db.models.fields import *

# DecimalField

>>> f = DecimalField(max_digits=4, decimal_places=2)

>>> f.to_python(3)
Decimal("3")

>>> f.to_python("3.14")
Decimal("3.14")

>>> f.to_python("abc")
Traceback (most recent call last):
...
ValidationError: [u'This value must be a decimal number.']

>>> f = DecimalField(max_digits=5, decimal_places=1)
>>> x = f.to_python(2)
>>> y = f.to_python('2.6')

>>> f._format(x)
u'2.0'
>>> f._format(y)
u'2.6'
>>> f._format(None)
>>> f.get_db_prep_lookup('exact', None)
[None]

# DateTimeField and TimeField to_python should support usecs:
>>> f = DateTimeField()
>>> f.to_python('2001-01-02 03:04:05.000006')
datetime.datetime(2001, 1, 2, 3, 4, 5, 6)
>>> f.to_python('2001-01-02 03:04:05.999999')
datetime.datetime(2001, 1, 2, 3, 4, 5, 999999)

>>> f = TimeField()
>>> f.to_python('01:02:03.000004')
datetime.time(1, 2, 3, 4)
>>> f.to_python('01:02:03.999999')
datetime.time(1, 2, 3, 999999)

"""
<|endoftext|>