Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
update tests, and add tests for custom docs and ObjectEl
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcjohnson committed Jan 30, 2021
1 parent fcb414e commit 5d1eebe
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 202 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"postbuild": "es-check es5 dash_html_components/*.js",
"build:watch": "watch 'npm run build' src",
"test:import": "python -m unittest tests.test_dash_import",
"test:py": "python -m unittest tests.test_dash_html_components tests.test_integration",
"test:py": "pytest tests/test_dash_html_components.py tests/test_integration.py",
"test": "run-s -c test:py test:import lint"
},
"author": "Chris Parmer <chris@plotly.com>",
Expand Down
91 changes: 48 additions & 43 deletions tests/test_dash_html_components.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
import unittest
import dash_html_components


class TestDashHtmlComponents(unittest.TestCase):
def test_imports(self):
with open('./scripts/data/elements.txt') as f:
elements = [
s[0].upper() + s[1:] for s in
f.read().split('\n')
]
elements += ['MapEl', 'ObjectEl']
for s in ['Map', 'Object']:
elements.remove(s)

print(dir(dash_html_components))

self.assertEqual(
set([d for d in dir(dash_html_components) if d[0] != '_' and d[0] == d[0].capitalize()]),
set(elements)
)

def test_sample_items(self):
Div = dash_html_components.Div
Img = dash_html_components.Img

layout = Div(
Div(
Img(src='https://plotly.com/~chris/1638.png')
), style={'color': 'red'}
)

self.assertEqual(
repr(layout),
''.join([
"Div(children=Div(Img(src='https://plotly.com/~chris/1638.png')), "
"style={'color': 'red'})"
])
)

self.assertEqual(
layout._namespace, 'dash_html_components'
)
import pytest
import dash_html_components as html


def test_imports():
with open("./scripts/data/elements.txt") as f:
elements = [s[0].upper() + s[1:] for s in f.read().split("\n")]
elements += ["MapEl", "ObjectEl"]
for s in ["Map", "Object"]:
elements.remove(s)

dir_set = set(
[
d
for d in dir(html)
if d[0] != "_" and d[0] == d[0].capitalize()
]
)
assert dir_set == set(elements)


def test_sample_items():
layout = html.Div(
html.Div(html.Img(src="https://plotly.com/~chris/1638.png")),
style={"color": "red"}
)

expected = (
"Div(children=Div(Img(src='https://plotly.com/~chris/1638.png')), "
"style={'color': 'red'})"
)
assert repr(layout) == expected

assert layout._namespace == "dash_html_components"


def test_objectEl():
layout = html.ObjectEl(data="something", **{"data-x": "else"})
assert repr(layout) == "ObjectEl(data='something', data-x='else')"

with pytest.raises(TypeError):
html.ObjectEl(datax="something")


def test_customDocs():
assert "CAUTION" in html.Script.__doc__[:100]
assert "OBSOLETE" in html.Blink.__doc__[:100]
assert "DEPRECATED" in html.Marquee.__doc__[:100]
8 changes: 4 additions & 4 deletions tests/test_dash_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
class TestDashImport(unittest.TestCase):
def setUp(self):
with open('dash.py', 'w') as f:
pass
pass

def tearDown(self):
try:
os.remove('dash.py')
os.remove('dash.pyc')
except OSError:
pass

def test_dash_import(self):
"""Test that program exits if the wrong dash module was imported"""

with self.assertRaises(SystemExit) as cm:
import dash_html_components

Expand Down
244 changes: 90 additions & 154 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,159 +1,95 @@
import base64
from datetime import datetime
import io
import itertools
from multiprocessing import Value
import os
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import sys
from textwrap import dedent
import time
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse

import dash
from dash.dependencies import Input, Output, State
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc

from .IntegrationTests import IntegrationTests
from .utils import assert_clean_console


class Tests(IntegrationTests):
def setUp(self):
pass

def wait_for_element_by_css_selector(self, selector):
start_time = time.time()
error = None
while time.time() < start_time + 20:
try:
return self.driver.find_element_by_css_selector(selector)
except Exception as e:
error = e
self.driver.implicitly_wait(1)
raise error

def wait_for_text_to_equal(self, selector, assertion_text):
start_time = time.time()
error = None
while time.time() < start_time + 20:
el = self.wait_for_element_by_css_selector(selector)
try:
return self.assertEqual(el.text, assertion_text)
except Exception as e:
error = e
time.sleep(0.25)
raise error

def snapshot(self, name):
if 'PERCY_PROJECT' in os.environ and 'PERCY_TOKEN' in os.environ:
python_version = sys.version.split(' ')[0]
print('Percy Snapshot {}'.format(python_version))
self.percy_runner.snapshot(name=name)

def test_click(self):
call_count = Value('i', 0)

app = dash.Dash()
app.layout = html.Div([
html.Div(id='container'),
html.Button('Click', id='button', n_clicks=0)
])

@app.callback(Output('container', 'children'), [Input('button', 'n_clicks')])
def update_output(n_clicks):
call_count.value += 1
return 'You have clicked the button {} times'.format(n_clicks)

self.startServer(app)

self.wait_for_element_by_css_selector('#container')

self.wait_for_text_to_equal(
'#container', 'You have clicked the button 0 times')
self.assertEqual(call_count.value, 1)
self.snapshot('button initialization')

self.driver.find_element_by_css_selector('#button').click()

self.wait_for_text_to_equal(
'#container', 'You have clicked the button 1 times')
self.assertEqual(call_count.value, 2)
self.snapshot('button click')


def test_click_prev(self):
call_count = Value('i', 0)
timestamp_1 = Value('d', -5)
timestamp_2 = Value('d', -5)

app = dash.Dash()
app.layout = html.Div([
html.Div(id='container'),
html.Button('Click', id='button-1', n_clicks=0, n_clicks_timestamp=-1),
html.Button('Click', id='button-2', n_clicks=0, n_clicks_timestamp=-1)
])

@app.callback(
Output('container', 'children'),
[Input('button-1', 'n_clicks'),
Input('button-1', 'n_clicks_timestamp'),
Input('button-2', 'n_clicks'),
Input('button-2', 'n_clicks_timestamp')])
def update_output(*args):
print(args)
call_count.value += 1
timestamp_1.value = args[1]
timestamp_2.value = args[3]
return '{}, {}'.format(args[0], args[2])

self.startServer(app)

self.wait_for_element_by_css_selector('#container')
time.sleep(2)
self.wait_for_text_to_equal('#container', '0, 0')
self.assertEqual(timestamp_1.value, -1)
self.assertEqual(timestamp_2.value, -1)
self.assertEqual(call_count.value, 1)
self.snapshot('button initialization 1')

self.driver.find_element_by_css_selector('#button-1').click()
time.sleep(2)
self.wait_for_text_to_equal('#container', '1, 0')
print(timestamp_1.value)
print((time.time() - (24 * 60 * 60)) * 1000)
self.assertTrue(
timestamp_1.value >
((time.time() - (24 * 60 * 60)) * 1000))
self.assertEqual(timestamp_2.value, -1)
self.assertEqual(call_count.value, 2)
self.snapshot('button-1 click')
prev_timestamp_1 = timestamp_1.value

self.driver.find_element_by_css_selector('#button-2').click()
time.sleep(2)
self.wait_for_text_to_equal('#container', '1, 1')
self.assertEqual(timestamp_1.value, prev_timestamp_1)
self.assertTrue(
timestamp_2.value >
((time.time() - 24 * 60 * 60) * 1000))
self.assertEqual(call_count.value, 3)
self.snapshot('button-2 click')
prev_timestamp_2 = timestamp_2.value

self.driver.find_element_by_css_selector('#button-2').click()
time.sleep(2)
self.wait_for_text_to_equal('#container', '1, 2')
self.assertEqual(timestamp_1.value, prev_timestamp_1)
self.assertTrue(
timestamp_2.value >
prev_timestamp_2)
self.assertTrue(timestamp_2.value > timestamp_1.value)
self.assertEqual(call_count.value, 4)
self.snapshot('button-2 click again')


def test_click(dash_duo):
call_count = Value('i', 0)

app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(id='container'),
html.Button('Click', id='button', n_clicks=0)
])

@app.callback(Output('container', 'children'), Input('button', 'n_clicks'))
def update_output(n_clicks):
call_count.value += 1
return 'clicked {} times'.format(n_clicks)

dash_duo.start_server(app)

dash_duo.find_element('#container')

dash_duo.wait_for_text_to_equal(
'#container', 'clicked 0 times')
assert call_count.value == 1
dash_duo.percy_snapshot('button initialization')

dash_duo.find_element('#button').click()

dash_duo.wait_for_text_to_equal(
'#container', 'clicked 1 times')
assert call_count.value == 2
dash_duo.percy_snapshot('button click')


def test_click_prev(dash_duo):
call_count = Value('i', 0)
timestamp_1 = Value('d', -5)
timestamp_2 = Value('d', -5)

app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(id='container'),
html.Button('Click', id='button-1', n_clicks=0, n_clicks_timestamp=-1),
html.Button('Click', id='button-2', n_clicks=0, n_clicks_timestamp=-1)
])

@app.callback(
Output('container', 'children'),
[Input('button-1', 'n_clicks'),
Input('button-1', 'n_clicks_timestamp'),
Input('button-2', 'n_clicks'),
Input('button-2', 'n_clicks_timestamp')])
def update_output(*args):
print(args)
call_count.value += 1
timestamp_1.value = args[1]
timestamp_2.value = args[3]
return '{}, {}'.format(args[0], args[2])

dash_duo.start_server(app)

dash_duo.wait_for_text_to_equal('#container', '0, 0')
assert timestamp_1.value == -1
assert timestamp_2.value == -1
assert call_count.value == 1
dash_duo.percy_snapshot('button initialization 1')

dash_duo.find_element('#button-1').click()
dash_duo.wait_for_text_to_equal('#container', '1, 0')
assert timestamp_1.value > ((time.time() - (24 * 60 * 60)) * 1000)
assert timestamp_2.value == -1
assert call_count.value == 2
dash_duo.percy_snapshot('button-1 click')
prev_timestamp_1 = timestamp_1.value

dash_duo.find_element('#button-2').click()
dash_duo.wait_for_text_to_equal('#container', '1, 1')
assert timestamp_1.value == prev_timestamp_1
assert timestamp_2.value > ((time.time() - 24 * 60 * 60) * 1000)
assert call_count.value == 3
dash_duo.percy_snapshot('button-2 click')
prev_timestamp_2 = timestamp_2.value

dash_duo.find_element('#button-2').click()
dash_duo.wait_for_text_to_equal('#container', '1, 2')
assert timestamp_1.value == prev_timestamp_1
assert timestamp_2.value > prev_timestamp_2
assert timestamp_2.value > timestamp_1.value
assert call_count.value == 4
dash_duo.percy_snapshot('button-2 click again')

0 comments on commit 5d1eebe

Please sign in to comment.