Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rfctr: move test fixtures to tests/fixtures #116

Merged
merged 1 commit into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/cr/cube/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import collections
import functools
import json
import os

import numpy as np

Expand Down Expand Up @@ -158,13 +156,6 @@ def __set__(self, obj, value):
raise AttributeError('can\'t set attribute')


def load_fixture(fixtures_directory, filename):
"""Loads fixtures for CrunchCube integration tests."""
with open(os.path.join(fixtures_directory, filename)) as ctx_file:
fixture = json.load(ctx_file)
return fixture


def lru_cache(maxsize=100):
"""Least-recently-used cache decorator.

Expand Down
63 changes: 63 additions & 0 deletions tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# encoding: utf-8

"""JSON cube-response source files for testing purposes."""

import json
import os

CUBES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'cubes')


class LazyCubeResponseLoader(object):
"""Loads and caches cube-responses by name from fixture directory.

Provides access to all the cube-response fixtures in a directory by
a standardized mapping of the file name, e.g. cat-x-cat.json is available
as the `.CAT_X_CAT` attribute of the loader.

The fixture directory is specified relative to this (fixture root)
directory.
"""

def __init__(self, relpath):
self._relpath = relpath
self._cache = {}

def __getattr__(self, fixture_name):
"""Return cube-dict from JSON file mapping to *fixture_name*.

A *fixture_name* like 'CAT_X_CAT' will map to the JSON file
'cat-x-cat.json' in the directory specified on construction.
"""
if fixture_name not in self._cache:
self._load_to_cache(fixture_name)
return self._cache[fixture_name]

@property
def _dirpath(self):
"""Absolute path to relative directory specified in relpath."""
thisdir = os.path.dirname(os.path.abspath(__file__))
return os.path.abspath(os.path.join(thisdir, self._relpath))

def _json_path(self, fixture_name):
"""Return absolute path to JSON file for *fixture_name*."""
return (
'%s/%s.json' %
(self._dirpath, fixture_name.replace('_', '-').lower())
)

def _load_json(self, path):
"""Return dict parsed from JSON at *path*."""
with open(path) as f:
cube_response = json.load(f)
return cube_response

def _load_to_cache(self, fixture_name):
json_path = self._json_path(fixture_name)
if not os.path.exists(json_path):
raise ValueError('no JSON fixture found at %s' % json_path)
self._cache[fixture_name] = self._load_json(json_path)


CR = LazyCubeResponseLoader('.') # ---mnemonic: CR = 'cube-response'---
SM = LazyCubeResponseLoader('./scale_means')
151 changes: 0 additions & 151 deletions tests/integration/fixtures/__init__.py

This file was deleted.

Empty file.
Loading