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_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_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_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_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_real_connection_to_assignments(): client = Client(api_key) assignments = client.assignments() assert len(assignments.current_page.data) > 0