def test_client_can_get_user_information(requests_mock): mock_user_info(requests_mock) client = Client("v2_api_key") user = client.user_information() assert isinstance(user, UserInformation)
def test_singular_subject_retrieval(requests_mock): mock_single_subject(requests_mock) v2_api_key = "arbitrary_api_key" client = Client(v2_api_key) subject = client.subject(1) assert isinstance(subject, Subject)
def test_real_connection_to_subjects(): client = Client(api_key) subjects = client.subjects() assert len(subjects.current_page.data) > 0 assert subjects.current_page.data[0].resource in [ "vocabulary", "kanji", "radical" ]
def test_client_can_get_summary(requests_mock): mock_summary(requests_mock) client = Client("v2_api_key") summary = client.summary() assert summary.lessons is not None assert summary.reviews is not None
def add_subject_ids(): from wanikani_api.client import Client from kw_webapp.tasks import get_vocab_by_kanji client = Client("2510f001-fe9e-414c-ba19-ccf79af40060") subjects = client.subjects(fetch_all=True, types="vocabulary", hidden=False) total_subs = len(subjects) match_count = 0 no_local_equivalent = [] for subject in subjects: try: local_vocabulary = get_vocab_by_kanji(subject.characters) local_vocabulary.wk_subject_id = subject.id local_vocabulary.reconcile(subject) match_count += 1 logger.info( f"{match_count} / {total_subs}:\t {subject.characters}") except Vocabulary.DoesNotExist: logger.warn( f"Found no local vocabulary with characters: {subject.characters}" ) no_local_equivalent.append(subject) unmatched = Vocabulary.objects.filter(wk_subject_id=0) return unmatched, no_local_equivalent
def test_client_correctly_renders_empty_collections(requests_mock): mock_empty_subjects(requests_mock) v2_api_key = "arbitrary_api_key" client = Client(v2_api_key) response = client.subjects(ids=[1, 2, 3], hidden=False, slugs=["abc", "123"]) assert len(response.current_page.data) == 0
def test_client_can_get_assignments(requests_mock): mock_assignments(requests_mock) client = Client("v2_api_key") assignments = client.assignments() assert len(assignments.current_page.data) > 0
def test_etag_cache_decorator_works(mocker, requests_mock): mock_subjects_with_cache(requests_mock) v2_api_key = "arbitrary_api_key" client = Client(v2_api_key) mocker.spy(client, "_fetch_result_from_cache") subjects = client.subjects() cached_subjects = client.subjects() assert client._fetch_result_from_cache.call_count == 1
def test_subjectable_mixin_works(requests_mock): mock_subjects(requests_mock) mock_assignments(requests_mock) client = Client("arbitrary_api_key", subject_cache_enabled=True) assignments = client.assignments() assignment = assignments[0] subj = assignment.subject assert subj.id == assignment.subject_id
def get_vocab_df(api_key, sync_vocab=False, type="kanji"): """Get vocab dataframe. Example: subject_id meanings characters readings pos srs_stage 0 2467 [One] 一 [いち] [numeral] 8 1 2468 [One Thing] 一つ [ひとつ] [numeral] 8 2 2469 [Seven] 七 [なな, しち] [numeral] 8 3 2470 [Seven Things] 七つ [ななつ] [numeral] 8 4 2471 [Nine] 九 [きゅう, く] [numeral] 8 Args: api_key (str): wanikani api key sync_vocab (bool, optional): Fetch and cache vocab. Defaults to False. Returns: [pandas df]: See example. """ # Get Vocab wkpath = jpl.get_dir(jpl.external_dir() / "wanikani") client = Client(api_key) if sync_vocab: all_vocabulary = client.subjects(types=type, fetch_all=True) vocab_db = [] for vocab in all_vocabulary: row = [] row.append(vocab.id) row.append([i.meaning for i in vocab.meanings]) row.append(vocab.characters) row.append([i.reading for i in vocab.readings]) if type == "vocabulary": row.append(vocab.parts_of_speech) else: row.append("") vocab_db.append(row) vocab_csv = pd.DataFrame( vocab_db, columns=[ "subject_id", "meanings", "characters", "readings", "pos" ], ) vocab_csv["srs_stage"] = 0 vocab_csv.to_parquet( wkpath / "{}.parquet".format(type), index=0, compression="gzip", ) else: vocab_csv = pd.read_parquet(wkpath / "{}.parquet".format(type)) # Get SRS Scores assignments = client.assignments(subject_types=type, fetch_all=True) for assignment in assignments: vocab_csv.loc[vocab_csv.subject_id == assignment.subject_id, "srs_stage"] = assignment.srs_stage return vocab_csv
def test_client_can_get_subjects(requests_mock): mock_subjects(requests_mock) client = Client("v2_api_key") subjects = client.subjects() assert len(subjects.current_page.data) > 0 assert subjects.current_page.data[0].resource in [ "vocabulary", "kanji", "radical" ]
def test_assignment_parameters_are_properly_converted(requests_mock): mock_assignments(requests_mock) v2_api_key = "arbitrary_api_key" client = Client(v2_api_key) client.assignments(ids=[1, 2, 3], hidden=False, srs_stages=[0, 1, 2]) assert requests_mock.call_count == 1 assert ( requests_mock.request_history[0].url == "https://api.wanikani.com/v2/assignments?hidden=false&ids=1,2,3&srs_stages=0,1,2" )
def test_parameters_convert_datetime_to_string_correctly(requests_mock): mock_subjects(requests_mock) v2_api_key = "arbitrary_api_key" client = Client(v2_api_key) now = datetime.datetime.now() client.subjects(updated_after=now) assert requests_mock.call_count == 1 assert (requests_mock.request_history[0].url == "https://api.wanikani.com/v2/subjects?updated_after=" + now.isoformat())
def test_subject_parameters_are_properly_converted(requests_mock): mock_subjects(requests_mock) v2_api_key = "arbitrary_api_key" client = Client(v2_api_key) client.subjects(ids=[1, 2, 3], hidden=False, slugs=["abc", "123"]) assert requests_mock.call_count == 1 assert ( requests_mock.request_history[0].url == "https://api.wanikani.com/v2/subjects?hidden=false&ids=1,2,3&slugs=abc,123" )
def test_client_uses_cache(requests_mock): mock_subjects(requests_mock) mock_assignments(requests_mock) v2_api_key = "arbitrary_api_key" client = Client(v2_api_key, subject_cache_enabled=True) assignments = client.assignments() for ass in assignments: print(ass.subject.level) # in theory here, if we have _not_ cached assert requests_mock.call_count == 2 history = requests_mock.request_history assert "subjects" in history[0].url assert "assignments" in history[1].url
def test_expected_subjectable_resources_work(requests_mock): mock_assignments(requests_mock) mock_summary(requests_mock) mock_subjects(requests_mock) mock_reviews(requests_mock) mock_single_subject(requests_mock) mock_study_materials(requests_mock) mock_summary(requests_mock) client = Client("arbitrary_api_key") assignments = client.assignments() assert assignments[0].subject is not None study_materials = client.study_materials() assert study_materials[0].subject is not None study_materials = client.study_materials() assert study_materials[0].subject is not None summary = client.summary() assert summary.reviews[0].subjects is not None reviews = client.reviews() assert reviews[0].subject is not None
def test_requests_mock(requests_mock): mock_subjects(requests_mock) client = Client("whatever") subjects = client.subjects() assert isinstance(subjects, Iterator)
def test_client_can_get_resets(requests_mock): mock_resets(requests_mock) client = Client("v2_api_key") resets = client.resets() assert len(resets.current_page.data) == 1
def test_client_can_get_level_progression(requests_mock): mock_level_progressions(requests_mock) client = Client("v2_api_key") progressions = client.level_progressions() assert len(progressions.current_page.data) > 0
def test_real_connection_to_wanikani_user_information(): client = Client(api_key) user = client.user_information() assert isinstance(user, UserInformation)
def test_real_connection_to_resets(): client = Client(api_key) resets = client.resets() assert len(resets.current_page.data) == 0
def test_client_can_get_review_statistics(requests_mock): mock_review_statistics(requests_mock) client = Client("v2_api_key") review_statistics = client.review_statistics() assert len(review_statistics.current_page.data) > 0
def test_real_connection_to_assignments(): client = Client(api_key) assignments = client.assignments() assert len(assignments.current_page.data) > 0
def test_real_connection_to_review_statistics(): client = Client(api_key) review_statistics = client.review_statistics() assert len(review_statistics.current_page.data) > 0
def test_real_connection_to_study_materials(): client = Client(api_key) study_materials = client.study_materials() assert len(study_materials.current_page.data) > 0
def test_real_connection_to_summary(): client = Client(api_key) summary = client.summary() assert summary.lessons is not None assert summary.reviews is not None
def test_real_connection_to_level_progression(): client = Client(api_key) progressions = client.level_progressions()
def test_client_can_get_study_materials(requests_mock): mock_study_materials(requests_mock) client = Client("v2_api_key") study_materials = client.study_materials() assert len(study_materials.current_page.data) > 0
def test_real_connection_to_singular_subject(): client = Client(api_key) subject = client.subject(1) assert isinstance(subject, Subject)